-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
75 lines (58 loc) · 2.09 KB
/
entrypoint.sh
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
#!/bin/bash
STATUS=0
# remember last error code
trap 'STATUS=$?' ERR
# problem matcher must exist in workspace
cp /error-matcher.json $HOME/issue-assigner-error-matcher.json
echo "::add-matcher::$HOME/issue-assigner-error-matcher.json"
echo "Repository: [$GITHUB_REPOSITORY]"
GITHUB_TOKEN="$INPUT_TOKEN"
echo " "
# determine repo info
REPO_INFO=($(echo $GITHUB_REPOSITORY | tr "/" "\n"))
USER_NAME=${REPO_INFO[0]}
echo "Username: [$USER_NAME]"
REPO_NAME=${REPO_INFO[1]}
echo "Repository name: [$REPO_NAME]"
echo " "
# get latest issue
echo "Getting latest issue on [${REPO_NAME}]"
ISSUES=$(curl -X GET -H "Accept: application/vnd.github.v3+json" --silent "${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues?sort=updated&direction=desc&state=open&per_page=1" | jq '.[].number')
readarray -t ISSUE_NUMBERS <<< "$ISSUES"
NUMBER="${ISSUE_NUMBERS[0]}"
echo "Issue Number: [${NUMBER}]"
echo " "
echo "Creating automated comment"
MESSAGE="#### AUTOMATICALLY GENERATED by kbrashears5/github-action-issue-notifier
Mentioning @${USER_NAME} to notify them. This will be looked at soon!"
jq -n --arg body "$MESSAGE" \
'{
body:$body
}' \
| curl -d @- \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
-u ${GITHUB_TOKEN} \
--silent \
${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${NUMBER}/comments
# assign issue
# turn repo username into array
readarray -t ASSIGNEES <<< "$USER_NAME"
ASSIGNEES_JSON=`printf '%s\n' "${ASSIGNEES[@]}" | jq -R . | jq -s .`
echo "Assigning issue to [${USER_NAME}]"
jq -n --argjson assignees "$ASSIGNEES_JSON" '{assignees:$assignees}'
# "try/catch" to figure out if token has admin or collaborator permissions to repo
jq -n --argjson assignees "$ASSIGNEES_JSON" \
'{
assignees:$assignees
}' \
| curl -d @- \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
-u ${GITHUB_TOKEN} \
--silent \
${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/issues/${NUMBER}/assignees \
|| echo "Token does not have admin permissions to repo. Unable to assign"
exit $STATUS