forked from hackforla/website
-
Notifications
You must be signed in to change notification settings - Fork 2
73 lines (65 loc) · 3.22 KB
/
add-issue-labels-to-pr.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
# Name that appears on the workflow
# Note: Below you will see ${{ github }} and the github variable used. The former is an env from GitHub actions and the latter is an object argument of the actions/github-script@v4 workflow. These two are not the same and should not be treated as such!
name: Add Linked Issue Labels to Pull Request
on:
pull_request:
types: [opened, edited]
branches:
- 'gh-pages'
- 'wins-feature-1'
jobs:
Add-Linked-Issue-Labels-to-Pull-Request:
runs-on: ubuntu-latest
steps:
# Note: This portion uses RegEx to extract linked issues. This is not the best choice, but is required as GitHub does not include linked issues in its env. If this changes in the future, please revise this code.
- name: Retrieve Linked Issue From Comment
# https://github.com/actions/github-script
uses: actions/github-script@v4
# Escapes user input for injection attacks
env:
BODY: ${{ github.event.pull_request.body }}
PRNUMBER: ${{ github.event.number }}
# Variable that stores result of script
id: linked-issue-result
with:
script: |
// Retrieve pull request body and pull request number
const { BODY, PRNUMBER } = process.env
// Create RegEx for capturing KEYWORD #ISSUE-NUMBER syntax (i.e. resolves #1234)
const KEYWORDS = ['close', 'closes', 'closed', 'fix', 'fixes', 'fixed', 'resolve', 'resolves', 'resolved']
let reArr = []
for (const word of KEYWORDS) {
reArr.push(`[\\n|\\s|^]${word} #\\d*\\s|^${word} #\\d*\\s|\\s${word} #\\d*$|^${word} #\\d*$`)
}
// Receive and unpack matches into an Array of Array objs
let re = new RegExp(reArr.join('|'), 'gi')
let matches = BODY.matchAll(re)
matches = [...matches]
// If only one match is found, return the issue number. Else return false. Also console.log results.
if (matches.length == 1) {
const issueNumber = matches[0][0].match(/\d+/)
console.log(`Issue number found for PR #${PRNUMBER}. Issue #${issueNumber}`)
return JSON.stringify({ prNumber: PRNUMBER, issueNumber: issueNumber[0] })
} else {
console.log(`Make sure there is only one issue on PR#${PRNUMBER}!`)
return false
}
# Returns loudly if previous step failed.
- name: Return Failure
if: steps.linked-issue-result.outputs.result == 'false'
run: |
echo "Please expand above outputs for errors."
exit 1
# Create an artifact with the results of the script
- name: Create Artifacts
# Escapes user input for injection attacks
env:
ARTIFACT: ${{ steps.linked-issue-result.outputs.result }}
run: |
mkdir -p addingLabelsAction/artifact
echo ${{ env.ARTIFACT }} > addingLabelsAction/artifact/artifact.txt
- name: Upload Artifacts
uses: actions/upload-artifact@v2
with:
name: adding-labels-action-artifact
path: addingLabelsAction/artifact/