-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo.create.sh
141 lines (123 loc) · 4.76 KB
/
repo.create.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env -S bash -e
#
# Creating repository on GitHub.
#
# @package Bash
# @author Kai Kimera <mail@kai.kim>
# @copyright 2023 iHub TO
# @license MIT
# @version 0.0.1
# @link https://lib.onl
# -------------------------------------------------------------------------------------------------------------------- #
(( EUID == 0 )) && { echo >&2 'This script should not be run as root!'; exit 1; }
# -------------------------------------------------------------------------------------------------------------------- #
# CONFIGURATION.
# -------------------------------------------------------------------------------------------------------------------- #
curl="$( command -v curl )"
sleep='2'
# Help.
read -r -d '' help <<- EOF
Options:
-x 'TOKEN' GitHub user token.
-o 'OWNER' Repository owner (organization). This is not case sensitive.
-r 'REPO_1;REPO_2;REPO_3' Repository name (array).
-d 'DESCRIPTION' Repository description.
-s 'https://example.org/' Repository site URL.
-l 'mit' Open source license template. For example, "mit" or "mpl-2.0".
-p Whether repository is private.
-i Enable issues for this repository.
-j Enable projects for this repository.
NOTE: If you're creating a repository in an organization that has disabled
repository projects, API returns an error.
-w Enable wiki for this repository.
-u Create an initial commit with empty README.
EOF
# -------------------------------------------------------------------------------------------------------------------- #
# OPTIONS.
# -------------------------------------------------------------------------------------------------------------------- #
OPTIND=1
while getopts 'x:o:r:d:s:l:pijwuh' opt; do
case ${opt} in
x)
token="${OPTARG}"
;;
o)
owner="${OPTARG}"
;;
r)
repos="${OPTARG}"; IFS=';' read -ra repos <<< "${repos}"
;;
d)
description="${OPTARG}"
;;
s)
homepage="${OPTARG}"
;;
l)
license="${OPTARG}"
;;
p)
private=1
;;
i)
has_issues=1
;;
j)
has_projects=1
;;
w)
has_wiki=1
;;
u)
auto_init=1
;;
h|*)
echo "${help}"; exit 2
;;
esac
done
shift $(( OPTIND - 1 ))
(( ! ${#repos[@]} )) && { echo >&2 '[ERROR] GitHub repository name not specified!'; exit 1; }
[[ -z "${token}" ]] && { echo >&2 '[ERROR] GitHub token not specified!'; exit 1; }
[[ -z "${owner}" ]] && { echo >&2 '[ERROR] GitHub repository owner (organization) not specified!'; exit 1; }
# -------------------------------------------------------------------------------------------------------------------- #
# INITIALIZATION.
# -------------------------------------------------------------------------------------------------------------------- #
init() {
repo_create
}
# -------------------------------------------------------------------------------------------------------------------- #
# GITHUB: CREATE REPOSITORY.
# -------------------------------------------------------------------------------------------------------------------- #
repo_create() {
[[ -n "${private}" ]] && private='true' || private='false'
[[ -n "${has_issues}" ]] && has_issues='true' || has_issues='false'
[[ -n "${has_projects}" ]] && has_projects='true' || has_projects='false'
[[ -n "${has_wiki}" ]] && has_wiki='true' || has_wiki='false'
[[ -n "${auto_init}" ]] && auto_init='true' || auto_init='false'
for repo in "${repos[@]}"; do
echo '' && echo "--- OPEN: '${repo}'"
${curl} -X POST \
-H "Authorization: Bearer ${token}" \
-H 'Accept: application/vnd.github+json' \
"https://api.github.com/orgs/${owner}/repos" \
-d @- << EOF
{
"name": "${repo}",
"description": "${description}",
"homepage": "${homepage}",
"private": ${private},
"has_issues": ${has_issues},
"has_projects": ${has_projects},
"has_wiki": ${has_wiki},
"auto_init": ${auto_init},
"license_template": "${license}"
}
EOF
echo '' && echo "--- DONE: '${repo}'" && echo ''; sleep ${sleep}
done
}
# -------------------------------------------------------------------------------------------------------------------- #
# -------------------------------------------------< INIT FUNCTIONS >------------------------------------------------- #
# -------------------------------------------------------------------------------------------------------------------- #
init "$@"; exit 0