-
Notifications
You must be signed in to change notification settings - Fork 14
98 lines (89 loc) · 4.35 KB
/
comment-cfg-grammar-changes.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
name: Comment config grammar changes (PR)
on:
workflow_run:
workflows: [Check config grammar changes (PR)]
types:
- completed
permissions:
pull-requests: write
jobs:
update-or-remove-comment-on-pr:
runs-on: ubuntu-latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
# The download-artifact action cannot download artifacts from other workflows.
# Copied from https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_run
- name: Download comment artifact
uses: actions/github-script@v7
with:
script: |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name == "comment"
})[0];
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/comment.zip`, Buffer.from(download.data));
- name: Unzip comment artifact
run: unzip comment.zip
- name: Get PR ID
id: pr-id
run: |
# The pull_requests array always come empty when querying a workflow run's data if it is started from a fork. This might be a GitHub bug.
# There is also no json field in the `gh run view` command, which could give us the PR ID, so we can only query it based on the fork and branch.
# See https://github.com/orgs/community/discussions/25220
if [[ "${{ github.event.workflow_run.head_repository.url }}" == "${{ github.event.workflow_run.repository.url }}" ]]; then
PR_ID=$(gh pr view -R "${{ github.repository }}" "${{ github.event.workflow_run.head_branch }}" --json "number" --jq ".number")
else
# PR is from a fork
PR_ID=$(gh pr view -R "${{ github.repository }}" "${{ github.event.workflow_run.head_repository.owner.login }}:${{ github.event.workflow_run.head_branch }}" --json "number" --jq ".number")
fi
echo "pr-id=${PR_ID}" >> $GITHUB_OUTPUT
- name: Update or remove PR comment
run: |
EXISTING_COMMENT_ID=$( \
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${{ github.repository }}/issues/${{ steps.pr-id.outputs.pr-id }}/comments \
--jq '.[] | select(.user.login=="github-actions[bot]") | select(.user.type=="Bot") | select(.user.id==41898282) | select(.body | startswith("#### This Pull Request introduces config grammar changes")) | .id')
if [[ -s "${GITHUB_WORKSPACE}/comment" ]]; then
if [[ -n "${EXISTING_COMMENT_ID}" ]]; then
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--method PATCH \
/repos/${{ github.repository }}/issues/comments/${EXISTING_COMMENT_ID} \
--input ${GITHUB_WORKSPACE}/comment
echo -e "\n::notice::Updated comment: ${EXISTING_COMMENT_ID}"
else
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--method POST \
/repos/${{ github.repository }}/issues/${{ steps.pr-id.outputs.pr-id }}/comments \
--input ${GITHUB_WORKSPACE}/comment
echo -e "\n::notice::Created new comment"
fi
else
if [[ -n "${EXISTING_COMMENT_ID}" ]]; then
gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
--method DELETE \
/repos/${{ github.repository }}/issues/comments/${EXISTING_COMMENT_ID}
echo -e "\n::notice::Removed comment: ${EXISTING_COMMENT_ID}"
else
echo -e "\n::notice::Nothing to do"
fi
fi