Fetch all repos that are accessible to a user


#1

HI,
The Github api https://api.github.com/user/repos?access_token=2d7 returns a paginated list of all the repos a user has access to… How do i perform the same using GraphQL… The GraphQL query seems to be returning only the repos that has a particular owner…

Thanks


#2

:wave: @robinsuri,

I’ve opened up an internal issue to track the effort of making all repositories that a user has access to accessible in GraphQL like we do in the /user/repos REST endpoint.

We’ll update this thread as we have more information.

Cheers,
Brooks


#3

:wave: Hi @robinsuri,

We now have a mechanism to filter repositories by a user’s affiliation. I believe this is what you were referring to in your original message:

query {
  viewer {
    repositories(last:30,affiliation:[OWNER,COLLABORATOR,ORGANIZATION_MEMBER]) {
      edges {
        node {
          name
          owner {
            login
          }
        }
      }
    }
  }
}

The affiliations map to those outlined in the REST API documentation.

The example I use above is specific to the currently authenticated user, but it should work for any user as well.

I hope this helps,
Brooks


#4

How is the “affiliation” supposed to work? Your snippet returns error:

{
  "data": null,
  "errors": [
    {
      "message": "Field 'repositories' doesn't accept argument 'affiliation'",
      "locations": [
        {
          "line": 3,
          "column": 5
        }
      ]
    }
  ]
}

#5

It is affiliations (plural)…


#6

:grimacing: sorry about that.


#7

Hi @bswinnerton, Graphql API still doesn’t return all the repos accessible on user/repos endpoint. Repositories where viewer!=OWNER are missing.


#8

I have both the v3 and v4 apis connected in my application. Using:

GraphQL:

viewer {
    repositories(first: 100, affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER]) {
      edges {
        node {
          name
          owner {
            login
          }
        }
      }
    }
  }

vs

https://api.github.com/user/repos?access_token=...

I’m using the same access token for both and…

  1. They return a completely different set of repos.

  2. I assumed this is some sort of ordering issue… but even if I switch first to last in the graphQL it still returns the same set, and it’s not 100… It’s only showing me my personal repos and one organization i’m apart of. (Perhaps it’s because i’m the owner of the org? I’m not really sure.


#9

Hi,
This query was working before. And it did return all the repos apart from personal and owner.
But its not returning now. Perhaps this is a bug or something has changed.
Thanks


#10

We are also getting only repositories which the user is Owner or Collaborator but not the Organization repositories. This used to work and is broken few days ago only.

Can someone look at it on priority?


#12

Hi @mwisner, @robinsuri and @atulaggarwal!

I think I know why this has happened - we recently made a security-related change to the way this part of the query works, and it may have been a bit overzealous.

If you wouldn’t mind, can you share the query that you’re using where the information has changed? Specifically, I’m curious to know what user you’re looking up, or if you’re using viewer like @mwisner’s query is.

I’m going to work on a fix now, I will follow up when I have something to show off.

Sorry about this, and thank you for sharing your feedback on the Discourse board!


#13

@nickvanw - we are also using viewer like @mwisner. Please let us know when this is fixed. This is impacting our production.


The contributedRepositories query doesnt give all repos
#14

Thank you everyone for your patience, we’ve deployed the fix and will now see the results include repositories for organizations the user is a member of.

Let us know if you have any further questions.


#15

@dewski - Thanks. I can confirm we are now getting proper response from the api. Thanks for fixing this quickly.


#16

Today I tried this query and it didn’t work:

 query { 
  viewer {
    repositories(first: 5, affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER]) {
      edges {
        node {
          name
          owner {
            login
          }
        }
      }
    }
  }
}

The response was

{
  "data": {
    "viewer": {
      "repositories": {
        "edges": []
      }
    }
  }
}

Some context:
The logged in user is neither an owner nor a collaborator of any repo. It is a member of an org with read access (also tried write access) to private org repos.

EDIT:

I found that by enabling “Organization access” when authorizing the GraphQL API Explorer, it does work.
One thing does not work though:
The API cannot respond with repositories, that are

  • private
  • owned by an organization
  • if the viewer has only read access to that organization

unless the organization allows read and write access.

EDIT 2:

This is only an issue with OAuth apps, because the OAuth flow requires explicit read and write access to the org, just for being able to read the org repos.

Using a personal Github access token with the repo scope, the GraphQL query above does work as intended.


#17

Hello @bswinnerton

I came across this thread a while back that helped me build an Alfred workflow, thanks! I’ve been running the following query for about 2 months now, but now appears to no longer work:

query { 
      viewer { 
         repositories(first: 100, affiliations: [OWNER, COLLABORATOR, ORGANIZATION_MEMBER]) { 
         pageInfo { 
         hasNextPage 
         endCursor
      } 
         edges { 
         node { 
         name 
         nameWithOwner 
         pushedAt 
         url
        }
      }
    }
  }
}

It seems to only be returning me the COLLABORATOR repositories now; I have verified that the personal access token I’m using has full permissions.

I also checked by using the V3 API, and it works as expected.

https://api.github.com/user/repos?access_token=...

any help in debugging would be appreciated :slight_smile: