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

[ci] Add workflow to cc teams #10322

Merged
merged 1 commit into from
Mar 3, 2022
Merged
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
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 @@ -406,5 +407,240 @@ def all_time_keys(time):
)


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,
"draft": 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,
"draft": 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