Distinguish private/internal github repositories in graphql query - github

I'm trying to fetch all private repositories from our organization. In the following graphql query it returns both internal and private repositories.
For privacy it is only possible to filter on PRIVATE and PUBLIC.
It seems there is only one field called isPrivate, nothing like isInternal as far I can see.
query organizationRepositories($owner: String!) {
organization(login:$owner) {
repositories(first: 100, privacy: PRIVATE) {
totalCount
nodes {
owner {
login
}
name
id
url
isPrivate
}
}
}
}
All results using above query show isPrivate: true, I know for a fact some of these are internal and some are private.
Is there a way to distinguish between private and internal repositories? Either by looping over the results based on some fields combo which indicates private vs internal or by querying in a different way.

You can use a search query:
query {
search(query: "org:<my-org> is:internal", type: REPOSITORY, first: 100) {
repositoryCount
nodes {
... on Repository {
name
}
}
}
}
For more info, see https://docs.github.com/en/github/searching-for-information-on-github/searching-for-repositories#search-by-repository-visibility

Related

How to search branch_count and repository list by name in single network call of Github Api?

I am trying to use Github Search Api to show a list of repositories by name. My requirement is to show a list of all repositories matching name or description with string provided by user in search bar.
I was sucessfully able to do so by using this call `
https://api.github.com/search/repositories?q=swiftui&page=1&per_page=10
`
But my problem is it does not returns branch count, which is an essential to show. How can I do this without making multiple network calls.
I have tried some different methods using Rest API, but couldn't find any solution for branch count. So In the end Graphql was the solution for my problem.
let getReposQuery: String =
"""
query getRepos($searchText: String!) {
search(query: $searchText type: REPOSITORY first: 10) {
nodes {
... on Repository {
name,
description,
createdAt,
refs(first: 100, refPrefix:"refs/heads/") {
totalCount
nodes {
name,
id
}
}
}
}
}
}
"""
With this query, you need to provide searchText as variable.

In Github's GraphQL API how to get repositories that do not have a particular repositoryTopic?

With Github's GraphQL API I recently found "Github API: Getting topics of a Github repository" that mentions you can get a count of topics:
{
repository(owner: "twbs", name: "bootstrap") {
repositoryTopics(first: 10) {
edges {
node {
topic {
name
}
}
}
}
}
}
but in the docs and in my search I'm not finding how I can query repositories that do not contain the topic template, example:
query ($github_org: String!, $repo_count: Int!) {
organization(login: $github_org) {
repositories(first: $repo_count, privacy: PUBLIC, isFork: false) {
nodes {
id
name
openGraphImageUrl
createdAt
stargazerCount
url
description
repositoryTopics(first: 10, after: "template") {
edges {
node {
id
}
}
}
}
}
}
}
is the correct implementation to use after? In Github's GraphQL API how to exclude a repository if it contains a certain topic?
My understanding is that with the organization query you cannot do that kind of repository filtering.
Instead, you can use the search query with something like this:
query ($repo_count: Int!) {
search (first: $repo_count,
type: REPOSITORY,
query: "org:my-organization topic:my-topic") {
nodes {
... on Repository {
name
}
}
}
}
The above query searches for all repositories within a given organization that have a given topic.
But you were looking for the repositories without a topic, in theory according to the documentation you should be able to do that with -topic:my-topic. Unfortunately the negation on topic doesn't work, there seems to be a bug. :-(

Getting all repositories query Github v4 API

I am trying to get all my repositories using the Github v4 API, but I can't find any query named repositories. I found a similar way to do so like this:
export const query = gql`
query repositories() {
search(query: "user:userName", type: REPOSITORY, first: 50) {
repositoryCount
edges {
node {
... on Repository {
name
}
}
}
}
}
But this is not what I want because I would have to provide the username which I should get through another query (and I do not want to type it by myself)
Is there a way to get all my repositories by only using one query? I think it should be easy enough, but I can't find the way.
I found the query:
export const query = gql`
query repositories {
viewer {
login
repositories(first: 30) {
edges {
node {
name
}
}
}
}
}
There are different ways to achieve this. You can define parameter in the query and then pass them. In the example below set usersName to your username and first to 50 or whatsoever required, keep in mind there is a limitation for first on GitHub API (value must be between 1 to 100 inclusive) which you can find here.
In this example if userName does not match any entry then an error is returned.
export const query = gql`
query repositories ($userName: String!, $first: Int){
user(login: $userName) {
repositories(first: $first) {
nodes {
name
}
}
}
}
`;
You can use search as in your example and pass variables to the query. However, this way the query will return repositories from users those their names or usernames match or include substring of provided searchText. This query will not return an error if there in no match with searchText.
export const query = gql`
query ($searchText: String!) {
search(query: $searchText, type: USER, first: 1) {
nodes {
... on User {
repositories(first: 50) {
nodes {
name
}
}
}
}
}
}
`;
If you are only interested in your own repository then your own answer can be used as well.

Get info about several repositories from Github Graphql API in a single call

I am trying to create a query to Github GraphQL API that receive a list of repos as a parameter, and returns the info of those repositories in a single API call, does anyone know how to do that?
Something like this (I know this doesn't work)
query myOrgRepos($repos: [String!]!) {
search(query:"""
repo in $repos
""", type: REPOSITORY, first: 100) {
repo: nodes {
... on Repository{
name
description
updatedAt
}
}
}
It took me some time to understand your comment so I will post the answer in full. The key is to use Github's advanced search “syntax” in the query string, see here for examples.
The following query will find all repositories of user “github” as well as the graphql-js repository of user “graphql”:
query {
search(query: "user:github repo:graphql/graphql-js" type: REPOSITORY first: 10) {
nodes {
... on Repository {
name
description
updatedAt
}
}
}
}
My understanding of your question is that you want to input a list of "known" repos and output information about them. In this case you can form a query of the following type.
{
nodes(ids: ["node_id_1", "node_id_2"]) {
... on Repository {
nameWithOwner
createdAt
}
}
}
Here the ids are the node_ids of the repositories you are interested in. You can obtain these by individual GraphQL calls as follows
{
repository(owner: "owner", name: "name") {
id
}
}

GitHub's GraphQL API: How can I get all of the commits I've contributed to a particular public repo?

I'm trying to query GitHub's GraphQL API (v4). What GraphQL query for this API can be used to get all of the commits I've contributed to a particular public repo?
I figured out the solution. (The solution shown below is for getting relevant commits from master, but can easily be adapted for other qualifiedName values.)
First, perform an initial query to obtain user ID:
query {
viewer {
id
}
}
Then the following query can be used to get all contributed commits to a particular repository:
query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
repository(owner: $repository_owner, name: $repository_name) {
ref(qualifiedName: "master") {
target {
... on Commit {
history(author: {id: $user_id}) {
totalCount
nodes {
id
authoredDate
message
}
}
}
}
}
}
}
The query variables take the following form:
{
"repository_owner": "REPOSITORY_OWNER_STRING",
"repository_name": "REPOSITORY_NAME_STRING",
"user_id": "USER_ID_FROM_STEP_1"
}