forked from marksost/github-label-maker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-labels
executable file
·80 lines (62 loc) · 2.01 KB
/
create-labels
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
#!/usr/bin/env bash
set -e
# Script variables
GITHUBORG=""
GITHUBREPO=""
# Include shell helpers
source shell-helpers.sh
# Main functionality of the script
main() {
echo_yellow "Creating labels for the following organization and repository:"
echo_yellow "${TAB}Organization: $GITHUBORG"
echo_yellow "${TAB}Repository: $GITHUBREPO"
# Create labels
create_labels
echo_green "Labels created succesfully!"
}
# Create labels for a specific repo
create_labels() {
while IFS= read -r LABELNAME && IFS= read -r LABELCOLOR; do
echo_yellow "${TAB}Creating label with the following settings:"
echo_yellow "${TABx2}Name: $LABELNAME"
echo_yellow "${TABx2}Color: $LABELCOLOR"
curl -X POST --header "Authorization: token ${GITHUBTOKEN}" \
--header "Content-Type: application/json" \
--data "{\"name\":\"${LABELNAME}\", \"color\":\"${LABELCOLOR}\"}" \
"https://api.github.com/repos/${GITHUBORG}/${GITHUBREPO}/labels"
echo ""
done < <(jq -r 'keys[] as $k | (.[$k].name, .[$k].color)' $(pwd)/labels.json)
}
# Function that outputs usage information
usage() {
cat <<EOF
Usage: $(basename $0) <options>
Description
Options:
-o (required) The Github organization (or user) that the repository resides under
-r (required) The name of the repository
-t A Github personal token, used for authenticating API requests
-h Print this message and quit
EOF
exit 0
}
# Function that verifies required input was passed in
verify_input() {
# Verify required inputs are not empty
[ ! -z "${GITHUBORG}" ] && [ ! -z "${GITHUBREPO}" ] && [ ! -z "${GITHUBTOKEN}" ]
}
# Parse input options
while getopts ":o:r:t:h" opt; do
case "$opt" in
o) GITHUBORG=$OPTARG;;
r) GITHUBREPO=$OPTARG;;
t) GITHUBTOKEN=$OPTARG;;
h) usage;;
\?) echo_red "Invalid option: -${OPTARG}." && usage;;
:) die "Option -${OPTARG} requires an argument.";;
esac
done
# Verify input
! verify_input && echo_red "Missing script options." && usage
# Execute main functionality
main