-
Notifications
You must be signed in to change notification settings - Fork 22
/
action.yml
96 lines (85 loc) · 4.62 KB
/
action.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
name: contributor-takes-action
description: This is an action to assign yourself to an issue for a repo you are not a contributor to.
author: Brian Douglas
branding:
icon: 'thumbs-up'
color: 'white'
inputs:
message:
description: 'Message to prospective contributor'
required: false
default: ''
issueCurrentlyAssignedMessage:
description: 'Message to contributors if issue is already assigned'
required: false
default: 'The issue you are trying to assign to yourself is already assigned.'
blockingLabels:
description: 'Optional labels that are on an issue that block an issue from being assigned by the trigger.'
required: false
default: ''
blockingLabelsMessage:
description: 'Message to contributors if issue is blocked by a label'
required: false
default: 'The issue you are trying to assign to yourself is blocked by a one or more labels on the issue.'
trigger:
description: 'The string that triggers the action'
required: false
default: '.take'
token:
description: 'The GitHub PAT used to authenticate when updating the Issue'
required: true
runs:
using: "composite"
steps:
- run: |
BODY="$(jq '.comment.body' $GITHUB_EVENT_PATH)"
ISSUE_NUMBER="$(jq '.issue.number' $GITHUB_EVENT_PATH)"
LOGIN="$(jq '.comment.user.login' $GITHUB_EVENT_PATH | tr -d \")"
REPO="$(jq '.repository.full_name' $GITHUB_EVENT_PATH | tr -d \")"
ISSUE_JSON="$(jq '.issue' $GITHUB_EVENT_PATH)"
ISSUE_LABELS="$(jq -r '.issue.labels[].name' $GITHUB_EVENT_PATH)"
ISSUE_LABELS=$(echo "$ISSUE_LABELS" | jq -n --arg str "$ISSUE_LABELS" '$str | split("\n")')
ISSUE_CURRENTLY_ASSIGNED=`echo $ISSUE_JSON | jq '.assignees | length == 0'`
if [[ $BODY == *"$INPUT_TRIGGER"* ]]; then
if [[ "$ISSUE_CURRENTLY_ASSIGNED" == true ]]; then
BLOCKING_LABELS=$(echo "$RAW_BLOCKING_LABELS" | jq -n --arg str "$RAW_BLOCKING_LABELS" '$str | split(",")')
echo "Issue labels: $ISSUE_LABELS"
echo "Blocking labels: $BLOCKING_LABELS"
# See if at least one label on the issue is in the list of blocking labels
blocking_label_found=$(jq -n --argjson ISSUE_LABELS "$ISSUE_LABELS" --argjson BLOCKING_LABELS "$BLOCKING_LABELS" '$ISSUE_LABELS as $il | $BLOCKING_LABELS as $bl | any($il[]; . as $i | any($bl[]; . == $i))')
echo "blocking label found: $blocking_label_found"
if [ "$blocking_label_found" == true ]; then
echo "Issue contains one or more blocking labels: $BLOCKING_LABELS"
echo "Unable to assign issue $ISSUE_NUMBER to $LOGIN"
# Post a comment on the issue that there are blocking labels on the issue preventing it from being assigned
if [[ ! -z $BLOCKING_LABELS_MESSAGE ]]; then
jq -n -r --arg body "$BLOCKING_LABELS_MESSAGE" '{body: $body}' > payload.json
curl -X POST -H "Authorization: bearer $GITHUB_PAT" --data @payload.json https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/comments
fi
else
# Assign the issue to the user
echo "Is issue currently assigned: $ISSUE_CURRENTLY_ASSIGNED"
echo "Assigning issue $ISSUE_NUMBER to $LOGIN"
echo "Using the link: https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/assignees"
curl -H "Authorization: bearer $GITHUB_PAT" -d '{"assignees":["'"$LOGIN"'"]}' https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/assignees
if [[ ! -z $INPUT_MESSAGE ]]; then
jq -n -r --arg body "$INPUT_MESSAGE" '{body: $body}' > payload.json
curl -X POST -H "Authorization: bearer $GITHUB_PAT" --data @payload.json https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/comments
fi
fi
else
echo "This issue is currently assigned to a different user"
if [[ ! -z $ISSUE_CURRENTLY_ASSIGNED_MESSAGE ]]; then
jq -n -r --arg body "$ISSUE_CURRENTLY_ASSIGNED_MESSAGE" '{body: $body}' > payload.json
curl -X POST -H "Authorization: bearer $GITHUB_PAT" --data @payload.json https://api.github.com/repos/$REPO/issues/$ISSUE_NUMBER/comments
fi
fi
fi
shell: bash
env:
INPUT_MESSAGE: "${{ inputs.message }}"
RAW_BLOCKING_LABELS: "${{ inputs.blockingLabels }}"
BLOCKING_LABELS_MESSAGE: "${{ inputs.blockingLabelsMessage }}"
INPUT_TRIGGER: "${{ inputs.trigger }}"
ISSUE_CURRENTLY_ASSIGNED_MESSAGE: "${{ inputs.issueCurrentlyAssignedMessage }}"
GITHUB_PAT: "${{ inputs.token }}"