DEV Community

Francis
Francis

Posted on

How do I find the teams I'm a member of on GitHub?

Using the UI

  • Navigate to your organization on GitHub
  • Navigate to your organization's Teams
  • In the team-scoped search input, type in members:me visibility:visible and run the search
    • Example URL: https://github.com/orgs/ORG_NAME/teams?query=members%3Ame+visibility%3Avisible (subject to change at any time)
  • The results show the teams that you're a member of, where each team is visible to the wider organization. It's possible to change the visibility to Secret or All if you prefer.

Using the API

It's possible to use the GitHub GraphQL API to find the teams that you're a member of on GitHub. Here's the query to save you from more scrolling:

{
  organization(login: "github") {
    totalCount
    teams(first: 10, role:MEMBER) {
      edges {
        node {
          name
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

But, how did you arrive at that conclusion Francis? 🤔

If you're curious, let's build the query together using the GitHub GraphQL API Explorer!

Once you've signed in, a "starter" query is shown that will fetch your GitHub username:

query {
  viewer {
    login
  }
}
Enter fullscreen mode Exit fullscreen mode

We can erase all of that that and target an Organization based on its login and return its name:

query {

  # new! replacing the current viewer with organization
  organization(login:"github") {
    name
  }
}
Enter fullscreen mode Exit fullscreen mode

Omitting name will result in an error since the intent of the executed query is to retrieve one or more available fields:

{
  "errors": [
    {
      "message": "Parse error on \"}\" (RCURLY) at [5, 3]",
      "locations": [
        {
          "line": 5,
          "column": 3
        }
      ]
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

We can build on this query by targeting the organization's teams:

{
  organization(login: "github") {

    # new! inclusion of teams and their names
    teams(first: 10) {
      edges {
        node {
          name
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

The inclusion of edges and node are a way of traversing the relationship between a set of objects in GraphQL (check out GraphQL.org's Learn Page on Pagination for more information). Here, we're targeting the github organization's teams and limiting it to the first 10 (though, you're welcome to choose any number upto 100, which is the global limit of returned objects).

But, that doesn't answer the question of which teams you're actually on. It only fetches the first 10 teams on the organization.

The documentation indicates that there's an available parameter called role. By default, it's a null value and totally optional. For our cases, we can either set role to ADMIN or MEMBER. I'm interested in finding teams that I'm solely a member of:

{
  organization(login: "github") {

    # new! inclusion of `role:MEMBER`
    teams(first: 10, role:MEMBER) {
      edges {
        node {
          name
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

I'm a member of a number of teams, but I don't know exactly how many teams I'm actually a part of. What's great about GitHub's GraphQL implementation is the inclusion of a totalCount field that's available on all collections. So, for something like say, teams, we can specify totalCount as a sibling field to edges:

{
  organization(login: "github") {

    # new! inclusion of the total number of teams I'm a part of 
    totalCount

    teams(first: 10, role:MEMBER) {
      edges {
        node {
          name
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Once you have the value of totalCount, you can replace the number of teams to fetch by that number. If it's more than 100, you can use cursors to get the next set of teams (check out this example to get an idea of how this works).

Discussion (0)