-
Notifications
You must be signed in to change notification settings - Fork 9.7k
132 lines (119 loc) · 4.95 KB
/
Copy pathcomment.atom.yml
File metadata and controls
132 lines (119 loc) · 4.95 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
name: comment.atom
on:
workflow_run:
workflows: [ci]
types: [completed]
permissions:
actions: read
contents: read
issues: write
pull-requests: write
concurrency:
group: comment-${{ github.event.workflow_run.id }}
cancel-in-progress: false
jobs:
comment:
name: Publish handoff comments
if: ${{ github.repository == 'nexu-io/open-design' && github.event.workflow_run.event == 'pull_request' }}
runs-on: ubuntu-24.04
timeout-minutes: 10
steps:
- name: Checkout trusted workflow code
uses: actions/checkout@v6.0.2
with:
repository: ${{ github.repository }}
ref: ${{ github.event.repository.default_branch }}
- name: Check handoff helper
run: python3 .github/scripts/handoff.py self-check
- name: Resolve comment handoff artifacts
id: artifacts
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
RUN_ID: ${{ github.event.workflow_run.id }}
shell: bash
run: |
set -euo pipefail
pattern="$(python3 .github/scripts/handoff.py artifact-pattern comment)"
count="$(
gh api "repos/$REPO/actions/runs/$RUN_ID/artifacts" \
--jq '.artifacts[]? | select(.expired == false and (.name | startswith("handoff-comment-"))) | .name' \
| sed '/^$/d' \
| wc -l \
| tr -d ' '
)"
if [ "$count" = "0" ]; then
echo "found=false" >> "$GITHUB_OUTPUT"
exit 0
fi
{
echo "found=true"
echo "pattern=$pattern"
} >> "$GITHUB_OUTPUT"
- name: Download comment handoff artifacts
if: ${{ steps.artifacts.outputs.found == 'true' }}
uses: actions/download-artifact@v8
with:
pattern: ${{ steps.artifacts.outputs.pattern }}
run-id: ${{ github.event.workflow_run.id }}
path: ${{ runner.temp }}/handoff-comment
merge-multiple: false
github-token: ${{ github.token }}
- name: Upsert handoff comments
if: ${{ steps.artifacts.outputs.found == 'true' }}
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
shell: bash
run: |
set -euo pipefail
root="$RUNNER_TEMP/handoff-comment"
helper=".github/scripts/handoff.py"
while IFS= read -r entry_json; do
id="$(jq -r '.id' <<< "$entry_json")"
pr_number="$(jq -r '.pr_number' <<< "$entry_json")"
head_sha="$(jq -r '.head_sha' <<< "$entry_json")"
base_sha="$(jq -r '.base_sha' <<< "$entry_json")"
marker="$(jq -r '.marker' <<< "$entry_json")"
body_path="$(jq -r '.body_path' <<< "$entry_json")"
if [ "$head_sha" != "$RUN_HEAD_SHA" ]; then
echo "Skipping comment handoff $id because artifact head $head_sha does not match workflow_run head $RUN_HEAD_SHA."
continue
fi
pr_json="$(gh api "repos/$REPO/pulls/$pr_number")"
pr_state="$(jq -r '.state' <<< "$pr_json")"
pr_draft="$(jq -r '.draft' <<< "$pr_json")"
current_head="$(jq -r '.head.sha' <<< "$pr_json")"
current_base="$(jq -r '.base.sha' <<< "$pr_json")"
if [ "$pr_state" != "open" ]; then
echo "Skipping comment handoff $id because PR $pr_number state is $pr_state."
continue
fi
if [ "$pr_draft" != "false" ]; then
echo "Skipping comment handoff $id because PR $pr_number is draft."
continue
fi
if [ "$current_head" != "$head_sha" ]; then
echo "Skipping stale comment handoff $id for $head_sha; current PR head is $current_head."
continue
fi
if [ "$current_base" != "$base_sha" ]; then
echo "Skipping stale comment handoff $id for base $base_sha; current PR base is $current_base."
continue
fi
payload_file="$RUNNER_TEMP/comment-$id.json"
jq -n --rawfile body "$body_path" '{body: $body}' > "$payload_file"
comment_id="$(
gh api --paginate "repos/$REPO/issues/$pr_number/comments" \
| jq -r --arg marker "$marker" '.[] | select((.body // "") | contains($marker)) | .id' \
| tail -n 1 || true
)"
if [ -n "$comment_id" ]; then
gh api --method PATCH "repos/$REPO/issues/comments/$comment_id" --input "$payload_file" >/dev/null
echo "Updated comment handoff $id on PR $pr_number."
else
gh api --method POST "repos/$REPO/issues/$pr_number/comments" --input "$payload_file" >/dev/null
echo "Created comment handoff $id on PR $pr_number."
fi
done < <(python3 "$helper" list comment "$root")