Skip to content

Commit 0b600f3

Browse files
committed
feat: misc improvements
add hostname add #!/bin/bash parameterize inputs
1 parent 822ef03 commit 0b600f3

10 files changed

+132
-76
lines changed
+21-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
# using gh repo edit native cli:
2-
gh repo edit <org>/<repo> --visibility internal
1+
#!/bin/bash
2+
3+
# changes the visibility of a repository
4+
5+
if [ $# -lt "3" ]; then
6+
echo "Usage: $0 <org> <repo> <visibility: public|internal|private> <optional: hostname>"
7+
exit 1
8+
fi
9+
10+
org=$1
11+
repo=$2
12+
visibility=$3
13+
hostname=${4:-"github.com"}
314

415
# alternative with gh api (can be used to modify multiple properties of repo at once):
5-
gh api \
6-
--method PATCH \
7-
-H "Accept: application/vnd.github+json" \
8-
/repos/<org>/<repo> \
9-
-f visibility='internal'
16+
result=$(gh api --hostname $hostname -X PATCH /repos/$org/$repo -f visibility=$visibility | jq -r '"name: \(.name), visibility: \(.visibility)"')
17+
18+
if [ $? -eq 0 ]; then
19+
echo "$result"
20+
fi
21+
22+
# using gh repo edit native cli:
23+
# gh repo edit $org/$repo --visibility $visibility
+28-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1-
gh api \
2-
--method POST \
3-
-H "Accept: application/vnd.github+json" \
4-
/repos/joshjohanning-org/template-repo/generate \
5-
-f owner='joshjohanning-org' \
6-
-f name='deletemeeeeeeeeee' \
7-
-f description='This is your first repository' \
8-
-F include_all_branches=false \
9-
-F private=true # either do true for private or false for public
10-
11-
# use change-repository-visibility.sh to change visibility to internal if needed
1+
#!/bin/bash
2+
3+
if [ $# -lt "6" ]; then
4+
echo "Usage: $0 <org> <template-repo> <repo-to-create> <repo-description> <include-all-branches: true|false> <private: true|false> <visibility: public|internal|private> <optional: hostname>"
5+
echo "Example: ./create-repository-from-template.sh joshjohanning-org template-repo deleteme12345 test false false internal"
6+
exit 1
7+
fi
8+
9+
org=$1
10+
template_repo=$2
11+
repo_to_create=$3
12+
repo_description=$4
13+
include_all_branches=${5:-false}
14+
private=${6:-true}
15+
visibility=${7:-"private"}
16+
hostname=${8:-"github.com"}
17+
18+
gh api --hostname $hostname -X POST /repos/$org/$template_repo/generate \
19+
-f owner="$org" \
20+
-f name="$repo_to_create" \
21+
-f description="$repo_description" \
22+
-F include_all_branches=$include_all_branches \
23+
-F private=$private # either do true for private or false for public
24+
25+
# if visibility was specified as internal, we need to run 1 more api call to update the visibility
26+
if [ "$visibility" == "internal" ]; then
27+
result=$(gh api --hostname $hostname -X PATCH /repos/$org/$repo_to_create -f visibility=$visibility | jq -r '"name: \(.name), visibility: \(.visibility)"')
28+
fi

gh-cli/get-enterprise-members.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
21
#!/bin/bash
32

43
# gh cli's token needs to be able to admin enterprise - run this first if it can't
54
# gh auth refresh -h github.com -s admin:enterprise
65

76
if [ -z "$1" ]; then
8-
echo "Usage: $0 <enterprise>"
9-
echo "Example: ./get-enterprise-members.sh avocado-corp"
7+
echo "Usage: $0 <enterprise> <hostname>"
8+
echo "Example: ./get-enterprise-members.sh avocado-corp github.com"
109
exit 1
1110
fi
1211

1312
enterprise="$1"
13+
hostname=${2:-"github.com"}
1414

15-
gh api graphql --paginate -f enterpriseSlug=$enterprise -f query='
15+
gh api graphql --hostname $hostname --paginate -f enterpriseSlug=$enterprise -f query='
1616
query ($enterpriseSlug: String!, $endCursor: String) {
1717
enterprise(slug: $enterpriseSlug) {
1818
members(first: 100, after: $endCursor) {

gh-cli/get-enterprise-owners.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#!/bin/bash
32

43
# gets a list of
@@ -11,14 +10,15 @@
1110
# - BILLING_MANAGER: The user is a billing manager of the enterprise.
1211

1312
if [ -z "$1" ]; then
14-
echo "Usage: $0 <enterprise>"
15-
echo "Example: ./get-enterprise-owners.sh avocado-corp"
13+
echo "Usage: $0 <enterprise> <hostname>"
14+
echo "Example: ./get-enterprise-owners.sh avocado-corp github.com"
1615
exit 1
1716
fi
1817

1918
enterprise="$1"
19+
hostname=${2:-"github.com"}
2020

21-
gh api graphql --paginate -f enterpriseSlug=$enterprise -f query='
21+
gh api graphql --hostname $hostname --paginate -f enterpriseSlug=$enterprise -f query='
2222
query ($enterpriseSlug: String!, $endCursor: String) {
2323
enterprise(slug: $enterpriseSlug) {
2424
ownerInfo {

gh-cli/get-organizations-projects-count-classic.sh

+2-8
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@
77
# note: format is tsv
88

99
if [ $# -lt 1 ]; then
10-
echo "usage: $0 <enterprise slug> <hostname> > output.tsv"
10+
echo "usage: $0 <enterprise-slug> <hostname> > output.tsv"
1111
exit 1
1212
fi
1313

1414
enterprise=$1
15-
hostname=$2
15+
hostname=${2:-"github.com"}
1616
export PAGER=""
1717

18-
# set hostname to github.com by default
19-
if [ -z "$hostname" ]
20-
then
21-
hostname="github.com"
22-
fi
23-
2418
echo -e "Organization\tProjects Count (classic)"
2519

2620
gh api graphql -f enterprise="$enterprise" --paginate --hostname $hostname -f query='query($enterprise:String!, $endCursor: String) {

gh-cli/get-organizations-projects-count.sh

+2-8
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@
77
# note: format is tsv
88

99
if [ $# -lt 1 ]; then
10-
echo "usage: $0 <enterprise slug> <hostname> > output.tsv"
10+
echo "usage: $0 <enterprise-slug> <hostname> > output.tsv"
1111
exit 1
1212
fi
1313

1414
enterprise=$1
15-
hostname=$2
15+
hostname=${2:-"github.com"}
1616
export PAGER=""
1717

18-
# set hostname to github.com by default
19-
if [ -z "$hostname" ]
20-
then
21-
hostname="github.com"
22-
fi
23-
2418
echo -e "Organization\tProjectv2 Count"
2519

2620
gh api graphql -f enterprise="$enterprise" --paginate --hostname $hostname -f query='query($enterprise:String!, $endCursor: String) {

gh-cli/get-organizations-settings.sh

+36-24
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,18 @@
99

1010
if [ $# -lt 1 ]
1111
then
12-
echo "usage: $0 <enterprise slug> <hostname> <format: tsv|json> > output.tsv/json"
12+
echo "usage: $0 <enterprise-slug> <hostname> <format: tsv|json> > output.tsv/json"
1313
exit 1
1414
fi
1515

1616
enterpriseslug=$1
17-
hostname=$2
18-
format=$3
17+
hostname=${2:-"github.com"}
18+
format=${3:-"tsv"}
1919
export PAGER=""
2020

21-
# set hostname to github.com by default
22-
if [ -z "$hostname" ]
23-
then
24-
hostname="github.com"
25-
fi
26-
27-
if [ -z "$format" ]
28-
then
29-
format="tsv"
30-
fi
21+
# Define color codes
22+
RED='\033[0;31m'
23+
NC='\033[0m' # No Color
3124

3225
organizations=$(gh api graphql --hostname $hostname --paginate -f enterpriseName="$enterpriseslug" -f query='
3326
query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
@@ -46,27 +39,46 @@ query getEnterpriseOrganizations($enterpriseName: String! $endCursor: String) {
4639
}' --jq '.data.enterprise.organizations.nodes[].login')
4740

4841
# check to see if organizations is null - null error message is confusing otherwise
49-
if [ -z "$organizations" ] || [ $? -ne 0 ]
50-
then
51-
# Define color codes
52-
RED='\033[0;31m'
53-
NC='\033[0m' # No Color
54-
55-
# Print colored messages
42+
if [ -z "$organizations" ] || [[ "$organizations" == *"INSUFFICIENT_SCOPES"* ]]; then
5643
echo -e "${RED}No organizations found for enterprise: $enterpriseslug${NC}"
57-
echo -e "${RED}Check that you have the proper scopes for enterprise, e.g.: 'gh auth refresh -h github.com -s read:org -s read:enterprise'${NC}"
44+
echo -e "${RED} - Check that you have the proper scopes for enterprise with 'gh auth status' - you need at least 'read:enterprise'${NC}"
45+
echo -e "${RED} - You can run 'gh auth refresh -h github.com -s read:org -s read:enterprise' to add the scopes${NC}"
46+
echo -e "${RED} - Or you can run 'gh auth login -h github.com' and authenticate using a PAT with the proper scopes${NC}"
5847
exit 1
5948
fi
6049

6150
if [ "$format" == "tsv" ]; then
62-
echo -e "Org Login\tOrg Name\tOrg Desc\tDefault Repo Permission\tMembers Can Create Repos\t\tMembers Allowed Repos Creation Type\tMembers Can Create Public Repos\tMembers Can Create Private Repos\tMembers Can Create Internal Repos\tMembers Can Fork Private Repos"
51+
echo -e "Organization Login\tDisplay Name\tDescription\tDefault Repo Permission\tMembers Can Create Repos\t\tMembers Allowed Repos Creation Type\tMembers Can Create Public Repos\tMembers Can Create Private Repos\tMembers Can Create Internal Repos\tMembers Can Fork Private Repos"
6352
fi
6453

54+
errors=""
55+
6556
for org in $organizations
6657
do
6758
if [ "$format" == "tsv" ]; then
68-
gh api "orgs/$org" --hostname $hostname --jq ". | [\"$org\", .name, .description, .default_repository_permission, .members_can_create_repositories, .members_allowed_repository_creation_type, .members_can_create_public_repositories, .members_can_create_private_repositories, .members_can_create_internal_repositories, .members_can_fork_private_repositories] | @tsv"
59+
output=$(gh api "orgs/$org" --hostname $hostname --jq ". | [\"$org\", .name, .description, .default_repository_permission, .members_can_create_repositories, .members_allowed_repository_creation_type, .members_can_create_public_repositories, .members_can_create_private_repositories, .members_can_create_internal_repositories, .members_can_fork_private_repositories] | @tsv" 2>&1)
6960
else
70-
gh api "orgs/$org" --hostname $hostname
61+
output=$(gh api "orgs/$org" --hostname $hostname 2>&1)
62+
fi
63+
64+
if [ $? -ne 0 ] || [[ ! "$output" =~ "true" ]] && [[ ! "$output" =~ "false" ]]; then
65+
if [ "$format" == "tsv" ]; then
66+
output=$(echo -e "$output" | tr -d '\t')
67+
output+=" - (you may not have admin access to the org)"
68+
fi
69+
errors="$errors\nError accessing organization: $org:\n$output"
70+
if [ "$format" == "tsv" ]; then
71+
echo -e "$org\tn/a\tn/a\tn/a\tn/a\tn/a\tn/a\tn/a\tn/a\tn/a"
72+
else
73+
echo -e "$org,n/a,n/a,n/a,n/a,n/a,n/a,n/a,n/a,n/a"
74+
fi
75+
else
76+
if [ -n "$output" ]; then
77+
echo "$output"
78+
fi
7179
fi
7280
done
81+
82+
if [ -n "$errors" ]; then
83+
echo -e "${RED}\nErrors encountered:\n$errors${NC}"
84+
fi

gh-cli/get-saml-identities-in-enterprise.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
#!/bin/bash
12

23
# gh cli's token needs to be able to admin enterprise - run this first if it can't
34
# gh auth refresh -h github.com -s admin:enterprise
45

5-
gh api graphql --paginate -f enterpriseName='your-enterprise-name' -f query='
6+
if [ $# -lt "1" ]; then
7+
echo "Usage: $0 <enterprise>"
8+
exit 1
9+
fi
10+
11+
enterprise="$1"
12+
13+
gh api graphql --paginate -f enterpriseName="$enterprise" -f query='
614
query listSSOUserIdentities ($enterpriseName:String! $endCursor: String) {
715
enterprise(slug: $enterpriseName) {
816
ownerInfo {

gh-cli/get-saml-identities-in-organization.sh

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
#!/bin/bash
12

23
# gh cli's token needs to be able to admin organizations - run this first if it can't
34
# gh auth refresh -h github.com -s admin:org
45

5-
gh api graphql --paginate -f organizationName='joshjohanning-org-saml' -f query='
6+
if [ $# -lt "1" ]; then
7+
echo "Usage: $0 <org>"
8+
exit 1
9+
fi
10+
11+
org="$1"
12+
13+
gh api graphql --paginate -f organizationName="$org" -f query='
614
query listSSOUserIdentities($organizationName: String! $endCursor: String) {
715
organization(login: $organizationName) {
816
samlIdentityProvider {

gh-cli/rename-repository.sh

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
# using gh repo edit native cli:
2-
gh repo edit <org>/<repo> --visibility internal
3-
gh repo rename --repo <org>/<old-repo-name> <new-repo-name>
1+
#!/bin/bash
2+
3+
# renames a repository
4+
5+
if [ -z "$3" ]; then
6+
echo "Usage: $0 <org> <repo> <new-repo-name> <optional: hostname>"
7+
exit 1
8+
fi
9+
10+
org="$1"
11+
repo="$2"
12+
new_repo_name="$3"
13+
hostname=${4:-"github.com"}
414

515
# alternative with gh api (can be used to modify multiple properties of repo at once):
6-
gh api \
7-
--method PATCH \
8-
-H "Accept: application/vnd.github+json" \
9-
/repos/<org>/<old-repo-name> \
10-
-f name='<new-repo-name>'
16+
gh api -X PATCH /repos/$org/$repo -f name="$new_repo_name"
17+
18+
# using gh repo edit native cli:
19+
# gh repo rename --repo $org/$repo $new_repo_name

0 commit comments

Comments
 (0)