Skip to content

Commit 093ff06

Browse files
authored
Dev update muted tests wf (#16456)
1 parent 1408171 commit 093ff06

File tree

4 files changed

+189
-46
lines changed

4 files changed

+189
-46
lines changed
Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,101 @@
1-
# .github/scripts/create_or_update_pr.py
1+
#!/usr/bin/env python3
22
import os
3+
import argparse
34
from github import Github
45

5-
def create_or_update_pr():
6-
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
7-
BASE_BRANCH = os.getenv('BASE_BRANCH')
8-
BRANCH_FOR_PR = os.getenv('BRANCH_FOR_PR')
9-
TITLE = os.getenv('TITLE')
10-
BODY = os.getenv('BODY')
11-
REVIEWERS = os.getenv('REVIEWERS').split(',') if os.getenv('REVIEWERS') else []
6+
def read_body_from_file(file_path):
7+
with open(file_path, 'r') as file:
8+
return file.read()
129

13-
g = Github(GITHUB_TOKEN)
14-
repo = g.get_repo(os.getenv('GITHUB_REPOSITORY'))
10+
def get_body_content(body_input):
11+
"""Determines if the body content is a file path or direct text."""
12+
if os.path.isfile(body_input):
13+
print(f"Body content will be read from file: {body_input}.")
14+
return read_body_from_file(body_input)
15+
else:
16+
print(f"Body content will be taken directly: '{body_input}.'")
17+
return body_input
18+
19+
def create_or_update_pr(args, repo):
20+
current_pr = None
21+
pr_number = None
22+
body = get_body_content(args.body)
1523

1624
# Check for an existing PR
17-
existing_prs = repo.get_pulls(head=BRANCH_FOR_PR, base=BASE_BRANCH, state='open')
18-
existing_pr = None
25+
existing_prs = repo.get_pulls(head=args.branch_for_pr, base=args.base_branch, state='open')
1926
for pr in existing_prs:
20-
if pr.title == TITLE:
21-
existing_pr = pr
27+
if pr.base.ref == args.base_branch and pr.head.ref == args.branch_for_pr:
28+
current_pr = pr
2229
break
23-
24-
if existing_pr:
25-
print(f"Existing PR found. Updating PR #{existing_pr.number}.")
26-
# Update existing PR
27-
existing_pr.edit(title=TITLE, body=BODY)
28-
# Add reviewers
29-
if REVIEWERS:
30-
existing_pr.create_review_request(reviewers=REVIEWERS)
30+
if current_pr:
31+
print(f"Existing PR found. Updating PR #{current_pr.number}.")
32+
current_pr.edit(title=args.title, body=body)
3133
else:
3234
print("No existing PR found. Creating a new PR.")
33-
# Create new PR
34-
pr = repo.create_pull(title=TITLE, body=BODY, head=BRANCH_FOR_PR, base=BASE_BRANCH)
35-
# Add reviewers
36-
if REVIEWERS:
37-
pr.create_review_request(reviewers=REVIEWERS)
35+
current_pr = repo.create_pull(title=args.title, body=body, head=args.branch_for_pr, base=args.base_branch)
36+
37+
if args.reviewers:
38+
reviewers = args.reviewers.split(',')
39+
print(f"Requesting review from: {', '.join(reviewers)}")
40+
current_pr.create_review_request(reviewers=reviewers)
41+
42+
pr_number = current_pr.number
43+
if os.environ['GITHUB_OUTPUT']:
44+
with open(os.environ['GITHUB_OUTPUT'], 'a') as gh_out:
45+
print(f"pr_number={pr_number}", file=gh_out)
46+
47+
print(f"PR created or updated successfully. PR number: {pr_number}")
48+
49+
def append_to_pr_body(args, repo):
50+
body_to_append = get_body_content(args.body)
51+
52+
pr = None
53+
if args.pr_number:
54+
pr = repo.get_pull(args.pr_number)
55+
else:
56+
existing_prs = repo.get_pulls(head=args.branch_for_pr, base=args.base_branch, state='open')
57+
for p in existing_prs:
58+
if p.base.ref == args.base_branch and p.head.ref == args.branch_for_pr:
59+
pr = p
60+
break
61+
62+
if pr:
63+
print(f"Appending to PR #{pr.number}.")
64+
current_body = pr.body or ""
65+
new_body = current_body + "\n\n" + body_to_append
66+
pr.edit(body=new_body)
67+
else:
68+
print("No matching pull request found to append body.")
3869

3970
if __name__ == '__main__':
40-
create_or_update_pr()
71+
parser = argparse.ArgumentParser(description='Operate on a GitHub Pull Request')
72+
subparsers = parser.add_subparsers(dest='mode', required=True, help='Mode of operation')
73+
74+
# Subparser for create or update PR mode
75+
create_parser = subparsers.add_parser('create_or_update', help='Create or update a pull request')
76+
create_parser.add_argument('--base_branch', type=str, required=True, help='Base branch for the PR')
77+
create_parser.add_argument('--branch_for_pr', type=str, required=True, help='Branch from which to create the PR')
78+
create_parser.add_argument('--title', type=str, required=True, help='Title of the PR')
79+
create_parser.add_argument('--body', type=str, default='', required=False, help='Body content of the PR, or path to a file with the content')
80+
create_parser.add_argument('--reviewers', type=str, default='', help='Comma separated list of reviewers')
81+
82+
# Subparser for append PR body mode
83+
append_parser = subparsers.add_parser('append_pr_body', help='Append text to the body of an existing pull request')
84+
group = append_parser.add_mutually_exclusive_group(required=True)
85+
group.add_argument('--pr_number', type=int, help='Pull request number')
86+
append_parser.add_argument('--body', type=str, required=True, help='Text to append to the PR body')
87+
88+
args = parser.parse_args()
89+
90+
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
91+
if not GITHUB_TOKEN:
92+
raise ValueError("GITHUB_TOKEN environment variable is not set")
93+
94+
g = Github(GITHUB_TOKEN)
95+
repo_name = os.getenv('GITHUB_REPOSITORY', 'ydb-platform/ydb')
96+
repo = g.get_repo(repo_name)
97+
98+
if args.mode == "create_or_update":
99+
create_or_update_pr(args, repo)
100+
elif args.mode == "append_pr_body":
101+
append_to_pr_body(args, repo)

.github/scripts/tests/create_new_muted_ya.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,16 @@ def create_mute_issues(all_tests, file_path):
348348

349349
print("\n\n")
350350
print("\n".join(results))
351+
if os.environ['GITHUB_OUTPUT']:
352+
353+
file_path = os.path.join(os.getcwd(), "created_issues.txt")
354+
with open(file_path, 'w') as f:
355+
f.write("```\n")
356+
f.write("\n".join(results))
357+
f.write("\n```")
358+
359+
with open(os.environ['GITHUB_OUTPUT'], 'a') as gh_out:
360+
gh_out.write(f"created_issues_file={file_path}")
351361

352362

353363
def mute_worker(args):
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Create issues for muted tests
2+
3+
on:
4+
pull_request:
5+
types:
6+
- labeled
7+
pull_request_review:
8+
types:
9+
- submitted
10+
env:
11+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
12+
BRANCH_FOR_PR: update-muted-ya
13+
BASE_BRANCH: main
14+
MUTED_YA_FILE_PATH: .github/scripts/tests/muted_ya.txt
15+
16+
jobs:
17+
run-python-script:
18+
runs-on: ubuntu-latest
19+
if: github.event_name == 'pull_request_review' && github.event.review.state == 'approved' && contains(github.pull_request.labels.*.name, 'update_muted_ya')
20+
steps:
21+
- name: Checkout repository
22+
uses: actions/checkout@v4
23+
with:
24+
ref: ${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }}
25+
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip
29+
pip install ydb[yc] PyGithub
30+
31+
- name: Setup ydb access
32+
uses: ./.github/actions/setup_ci_ydb_service_account_key_file_credentials
33+
with:
34+
ci_ydb_service_account_key_file_credentials: ${{ secrets.CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS }}
35+
36+
- name: Create issues for muted tests
37+
id: create_issues
38+
run: |
39+
.github/scripts/tests/create_new_muted_ya.py create_issues --file_path=${{ github.workspace }}/${{ env.MUTED_YA_FILE_PATH }}
40+
41+
- name: Add issues to PR
42+
env:
43+
GITHUB_TOKEN: ${{ env.GH_TOKEN }}
44+
run: |
45+
python .github/scripts/create_or_update_pr.py append_pr_body --pr_number=${{ github.event.pull_request.number }} --body=${{ steps.create_issues.outputs.created_issues_file }}
46+

.github/workflows/update_muted_ya.yml

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ env:
88
BRANCH_FOR_PR: update-muted-ya
99
TITLE: "Update muted_ya.txt"
1010
BASE_BRANCH: main
11-
REVIEWERS: ydb-platform/ci
11+
REVIEWERS: ci
1212

1313
jobs:
1414
create-or-update-muted-ya:
@@ -38,10 +38,10 @@ jobs:
3838
git fetch origin
3939

4040
# Checkout BRANCH_FOR_PR, create if it doesn't exist based on BASE_BRANCH
41-
if git show-ref --quiet refs/heads/${{ env.BRANCH_FOR_PR }}; then
42-
git checkout ${{ env.BRANCH_FOR_PR }}
41+
if git show-ref --quiet refs/heads/${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }}; then
42+
git checkout ${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }}
4343
else
44-
git checkout -b ${{ env.BRANCH_FOR_PR }} origin/${{ env.BASE_BRANCH }}
44+
git checkout -b ${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }} origin/${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }}
4545
fi
4646

4747
# Attempt to rebase BRANCH_FOR_PR onto BASE_BRANCH
@@ -54,12 +54,8 @@ jobs:
5454
# Reset the branch to BASE_BRANCH
5555
git reset --hard origin/${{ env.BASE_BRANCH }}
5656

57-
# Force push the reset branch to remote
58-
git push origin ${{ env.BRANCH_FOR_PR }} --force
59-
else
60-
# If rebase is successful, push the updated branch
61-
git push origin ${{ env.BRANCH_FOR_PR }}
6257
fi
58+
git push origin ${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }} --force
6359

6460
- name: Run the script
6561
run: |
@@ -84,6 +80,8 @@ jobs:
8480
id: pr_description
8581
run: |
8682
PR_BODY=''
83+
PR_BODY_FILE="pr_body_content.txt"
84+
8785
if [ -s mute_update/deleted_tests_in_mute_debug.txt ]; then
8886
DELETED_COUNT=$(wc -l < mute_update/deleted_tests_in_mute_debug.txt)
8987
PR_BODY+=$'**Removed from mute: '"${DELETED_COUNT}**"$'\n\n'
@@ -106,8 +104,10 @@ jobs:
106104
PR_BODY+=$'\n```\n\n'
107105
fi
108106
109-
# Use printf to handle special characters and newlines
110-
printf "PR_BODY<<EOF\n%s\nEOF\n" "$PR_BODY" >> $GITHUB_ENV
107+
# Save PR_BODY to the file
108+
echo "$PR_BODY" > "$PR_BODY_FILE"
109+
# Export the path to the file to the GitHub environment
110+
echo "PR_BODY_PATH=$PR_BODY_FILE" >> $GITHUB_ENV
111111
112112
- name: Stage changes if any
113113
if: env.changes == 'true'
@@ -127,7 +127,7 @@ jobs:
127127
uses: ad-m/github-push-action@v0.8.0
128128
with:
129129
github_token: ${{ secrets.GITHUB_TOKEN }}
130-
branch: ${{ env.BRANCH_FOR_PR }}
130+
branch: ${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }}
131131
force: true
132132

133133
- name: Install PyGithub
@@ -138,10 +138,36 @@ jobs:
138138
id: create_or_update_pr
139139
env:
140140
GITHUB_TOKEN: ${{ env.GH_TOKEN }}
141-
BRANCH_FOR_PR: ${{ env.BRANCH_FOR_PR }}
142-
TITLE: ${{ env.TITLE }}
143-
BASE_BRANCH: ${{ env.BASE_BRANCH }}
144-
BODY: ${{ env.PR_BODY }}
145-
REVIEWERS: ${{ env.REVIEWERS }}
146141
run: |
147-
python .github/scripts/create_or_update_pr.py
142+
python .github/scripts/create_or_update_pr.py create_or_update --base_branch="${{ env.BASE_BRANCH }}" --branch_for_pr="${{ env.BRANCH_FOR_PR }}_${{ env.BASE_BRANCH }}" --title="${{ env.TITLE }}" --body="${{ env.BODY }}"
143+
144+
145+
- name: Comment PR
146+
uses: actions/github-script@v7
147+
with:
148+
script: |
149+
github.rest.issues.createComment({
150+
issue_number: ${{ steps.create_or_update_pr.outputs.pr_number }},
151+
owner: ${{ github.repository_owner }},
152+
repo: ${{ github.event.repository.name }},
153+
body: ${{ env.PR_BODY }}
154+
155+
});
156+
157+
github.rest.issues.addLabels({
158+
...context.repo,
159+
issue_number: ${{ steps.create_or_update_pr.outputs.pr_number }},
160+
labels: 'update_muted_ya'
161+
});
162+
163+
- name: Add reviewers
164+
uses: octokit/request-action@v2.x
165+
with:
166+
route: POST /repos/{owner}/{repo}/pulls/{pull_number}/requested_reviewers
167+
owner: ${{ github.repository_owner }}
168+
repo: ${{ github.event.repository.name }}
169+
pull_number: ${{ steps.create_or_update_pr.outputs.pr_number }}
170+
team_reviewers: ${{ env.REVIEWERS }}
171+
token: ${{ secrets.GITHUB_TOKEN }}
172+
173+

0 commit comments

Comments
 (0)