Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test code #33

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ concurrency:

jobs:
MacOS:
if: ${{ github.repository == 'apache/tvm' }}
runs-on: macOS-latest
if: ${{ github.repository == 'driazati/tvm' }}
runs-on: ubuntu-latest
steps:
- name: Start
run: |
exit 1
- uses: actions/checkout@v2
with:
submodules: 'recursive'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tvmbot.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

name: tvm-bot
on:
status:
pull_request_review:
types:
- submitted
Expand All @@ -21,13 +20,14 @@ jobs:
issues: write
pull-requests: write
statuses: write
if: ${{ github.event.issue.pull_request && github.repository == 'apache/tvm' }}
if: ${{ github.event.issue.pull_request && github.repository == 'driazati/tvm' }}
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: Run tvm-bot
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_ACTIONS_TOKEN: ${{ secrets.GH_ACTIONS_TOKEN }}
TVM_BOT_JENKINS_TOKEN: ${{ secrets.TVM_BOT_JENKINS_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
ISSUE_COMMENT: ${{ toJson(github.event.comment) }}
Expand Down
1 change: 1 addition & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ tasks.
```bash
./docker/build.sh ci_cpu tests/scripts/task_golang.sh
```
hi
6 changes: 3 additions & 3 deletions tests/scripts/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ def _request(self, full_url: str, body: Dict[str, Any], method: str) -> Dict[str
with request.urlopen(req, data) as response:
content = response.read()
except error.HTTPError as e:
logging.info(f"Error response: {e.read().decode()}")
e.seek(0)
raise e
msg = str(e)
error_data = e.read().decode()
raise RuntimeError(f"Error response: {msg}\n{error_data}")

logging.info(f"Got response from {full_url}: {content}")
try:
Expand Down
129 changes: 105 additions & 24 deletions tests/scripts/github_tvmbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

EXPECTED_JOBS = ["tvm-ci/pr-head"]
TVM_BOT_JENKINS_TOKEN = os.environ["TVM_BOT_JENKINS_TOKEN"]
GH_ACTIONS_TOKEN = os.environ["GH_ACTIONS_TOKEN"]
JENKINS_URL = "https://ci.tlcpack.ai/"
THANKS_MESSAGE = r"(\s*)Thanks for contributing to TVM! Please refer to guideline https://tvm.apache.org/docs/contribute/ for useful information and tips. After the pull request is submitted, please request code reviews from \[Reviewers\]\(https://github.com/apache/incubator-tvm/blob/master/CONTRIBUTORS.md#reviewers\) by them in the pull request thread.(\s*)"

Expand All @@ -57,6 +58,18 @@ def to_json_str(obj: Any) -> str:
}
"""

MENTIONABLE_QUERY = """
query ($owner: String!, $name: String!, $user: String!) {
repository(owner: $owner, name: $name) {
mentionableUsers(query: $user, first: 1) {
nodes {
login
}
}
}
}
"""


PR_QUERY = """
query ($owner: String!, $name: String!, $number: Int!) {
Expand Down Expand Up @@ -106,6 +119,7 @@ def to_json_str(obj: Any) -> str:
nodes {
... on CheckRun {
name
databaseId
checkSuite {
workflowRun {
workflow {
Expand Down Expand Up @@ -221,12 +235,12 @@ def checker(obj, parent_key):
def __repr__(self):
return json.dumps(self.raw, indent=2)

def plus_one(self, comment: Dict[str, Any]):
def react(self, comment: Dict[str, Any], content: str):
"""
React with a thumbs up to a comment
"""
url = f"issues/comments/{comment['id']}/reactions"
data = {"content": "+1"}
data = {"content": content}
if self.dry_run:
logging.info(f"Dry run, would have +1'ed to {url} with {data}")
else:
Expand Down Expand Up @@ -326,15 +340,21 @@ def search_collaborator(self, user: str) -> List[Dict[str, Any]]:
"""
Query GitHub for collaborators matching 'user'
"""
return self.search_users(user, COLLABORATORS_QUERY)["collaborators"]["nodes"]

def search_users(self, user: str, query: str) -> List[Dict[str, Any]]:
return self.github.graphql(
query=COLLABORATORS_QUERY,
query=query,
variables={
"owner": self.owner,
"name": self.repo_name,
"user": user,
},
)["data"]["repository"]["collaborators"]["nodes"]

def search_mentionable_users(self, user: str) -> List[Dict[str, Any]]:
return self.search_users(user, MENTIONABLE_QUERY)["mentionableUsers"]["nodes"]

def comment(self, text: str) -> None:
"""
Leave the comment 'text' on this PR
Expand Down Expand Up @@ -503,6 +523,27 @@ def rerun_jenkins_ci(self) -> None:
else:
post(url, auth=("tvm-bot", TVM_BOT_JENKINS_TOKEN))

def rerun_github_actions(self) -> None:
job_ids = []
for item in self.head_commit()["statusCheckRollup"]["contexts"]["nodes"]:
if "checkSuite" in item:
job_ids.append(item["databaseId"])

logging.info(f"Rerunning GitHub Actions jobs with IDs: {job_ids}")
actions_github = GitHubRepo(
user=self.github.user, repo=self.github.repo, token=GH_ACTIONS_TOKEN
)
for job_id in job_ids:
try:
actions_github.post(f"actions/jobs/{job_id}/rerun", data={})
except RuntimeError as e:
# Ignore errors about jobs that are part of the same workflow to avoid
# having to figure out which jobs are in which workflows ahead of time
if "The workflow run containing this job is already running" in str(e):
pass
else:
raise e

def comment_failure(self, msg: str, exception: Exception):
if not self.dry_run:
exception_msg = traceback.format_exc()
Expand All @@ -514,13 +555,58 @@ def comment_failure(self, msg: str, exception: Exception):
return exception


def check_author(pr, triggering_comment, args):
comment_author = triggering_comment["user"]["login"]
if pr.author() == comment_author:
logging.info("Comment user is PR author, continuing")
return True
return False


def check_collaborator(pr, triggering_comment, args):
logging.info("Checking collaborators")
# Get the list of collaborators for the repo filtered by the comment
# author
if args.testing_collaborators_json:
collaborators = json.loads(args.testing_collaborators_json)
else:
collaborators = pr.search_collaborator(triggering_comment["user"]["login"])
logging.info(f"Found collaborators: {collaborators}")

return len(collaborators) > 0


def check_mentionable_users(pr, triggering_comment, args):
logging.info("Checking mentionable users")
# Get the list of collaborators for the repo filtered by the comment
# author
if args.testing_mentionable_users_json:
mentionable_users = json.loads(args.testing_mentionable_users_json)
else:
mentionable_users = pr.search_mentionable_users(triggering_comment["user"]["login"])
logging.info(f"Found mentionable_users: {mentionable_users}")

return len(mentionable_users) > 0


AUTH_CHECKS = {
"metionable_users": check_mentionable_users,
"collaborators": check_collaborator,
"author": check_author,
}
# Stash the keys so they're accessible from the values
AUTH_CHECKS = {k: (k, v) for k, v in AUTH_CHECKS.items()}


class Merge:
triggers = [
"merge",
"merge this",
"merge this pr",
]

auth = [AUTH_CHECKS["collaborators"], AUTH_CHECKS["author"]]

@staticmethod
def run(pr: PR):
info = None
Expand Down Expand Up @@ -548,9 +634,15 @@ class Rerun:
"run ci",
]

auth = [AUTH_CHECKS["metionable_users"]]

@staticmethod
def run(pr: PR):
pr.rerun_jenkins_ci()
try:
pr.rerun_jenkins_ci()
pr.rerun_github_actions()
except Exception as e:
pr.comment_failure("Failed to re-run CI", e)


if __name__ == "__main__":
Expand Down Expand Up @@ -615,29 +707,18 @@ def run(pr: PR):
else:
pr = PR(number=int(args.pr), owner=owner, repo=repo, dry_run=args.dry_run)

# Acknowledge the comment with a react
pr.plus_one(comment)

# Check the comment author
comment_author = comment["user"]["login"]
if pr.author() == comment_author:
logging.info("Comment user is PR author, continuing")
else:
logging.info("Comment is not from PR author, checking collaborators")
# Get the list of collaborators for the repo filtered by the comment
# author
if args.testing_collaborators_json:
collaborators = json.loads(args.testing_collaborators_json)
else:
collaborators = pr.search_collaborator(comment_author)
logging.info(f"Found collaborators: {collaborators}")

if len(collaborators) > 0:
logging.info("Comment is from collaborator")
for name, check in command_to_run.auth:
if check(pr, comment, args):
logging.info(f"Passed auth check '{name}', continuing")
else:
logging.info("Comment is not from from PR author or collaborator, quitting")
logging.info(f"Failed auth check '{name}', quitting")
# Add a sad face
pr.react(comment, "confused")
exit(0)

# Acknowledge the comment with a react
pr.react(comment, "+1")

state = pr.state()

if state != "OPEN":
Expand Down