├── .editorconfig ├── LICENSE ├── README.md └── gogs /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | end_of_line = lf 5 | insert_final_newline = true 6 | indent_style = tab # must be tab for heredocs to work 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2017 Matthew Downey (mattddowney@gmail_NOSPAM_.com) 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gogs-bash 2 | ========= 3 | 4 | Interact with the [GOGS](https://gogs.io/) API from bash. 5 | 6 | Create repositories and manage webooks. 7 | 8 | Setup: 9 | ------ 10 | 11 | Set the following environment variables: 12 | 13 | * GOGS_ROOT_URL - The url of the GOGS server (IE: http://try.gogs.io) 14 | * GOGS_TOKEN - Access token obtained from GOGS (http://try.gogs.io/user/settings/applications) 15 | 16 | Usage: 17 | ------ 18 | 19 | ``` 20 | $ ./gogs 21 | ./gogs is a tool for interacting with the GOGS API. 22 | 23 | Usage: 24 | ./gogs command [arguments] 25 | 26 | The commands are: 27 | 28 | create-repo create a repository 29 | create-webhook create a webhook 30 | delete-webhook delete a webhook 31 | env print environment information 32 | get-repos get a list of repos by search keyword 33 | get-user-repos get a list of repos for a user 34 | get-users get a list of users by search keyword 35 | get-webhooks get a list of webhooks for a repo 36 | 37 | Use "./gogs help [command]" for more information about a command. 38 | ``` 39 | -------------------------------------------------------------------------------- /gogs: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Interact with the GOGS API 3 | # Copyright (c) 2017 Matthew Downey (mattddowney@gmail_NOSPAM_.com) 4 | # Relased under the MIT License 5 | # See https://raw.githubusercontent.com/mattddowney/gogs-bash/master/LICENSE for the complete license text 6 | 7 | action=${1:-"help"} 8 | 9 | # preseed for checks 10 | response_code=0 11 | 12 | # ensure GOGS_ROOT_URL is set 13 | if [ -z $GOGS_ROOT_URL ] 14 | then 15 | printf "Need to set GOGS_ROOT_URL environment variable\n" >&2 16 | printf "\tIE: https://try.gogs.io\n" >&2 17 | 18 | response_code=1 19 | fi 20 | 21 | # ensure GOGS_TOKEN is set 22 | if [ -z $GOGS_TOKEN ] 23 | then 24 | printf "Need to set GOGS_TOKEN environment variable\n" >&2 25 | printf "\tThis can be obtained at $GOGS_ROOT_URL/user/settings/applications\n" >&2 26 | 27 | response_code=1 28 | fi 29 | 30 | # exit if either of the environment variables above are not set 31 | if [ $response_code -ne 0 ] 32 | then 33 | exit $response_code 34 | fi 35 | 36 | # create a GOGS repository 37 | # https://github.com/gogs/docs-api/blob/master/Administration/Repositories.md#create-a-new-repository 38 | create-repo() { 39 | local user_name=$1 # name of the user / organization where the repo will be created 40 | local repo_name=$2 # name of the repo to be created 41 | 42 | # ensure user_name is passed in 43 | if [ -z $user_name ] 44 | then 45 | response_code=1 46 | fi 47 | 48 | # ensure repo_name is passed in 49 | if [ -z $repo_name ] 50 | then 51 | response_code=1 52 | fi 53 | 54 | # print help and exit if either of the variables above are not passed in 55 | if [ $response_code -ne 0 ] 56 | then 57 | help "create-repo" 58 | 59 | exit $response_code 60 | fi 61 | 62 | printf "--" "---> Creating GOGS repository\n" 63 | printf "--" "---> User/Org Name: $user_name\n" 64 | printf "--" "---> Repo Name: $repo_name\n" 65 | 66 | local body=$(cat <<-END 67 | { 68 | "name": "$repo_name", 69 | "private": false 70 | } 71 | END 72 | ) 73 | 74 | curl -H "Content-Type: application/json" \ 75 | -H "Authorization: token $GOGS_TOKEN" \ 76 | -d "$body" \ 77 | -v $GOGS_ROOT_URL/api/v1/admin/users/$user_name/repos 78 | } 79 | 80 | # create a GOGS webhook 81 | # https://github.com/gogs/docs-api/blob/master/Repositories/Webhooks.md#create-a-hook 82 | create-webhook() { 83 | local user_name=$1 # name of the user / organization that owns the repo where the webhook will be created 84 | local repo_name=$2 # name of the repo where the webhook will be created 85 | local webhook_url=$3 # the url for the webhook that will be created 86 | local secret=$4 # optional secret that will be passed in the X-Gogs-Signature header 87 | 88 | # ensure user_name is passed in 89 | if [ -z $user_name ] 90 | then 91 | response_code=1 92 | fi 93 | 94 | # ensure repo_name is passed in 95 | if [ -z $repo_name ] 96 | then 97 | response_code=1 98 | fi 99 | 100 | # ensure that webhook_url is passed in 101 | if [ -z $webhook_url ] 102 | then 103 | response_code=1 104 | fi 105 | 106 | # print help and exit if either of the variables above are not passed in 107 | if [ $response_code -ne 0 ] 108 | then 109 | help "create-webhook" 110 | 111 | exit $response_code 112 | fi 113 | 114 | printf "--" "---> Creating GOGS webhook\n" 115 | printf "--" "---> Repo Name: $repo_name\n" 116 | printf "--" "---> Webhook URL: $webhook_url\n" 117 | 118 | local body=$(cat <<-END 119 | { 120 | "type": "gogs", 121 | "config": { 122 | "content_type": "json", 123 | "url": "$webhook_url" 124 | }, 125 | "events": [ "create", "push" ], 126 | "active": true 127 | } 128 | END 129 | ) 130 | 131 | # add secret to body if one is passed in 132 | if [ -n $secret ] 133 | then 134 | body=$(echo "$body" | jq ".config.secret=\"$secret\"") 135 | fi 136 | 137 | curl -H "Content-Type: application/json" \ 138 | -H "Authorization: token $GOGS_TOKEN" \ 139 | -d "$body" \ 140 | -v $GOGS_ROOT_URL/api/v1/repos/$user_name/$repo_name/hooks 141 | } 142 | 143 | # delete a GOGS webhook for a repo 144 | # https://github.com/gogs/docs-api/blob/master/Repositories/Webhooks.md#delete-a-hook 145 | delete-webhook() { 146 | local user_name=$1 147 | local repo_name=$2 148 | local webhook_id=$3 149 | 150 | # ensure user_name is passed in 151 | if [ -z $user_name ] 152 | then 153 | response_code=1 154 | fi 155 | 156 | # ensure repo_name is passed in 157 | if [ -z $repo_name ] 158 | then 159 | response_code=1 160 | fi 161 | 162 | # ensure webhook_id is passed in 163 | if [ -z $webhook_id ] 164 | then 165 | response_code=1 166 | fi 167 | 168 | # print help and exit if any of the variables above are not passed in 169 | if [ $reponse_code -ne 0 ] 170 | then 171 | help "delete-webhook" 172 | 173 | exit $response_code 174 | fi 175 | 176 | printf "--" "---> Deleting GOGS webhook\n" 177 | printf "--" "---> User Name: $user_name\n" 178 | printf "--" "---> Repo Name: $repo_name\n" 179 | printf "--" "---> Webhook Id: $webhook_id\n" 180 | 181 | curl -H "Authorization: token $GOGS_TOKEN" \ 182 | -X DELETE \ 183 | -v $GOGS_ROOT_URL/api/v1/repos/$user_name/$repo_name/hooks/$webhook_id 184 | } 185 | 186 | # print environment information 187 | env() { 188 | printf "GOGS_ROOT_URL=$GOGS_ROOT_URL\n" 189 | printf "GOGS_TOKEN=$GOGS_TOKEN\n" 190 | } 191 | 192 | # get a list of repos by search keyword 193 | # https://github.com/gogs/docs-api/tree/master/Repositories#search-repositories 194 | get-repos() { 195 | local search_keyword=$1 # term to find inside the repo name 196 | 197 | # ensure search_keyword is passed in 198 | if [ -z $search_keyword ] 199 | then 200 | help "get-repos" 201 | 202 | exit 1 203 | fi 204 | 205 | printf "--" "---> Getting list of GOGS repos\n" 206 | 207 | curl -H "Content-Type: application.json" \ 208 | -H "Authorization: token $GOGS_TOKEN" \ 209 | -v "$GOGS_ROOT_URL/api/v1/repos/search?q=$search_keyword" 210 | } 211 | 212 | # get a list of GOGS repos for a user 213 | # https://github.com/gogs/docs-api/tree/master/Repositories#list-user-repositories 214 | get-user-repos() { 215 | local user_name=$1 # name of the user who's repos to list 216 | 217 | # ensure user_name is passed in 218 | if [ -z $user_name ] 219 | then 220 | help "get-user-repos" 221 | 222 | exit 1 223 | fi 224 | 225 | printf "--" "---> Getting list of GOGS repos\n" 226 | printf "--" "---> User Name: $user_name\n" 227 | 228 | curl -H "Content-Type: application/json" \ 229 | -H "Authorization: token $GOGS_TOKEN" \ 230 | -v "$GOGS_ROOT_URL/api/v1/users/$user_name/repos" 231 | } 232 | 233 | # get a list of users by search keyword 234 | # https://github.com/gogs/docs-api/tree/master/Users#search-users 235 | get-users() { 236 | local search_keyword=$1 # term to find inside the repo name 237 | 238 | # ensure search_keyword is passed in 239 | if [ -z $search_keyword ] 240 | then 241 | help "get-users" 242 | 243 | exit 1 244 | fi 245 | 246 | printf "--" "---> Getting list of GOGS users\n" 247 | 248 | curl -H "Content-Type: application.json" \ 249 | -H "Authorization: token $GOGS_TOKEN" \ 250 | -v "$GOGS_ROOT_URL/api/v1/users/search?q=$search_keyword" 251 | } 252 | 253 | # get a list of GOGS webhooks for a repo 254 | # https://github.com/gogs/docs-api/blob/master/Repositories/Webhooks.md#list-hooks 255 | get-webhooks() { 256 | local user_name=$1 # name of the user / organization that owns the repo 257 | local repo_name=$2 # name of the repo to list webhooks for 258 | 259 | # ensure user_name is passed in 260 | if [ -z $user_name ] 261 | then 262 | response_code=1 263 | fi 264 | 265 | # ensure repo_name is passed in 266 | if [ -z $repo_name ] 267 | then 268 | response_code=1 269 | fi 270 | 271 | # print help and exit if either of the variables above are not passed in 272 | if [ $response_code -ne 0 ] 273 | then 274 | help "get-webhooks" 275 | 276 | exit $response_code 277 | fi 278 | 279 | printf "--" "---> Getting list of GOGS webhooks\n" 280 | printf "--" "---> User Name: $user_name\n" 281 | printf "--" "---> Repo Name: $repo_name\n" 282 | 283 | curl -H "Authorization: token $GOGS_TOKEN" \ 284 | -v $GOGS_ROOT_URL/api/v1/repos/$user_name/$repo_name/hooks 285 | } 286 | 287 | # print help 288 | help() { 289 | local prog_name=$(echo "$0" | rev | cut -d'/' -f1 | rev) 290 | local topic=$1 291 | 292 | case "$topic" in 293 | create-repo) 294 | cat <<-END 295 | Create a GOGS repository. 296 | 297 | Usage: 298 | 299 | $prog_name $1 300 | 301 | Arguments: 302 | 303 | user_name name of the user or organization where the repo will be created 304 | repo_name name of the repository to be created 305 | END 306 | ;; 307 | 308 | create-webhook) 309 | cat <<-END 310 | Create a GOGS webhook. 311 | 312 | Usage: 313 | 314 | $prog_name $1 [secret] 315 | 316 | Arguments: 317 | 318 | user_name name of the user / organizationo that owns the repo where the webhook will be created 319 | repo_name name of the repo where the webhook will be created 320 | webhook_url the url for the webhook that will be created 321 | secret optional secret that will be passed in the X-Gogs-Signature header 322 | END 323 | ;; 324 | 325 | delete-webhook) 326 | cat <<-END 327 | Delete a GOGS webhook. 328 | 329 | Usage: 330 | 331 | $prog_name $1 332 | 333 | Arguments: 334 | 335 | user_name name of the user / organizationo that owns the repo containing the webhook 336 | repo_name name of the repo that contains the webhook 337 | webhook_id id of the webhook that will be deleted 338 | END 339 | ;; 340 | 341 | env) 342 | cat <<-END 343 | Prints environment variables needed by this script. 344 | 345 | Variables: 346 | 347 | GOGS_ROOT_URL The url of the GOGS server 348 | (IE: https://try.gogs.io) 349 | 350 | GOGS_TOKEN Application token configured in GOGS 351 | (Obtained from $GOGS_ROOT_URL/user/settings/applications) 352 | END 353 | ;; 354 | 355 | get-repos) 356 | cat <<-END 357 | Get a list of GOGS repos by search keyword. 358 | 359 | Usage: 360 | 361 | $prog_name $1 362 | 363 | Arguments: 364 | 365 | search_keyword term to find inside the repo name 366 | END 367 | ;; 368 | 369 | get-user-repos) 370 | cat <<-END 371 | Get a list of GOGS repos for a user. 372 | 373 | Usage: 374 | 375 | $prog_name $1 376 | 377 | Arguments: 378 | 379 | user_name name of the user who's repos to list 380 | END 381 | ;; 382 | 383 | get-users) 384 | cat <<-END 385 | Get a list of GOGS users by search keyword. 386 | 387 | Usage: 388 | $prog_name $1 389 | 390 | Arguments: 391 | 392 | search_keyword term to find inside the username 393 | END 394 | ;; 395 | 396 | get-webhooks) 397 | cat <<-END 398 | Get a list of GOGS webhooks for a repo. 399 | 400 | Usage: 401 | 402 | $prog_name $1 403 | 404 | Arguments: 405 | 406 | user_name name of the user / organization that owns the repo 407 | repo_name name of the repo to list webhooks for 408 | END 409 | ;; 410 | 411 | *) 412 | cat <<-END 413 | $prog_name is a tool for interacting with the GOGS API. 414 | 415 | Usage: 416 | $prog_name command [arguments] 417 | 418 | The commands are: 419 | 420 | create-repo create a repository 421 | create-webhook create a webhook 422 | delete-webhook delete a webhook 423 | env print environment information 424 | get-repos get a list of repos by search keyword 425 | get-user-repos get a list of repos for a user 426 | get-users get a list of users by search keyword 427 | get-webhooks get a list of webhooks for a repo 428 | 429 | Use "$prog_name help [command]" for more information about a command. 430 | END 431 | ;; 432 | esac 433 | } 434 | 435 | "$action" "${@:2}" 436 | --------------------------------------------------------------------------------