Skip to content

Commit 4f398bf

Browse files
authored
Verify code format changes come from the github-actions bot (microsoft#8253)
The change makes it so that code format changes are only applied if the comment was updated by the github-actions identity.
1 parent 495e369 commit 4f398bf

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

.github/workflows/clang-format-checker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
--end-rev $END_REV \
8383
--changed-files "$CHANGED_FILES"
8484
apply_diff:
85-
if: ${{ github.event_name == 'issue_comment' && endsWith(github.event.comment.body, '- [x] Check this box to apply formatting changes to this branch.') }}
85+
if: ${{ github.event_name == 'issue_comment' && github.event.comment.user.login == 'github-actions[bot]' && endsWith(github.event.comment.body, '- [x] Check this box to apply formatting changes to this branch.') }}
8686
runs-on: ubuntu-latest
8787
env:
8888
TMP_DIFF_FILE: /tmp/diff.patch

utils/git/code-format-save-diff.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
CR = '\r'
2525

2626

27+
COMMENT_TAG = "<!--LLVM CODE FORMAT COMMENT:"
28+
29+
2730
def get_diff_from_comment(comment: IssueComment.IssueComment) -> str:
2831
diff_pat = re.compile(r"``````````diff(?P<DIFF>.+)``````````", re.DOTALL)
2932
m = re.search(diff_pat, comment.body)
@@ -50,6 +53,18 @@ def apply_patches(args: argparse.Namespace) -> None:
5053
if comment is None:
5154
raise Exception(f"Comment {args.comment_id} does not exist")
5255

56+
if comment.user.login != "github-actions[bot]":
57+
raise Exception(
58+
f"Comment {args.comment_id} was not created by the expected bot "
59+
f"(author: {comment.user.login})"
60+
)
61+
62+
if COMMENT_TAG not in comment.body:
63+
raise Exception(
64+
f"Comment {args.comment_id} does not contain the expected "
65+
f"format comment tag"
66+
)
67+
5368
# get the diff from the comment
5469
diff = get_diff_from_comment(comment)
5570

0 commit comments

Comments
 (0)