-
Notifications
You must be signed in to change notification settings - Fork 462
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
Trigger AWS Lambda tests on label #2538
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ae59637
wip
sentrivana 635d001
wip
sentrivana 597e9f7
Merge branch 'master' into ivana/trigger-tests-on-label-2
sentrivana e969ff2
Label-based test trigger
sentrivana dbf6c7f
comment
sentrivana 06aed31
formatting
sentrivana 654c389
wip
sentrivana 815a426
trying something out
sentrivana 862a367
attempt
sentrivana 77545ff
change back to pull_request_target
sentrivana f446937
Better label name
sentrivana 7c624e9
Merge branch 'master' into ivana/trigger-tests-on-label-2
sentrivana 56dbea2
Update scripts/split-tox-gh-actions/templates/base.jinja
sentrivana abf4554
Regenerate yamls
sentrivana File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env python3 | ||
import argparse | ||
import json | ||
import os | ||
from urllib.parse import quote | ||
from urllib.request import Request, urlopen | ||
|
||
LABEL = "Trigger: tests" | ||
|
||
|
||
def _has_write(repo_id: int, username: str, *, token: str) -> bool: | ||
req = Request( | ||
f"https://api.github.com/repositories/{repo_id}/collaborators/{username}/permission", | ||
headers={"Authorization": f"token {token}"}, | ||
) | ||
contents = json.load(urlopen(req, timeout=10)) | ||
|
||
return contents["permission"] in {"admin", "write"} | ||
|
||
|
||
def _remove_label(repo_id: int, pr: int, label: str, *, token: str) -> None: | ||
quoted_label = quote(label) | ||
req = Request( | ||
f"https://api.github.com/repositories/{repo_id}/issues/{pr}/labels/{quoted_label}", | ||
method="DELETE", | ||
headers={"Authorization": f"token {token}"}, | ||
) | ||
urlopen(req) | ||
|
||
|
||
def main() -> int: | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--repo-id", type=int, required=True) | ||
parser.add_argument("--pr", type=int, required=True) | ||
parser.add_argument("--event", required=True) | ||
parser.add_argument("--username", required=True) | ||
parser.add_argument("--label-names", type=json.loads, required=True) | ||
args = parser.parse_args() | ||
|
||
token = os.environ["GITHUB_TOKEN"] | ||
|
||
write_permission = _has_write(args.repo_id, args.username, token=token) | ||
|
||
if ( | ||
not write_permission | ||
# `reopened` is included here due to close => push => reopen | ||
and args.event in {"synchronize", "reopened"} | ||
and LABEL in args.label_names | ||
): | ||
print(f"Invalidating label [{LABEL}] due to code change...") | ||
_remove_label(args.repo_id, args.pr, LABEL, token=token) | ||
args.label_names.remove(LABEL) | ||
|
||
if write_permission or LABEL in args.label_names: | ||
print("Permissions passed!") | ||
print(f"- has write permission: {write_permission}") | ||
print(f"- has [{LABEL}] label: {LABEL in args.label_names}") | ||
return 0 | ||
else: | ||
print("Permissions failed!") | ||
print(f"- has write permission: {write_permission}") | ||
print(f"- has [{LABEL}] label: {LABEL in args.label_names}") | ||
print(f"- args.label_names: {args.label_names}") | ||
print( | ||
f"Please have a collaborator add the [{LABEL}] label once they " | ||
f"have reviewed the code to trigger tests." | ||
) | ||
return 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
raise SystemExit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
scripts/split-tox-gh-actions/templates/check_permissions.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
check-permissions: | ||
name: permissions check | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- uses: actions/checkout@93ea575cb5d8a053eaa0ac8fa3b40d7e05a33cc8 # v3.1.0 | ||
with: | ||
persist-credentials: false | ||
|
||
- name: permissions | ||
run: | | ||
{% raw %} | ||
python3 -uS .github/workflows/scripts/trigger_tests_on_label.py \ | ||
--repo-id ${{ github.event.repository.id }} \ | ||
--pr ${{ github.event.number }} \ | ||
--event ${{ github.event.action }} \ | ||
--username "$ARG_USERNAME" \ | ||
--label-names "$ARG_LABEL_NAMES" | ||
{% endraw %} | ||
env: | ||
{% raw %} | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
# these can contain special characters | ||
ARG_USERNAME: ${{ github.event.pull_request.user.login }} | ||
ARG_LABEL_NAMES: ${{ toJSON(github.event.pull_request.labels.*.name) }} | ||
{% endraw %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would maybe make this more explicit that it's sensitive tests requiring secrets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to
"Trigger: tests using secrets"
(open to better names)