Skip to content

Commit

Permalink
[CI] trigger apply format with task list (#6004)
Browse files Browse the repository at this point in the history
Add task list to format fail comment.
- [ ] Check this box to apply formatting changes to this branch.
If the task is checked, trigger apply format.
  • Loading branch information
python3kgae authored Nov 15, 2023
1 parent 364a523 commit 6c0d4b8
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 83 deletions.
76 changes: 74 additions & 2 deletions .github/workflows/clang-format-checker.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
name: "Check code formatting"
on: pull_request_target
on:
pull_request_target:
types: [opened,synchronize]
issue_comment:
types: edited
permissions:
pull-requests: write

jobs:
code_formatter:
if: github.event_name == 'pull_request_target'
runs-on: ubuntu-latest
steps:
- name: Fetch LLVM sources
Expand Down Expand Up @@ -40,15 +45,82 @@ jobs:
run: pip install -r utils/git/requirements_formatting.txt

- name: Run code formatter
id: formatter
env:
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
START_REV: ${{ github.event.pull_request.base.sha }}
END_REV: ${{ github.event.pull_request.head.sha }}
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
python utils/git/code-format-helper.py \
python utils/git/code-format-helper.py \
--token ${{ secrets.GITHUB_TOKEN }} \
--issue-number $GITHUB_PR_NUMBER \
--start-rev $START_REV \
--end-rev $END_REV \
--changed-files "$CHANGED_FILES"
apply_diff:
if: ${{ github.event_name == 'issue_comment' && endsWith(github.event.comment.body, '- [x] Check this box to apply formatting changes to this branch.') }}
runs-on: ubuntu-latest
env:
TMP_DIFF_FILE: /tmp/diff.patch
steps:
- uses: actions/github-script@v3
id: get-pr
with:
script: |
const request = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
}
core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
try {
const result = await github.pulls.get(request)
return result.data
} catch (err) {
core.setFailed(`Request failed with error ${err}`)
}
- name: Fetch LLVM sources
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Setup Python env
uses: actions/setup-python@v4
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: 'utils/git/requirements_formatting.txt'

- name: Install python dependencies
run: pip install -r utils/git/requirements_formatting.txt

- name: Apply code diff
env:
GITHUB_PR_NUMBER: ${{ github.event.issue.number }}
COMMENT_ID: ${{ github.event.comment.id }}
run: |
python utils/git/code-format-save-diff.py \
--token ${{ secrets.GITHUB_TOKEN }} \
--issue-number $GITHUB_PR_NUMBER \
--tmp-diff-file $TMP_DIFF_FILE \
--comment-id $COMMENT_ID
- name: Fetch LLVM sources for head
uses: actions/checkout@v4
with:
fetch-depth: 2
ref: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}

- name: apply diff
run: |
git apply $TMP_DIFF_FILE
git add .
- name: Commit & Push changes
uses: actions-js/push@master
with:
branch: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
github_token: ${{ secrets.GITHUB_TOKEN }}
61 changes: 0 additions & 61 deletions .github/workflows/clang-format-diff-apply.yml

This file was deleted.

3 changes: 2 additions & 1 deletion utils/git/code-format-helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def pr_comment_text(self, diff: str) -> str:
``````````
</details>
"""
- [ ] Check this box to apply formatting changes to this branch."""

def find_comment(
self, pr: PullRequest.PullRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
#
# ====- code-format-diff-apply, apply diff from comment --*- python -*-------==#
# ====- code-format-save-diff, save diff from comment --*- python -*---------==#
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
Expand All @@ -23,9 +23,9 @@
CRLF = '\r\n'
CR = '\r'

diff_pat = re.compile(r"``````````diff(?P<DIFF>.+)``````````", re.DOTALL)

def get_diff_from_comment(comment: IssueComment.IssueComment) -> str:
diff_pat = re.compile(r"``````````diff(?P<DIFF>.+)``````````", re.DOTALL)
m = re.search(diff_pat, comment.body)
if m is None:
raise Exception(f"Could not find diff in comment {comment.id}")
Expand Down Expand Up @@ -54,25 +54,13 @@ def apply_patches(args: argparse.Namespace) -> None:
diff = get_diff_from_comment(comment)

# write diff to temporary file and apply
with tempfile.NamedTemporaryFile() as tmp:
if os.path.exists(args.tmp_diff_file):
os.remove(args.tmp_diff_file)

with open(args.tmp_diff_file, 'w+') as tmp:
tmp.write(diff.encode("utf-8"))
tmp.flush()

# run git apply tmp.name
apply_cmd = [
"git",
"apply",
tmp.name
]
run_cmd(apply_cmd)

# run git add .
add_cmd = [
"git",
"add",
"."
]
run_cmd(add_cmd)
tmp.close()

if __name__ == "__main__":
parser = argparse.ArgumentParser()
Expand All @@ -87,6 +75,12 @@ def apply_patches(args: argparse.Namespace) -> None:
)
parser.add_argument("--issue-number", type=int, required=True)
parser.add_argument("--comment-id", type=int, required=True)
parser.add_argument(
"--tmp-diff-file",
type=str,
required=True,
help="Temporary file to write diff to",
)

args = parser.parse_args()

Expand Down

0 comments on commit 6c0d4b8

Please sign in to comment.