├── LICENSE ├── README.md ├── gh-orgstats └── helper_functions /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Sebastian Steenssøe 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 all 13 | 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 THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gh-orgstats 2 | 3 | ## Description 4 | Organisation specific extension for `gh cli` to retrieve different statistics 5 | 6 | ## Install 7 | ```sh 8 | gh extension install VildMedPap/gh-orgstats 9 | ``` 10 | 11 | ## Usage 12 | General usage 13 | 14 | ```sh 15 | # gh orgstats [flags] 16 | ``` 17 | 18 | Get a single statistic from an organisation 19 | 20 | ```sh 21 | gh orgstats --org cli --stats repos 22 | # ✓ 4 repositories 23 | ``` 24 | 25 | Get a single statistic from an organisation with very plain output (only the number) 26 | 27 | ```sh 28 | gh orgstats --org cli --stats repos --plain 29 | # 4 30 | ``` 31 | 32 | Get multiple statistics from an organisation and output as json 33 | 34 | ```sh 35 | gh orgstats --org cli --stats open_issues,closed_issues --json 36 | # { 37 | # "open_issues": 356, 38 | # "closed_issues": 1737 39 | # } 40 | ``` 41 | 42 | Get _all_ statistics from an organisation and output as json 43 | 44 | ```sh 45 | gh orgstats --org cli --json 46 | # { 47 | # "repos": 4, 48 | # "open_issues": 356, 49 | # "closed_issues": 1737 50 | # } 51 | ``` 52 | 53 | ## Help 54 | ```sh 55 | gh orgstats --help 56 | ``` 57 | 58 | ## Issues and Feature Request 59 | Any help or feedback are very welcome! 👋🏼 60 | 61 | - https://github.com/VildMedPap/gh-orgstats/issues 62 | -------------------------------------------------------------------------------- /gh-orgstats: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright (c) 2021 Sebastian Steenssøe 4 | # All rights reserved. 5 | 6 | # This source code is licensed under the MIT-style license found in the 7 | # LICENSE file in the root directory of this source tree. 8 | 9 | # Author: Sebastian Steenssøe 10 | # Date Created: 02/09/2021 11 | # Last Modified: 02/09/2021 12 | 13 | # Description 14 | # Organisation specific extension for gh cli to retrieve different statistics 15 | 16 | # Source(s) 17 | # https://github.com/VildMedPap/gh-orgstats 18 | 19 | # Usage 20 | # gh extension install VildMedPap/gh-orgstats 21 | # gh orgstats --help 22 | set -e 23 | 24 | # Source helper functions 25 | DIR="$(dirname "$0")" 26 | . "$DIR/helper_functions" 27 | 28 | # Parse arguments 29 | # Solution borrowed from https://unix.stackexchange.com/a/580258 30 | while [ $# -gt 0 ] 31 | do 32 | case "$1" in 33 | --org*|-o*) 34 | if [[ "$1" != *=* ]]; then shift; fi 35 | ORG="${1#*=}" 36 | ;; 37 | --stats*|-s*) 38 | if [[ "$1" != *=* ]]; then shift; fi 39 | STATS="${1#*=}" 40 | ;; 41 | --plain*|-p*) 42 | PLAIN=0 43 | ;; 44 | --json*|-j*) 45 | JSON=0 46 | ;; 47 | --help|-h) 48 | get_help 49 | exit 0 50 | ;; 51 | *) 52 | >&2 printf "Error: Invalid argument\n" 53 | exit 1 54 | ;; 55 | esac 56 | shift 57 | done 58 | 59 | if [ -z $STATS ] 60 | then 61 | STATS=repos,open_issues,closed_issues 62 | fi 63 | 64 | # Retrieve number of repos 65 | if [[ $STATS == *"repos"* ]] 66 | then 67 | value=$(get_repos $ORG) 68 | 69 | if [ ! -z $JSON ] 70 | then 71 | JSON_FMT+="\"repos\": $value," 72 | else 73 | if [ -z $PLAIN ]; then printf "\033[32m✓\033[0m ${value} repositories\n"; else echo $value;fi 74 | fi 75 | fi 76 | 77 | # Retrieve open issues 78 | if [[ $STATS == *"open_issues"* ]] 79 | then 80 | value=$(get_open_issues $ORG) 81 | 82 | if [ ! -z $JSON ] 83 | then 84 | JSON_FMT+="\"open_issues\": $value," 85 | else 86 | if [ -z $PLAIN ]; then printf "\033[32m✓\033[0m ${value} issues are marked as \"open\"\n"; else echo $value;fi 87 | fi 88 | fi 89 | 90 | # Retrieve closed issues 91 | if [[ $STATS == *"closed_issues"* ]] 92 | then 93 | value=$(get_closed_issues $ORG) 94 | 95 | if [ ! -z $JSON ] 96 | then 97 | JSON_FMT+="\"closed_issues\": $value," 98 | else 99 | if [ -z $PLAIN ]; then printf "\033[32m✓\033[0m ${value} issues are marked as \"closed\"\n"; else echo $value;fi 100 | fi 101 | fi 102 | 103 | # Concatenate to json 104 | if [ ! -z $JSON ];then echo "{${JSON_FMT::-1}}"|jq .;fi 105 | -------------------------------------------------------------------------------- /helper_functions: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Copyright (c) 2021 Sebastian Steenssøe 4 | # All rights reserved. 5 | 6 | # This source code is licensed under the MIT-style license found in the 7 | # LICENSE file in the root directory of this source tree. 8 | 9 | # Author: Sebastian Steenssøe 10 | # Date Created: 02/09/2021 11 | # Last Modified: 02/09/2021 12 | 13 | # Description 14 | # Helper functions 15 | 16 | # Usage 17 | # get_help 18 | # get_repos [organization name] 19 | # get_open_issues [organization name] 20 | # get_closed_issues [organization name] 21 | 22 | get_help() { 23 | printf "Organisation specific extension for gh cli to retrieve different statistics 24 | 25 | \033[1mUSAGE\033[0m 26 | gh orgstats [flags] 27 | 28 | \033[1mFLAGS\033[0m 29 | --org Name of organization 30 | --stats Comma separated string of statistics 31 | stats available: repos, closed_issues, open_issues 32 | --plain Get numbers without pretty output 33 | --json Output as json 34 | 35 | \033[1mEXAMPLES\033[0m 36 | $ gh orgstats --org cli --stats open_issues,closed_issues,repos 37 | $ gh orgstats --org cli --stats open_issues --plain 38 | 39 | \033[1mLEARN MORE\033[0m 40 | https://github.com/VildMedPap/gh-orgstats 41 | " 42 | } 43 | 44 | get_repos() { 45 | QUERY=' 46 | query ($login: String!) { 47 | organization(login: $login) { 48 | repositories(isFork: false) { 49 | totalCount 50 | } 51 | } 52 | } 53 | ' 54 | 55 | gh api graphql -F login="${1}" -f query="${QUERY}" --jq '.data.organization.repositories.totalCount' 56 | } 57 | 58 | get_open_issues() { 59 | QUERY=' 60 | query ($endCursorRepos: String, $login: String!) { 61 | organization(login: $login) { 62 | repositories(isFork: false, first: 100, after: $endCursorRepos) { 63 | pageInfo { 64 | endCursor 65 | } 66 | edges { 67 | node { 68 | issues(states: OPEN) { 69 | totalCount 70 | } 71 | } 72 | } 73 | } 74 | } 75 | } 76 | ' 77 | 78 | open_issues_arr=$(gh api graphql --paginate -F login="${1}" -f query="${QUERY}" --jq '.data.organization.repositories.edges.[].node.issues.totalCount') 79 | echo ${open_issues_arr[@]} | sed 's/ /+/g' | bc 80 | } 81 | 82 | get_closed_issues() { 83 | QUERY=' 84 | query ($endCursorRepos: String, $login: String!) { 85 | organization(login: $login) { 86 | repositories(isFork: false, first: 100, after: $endCursorRepos) { 87 | pageInfo { 88 | endCursor 89 | } 90 | edges { 91 | node { 92 | issues(states: CLOSED) { 93 | totalCount 94 | } 95 | } 96 | } 97 | } 98 | } 99 | } 100 | ' 101 | 102 | closed_issues_arr=$(gh api graphql --paginate -F login="${1}" -f query="${QUERY}" --jq '.data.organization.repositories.edges.[].node.issues.totalCount') 103 | echo ${closed_issues_arr[@]} | sed 's/ /+/g' | bc 104 | } 105 | --------------------------------------------------------------------------------