Skip to content

Commit 39c3efb

Browse files
committed
Add autofix reminder and label manager
Remind query authors to validate their changes in autofix before merging.
1 parent 46b92f3 commit 39c3efb

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This workflow ensures that if the "No Autofix Validation Required" label is
2+
# added to a pull request, the "Autofix Validation Required" label is removed.
3+
name: Autofix Label Manager
4+
5+
on:
6+
pull_request_target:
7+
types: [labeled]
8+
9+
# Allows manual triggering of the workflow for testing
10+
workflow_dispatch:
11+
12+
jobs:
13+
check-to-remove-autofix-label:
14+
env:
15+
GITHUB_REPOSITORY: ${{ github.repository }}
16+
PR_NUMBER: ${{ github.event.number }}
17+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
18+
REQUIRES_AUTOFIX_LABEL: "Autofix Validation Required"
19+
DOES_NOT_REQUIRE_AUTOFIX_LABEL: "No Autofix Validation Required"
20+
LABEL_ADDED: ${{ github.event.label.name }}
21+
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Check if label "No Autofix Validation Required" is added
25+
shell: bash
26+
run: |
27+
if [ "$LABEL_ADDED" != "$DOES_NOT_REQUIRE_AUTOFIX_LABEL" ]; then
28+
echo "Label $DOES_NOT_REQUIRE_AUTOFIX_LABEL was not added."
29+
exit 0
30+
fi
31+
32+
echo "Label $DOES_NOT_REQUIRE_AUTOFIX_LABEL was added."
33+
34+
# Check if Label $REQUIRES_AUTOFIX_LABEL exists and remove it
35+
REQUIRES_AUTOFIX_LABEL_EXISTS=$(gh api /repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels | jq --arg label "Autofix Validation Required" '.[] | select(.name==$label) | .name')
36+
if [ "$REQUIRES_AUTOFIX_LABEL_EXISTS" == "$REQUIRES_AUTOFIX_LABEL" ]; then
37+
gh api -X DELETE "/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/labels/$REQUIRES_AUTOFIX_LABEL"
38+
echo "$REQUIRES_AUTOFIX_LABEL Label removed."
39+
else
40+
echo "$REQUIRES_AUTOFIX_LABEL Label does not exist or was already removed."
41+
fi
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# This workflow creates a reminder to query authors to test their queries
2+
# in autofix.
3+
name: Autofix reminder
4+
5+
permissions:
6+
contents: read
7+
pull-requests: write
8+
issues: write
9+
10+
on:
11+
pull_request:
12+
branches:
13+
- main
14+
- "rc/*"
15+
paths:
16+
- "**/*.qhelp"
17+
- "**/*.ql"
18+
- "**/*.qll"
19+
# This workflow
20+
- ".github/workflows/autofix-reminder.yml"
21+
22+
workflow_dispatch:
23+
24+
jobs:
25+
autofix-reminder:
26+
env:
27+
GITHUB_REPOSITORY: ${{ github.repository }}
28+
PR_NUMBER: ${{ github.event.number }}
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
REQUIRES_AUTOFIX_LABEL: "Autofix Validation Required"
31+
DOES_NOT_REQUIRE_AUTOFIX_LABEL: "No Autofix Validation Required"
32+
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Check existing labels
36+
id: label_check
37+
shell: bash
38+
run: |
39+
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/labels" | jq -r '.[].name' > labels.txt
40+
41+
if grep -q -x -e "${REQUIRES_AUTOFIX_LABEL}" labels.txt || grep -q -x -e "${DOES_NOT_REQUIRE_AUTOFIX_LABEL}" labels.txt; then
42+
echo "Stopping workflow due to label presence."
43+
echo "should_continue=false" >> $GITHUB_OUTPUT
44+
else
45+
echo "Add $REQUIRES_AUTOFIX_LABEL label."
46+
echo "should_continue=true" >> $GITHUB_OUTPUT
47+
fi
48+
49+
- name: Add label
50+
if: steps.label_check.outputs.should_continue == 'true'
51+
run: |
52+
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/labels" -X POST -f "labels[]=$REQUIRES_AUTOFIX_LABEL"
53+
54+
- name: Comment on PR
55+
if: steps.label_check.outputs.should_continue == 'true'
56+
run: gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -X POST --field body="This pull request updates '.ql', '.qll', or '.qhelp' files, Please validate that autofixes generated based on these changes are valid. See [the documentation](https://github.com/github/codeql-team/blob/main/docs/best-practices/validating-autofix-for-query-changes.md) (internal access required). If autofix validation is not required, please add the label '${DOES_NOT_REQUIRE_AUTOFIX_LABEL}' to this pull request."

0 commit comments

Comments
 (0)