-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcreate_issue.sh
executable file
·63 lines (51 loc) · 1.81 KB
/
create_issue.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
#!/bin/bash
# This script is invoked from GitHub actions that run every night.
# It creates an issue on GitHub that lets me know the tests failed.
set -e -o pipefail
if [ -z "$GITHUB_RUN_ID" ]; then
echo "This script can only be ran in GitHub Actions"
exit 1
fi
if [ $# != 2 ]; then
echo "Usage: $0 <github-token> <issue-title>"
exit 2
fi
auth_header="Authorization: token $1"
issue_title="$2"
# Usage: find_issue <title>
# Echos issue number, if any
function find_issue() {
curl -s "https://api.github.com/repos/Akuli/jou/issues?state=open" \
| jq -r --arg t "$1" '.[] | select(.title==$t) | .number'
}
# Usage: get_body <issue_number>
function get_body() {
curl -s "https://api.github.com/repos/Akuli/jou/issues/$1" | jq -r .body
}
# Usage: echo body | set_body <issue_number>
function set_body() {
curl -s -X PATCH \
-H "$auth_header" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/Akuli/jou/issues/$1" \
-d "$(jq -n --arg b "$(cat)" '{body: $b}')"
}
# Usage: echo body | new_issue <title>
function new_issue() {
curl -s -X POST \
-H "$auth_header" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/Akuli/jou/issues" \
-d "$(jq -n --arg t "$1" --arg b "$(cat)" '{title: $t, body: $b}')"
}
link="- [$(date)](https://github.com/Akuli/jou/actions/runs/$GITHUB_RUN_ID)"
echo $link
echo "Checking if there is already an issue with title \"$issue_title\"..."
n=$(find_issue "$issue_title")
if [ -n "$n" ]; then
echo "There is already an open issue (#$n). Appending link to issue description."
( (get_body $n | awk 1) && echo "$link") | set_body $n
else
echo "No open issue found, creating a new one."
(echo "Test outputs:" && echo "$link") | new_issue "$issue_title"
fi