Skip to content

Commit 1cddcdf

Browse files
authored
Merge pull request github#6123 from tamasvajk/feature/framework-coverage-pr
Add scheduled job to update framework coverage
2 parents 28c060e + ad6e47b commit 1cddcdf

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Update framework coverage reports
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: "0 0 * * *"
7+
8+
jobs:
9+
update:
10+
name: Update framework coverage report
11+
if: github.event.repository.fork == false
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Dump GitHub context
16+
env:
17+
GITHUB_CONTEXT: ${{ toJSON(github.event) }}
18+
run: echo "$GITHUB_CONTEXT"
19+
- name: Clone self (github/codeql)
20+
uses: actions/checkout@v2
21+
with:
22+
path: ql
23+
fetch-depth: 0
24+
- name: Set up Python 3.8
25+
uses: actions/setup-python@v2
26+
with:
27+
python-version: 3.8
28+
- name: Download CodeQL CLI
29+
env:
30+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
run: |
32+
gh release download --repo "github/codeql-cli-binaries" --pattern "codeql-linux64.zip"
33+
- name: Unzip CodeQL CLI
34+
run: unzip -d codeql-cli codeql-linux64.zip
35+
36+
- name: Generate coverage files
37+
run: |
38+
PATH="$PATH:codeql-cli/codeql" python ql/misc/scripts/library-coverage/generate-report.py ci ql ql
39+
40+
- name: Create pull request with changes
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
run: |
44+
python ql/misc/scripts/library-coverage/create-pr.py ql "$GITHUB_REPOSITORY"
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import sys
2+
import settings
3+
import utils
4+
import shutil
5+
import json
6+
import os
7+
8+
"""
9+
This script compares the generated CSV coverage files with the ones in the codebase. And if they are different, it
10+
creates a PR with the changes. If a PR already exists, then it will be updated to reflect the latest changes.
11+
"""
12+
13+
# Name of the branch that's used to push the updated coverage files, also the head of the PR
14+
branch_name = "workflow/coverage/update"
15+
# Name of the remote where the new commit is pushed
16+
remote = "origin"
17+
# Name of the branch that provides the base for the new branch, and the base of the PR
18+
main = "main"
19+
20+
repo = sys.argv[2]
21+
owner = repo.split('/')[0]
22+
23+
24+
def overwrite_files():
25+
languages = ['java']
26+
for lang in languages:
27+
repo_output_rst = settings.repo_output_rst.format(language=lang)
28+
repo_output_csv = settings.repo_output_csv.format(language=lang)
29+
30+
generated_output_rst = settings.generated_output_rst.format(
31+
language=lang)
32+
generated_output_csv = settings.generated_output_csv.format(
33+
language=lang)
34+
35+
exists = utils.check_file_exists(generated_output_rst)
36+
if not exists:
37+
print(
38+
f"Generated RST file {generated_output_rst} is missing", file=sys.stderr)
39+
sys.exit(1)
40+
41+
exists = utils.check_file_exists(generated_output_csv)
42+
if not exists:
43+
print(
44+
f"Generated RST file {generated_output_csv} is missing", file=sys.stderr)
45+
sys.exit(1)
46+
47+
shutil.move(generated_output_rst, repo_output_rst)
48+
shutil.move(generated_output_csv, repo_output_csv)
49+
50+
51+
def get_pr_number(repo, owner, from_branch, to_branch):
52+
ids = utils.subprocess_check_output(["gh", "api", "-X", "GET", f"repos/{repo}/pulls", "-f",
53+
f"name={to_branch}", "-f", f"head={owner}:{from_branch}", "--jq", "[.[].number]"])
54+
55+
ids = json.loads(ids)
56+
57+
if len(ids) > 1:
58+
print(
59+
f"Found more than one PR that matches the branches. {ids}", file=sys.stderr)
60+
sys.exit(1)
61+
62+
if len(ids) == 1:
63+
print(f"Matching PR found: {ids[0]}")
64+
return ids[0]
65+
66+
return 0
67+
68+
69+
def create_pr(repo, from_branch, to_branch):
70+
print(f"Creating PR for {from_branch}")
71+
utils.subprocess_check_output(["gh", "pr", "create", "-R", repo, "--base", to_branch,
72+
"--head", from_branch, "--title", "Update CSV framework coverage reports",
73+
"--body", "This PR changes the CSV framework coverage reports."])
74+
75+
76+
working_dir = ""
77+
if len(sys.argv) > 1:
78+
working_dir = sys.argv[1]
79+
else:
80+
print("Working directory is not specified")
81+
exit(1)
82+
83+
found_diff = False
84+
overwrite_files()
85+
86+
os.chdir(working_dir)
87+
88+
already_open_pr = get_pr_number(repo, owner, branch_name, main)
89+
try:
90+
utils.subprocess_check_output(["git", "diff", "--exit-code"])
91+
print("No differences found")
92+
found_diff = False
93+
except:
94+
print("Differences found")
95+
found_diff = True
96+
97+
if not found_diff:
98+
if already_open_pr != 0:
99+
# close the PR
100+
utils.subprocess_run(
101+
["gh", "pr", "close", str(already_open_pr), "-R", repo])
102+
else:
103+
utils.subprocess_run(["git", "config", "user.name",
104+
"github-actions[bot]"])
105+
utils.subprocess_run(["git", "config", "user.email",
106+
"41898282+github-actions[bot]@users.noreply.github.com"])
107+
utils.subprocess_run(["git", "add", "."])
108+
utils.subprocess_run(
109+
["git", "commit", "-m", "Add changed framework coverage reports"])
110+
utils.subprocess_run(["git", "branch", "-f", branch_name, main])
111+
utils.subprocess_run(["git", "push", "-f", remote, branch_name])
112+
if already_open_pr == 0:
113+
create_pr(repo, branch_name, main)

0 commit comments

Comments
 (0)