Skip to content

Commit

Permalink
Add action which automatically runs CI for public OpenXLA GitHub org …
Browse files Browse the repository at this point in the history
…members

FORCE_TEST_AUTORUN_CI

PiperOrigin-RevId: 705211520
  • Loading branch information
ddunl authored and Google-ML-Automation committed Dec 11, 2024
1 parent 209cbfa commit 58de9c2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/autorun_ci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2023 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Autoruns CI for OpenXLA org members with membership set to public."""
import os

import github_api

_OPENXLA_ORG_ID = 107584881 # https://api.github.com/orgs/107584881


def main():
username = os.getenv("PR_AUTHOR_USERNAME")
pr_number = os.getenv("PR_NUMBER")
api = github_api.GitHubAPI()

orgs = api.get_user_orgs(username)
print(orgs)

if _OPENXLA_ORG_ID in {org.id for org in orgs}:
api.add_issue_labels("openxla/xla", pr_number, ["invalid"])


if __name__ == "__main__":
main()
39 changes: 39 additions & 0 deletions .github/workflows/autorun_ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright 2024 The OpenXLA Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
name: Autorun CI for OpenXLA Public Members
permissions:
pull-requests: write
on:
pull_request:

jobs:
autorun-ci:
runs-on: ubuntu-22.04
defaults:
run:
shell: bash
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.number }}
PR_AUTHOR_USERNAME: ${{ github.event.pull_request.user.login }}
timeout-minutes: 6
if: |
github.event.sender.type == 'User' ||
contains(github.event.pull_request.body, 'FORCE_TEST_AUTORUN_CI')
steps:
- name: "Checking out repository"
uses: actions/checkout@e2f20e631ae6d7dd3b768f56a5d2af784dd54791 # v2.5.0
- name: "Autorun CI for public OpenXLA org members"
run: python3 .github/workflows/autorun_ci.py
38 changes: 38 additions & 0 deletions .github/workflows/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,41 @@ def set_issue_status(
"""
endpoint = f"repos/{repo}/issues/{issue_number}"
return self._make_request("POST", endpoint, status=status)

def add_issue_labels(
self, repo: str, issue_number: int, labels: list[str]
) -> requests.Response:
"""Adds labels to an issue (or PR).
https://docs.github.com/en/actions/managing-issues-and-pull-requests/adding-labels-to-issues
Arguments:
repo: a string of the form `owner/repo_name`, e.g. openxla/xla
issue_number: the issue (or PR) to set the status of
labels: the labels to add to the issue
Returns:
a requests.Response object containing the response from the API.
Raises:
requests.exceptions.HTTPError
"""
endpoint = f"repos/{repo}/issues/{issue_number}/labels"
return self._make_request("POST", endpoint, labels=labels)

def get_user_orgs(self, username: str) -> requests.Response:
"""Gets all public org memberships for a user.
https://docs.github.com/en/rest/orgs/orgs?apiVersion=2022-11-28#list-organizations-for-a-user
Arguments:
username: The user's GitHub username as a string.
Returns:
a requests.Response object containing the response from the API.
Raises:
requests.exceptions.HTTPError
"""
endpoint = f"users/{username}/orgs"
return self._make_request("GET", endpoint, username=username)

0 comments on commit 58de9c2

Please sign in to comment.