Repository navigation
294 lines (268 loc) · 15.5 KB
/
Copy pathlink-issue.yml
File metadata and controls
294 lines (268 loc) · 15.5 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
name: Link PR to Issue
on:
pull_request_target:
types: [ opened, reopened, edited, synchronize ]
jobs:
determine_issue_number:
name: Determine issue number
runs-on: ubuntu-slim
if: >
(github.repository == 'JabRef/jabref') &&
(github.event.pull_request.head.repo.full_name != 'JabRef/jabref') &&
!(
(github.event.pull_request.user.login == 'dependabot[bot]') || (github.event.pull_request.user.login == 'renovate-bot') ||
(
startsWith(github.event.pull_request.title, '[Bot] ') ||
startsWith(github.event.pull_request.title, 'Bump ') ||
startsWith(github.event.pull_request.title, 'New Crowdin updates') ||
startsWith(github.event.pull_request.title, 'Update Gradle Wrapper from')
)
)
permissions:
contents: read
issues: read
outputs:
issue_number: ${{ steps.get_issue_number.outputs.ticketNumber }}
needs_triage: ${{ steps.needs_triage.outputs.needs_triage }}
allow_missing_issue_link: ${{ steps.allow_missing_issue_link.outputs.allowed }}
steps:
- name: echo PR data
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_URL: ${{ github.event.pull_request.html_url }}
PR_BODY: ${{ github.event.pull_request.body }}
run: |
echo "PR Number: $PR_NUMBER"
echo "PR URL: $PR_URL"
printf 'PR Body:\n%s\n' "$PR_BODY"
- name: Determine issue number
id: get_issue_number
uses: koppor/ticket-check-action@5c5d28364fe8d3c2822611e0556fcacdde473b49 # add-output
with:
token: ${{ secrets.GITHUB_TOKEN }}
ticketLink: 'https://github.com/JabRef/jabref/issues/%ticketNumber%'
ticketPrefix: '#'
titleRegex: '^#(?<ticketNumber>\d+)'
branchRegex: '^(?<ticketNumber>\d+)'
# Matches GitHub's closes/fixes/resolves #{number}, but does not match our example `Closes #13109` in PULL_REQUEST_TEMPLATE
# Also matches URLs that are wrapped in `<>`.
bodyRegex: '(?<action>fixes|closes|resolves|refs)\s+<?(?:https?:\/\/github\.com\/JabRef\/jabref\/issues\/)?#?(?<ticketNumber>(?!13109\b)\d+)>?'
bodyRegexFlags: 'i'
outputOnly: true
- run: echo "${{ steps.get_issue_number.outputs.ticketNumber }}"
- name: Allow missing issue link for maintainers using `Closes NA` or `Closes N/A`
id: allow_missing_issue_link
env:
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_AUTHOR_ASSOCIATION: ${{ github.event.pull_request.author_association }}
PR_BODY: ${{ github.event.pull_request.body }}
TICKET_NUMBER: ${{ steps.get_issue_number.outputs.ticketNumber }}
run: |
set -euo pipefail
echo "allowed=false" >> "$GITHUB_OUTPUT"
if [ "$TICKET_NUMBER" != '-1' ]; then
exit 0
fi
if printf '%s\n' "$PR_BODY" | grep -Eiq '^[[:space:]]*closes[[:space:]]+n/?a[[:space:]]*$'; then
case "$PR_AUTHOR_ASSOCIATION" in
OWNER|MEMBER|COLLABORATOR)
echo "Maintainer '$PR_AUTHOR' used 'Closes NA'; allowing missing issue link."
echo "allowed=true" >> "$GITHUB_OUTPUT"
;;
esac
fi
- name: Check whether the issue still needs triage
id: needs_triage
if: steps.get_issue_number.outputs.ticketNumber != '-1'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ steps.get_issue_number.outputs.ticketNumber }}
run: |
set -euo pipefail
ISSUE_LABELS=$(gh api "/repos/$GITHUB_REPOSITORY/issues/$ISSUE_NUMBER" --jq '.labels[].name')
if grep -qxF 'status: needs-triage' <<< "$ISSUE_LABELS"; then
echo "needs_triage=true" >> "$GITHUB_OUTPUT"
fi
- name: Issue number present
if: steps.get_issue_number.outputs.ticketNumber == '-1' && steps.allow_missing_issue_link.outputs.allowed != 'true'
run: |
echo "No valid ticket number found!"
exit 1
move_issue:
name: Mark issue as in progress
# An untriaged issue is not in progress: ensure_assignment closes the pull request.
if: needs.determine_issue_number.outputs.issue_number != '-1' && needs.determine_issue_number.outputs.needs_triage != 'true'
# after determine_issue_number to ensure that there is only one failure because of no ticket number
needs: determine_issue_number
runs-on: ubuntu-slim
permissions:
issues: write
steps:
- name: Move issue to "In Progress" in "Good First Issues"
uses: m7kvqbe1/github-action-move-issues/@1e0e8ac3ab57afe93ddec45b2d48cbe2f1a6595b # main
with:
github-token: ${{ secrets.GH_TOKEN_ACTION_MOVE_ISSUE }}
project-url: "https://github.com/orgs/JabRef/projects/5"
target-labels: "📍 Assigned"
target-column: "In Progress"
ignored-columns: ""
default-column: "In Progress"
issue-number: ${{ needs.determine_issue_number.outputs.issue_number }}
skip-if-not-in-project: true
- name: Move issue to "In Progress" in "Candidates for University Projects"
uses: m7kvqbe1/github-action-move-issues/@1e0e8ac3ab57afe93ddec45b2d48cbe2f1a6595b # main
with:
github-token: ${{ secrets.GH_TOKEN_ACTION_MOVE_ISSUE }}
project-url: "https://github.com/orgs/JabRef/projects/3"
target-labels: "📍 Assigned"
target-column: "In Progress"
ignored-columns: ""
default-column: "In Progress"
issue-number: ${{ needs.determine_issue_number.outputs.issue_number }}
skip-if-not-in-project: true
ensure_assignment:
name: Ensure that contributor is assigned (fails if not commented on issue)
if: github.event.pull_request.head.repo.full_name != 'JabRef/jabref' && needs.determine_issue_number.outputs.issue_number != '-1'
# after determine_issue_number to ensure that there is only one failure because of no ticket number
needs: determine_issue_number
runs-on: ubuntu-slim
# The job posts comments and updates the STRANGE_USERS ledger with separate API calls; two runs of the same pull
# request must not interleave. Grouping per pull request keeps a busy repository from dropping other pull requests'
# runs - GitHub keeps only one pending run per group. A ledger update lost to a run of another pull request is
# re-applied on the next event of this pull request, because every effect below is keyed by pull request number.
concurrency:
group: link-issue-ensure-assignment-${{ github.event.pull_request.number }}
cancel-in-progress: false
permissions:
issues: write
pull-requests: write
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
ISSUE_NUMBER: ${{ needs.determine_issue_number.outputs.issue_number }}
PR_NUMBER: ${{ github.event.pull_request.number }}
PR_AUTHOR: ${{ github.event.pull_request.user.login }}
PR_BODY: ${{ github.event.pull_request.body }}
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
with:
show-progress: 'false'
- uses: awalsh128/cache-apt-pkgs-action@bb2fa420ce4bd7a598b0e3bbd1aad278a0af5544 # v2.0.2
with:
packages: jq
version: 1.0
- name: Pin the issue, or warn about a pull request on somebody else's issue
id: assignment
run: |
set -euo pipefail
echo "warned=false" >> "$GITHUB_OUTPUT"
# An untriaged issue may turn out to be invalid or a duplicate: neither assign nor pin it.
# "edited" also fires on closed pull requests; only an open one is commented on and closed.
# "Refs #n" does not claim to fix the issue, so such a pull request stays open.
ISSUE_LABELS=$(gh api "/repos/$REPO/issues/$ISSUE_NUMBER" --jq '.labels[].name')
if grep -qxF 'status: needs-triage' <<< "$ISSUE_LABELS"; then
if ! grep -Eiq "refs\s+<?(https?://github\.com/JabRef/jabref/issues/)?#?$ISSUE_NUMBER\b" <<< "$PR_BODY" &&
[ "$(gh pr view "$PR_NUMBER" --repo "$REPO" --json state --jq '.state')" = 'OPEN' ]; then
gh pr close "$PR_NUMBER" --repo "$REPO" --comment "The respective issue needs to be triaged first."
fi
exit 0
fi
# Read and act in one step: assignees can change at any moment, and a decision made in an earlier step
# would pin or warn based on an assignment that no longer holds.
ASSIGNEES=$(gh api "/repos/$REPO/issues/$ISSUE_NUMBER" --jq '[.assignees[].login]' | jq -c '.')
echo "Assignees of issue #$ISSUE_NUMBER: $ASSIGNEES"
if [ "$(jq 'length' <<< "$ASSIGNEES")" -ne 0 ] && ! jq -e --arg u "$PR_AUTHOR" 'index($u)' <<< "$ASSIGNEES" > /dev/null; then
MENTIONS=$(jq -r 'map("@" + .) | join(", ")' <<< "$ASSIGNEES")
# The workflow also runs on "edited"/"synchronize", so each comment is posted only once. Both markers are
# checked separately: a run that failed after the first comment has to be able to catch up on the second one.
# Only comments written by this workflow itself count - anybody can post the marker text and would otherwise
# silence their own strike. The comments are collected before searching them, because "grep -q" closes the
# pipe on the first match, which would fail the paginating "gh api" under "pipefail".
PR_MARKER="<!-- pr-on-foreign-issue -->"
PR_COMMENTS=$(gh api "/repos/$REPO/issues/$PR_NUMBER/comments" --paginate --jq '.[] | select(.user.type == "Bot" and .user.login == "github-actions[bot]") | .body')
if ! grep -qF "$PR_MARKER" <<< "$PR_COMMENTS"; then
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$PR_MARKER
Hi @$PR_AUTHOR, issue #$ISSUE_NUMBER is assigned to $MENTIONS, not to you. Please only work on issues assigned to you - see [CONTRIBUTING.md](https://github.com/JabRef/jabref/blob/main/CONTRIBUTING.md). The issue stays with its assignee and this pull request may be closed without review."
fi
ISSUE_MARKER="<!-- pr-on-foreign-issue:$PR_NUMBER -->"
ISSUE_COMMENTS=$(gh api "/repos/$REPO/issues/$ISSUE_NUMBER/comments" --paginate --jq '.[] | select(.user.type == "Bot" and .user.login == "github-actions[bot]") | .body')
if ! grep -qF "$ISSUE_MARKER" <<< "$ISSUE_COMMENTS"; then
gh issue comment "$ISSUE_NUMBER" --repo "$REPO" --body "$ISSUE_MARKER
@$PR_AUTHOR opened #$PR_NUMBER for this issue without being assigned. $MENTIONS: this does not affect your assignment."
fi
echo "warned=true" >> "$GITHUB_OUTPUT"
exit 0
fi
gh issue edit "$ISSUE_NUMBER" --repo "$REPO" --add-label "📌 Pinned"
# Copy over labels from issue to PR if they match good first/second/third/forth issue or component:* labels
LABELS=$(gh api "repos/$REPO/issues/$ISSUE_NUMBER/labels" --jq '.[].name')
# Check for good first/second/.. issue label
LABEL=$(echo "$LABELS" | grep -E '^good (first|second|third|forth) issue$' || true)
# Check for component:* labels (can be multiple)
COMPONENT_LABELS=$(echo "$LABELS" | grep -E '^component:' || true)
if [ -n "$LABEL" ] || [ -n "$COMPONENT_LABELS" ]; then
SILENT=false
if [ -n "$LABEL" ]; then
echo "✅ Found label: $LABEL"
gh pr edit "$PR_NUMBER" --add-label "$LABEL"
fi
if [ -n "$COMPONENT_LABELS" ]; then
echo "✅ Found component labels:"
while IFS= read -r COMPONENT_LABEL; do
echo " - $COMPONENT_LABEL"
gh pr edit "$PR_NUMBER" --add-label "$COMPONENT_LABEL"
done <<< "$COMPONENT_LABELS"
fi
else
echo "🚫 Silent fail if not possible to add assignee"
SILENT=true
fi
# "gh issue edit" cannot be used - workaround found at https://github.com/cli/cli/issues/9620#issuecomment-2703135049
# The "assignees" endpoint adds and is idempotent; PATCHing the full list would drop an assignee added since the read above.
echo "Assigning '$PR_AUTHOR' to issue #$ISSUE_NUMBER"
if [ "$SILENT" = true ]; then
gh api -X POST "/repos/$REPO/issues/$ISSUE_NUMBER/assignees" -f "assignees[]=$PR_AUTHOR" || true
else
gh api -X POST "/repos/$REPO/issues/$ISSUE_NUMBER/assignees" -f "assignees[]=$PR_AUTHOR"
fi
- name: Record incident in STRANGE_USERS
id: record
if: steps.assignment.outputs.warned == 'true'
env:
STRANGE_USERS: ${{ secrets.STRANGE_USERS }}
GH_TOKEN_STRANGE_USERS: ${{ secrets.GH_TOKEN_STRANGE_USERS }}
run: |
set -euo pipefail
STRANGE_USERS="${STRANGE_USERS:-}"
[ -n "$STRANGE_USERS" ] || STRANGE_USERS='{}'
# One incident per pull request: a rerun after a partial failure records it, a rerun after success does not duplicate it.
if [ "$(jq --arg u "$PR_AUTHOR" --arg pr "$PR_NUMBER" 'any((.[$u] // [])[]; .pr == $pr)' <<< "$STRANGE_USERS")" = 'true' ]; then
echo "Incident of '$PR_AUTHOR' on #$PR_NUMBER is already recorded."
jq -r --arg u "$PR_AUTHOR" '"count=" + (.[$u] | length | tostring)' <<< "$STRANGE_USERS" >> "$GITHUB_OUTPUT"
exit 0
fi
# No "count" output unless the ledger was written: an incident that cannot be persisted must not close a pull request.
if [ -z "${GH_TOKEN_STRANGE_USERS:-}" ]; then
echo "::warning::Secret GH_TOKEN_STRANGE_USERS is not set - incident of '$PR_AUTHOR' not persisted."
exit 0
fi
UPDATED=$(jq -c --arg u "$PR_AUTHOR" --arg d "$(date -u +%F)" --arg pr "$PR_NUMBER" --arg issue "$ISSUE_NUMBER" \
'.[$u] = ((.[$u] // []) + [{date: $d, pr: $pr, reason: ("PR on issue #" + $issue + " assigned to somebody else")}])' \
<<< "$STRANGE_USERS")
GH_TOKEN="$GH_TOKEN_STRANGE_USERS" gh secret set STRANGE_USERS --repo "$REPO" --body "$UPDATED"
jq -r --arg u "$PR_AUTHOR" '"count=" + (.[$u] | length | tostring)' <<< "$UPDATED" >> "$GITHUB_OUTPUT"
- name: Close the pull request causing the third incident
if: steps.record.outputs.count >= 3
run: |
set -euo pipefail
# The count is the ledger total, so it stays at three on later events of the same pull request. The
# notice is therefore posted at most once, while a reopened pull request is closed again.
CLOSE_MARKER="<!-- pr-closed-after-third-incident -->"
PR_COMMENTS=$(gh api "/repos/$REPO/issues/$PR_NUMBER/comments" --paginate --jq '.[] | select(.user.type == "Bot" and .user.login == "github-actions[bot]") | .body')
if ! grep -qF "$CLOSE_MARKER" <<< "$PR_COMMENTS"; then
gh pr comment "$PR_NUMBER" --repo "$REPO" --body "$CLOSE_MARKER
Hi @$PR_AUTHOR, this pull request is closed automatically, because three of your contributions did not follow our process (see [CONTRIBUTING.md](https://github.com/JabRef/jabref/blob/main/CONTRIBUTING.md)). Please reach out to the maintainers before opening further pull requests."
fi
if [ "$(gh pr view "$PR_NUMBER" --repo "$REPO" --json state --jq '.state')" = 'OPEN' ]; then
gh pr close "$PR_NUMBER" --repo "$REPO"
fi