├── .editorconfig ├── .gitignore ├── HELP.txt ├── LICENSE.txt ├── README.md ├── ash_config.yaml ├── callable.sh ├── extras └── label_configs │ ├── carrots │ ├── carrots-android │ ├── carrots-ios │ ├── carrots-web │ └── clear-defaults └── lib ├── labels.sh └── util.sh /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | 4 | root=true 5 | 6 | [*] 7 | charset=utf-8 8 | end_of_line=lf 9 | insert_final_newline=true 10 | trim_trailing_whitespace=true 11 | indent_style=space 12 | indent_size=4 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | -------------------------------------------------------------------------------- /HELP.txt: -------------------------------------------------------------------------------- 1 | Usage: 2 | 3 | github:labels [$label-config|list] 4 | Bulk creates labels in a Github repository from a label-config file. 5 | You will be prompted for a Github repository to apply the labels. 6 | 7 | Label config files are located the /extras/label_configs directory, 8 | and are formatted with one label per line, with the label name on 9 | the left, separated by a colon (:) and the color hex value on the 10 | right. 11 | 12 | You will need to set GITHUB_TOKEN in your .ashrc file that is a 13 | valid Github access token with permissions that are enough to 14 | create labels in the repo you use. 15 | 16 | github:repo new 17 | github:repo new repo Tied to your account 18 | github:repo new org/repo Tied to org 19 | 20 | Opts: 21 | -n --name Name of repository 22 | -d --description Description of repository 23 | -h --homepage URL after description 24 | -p --private 25 | -i --disable-issues 26 | -w --disable-wiki 27 | -D --disable-download 28 | -t --teams Pass team ID to set team permission 29 | -a --auto-init Adds blank readme 30 | -g --gitignore gitignore style (ie, Haskell) 31 | -l --license license template (ie, MIT) 32 | -v --verbose Verbose CURL request 33 | -r --dry-run Pass CURL request thru netcat proxy 34 | 35 | github:repo delete user/repo 36 | Delete repository associated with user/repo 37 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Carrot Creative, Brandon Romano 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ash-Github 2 | 3 | Ash-Github is an [Ash](https://github.com/ash-shell/ash) module that contains collection of utility scripts that help automate using Github. 4 | 5 | Currently there's only support for bulk creating labels from config files, but this repository is generically named in hopes that it will be expanded upon in the future. 6 | 7 | ## Getting Started 8 | 9 | #### Install 10 | 11 | You're going to have to install [Ash](https://github.com/ash-shell/ash) to use this module. 12 | 13 | After you have Ash installed, run either one of these two commands depending on your git clone preference: 14 | 15 | - `ash apm:install git@github.com:carrot/ash-github.git --global` 16 | - `ash apm:install https://github.com/carrot/ash-github.git --global` 17 | 18 | #### Config 19 | 20 | Inside of your [~/.ashrc](https://github.com/ash-shell/ash#the-ashrc-file) file, you'll need to add a Github token with permissions to your repositories you want to work with: 21 | 22 | ```bash 23 | export GITHUB_TOKEN="YOUR_GITHUB_TOKEN_HERE" 24 | ``` 25 | 26 | ## Labels 27 | 28 | #### Labels Config File 29 | 30 | The labels command will load in a label config file into a Github repo. 31 | 32 | All label configs are located at `extras/label_configs` in this repo. 33 | 34 | You can add your own custom label configs in that directory for your own use. 35 | 36 | If you work with a team and have a consistent label config, it might make sense to fork this repo and add your own label config files. If you're convinced you have a really good label config, feel free to create a PR for it. 37 | 38 | Label config files are very simple, and follow the following format, with one label per line: 39 | 40 | ``` 41 | label_name:hex_color 42 | ``` 43 | 44 | An example file would look something like this: 45 | 46 | ``` 47 | blocked:000000 48 | bug:fc2929 49 | ``` 50 | 51 | #### Deleting Labels 52 | 53 | Sometimes you might want to delete some labels (likely Github's default labels) in the process of setting up a repo with your label config. 54 | 55 | You can add in the your label config file a line with `-delete:tag-name` to go and delete a specific label. 56 | 57 | For example: 58 | 59 | ``` 60 | -delete:wontfix 61 | ``` 62 | 63 | #### Importing other Label Configs 64 | 65 | You may run into a case where you have a base label config, but you may have some slight differences between project types. 66 | 67 | This case is handled, and you may use the `-import:label-config-file-name`. 68 | 69 | For example, our iOS team and Android team use a majority of the same labels, but we have custom platform specific tags. Here is what our Android config file looks like: 70 | 71 | ``` 72 | # Carrots Mobile 73 | -import:carrots-mobile 74 | 75 | # issue platform 76 | android-4.0:a6c427 77 | android-4.1:a6c427 78 | android-4.4:a6c427 79 | android-5.0:a6c427 80 | android-6.0:a6c427 81 | small-screens:a6c427 82 | large-screens:a6c427 83 | ``` 84 | 85 | You will find `-import:carrots-mobile` at the top of our iOS config too. 86 | 87 | You're absolutely allowed to import multiple config files, but just be sure to watch out for circular references, as this library doesn't handle that case (and your script will run forever). 88 | 89 | #### Comments 90 | 91 | Bash style comments are supported in Label Config files, so anything after `#` is ignored. 92 | 93 | #### Using the Command 94 | 95 | After you have a labels config file you're happy with, using this command is very straight forward. 96 | 97 | If I were using the carrots-web label config I would run on the command line: 98 | 99 | ```bash 100 | ash github:labels carrots-web 101 | ``` 102 | 103 | I would then be prompted to input the repository, and if it were for this repo, I would input: 104 | 105 | ``` 106 | carrot/ash-github 107 | ``` 108 | 109 | After entering this information in, your repository will be populated with the labels as defined in the labels config file. 110 | 111 | ## Repo 112 | 113 | #### Basic Usage 114 | 115 | Creating/deleting a repository on your Github. 116 | 117 | ```bash 118 | ash github:repo new foobar 119 | ash github:repo delete owner/foobar 120 | ``` 121 | 122 | For more advance usage checkout [HELP.txt] or type `ash github:help` 123 | 124 | ## License 125 | 126 | [MIT](LICENSE.txt) 127 | -------------------------------------------------------------------------------- /ash_config.yaml: -------------------------------------------------------------------------------- 1 | name: Github 2 | package: github.com/carrot/ash-github 3 | is_callable: true 4 | callable_prefix: Github 5 | default_alias: github 6 | -------------------------------------------------------------------------------- /callable.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ################################################## 4 | # Just an alias for help 5 | ################################################## 6 | Github__callable_main(){ 7 | Github__callable_help 8 | } 9 | 10 | ################################################## 11 | # Displays out the HELP.txt file 12 | ################################################## 13 | Github__callable_help(){ 14 | more "$Ash__ACTIVE_MODULE_DIRECTORY/HELP.txt" 15 | } 16 | 17 | ################################################## 18 | # Loads up a github repo with the labels 19 | # as defined in the passed in label config 20 | # 21 | # @param $1: The label config file 22 | ################################################## 23 | Github__callable_labels(){ 24 | # Checking if variable exists 25 | Github_validate_token 26 | if [[ $? -ne 0 ]]; then 27 | Logger__error "GITHUB_TOKEN must be set in the .ashrc file before calling labels" 28 | return 29 | fi 30 | 31 | if [[ "$1" == "list" ]]; then 32 | ls "$Github_label_config_directory" 33 | return 34 | elif [[ ! $1 =~ (.*)\/(.*) ]]; then 35 | repo="" 36 | label=$1 37 | else 38 | repo=$1 39 | label=$2 40 | fi 41 | 42 | # Grabbing repo + validating input 43 | if [[ "$repo" == "" ]]; then 44 | if [[ -d .git ]] && [[ $(git config --local --get remote.origin.url) =~ git@github\.com:(.*)\/(.*)\.git ]]; then 45 | repo="${BASH_REMATCH[1]}/${BASH_REMATCH[2]}" 46 | else 47 | Logger__prompt "Input the the repository to add labels (ex, carrot/ash-github): "; read repo 48 | if [[ ! "$repo" =~ .+/.+ ]]; then 49 | Logger__error "Invalid repository format (ex, carrot/ash-github)" 50 | return 51 | fi 52 | fi 53 | fi 54 | 55 | # Handling the config file 56 | Github__labels_handle_config_file "$repo" "$label" 0 57 | } 58 | 59 | ################################################## 60 | # Defines repo callable 61 | # 62 | # @params: action [params] 63 | ################################################## 64 | Github__callable_repo(){ 65 | if [[ "$1" == "" ]];then 66 | Github__callable_help 67 | else 68 | for param in "$@"; do 69 | if [[ "$param" == "new" ]]; then 70 | Github__repo_new ${@:2} 71 | break 72 | elif [[ "$param" == "delete" ]]; then 73 | Github__repo_delete ${@:2} 74 | break 75 | else 76 | Logger__error "Unknown operation: \"$param\"" 77 | Github__callable_help 78 | break 79 | fi 80 | done 81 | fi 82 | } 83 | 84 | ################################################## 85 | # Creates a new Gitub repository 86 | # 87 | # @param $1: name 88 | # 89 | # @flag -n|--name *Required 90 | # @flag -d|--description 91 | # @flag -h|--homepage 92 | # @flag -p|--private 93 | # @flag -i|--disable-issues 94 | # @flag -w|--disable-wiki 95 | # @flag -D|--disable-downloads 96 | # @flag -t|--teams **integer 97 | # @flag -a|--auto-init 98 | # @flag -g|--gitignore 99 | # @flag -l|--license 100 | # @flag -v|--verbose 101 | # @flag -r|--dry-run 102 | ################################################## 103 | Github__repo_new(){ 104 | Github_validate_token 105 | 106 | # flags 107 | local name='' # default: '' *Required 108 | local description='' # default: '' 109 | local homepage='' # default: '' 110 | local private=false # default: false 111 | local hasIssues=true # default: true 112 | local hasWiki=true # default: true 113 | local hasDownloads=true # default: true 114 | local teams='' # default: '' **integer 115 | local autoInit=false # default: false 116 | local gitignore='' # default: '' 117 | local license='' # default: '' 118 | local verbose=false # default: false 119 | local dryRun=false # default: false 120 | local curlOpts='' # curlOpts 121 | local url="https://api.github.com/user/repos" # api url 122 | 123 | if [[ ! "$#" -ne 1 ]]; then # only name 124 | name="$1" 125 | elif [[ "$1" =~ ^((-{1,2})([Hh]$|[Hh][Ee][Ll][Pp])|)$ ]]; then 126 | Github__callable_help 127 | else 128 | while [[ $# -gt 1 ]]; do 129 | opt="$1" 130 | shift; 131 | current_arg="$1" 132 | case "$opt" in 133 | "-n"|"--name" ) name="$1"; shift;; 134 | "-d"|"--description" ) description="$1"; shift;; 135 | "-h"|"--homepage" ) homepage="$1"; shift;; 136 | "-p"|"--private" ) private=true; shift;; 137 | "-i"|"--disable-issues" ) hasIssues=false; shift;; 138 | "-w"|"--disable-wiki" ) hasWiki=false; shift;; 139 | "-D"|"--disable-downloads" ) hasDownloads=false; shift;; 140 | "-t"|"--teams" ) teams="$1"; shift;; 141 | "-a"|"--auto-init" ) autoInit=true; shift;; 142 | "-g"|"--gitignore" ) gitignore="$1"; shift;; 143 | "-l"|"--license" ) license="$1"; shift;; 144 | "-v"|"--verbose" ) verbose=true; shift;; 145 | "-r"|"--dry-run" ) dryRun=true; shift;; 146 | * ) Logger__error "Invalid option: \""$opt"\"" 147 | esac 148 | done 149 | fi 150 | 151 | if [[ "$name" == "" ]]; then 152 | Logger__error "You must pass a name" 153 | return 154 | fi 155 | 156 | # data 157 | local data="{ 158 | \"name\": \"$name\", 159 | \"description\": \"$description\", 160 | \"homepage\": \"$homepage\", 161 | \"private\": $private, 162 | \"has_issues\": $hasIssues, 163 | \"has_wiki\": $hasWiki, 164 | \"has_downloads\": $hasDownloads, 165 | \"team_id\": \"$teams\", 166 | \"auto_init\": $autoInit, 167 | \"gitignore_template\": \"$gitignore\", 168 | \"license_template\": \"$license\" 169 | }" 170 | 171 | if [[ "$verbose" == true ]]; then 172 | curlOpts+='--verbose' 173 | else 174 | curlOpts+='-s' 175 | fi 176 | 177 | if [[ "$org" != "" ]]; then # orgs 178 | url="https://api.github.com/orgs/$org/repos" 179 | fi 180 | 181 | if [[ "$dryRun" == true ]]; then # dry run thru proxy 182 | nc -l localhost 8000 & \ 183 | curl "$curlOpts" \ 184 | --proxy localhost:8000 \ 185 | -H "Authorization: token $GITHUB_TOKEN" \ 186 | -H "Content-Type: application/json" \ 187 | -X POST "$url" \ 188 | -d "$data" 189 | printf "body: $data\n" 190 | else # real request 191 | local res=$(curl -s \ 192 | "$curlOpts" \ 193 | -H "Authorization: token $GITHUB_TOKEN" \ 194 | -H "Content-Type: application/json" \ 195 | -X POST "$url" \ 196 | -d "$data") 197 | 198 | if [[ "$verbose" == true ]]; then 199 | printf "$res\n" 200 | fi 201 | 202 | if [[ ! "$res" =~ 2.. ]]; then 203 | printf "Failed to create your repository.\n" 204 | printf "Status: $res\n" 205 | else 206 | printf "Successfully created your repository.\n" 207 | fi 208 | fi 209 | } 210 | 211 | ################################################## 212 | # Delets a Gitub repository 213 | # 214 | # @param $1: owner/repository 215 | ################################################## 216 | Github__repo_delete(){ 217 | local url="https://api.github.com/repos/$@" 218 | local repo='' 219 | 220 | if [[ ! "$@" =~ .*\/.* ]]; then 221 | printf "Pass your repository as owner/repo. (ie, ash-shell/foo)\n" 222 | return 223 | fi 224 | 225 | Github_validate_token 226 | Logger__prompt "Please type in the name of the repository to confirm: "; read response 227 | if [[ "$@" =~ (.*)\/(.*) ]]; then 228 | if [[ "$response" = "${BASH_REMATCH[2]}" ]]; then 229 | local res=$(curl \ 230 | -s \ 231 | -H "Authorization: token $GITHUB_TOKEN" \ 232 | -X DELETE "$url") 233 | 234 | if [[ $res != "" ]]; then 235 | printf "Failed to delete repository.\n" 236 | printf "Status: $res\n" 237 | else 238 | printf "Successfully deleted repository.\n" 239 | fi 240 | else 241 | return 242 | fi 243 | fi 244 | } 245 | -------------------------------------------------------------------------------- /extras/label_configs/carrots: -------------------------------------------------------------------------------- 1 | # Remove Github defaults 2 | -import:clear-defaults 3 | 4 | # Add Bug Labels 5 | bug:ffaca9 6 | 7 | # Issue Type 8 | chore:e7f7f8 9 | enhancement:d6e5e6 10 | feature-request:adc6c8 11 | feature:75989b 12 | 13 | # Status 14 | blocked:000000 15 | on-hold:939b75 16 | 17 | # Priorities 18 | priority-high:bbc9ee 19 | priority-medium:8692b2 20 | priority-low:4d5a7e 21 | 22 | # Other 23 | wontfix:FFFFFF 24 | -------------------------------------------------------------------------------- /extras/label_configs/carrots-android: -------------------------------------------------------------------------------- 1 | # Carrots Base 2 | -import:carrots 3 | 4 | # Android Version 5 | android-version-4.0:59ac55 6 | android-version-4.1:59ac55 7 | android-version-4.4:59ac55 8 | android-version-5.0:59ac55 9 | android-version-6.0:59ac55 10 | 11 | # Screen Size 12 | android-screens-small:add6ab 13 | android-screens-large:add6ab 14 | -------------------------------------------------------------------------------- /extras/label_configs/carrots-ios: -------------------------------------------------------------------------------- 1 | # Carrots Base 2 | -import:carrots 3 | 4 | # Platform 5 | platform-phone:a6b1b7 6 | platform-tablet:a6b1b7 7 | platform-watch:a6b1b7 8 | platform-tv:a6b1b7 9 | -------------------------------------------------------------------------------- /extras/label_configs/carrots-web: -------------------------------------------------------------------------------- 1 | # Carrots Base 2 | -import:carrots 3 | 4 | # Browsers 5 | browser-chrome:d6b79b 6 | browser-ff:d6b79b 7 | browser-ie:d6b79b 8 | browser-safari:d6b79b 9 | 10 | # Platforms 11 | platform-mobile:a6b1b7 12 | platform-tablet:a6b1b7 13 | -------------------------------------------------------------------------------- /extras/label_configs/clear-defaults: -------------------------------------------------------------------------------- 1 | # Remove Github defaults 2 | -delete:bug 3 | -delete:duplicate 4 | -delete:enhancement 5 | -delete:help wanted 6 | -delete:invalid 7 | -delete:question 8 | -delete:wontfix 9 | -delete:good first issue 10 | -------------------------------------------------------------------------------- /lib/labels.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Label Configs location 4 | Github_label_config_directory="$Ash__ACTIVE_MODULE_DIRECTORY/extras/label_configs" 5 | 6 | ################################################## 7 | # Loads in a config file and handles it 8 | # 9 | # @param $1: The repo name 10 | # @param $2: The labels config name 11 | # @param $3: 1 if this is an import 12 | # 0 if this is the base file 13 | ################################################## 14 | Github__labels_handle_config_file(){ 15 | # Checking if we've got a valid config file 16 | local label_config_file="$Github_label_config_directory/$2" 17 | if [[ ! -f "$label_config_file" ]]; then 18 | # Import 19 | if [[ $3 -eq 1 ]]; then 20 | Logger__error "Failed to import: $2" 21 | # Base File 22 | else 23 | Logger__error "Requires a valid label config file to be passed in" 24 | Logger__error "Here are the current available label config files:" 25 | ls $Github_label_config_directory 26 | Logger__prompt "Input a label config from above (ex, carrots): "; read label 27 | label_config_file="$Github_label_config_directory/$label" 28 | if [[ ! -f "$label_config_file" ]]; then 29 | # Retry Import 30 | if [[ $3 -eq 1 ]]; then 31 | Logger__error "Failed to import: $label" 32 | else 33 | Logger__error "Label config does not exist." 34 | return 35 | fi 36 | fi 37 | fi 38 | fi 39 | 40 | # Adding all labels 41 | while read line; do 42 | # Removing comments 43 | local line=$(echo "$line" | sed 's/\ *#.*//g') 44 | if [[ ${#line} -eq 0 ]]; then 45 | continue 46 | fi 47 | 48 | # Handling action 49 | Github__handle_action "$1" "$line" 50 | done < $label_config_file 51 | } 52 | 53 | ################################################## 54 | # Handles a single line within a config file 55 | # 56 | # @param $1: The repo name 57 | # @param $2: The comment parsed line in a 58 | # label_config file. 59 | ################################################## 60 | Github__handle_action(){ 61 | # Checking if action 62 | if [[ $2 == -* ]]; then 63 | local action=$(echo $2 | awk -F':' '{print $1}') 64 | 65 | # Action is delete 66 | if [[ "$action" == "-delete" ]]; then 67 | local label=$(echo $2 | awk -F':' '{print $2}') 68 | 69 | if [[ "$label" == "all" ]]; then 70 | Github__delete_labels "$1" 71 | else 72 | local response=$(Github__delete_label "$1" "$label") 73 | if [[ "$response" = "deleted" ]]; then 74 | Logger__success "Deleted Label: $label" 75 | else 76 | Logger__warning "Failed to delete label: $label" 77 | fi 78 | fi 79 | 80 | # Action is import 81 | elif [[ "$action" == "-import" ]]; then 82 | local import_file=$(echo $2 | awk -F':' '{print $2}') 83 | Github__labels_handle_config_file "$1" "$import_file" 1 84 | fi 85 | 86 | # Default add line 87 | else 88 | local label=$(echo $2 | awk -F':' '{print $1}') 89 | local color=$(echo $2 | awk -F':' '{print $2}') 90 | 91 | local response=$(Github__create_single_label "$1" "$label" "$color") 92 | 93 | if [[ "$response" = "added" ]]; then 94 | Logger__success "Added label: $label" 95 | elif [[ "$response" = "updated" ]]; then 96 | Logger__success "Updated label: $label" 97 | else 98 | Logger__warning "Failed to add label: $label" 99 | fi 100 | fi 101 | } 102 | 103 | ################################################## 104 | # Deletes a single label from a repository 105 | # 106 | # @param $1: The repo name 107 | # @param $2: The label name 108 | # 109 | # @returns: 'failure' if we failed to delete the label 110 | # 'deleted' if we successfully deleted the label 111 | ################################################## 112 | Github__delete_label(){ 113 | local repo="$1" 114 | local label="$2" 115 | label=$(echo "$label" | sed 's/\ /%20/g') 116 | 117 | # Try to delete via DELETE 118 | local delete_response=$(curl \ 119 | -s -o /dev/null -w "%{http_code}" \ 120 | -H "Authorization: token $GITHUB_TOKEN" \ 121 | -X DELETE "https://api.github.com/repos/$repo/labels/$label") 122 | 123 | # Checking if DELETE worked 124 | if [[ $delete_response =~ 2.. ]]; then 125 | echo "deleted" 126 | return 127 | fi 128 | 129 | echo "failure" 130 | } 131 | 132 | ################################################## 133 | # Deletes all labels from a repository 134 | # 135 | # @param $1: The repo name 136 | # 137 | # @returns: 'failure' if we failed to delete the label 138 | # 'deleted' if we successfully deleted the label 139 | ################################################## 140 | Github__delete_labels(){ 141 | local repo="$1" 142 | # fetch all labels 143 | local labels=$(curl \ 144 | -s \ 145 | -H "Authorization: token $GITHUB_TOKEN" \ 146 | -X GET "https://api.github.com/repos/$repo/labels") 147 | 148 | echo "$labels" | while IFS='' read -r line || [[ -n "$line" ]]; do 149 | if [[ "$line" =~ (\"name\":)(\s?)(.*)\" ]]; then 150 | if [[ "${BASH_REMATCH[3]}" =~ \"(.*) ]]; then 151 | label="${BASH_REMATCH[1]}" 152 | local delete_response=$(curl \ 153 | -s -o /dev/null -w "%{http_code}" \ 154 | -H "Authorization: token $GITHUB_TOKEN" \ 155 | -X DELETE "https://api.github.com/repos/$repo/labels/$label") 156 | if [[ $delete_response =~ 2.. ]]; then 157 | Logger__success "Deleted Label: $label" 158 | else 159 | Logger__warning "Failed to delete label: $label" 160 | fi 161 | fi 162 | fi 163 | done 164 | } 165 | 166 | ################################################## 167 | # Creates a single label on a repository 168 | # 169 | # @param $1: Repository name 170 | # @param $2: Label name 171 | # @param $3: Label color 172 | # 173 | # @returns: 'failure' if we failed to add / update the label 174 | # 'added' if we successfully added the label 175 | # 'updated' if we successfully updated the label 176 | ################################################## 177 | Github__create_single_label(){ 178 | # Try to create via POST 179 | local post_response=$(curl \ 180 | -s -o /dev/null -w "%{http_code}" \ 181 | -H "Authorization: token $GITHUB_TOKEN" \ 182 | -X POST "https://api.github.com/repos/$1/labels" \ 183 | -d "{\"name\":\"$2\", \"color\":\"$3\"}") 184 | 185 | # Checking if POST worked 186 | if [[ $post_response =~ 2.. ]]; then 187 | echo "added" 188 | return 189 | fi 190 | 191 | # Update via PATCH 192 | local patch_response=$(curl \ 193 | -s -o /dev/null -w "%{http_code}" \ 194 | -H "Authorization: token $GITHUB_TOKEN" \ 195 | -X PATCH "https://api.github.com/repos/$1/labels/$2" \ 196 | -d "{\"name\":\"$2\", \"color\":\"$3\"}") 197 | 198 | # Checking if PATCH worked 199 | if [[ $patch_response =~ 2.. ]]; then 200 | echo "updated" 201 | else 202 | echo "failure" 203 | fi 204 | } 205 | -------------------------------------------------------------------------------- /lib/util.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | ################################################## 4 | # Validates if the GITHUB_TOKEN is set in the 5 | # .ashrc file. Prompts the user to set one if it 6 | # hasn't been set yet. 7 | # 8 | # @returns: 0 if there is a Github token set at 9 | # the end of this function call 10 | # 1 if there is *not* a Github token set 11 | # at the end of this function call 12 | ################################################## 13 | Github_validate_token(){ 14 | # Checking if variable exists 15 | if [[ -z "$GITHUB_TOKEN" ]]; then 16 | local response 17 | Logger__alert "GITHUB_TOKEN is not set in the .ashrc file" 18 | Logger__prompt "Would you like to set one now? (y/n): "; read response 19 | 20 | # If user wants to input their token 21 | if [[ "$response" = "y" || "$response" = "Y" ]]; then 22 | local github_token 23 | Logger__prompt "Enter Github Token (https://github.com/settings/tokens): "; read github_token 24 | 25 | local export_line="export GITHUB_TOKEN=\"$github_token\"" 26 | echo "$export_line" >> $Ash__RC_FILE # Store it to .ashrc 27 | export GITHUB_TOKEN="$github_token" # Exporting for current use 28 | return 0 29 | else 30 | return 1 31 | fi 32 | else 33 | return 0 34 | fi 35 | } 36 | --------------------------------------------------------------------------------