-
Notifications
You must be signed in to change notification settings - Fork 1
/
callable.sh
244 lines (223 loc) · 7.64 KB
/
callable.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
##################################################
# Just an alias for help
##################################################
Github__callable_main(){
Github__callable_help
}
##################################################
# Displays out the HELP.txt file
##################################################
Github__callable_help(){
more "$Ash__ACTIVE_MODULE_DIRECTORY/HELP.txt"
}
##################################################
# Loads up a github repo with the labels
# as defined in the passed in label config
#
# @param $1: The label config file
##################################################
Github__callable_labels(){
# Checking if variable exists
Github_validate_token
if [[ $? -ne 0 ]]; then
Logger__error "GITHUB_TOKEN must be set in the .ashrc file before calling labels"
return
fi
if [[ "$1" == "list" ]]; then
ls "$Github_label_config_directory"
return
elif [[ ! $1 =~ (.*)\/(.*) ]]; then
repo=""
label=$1
else
repo=$1
label=$2
fi
# Grabbing repo + validating input
if [[ "$repo" == "" ]]; then
if [[ -d .git ]] && [[ $(git config --local --get remote.origin.url) =~ git@github\.com:(.*)\/(.*)\.git ]]; then
repo="${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
else
Logger__prompt "Input the the repository to add labels (ex, carrot/ash-github): "; read repo
if [[ ! "$repo" =~ .+/.+ ]]; then
Logger__error "Invalid repository format (ex, carrot/ash-github)"
return
fi
fi
fi
# Handling the config file
Github__labels_handle_config_file "$repo" "$label" 0
}
##################################################
# Defines repo callable
#
# @params: action [params]
##################################################
Github__callable_repo(){
if [[ "$1" == "" ]];then
Github__callable_help
else
for param in "$@"; do
if [[ "$param" == "new" ]]; then
Github__repo_new ${@:2}
break
elif [[ "$param" == "delete" ]]; then
Github__repo_delete ${@:2}
break
else
Logger__error "Unknown operation: \"$param\""
Github__callable_help
break
fi
done
fi
}
##################################################
# Creates a new Gitub repository
#
# @param $1: name
#
# @flag -n|--name *Required
# @flag -d|--description
# @flag -h|--homepage
# @flag -p|--private
# @flag -i|--disable-issues
# @flag -w|--disable-wiki
# @flag -D|--disable-downloads
# @flag -t|--teams **integer
# @flag -a|--auto-init
# @flag -g|--gitignore
# @flag -l|--license
# @flag -v|--verbose
# @flag -r|--dry-run
##################################################
Github__repo_new(){
Github_validate_token
# flags
local name='' # default: '' *Required
local description='' # default: ''
local homepage='' # default: ''
local private=false # default: false
local hasIssues=true # default: true
local hasWiki=true # default: true
local hasDownloads=true # default: true
local teams='' # default: '' **integer
local autoInit=false # default: false
local gitignore='' # default: ''
local license='' # default: ''
local verbose=false # default: false
local dryRun=false # default: false
local curlOpts='' # curlOpts
local url="https://api.github.com/user/repos" # api url
if [[ ! "$#" -ne 1 ]]; then # only name
name="$1"
elif [[ "$1" =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$ ]]; then
Github__callable_help
else
while [[ $# -gt 1 ]]; do
opt="$1"
shift;
current_arg="$1"
case "$opt" in
"-n"|"--name" ) name="$1"; shift;;
"-d"|"--description" ) description="$1"; shift;;
"-h"|"--homepage" ) homepage="$1"; shift;;
"-p"|"--private" ) private=true; shift;;
"-i"|"--disable-issues" ) hasIssues=false; shift;;
"-w"|"--disable-wiki" ) hasWiki=false; shift;;
"-D"|"--disable-downloads" ) hasDownloads=false; shift;;
"-t"|"--teams" ) teams="$1"; shift;;
"-a"|"--auto-init" ) autoInit=true; shift;;
"-g"|"--gitignore" ) gitignore="$1"; shift;;
"-l"|"--license" ) license="$1"; shift;;
"-v"|"--verbose" ) verbose=true; shift;;
"-r"|"--dry-run" ) dryRun=true; shift;;
* ) Logger__error "Invalid option: \""$opt"\""
esac
done
fi
if [[ "$name" == "" ]]; then
Logger__error "You must pass a name"
return
fi
# data
local data="{
\"name\": \"$name\",
\"description\": \"$description\",
\"homepage\": \"$homepage\",
\"private\": $private,
\"has_issues\": $hasIssues,
\"has_wiki\": $hasWiki,
\"has_downloads\": $hasDownloads,
\"team_id\": \"$teams\",
\"auto_init\": $autoInit,
\"gitignore_template\": \"$gitignore\",
\"license_template\": \"$license\"
}"
if [[ "$verbose" == true ]]; then
curlOpts+='--verbose'
else
curlOpts+='-s'
fi
if [[ "$org" != "" ]]; then # orgs
url="https://api.github.com/orgs/$org/repos"
fi
if [[ "$dryRun" == true ]]; then # dry run thru proxy
nc -l localhost 8000 & \
curl "$curlOpts" \
--proxy localhost:8000 \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST "$url" \
-d "$data"
printf "body: $data\n"
else # real request
local res=$(curl -s \
"$curlOpts" \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST "$url" \
-d "$data")
if [[ "$verbose" == true ]]; then
printf "$res\n"
fi
if [[ ! "$res" =~ 2.. ]]; then
printf "Failed to create your repository.\n"
printf "Status: $res\n"
else
printf "Successfully created your repository.\n"
fi
fi
}
##################################################
# Delets a Gitub repository
#
# @param $1: owner/repository
##################################################
Github__repo_delete(){
local url="https://api.github.com/repos/$@"
local repo=''
if [[ ! "$@" =~ .*\/.* ]]; then
printf "Pass your repository as owner/repo. (ie, ash-shell/foo)\n"
return
fi
Github_validate_token
Logger__prompt "Please type in the name of the repository to confirm: "; read response
if [[ "$@" =~ (.*)\/(.*) ]]; then
if [[ "$response" = "${BASH_REMATCH[2]}" ]]; then
local res=$(curl \
-s \
-H "Authorization: token $GITHUB_TOKEN" \
-X DELETE "$url")
if [[ $res != "" ]]; then
printf "Failed to delete repository.\n"
printf "Status: $res\n"
else
printf "Successfully deleted repository.\n"
fi
else
return
fi
fi
}