Skip to content

Commit 9084133

Browse files
committed
fix: add workflows
Signed-off-by: Debarati Basu-Nag <dbasunag@redhat.com>
1 parent 51519f8 commit 9084133

4 files changed

Lines changed: 129 additions & 6 deletions

File tree

.github/workflows/add-remove-labels.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ on:
1111
contains(github.event.comment.body, '/wip') ||
1212
contains(github.event.comment.body, '/verified') ||
1313
contains(github.event.comment.body, '/lgtm') ||
14-
contains(github.event.comment.body, '/hold')
14+
contains(github.event.comment.body, '/hold') ||
15+
contains(github.event.comment.body, '/build-push-pr-image')
1516
1617
1718
permissions:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Delete PR Image On PR Close Action
2+
3+
on:
4+
pull_request_target:
5+
types: [closed]
6+
7+
permissions:
8+
pull-requests: write
9+
contents: write
10+
issues: write
11+
12+
jobs:
13+
delete-quay-tag:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Install regctl
17+
run: |
18+
curl -LO https://github.com/regclient/regclient/releases/latest/download/regctl-linux-amd64
19+
chmod +x regctl-linux-amd64
20+
sudo mv regctl-linux-amd64 /usr/local/bin/regctl
21+
regctl version
22+
23+
- name: Configure regctl authentication
24+
run: |
25+
regctl registry login quay.io -u ${{ secrets.QUAY_USERNAME }} -p ${{ secrets.QUAY_PASSWORD }}
26+
echo "PR number: ${{ github.event.pull_request.number }}"
27+
echo "TAG_TO_DELETE=$(regctl tag ls quay.io/opendatahub/opendatahub-tests --include pr-${{ github.event.pull_request.number }})" >> $GITHUB_ENV
28+
- name: Delete Quay Tag
29+
if: env.TAG_TO_DELETE != ''
30+
run: |
31+
echo "Deleting tag '$TAG_TO_DELETE' from repository..."
32+
regctl tag rm quay.io/opendatahub/opendatahub-tests:pr-${{ github.event.pull_request.number }}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Push Container Image On PR Comment
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
permissions:
8+
pull-requests: write
9+
contents: write
10+
issues: write
11+
12+
jobs:
13+
push-container-on-comment:
14+
if: contains(github.event.comment.body, '/build-push-pr-image')
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout pull request
18+
uses: actions/checkout@v4
19+
with:
20+
ref: refs/pull/${{ github.event.issue.number }}/head
21+
- name: Install uv
22+
uses: astral-sh/setup-uv@v5
23+
24+
- name: Check if the user is authorized
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.RHODS_CI_BOT_PAT }}
27+
GITHUB_PR_NUMBER: ${{ github.event.issue.number }}
28+
GITHUB_EVENT_ACTION: ${{ github.event.action }}
29+
GITHUB_EVENT_REVIEW_STATE: ${{ github.event.review.state }}
30+
GITHUB_EVENT_NAME: ${{ github.event_name }}
31+
COMMENT_BODY: ${{ github.event.comment.body }}
32+
REVIEW_COMMENT_BODY: ${{ github.event.review.body }}
33+
GITHUB_USER_LOGIN: ${{ github.event.sender.login }}
34+
ACTION: "push-container-on-comment"
35+
run: uv run python .github/workflows/scripts/pr_workflow.py
36+
- name: Set env TAG for image
37+
run: |
38+
echo "TAG=pr-${{ github.event.issue.number }}" >> "$GITHUB_ENV"
39+
- name: Build Image to push
40+
id: build-image
41+
uses: redhat-actions/buildah-build@v2
42+
with:
43+
image: opendatahub-tests
44+
tags: ${{ env.TAG }}
45+
containerfiles: |
46+
./Dockerfile
47+
- name: Push To Image Registry
48+
id: push-to-registry
49+
uses: redhat-actions/push-to-registry@v2
50+
with:
51+
image: ${{ steps.build-image.outputs.image }}
52+
tags: ${{ steps.build-image.outputs.tags }}
53+
registry: quay.io/opendatahub
54+
username: ${{ secrets.QUAY_USERNAME }}
55+
password: ${{ secrets.QUAY_PASSWORD }}
56+
57+
- name: Add comment to PR
58+
if: always()
59+
env:
60+
URL: ${{ github.event.issue.comments_url }}
61+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
62+
run: |
63+
curl \
64+
-X POST \
65+
$URL \
66+
-H "Content-Type: application/json" \
67+
-H "Authorization: token $GITHUB_TOKEN" \
68+
--data '{ "body": "Status of building tag ${{ env.TAG }}: ${{ steps.build-image.outcome }}. \nStatus of pushing tag ${{ env.TAG }} to image registry: ${{ steps.push-to-registry.outcome }}." }'

.github/workflows/scripts/pr_workflow.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ class SupportedActions:
3232
add_remove_labels_action_name: str = "add-remove-labels"
3333
pr_size_action_name: str = "add-pr-size-label"
3434
welcome_comment_action_name: str = "add-welcome-comment"
35+
build_push_pr_image_action_name: str = "push-container-on-comment"
3536
supported_actions: set[str] = {
3637
pr_size_action_name,
3738
add_remove_labels_action_name,
3839
welcome_comment_action_name,
40+
build_push_pr_image_action_name,
3941
}
4042

4143
def __init__(self) -> None:
@@ -55,7 +57,7 @@ def __init__(self) -> None:
5557
def verify_base_config(self) -> None:
5658
if not self.action or self.action not in self.SupportedActions.supported_actions:
5759
sys.exit(
58-
"`ACTION` is not set in workflow or is not supported. "
60+
f"{self.action} is not set in workflow or is not supported. "
5961
f"Supported actions: {self.SupportedActions.supported_actions}"
6062
)
6163

@@ -77,8 +79,8 @@ def verify_base_config(self) -> None:
7779
)
7880

7981
def set_gh_config(self) -> None:
80-
gh_client: Github = Github(login_or_token=self.github_token)
81-
self.repo = gh_client.get_repo(full_name_or_id=self.repo_name)
82+
self.gh_client: Github = Github(login_or_token=self.github_token)
83+
self.repo = self.gh_client.get_repo(full_name_or_id=self.repo_name)
8284
self.pr = self.repo.get_pull(number=self.pr_number)
8385

8486

@@ -88,9 +90,10 @@ def __init__(self) -> None:
8890
self.user_login = os.getenv("GITHUB_USER_LOGIN")
8991
self.review_state = os.getenv("GITHUB_EVENT_REVIEW_STATE")
9092
self.comment_body = os.getenv("COMMENT_BODY", "")
93+
if self.event_name == "pull_request_review":
94+
self.comment_body = os.getenv("REVIEW_COMMENT_BODY", "")
9195
self.last_commit = list(self.pr.get_commits())[-1]
9296
self.last_commit_sha = self.last_commit.sha
93-
9497
self.verify_labeler_config()
9598

9699
def verify_labeler_config(self) -> None:
@@ -107,12 +110,31 @@ def verify_labeler_config(self) -> None:
107110
if self.event_name == "pull_request_review" and not self.review_state:
108111
sys.exit("`GITHUB_EVENT_REVIEW_STATE` is not set")
109112

113+
def verify_allowed_user(self) -> bool:
114+
org = self.gh_client.get_organization("opendatahub-io")
115+
# slug is the team name with replaced special characters,
116+
# all words to lowercase and spaces replace with a -
117+
try:
118+
team = org.get_team_by_slug("opendatahub-tests-contributors")
119+
# check if the user is a member of opendatahub-tests-contributors
120+
membership = team.get_team_membership(member=self.user_login)
121+
LOGGER.info(f"User {self.user_login} is a member of the test contributor team. {membership}")
122+
return True
123+
except UnknownObjectException:
124+
LOGGER.error(f"User {self.user_login} is not allowed for this action. Exiting.")
125+
return False
126+
110127
def run_pr_label_action(self) -> None:
111128
if self.action == self.SupportedActions.pr_size_action_name:
112129
self.set_pr_size()
113130

131+
if self.action == self.SupportedActions.build_push_pr_image_action_name:
132+
if not self.verify_allowed_user():
133+
sys.exit(1)
134+
114135
if self.action == self.SupportedActions.add_remove_labels_action_name:
115-
self.add_remove_pr_labels()
136+
if self.verify_allowed_user():
137+
self.add_remove_pr_labels()
116138

117139
if self.action == self.SupportedActions.welcome_comment_action_name:
118140
self.add_welcome_comment()

0 commit comments

Comments
 (0)