Skip to content

Commit 3f9a61d

Browse files
committed
bugfix: show private repositories as well
Listing private repositories requires the 'repo' OAuth scope.
1 parent fc80992 commit 3f9a61d

2 files changed

Lines changed: 73 additions & 24 deletions

File tree

git-clone-completions.bash

Lines changed: 72 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,11 @@ stop_spinner()
494494
}
495495

496496

497-
#######################################
498-
# #
499-
# REST Call Utilities #
500-
# #
501-
#######################################
497+
###################################################
498+
# #
499+
# REST and GraphQL Call Utilities #
500+
# #
501+
###################################################
502502

503503
# call an endpoint and retrieve the full result.
504504
# reads all pages if the result is paginated and has the
@@ -524,6 +524,21 @@ _rest_call()
524524
rm -f "$tmp"
525525
}
526526

527+
#
528+
# Store a graphql query into a variable $varname in a format
529+
# that's ready to be sent as a javascript string (w/o newlines)
530+
#
531+
# usage: defgraphql <varname> <<-'EOF' ... query text ... EOF
532+
#
533+
defgraphql()
534+
{
535+
# squash the graphql text into a single line (as Javascript
536+
# doesn't allow multiline strings) and assign it to
537+
# variable $1
538+
read -r -d '' "$1" < <(tr -s ' \t\n' ' ')
539+
}
540+
541+
527542
########################################################################
528543
#
529544
# Repository hosting services
@@ -657,7 +672,7 @@ init-github-completion()
657672
echo "and generate a new personal access token:"
658673
echo
659674
echo " 1. Under 'Note', write 'git-clone-completions access for $USER@$(hostname)'"
660-
echo " 2. Under 'Select scopes', check 'repo:status' and leave otherwise unchecked."
675+
echo " 2. Under 'Select scopes', check 'repo' and leave otherwise unchecked."
661676
echo
662677
echo "Then click the 'Generate Token' green button (bottom of the page)."
663678
echo
@@ -691,28 +706,62 @@ init-github-completion()
691706
fi
692707
}
693708

694-
# curl call with github authentication
695-
_github_curl()
696-
{
697-
curl --netrc-file "$GG_AUTH_github" "$@"
698-
}
699-
700-
# _github_call <endpoint> <options>
701-
#
702-
# example: _github_call groups/github-org/projects simple=true
703709
#
704-
_github_call()
705-
{
706-
local endpoint="$1"
707-
local options="$2"
708-
709-
_rest_call _github_curl "https://api.github.com/$endpoint?per_page=100&$options"
710-
}
710+
# GraphQL query returning all repositories with given user/org.
711+
# used by _github_repo_list()
712+
#
713+
defgraphql __github_list_repos_query <<-'EOF'
714+
query list_repos($queryString: String!, $first: Int = 100, $after: String) {
715+
search(query: $queryString, type:REPOSITORY, first:$first, after: $after) {
716+
repositoryCount
717+
pageInfo {
718+
endCursor
719+
hasNextPage
720+
}
721+
edges {
722+
node {
723+
... on Repository {
724+
name
725+
}
726+
}
727+
}
728+
}
729+
}
730+
EOF
711731

712732
# download the repository list of <user|org>
713733
_github_repo_list()
714734
{
715-
_github_call users/"$1"/repos | jq -r '.[].name'
735+
local after="null"
736+
local hasNextPage="true"
737+
local data result
738+
739+
while [[ $hasNextPage == true ]]; do
740+
read -r -d '' data <<-EOF
741+
{
742+
"query": "$__github_list_repos_query",
743+
"variables": {
744+
"queryString": "user:$1 fork:true",
745+
"after": $after
746+
}
747+
}
748+
EOF
749+
750+
# execute the query
751+
result=$(curl \
752+
-s \
753+
--netrc-file "$GG_AUTH_github" \
754+
-X POST \
755+
--data "$data" \
756+
--url https://api.github.com/graphql)
757+
758+
# get information about the enxt page
759+
IFS=$'\t' read -r hasNextPage endCursor < <(jq -r '.data.search.pageInfo | [.hasNextPage, .endCursor] | @tsv' <<<"$result")
760+
after="\"$endCursor\""
761+
762+
# write out the desired result
763+
jq -r '.data.search.edges[].node.name' <<<"$result"
764+
done
716765
}
717766

718767
##########################

tests/test_github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_github(self, bash):
3737
self._auth(bash)
3838

3939
for cmdline, expected_result in [
40-
("git clone git@github.com:gh-test-acc/", ['gh-test-acc/fomatic', 'gh-test-acc/foobar', 'gh-test-acc/foo', 'gh-test-acc/bar']),
40+
("git clone git@github.com:gh-test-acc/", ['gh-test-acc/fomatic', 'gh-test-acc/foobar', 'gh-test-acc/foo', 'gh-test-acc/bar', 'gh-test-acc/private-repo']),
4141
("git clone git@github.com:gh-test-acc/b", ['git@github.com:gh-test-acc/bar '])
4242
]:
4343
expected_result.sort()

0 commit comments

Comments
 (0)