Skip to content

Commit

Permalink
[ci] Add workflow to cc teams
Browse files Browse the repository at this point in the history
As discussed in https://discuss.tvm.apache.org/t/rfc-remove-codeowners/12095/2?u=driazati, this adds a mechanism to auto-tag people based on PR/issue titles and labels. This should improve visibility across the project and make it easy for interested people to subscribe to various topics.

Details on usage will be posted in the relevant issue: #10317
  • Loading branch information
driazati committed Feb 22, 2022
1 parent b5f1dab commit 25cd8ab
Show file tree
Hide file tree
Showing 4 changed files with 584 additions and 3 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/tag_teams.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

# GH actions.
# We use it to cover windows and mac builds
# Jenkins is still the primary CI

name: Teams

on:
# See https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request_target
pull_request_target:
types: [opened, reopened, edited, ready_for_review, labeled]
issues:
types: [opened, edited, reopened, labeled]

concurrency:
group: Teams-${{ github.event.pull_request.number }}-${{ github.event.issue.number }}
cancel-in-progress: true

jobs:
tag-teams:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Tag people from relevant teams
env:
PR: ${{ toJson(github.event.pull_request) }}
ISSUE: ${{ toJson(github.event.issue) }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -eux
python tests/scripts/github_tag_teams.py || echo failed
236 changes: 236 additions & 0 deletions tests/python/unittest/test_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import subprocess
import sys
import json
import textwrap
import tempfile

import pytest
Expand Down Expand Up @@ -394,5 +395,240 @@ def run(pr, check):
)


def assert_in(needle: str, haystack: str):
if needle not in haystack:
raise AssertionError(f"item not found:\n{needle}\nin:\n{haystack}")


def test_github_tag_teams(tmpdir_factory):
tag_script = REPO_ROOT / "tests" / "scripts" / "github_tag_teams.py"

def run(type, data, check):
git = TempGit(tmpdir_factory.mktemp("tmp_git_dir"))
git.run("init")
git.run("checkout", "-b", "main")
git.run("remote", "add", "origin", "https://github.com/apache/tvm.git")

issue_body = """
some text
[temporary] opt-in: @person5
- something: @person1 @person2
- something else @person1 @person2
- something else2: @person1 @person2
- something-else @person1 @person2
"""
comment1 = """
another thing: @person3
another-thing @person3
"""
comment2 = """
something @person4
"""
teams = {
"data": {
"repository": {
"issue": {
"body": issue_body,
"comments": {"nodes": [{"body": comment1}, {"body": comment2}]},
}
}
}
}
env = {
type: json.dumps(data),
}
proc = subprocess.run(
[
str(tag_script),
"--dry-run",
"--team-issue-json",
json.dumps(teams),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding="utf-8",
cwd=git.cwd,
env=env,
)
if proc.returncode != 0:
raise RuntimeError(f"Process failed:\nstdout:\n{proc.stdout}\n\nstderr:\n{proc.stderr}")

assert_in(check, proc.stdout)

run(
"ISSUE",
{
"title": "A title",
"number": 1234,
"user": {
"login": "person5",
},
"labels": [{"name": "abc"}],
"body": textwrap.dedent(
"""
hello
""".strip()
),
},
"No one to cc, exiting",
)

run(
"ISSUE",
{
"title": "A title",
"number": 1234,
"user": {
"login": "person5",
},
"labels": [{"name": "abc"}],
"body": textwrap.dedent(
"""
hello
cc @test
""".strip()
),
},
"No one to cc, exiting",
)

run(
type="ISSUE",
data={
"title": "A title",
"number": 1234,
"user": {
"login": "person5",
},
"labels": [{"name": "something"}],
"body": textwrap.dedent(
"""
hello
something"""
),
},
check="would have updated issues/1234 with {'body': '\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}",
)

run(
type="ISSUE",
data={
"title": "A title",
"number": 1234,
"user": {
"login": "person6",
},
"labels": [{"name": "something"}],
"body": textwrap.dedent(
"""
hello
something"""
),
},
check="Author person6 is not opted in, quitting",
)

run(
type="ISSUE",
data={
"title": "A title",
"number": 1234,
"user": {
"login": "person5",
},
"labels": [{"name": "something"}],
"body": textwrap.dedent(
"""
hello
cc @person1 @person2 @person4"""
),
},
check="Everyone to cc is already cc'ed, no update needed",
)

run(
type="ISSUE",
data={
"title": "[something] A title",
"number": 1234,
"user": {
"login": "person5",
},
"labels": [{"name": "something2"}],
"body": textwrap.dedent(
"""
hello
something"""
),
},
check="would have updated issues/1234 with {'body': '\\nhello\\n\\nsomething\\n\\ncc @person1 @person2 @person4'}",
)

run(
type="ISSUE",
data={
"title": "[something] A title",
"number": 1234,
"user": {
"login": "person5",
},
"labels": [{"name": "something2"}],
"body": textwrap.dedent(
"""
hello
cc @person1 @person2 @person4"""
),
},
check="Everyone to cc is already cc'ed, no update needed",
)

run(
type="PR",
data={
"title": "[something] A title",
"number": 1234,
"isDraft": False,
"user": {
"login": "person5",
},
"labels": [{"name": "something2"}],
"body": textwrap.dedent(
"""
hello
cc @person1 @person2 @person4"""
),
},
check="Everyone to cc is already cc'ed, no update needed",
)

run(
type="PR",
data={
"title": "[something] A title",
"number": 1234,
"isDraft": True,
"user": {
"login": "person5",
},
"labels": [{"name": "something2"}],
"body": textwrap.dedent(
"""
hello
cc @person1 @person2 @person4"""
),
},
check="Terminating since 1234 is a draft",
)


if __name__ == "__main__":
sys.exit(pytest.main([__file__] + sys.argv[1:]))
29 changes: 26 additions & 3 deletions tests/scripts/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import subprocess
import re
from urllib import request
from typing import Dict, Tuple, Any
from typing import Dict, Tuple, Any, Optional, List


class GitHubRepo:
Expand All @@ -35,8 +35,16 @@ def headers(self):
"Authorization": f"Bearer {self.token}",
}

def graphql(self, query: str) -> Dict[str, Any]:
return self._post("https://api.github.com/graphql", {"query": query})
def graphql(self, query: str, variables: Optional[Dict[str, str]] = None) -> Dict[str, Any]:
if variables is None:
variables = {}
response = self._post(
"https://api.github.com/graphql", {"query": query, "variables": variables}
)
if "data" not in response:
msg = f"Error fetching data with query:\n{query}\n\nvariables:\n{variables}\n\nerror:\n{json.dumps(response, indent=2)}"
raise RuntimeError(msg)
return response

def _post(self, full_url: str, body: Dict[str, Any]) -> Dict[str, Any]:
print("Requesting POST to", full_url, "with", body)
Expand Down Expand Up @@ -95,3 +103,18 @@ def git(command, **kwargs):
if proc.returncode != 0:
raise RuntimeError(f"Command failed {command}:\nstdout:\n{proc.stdout}")
return proc.stdout.strip()


def find_ccs(body: str) -> List[str]:
matches = re.findall(r"(cc( @[-A-Za-z0-9]+)+)", body, flags=re.MULTILINE)
matches = [full for full, last in matches]

reviewers = []
for match in matches:
if match.startswith("cc "):
match = match.replace("cc ", "")
users = [x.strip() for x in match.split("@")]
reviewers += users

reviewers = set(x for x in reviewers if x != "")
return list(reviewers)
Loading

0 comments on commit 25cd8ab

Please sign in to comment.