Tom Truyen
Tom Truyen

Reputation: 363

Github API - Get private repositories of user

I have created a script that automatically backs up my GitHub repositories on a hard drive.

I use my Github username in combination with a personal access token to get authorized to Github. Now I've been reading a bit in their documentation about how to get ALL my repositories from the API (public & private), but I can only seem to get the public...

My script: https://github.com/TomTruyen/GitHub-Backup-Script/blob/main/github_backup_script.sh

From what I can understand on line 78, the url should return all my 'owned' repositories (which should include my privates ones)

repositories=$(curl -XGET -s https://"${GITHUB_USERNAME}":"${GITHUB_TOKEN}"@api.github.com/users/"${GITHUB_USERNAME}"/repos?per_page="${repository_count}" | jq -c --raw-output ".[] | {name, ssh_url}")

I have already enabled ALL repository scopes which should give me 'Full control of private repositories (and public)'enter image description here

I'm out of ideas right now... am I doing something wrong?

NOTE: I'm trying to get my private repositories as a USER, not as an organization

NOTE: ${GITHUB_USERNAME} & ${GITHUB_TOKEN} are variables that I have of course filled in, in my script

Upvotes: 0

Views: 2277

Answers (2)

Saurabh
Saurabh

Reputation: 83

visibility=all is the key thing here, guys. I spent hours but end up not getting what I wanted. It's been mentioned in the doc as well - https://docs.github.com/en/rest/repos/repos#list-repositories-for-the-authenticated-user

Upvotes: 0

larsks
larsks

Reputation: 312233

You're calling the /users endpoint, but looking at List repositories for the authenticated user it looks like you should be calling /user/repos.

By default this will return all repositories, both public and private, for the currently authenticated user. You'll also need to correctly handle pagination (unless you know for sure you have fewer than 100 repositories).

I was able to fetch a list of all my repositories using the following script:

#!/bin/sh
#
# you must set GH_API_USER and GH_API_TOKEN in your environment

tmpfile=$(mktemp curlXXXXXX)
trap "rm -f $tmpfile" EXIT

page=0
while :; do
    let page++
    curl -sf -o $tmpfile \
        -u "$GH_API_USER:$GH_API_TOKEN" \
        "https://api.github.com/user/repos?per_page=100&page=$page&visibility=all"

    count=$(jq length $tmpfile)
    if [[ $count = 0 ]]; then
        break
    fi

    jq '.[]|.full_name' $tmpfile
done

Upvotes: 1

Related Questions