github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/api/queries_org.go (about)

     1  package api
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/cli/cli/internal/ghrepo"
     7  	"github.com/shurcooL/githubv4"
     8  )
     9  
    10  // OrganizationProjects fetches all open projects for an organization
    11  func OrganizationProjects(client *Client, repo ghrepo.Interface) ([]RepoProject, error) {
    12  	type responseData struct {
    13  		Organization struct {
    14  			Projects struct {
    15  				Nodes    []RepoProject
    16  				PageInfo struct {
    17  					HasNextPage bool
    18  					EndCursor   string
    19  				}
    20  			} `graphql:"projects(states: [OPEN], first: 100, orderBy: {field: NAME, direction: ASC}, after: $endCursor)"`
    21  		} `graphql:"organization(login: $owner)"`
    22  	}
    23  
    24  	variables := map[string]interface{}{
    25  		"owner":     githubv4.String(repo.RepoOwner()),
    26  		"endCursor": (*githubv4.String)(nil),
    27  	}
    28  
    29  	gql := graphQLClient(client.http, repo.RepoHost())
    30  
    31  	var projects []RepoProject
    32  	for {
    33  		var query responseData
    34  		err := gql.QueryNamed(context.Background(), "OrganizationProjectList", &query, variables)
    35  		if err != nil {
    36  			return nil, err
    37  		}
    38  
    39  		projects = append(projects, query.Organization.Projects.Nodes...)
    40  		if !query.Organization.Projects.PageInfo.HasNextPage {
    41  			break
    42  		}
    43  		variables["endCursor"] = githubv4.String(query.Organization.Projects.PageInfo.EndCursor)
    44  	}
    45  
    46  	return projects, nil
    47  }
    48  
    49  type OrgTeam struct {
    50  	ID   string
    51  	Slug string
    52  }
    53  
    54  // OrganizationTeams fetches all the teams in an organization
    55  func OrganizationTeams(client *Client, repo ghrepo.Interface) ([]OrgTeam, error) {
    56  	type responseData struct {
    57  		Organization struct {
    58  			Teams struct {
    59  				Nodes    []OrgTeam
    60  				PageInfo struct {
    61  					HasNextPage bool
    62  					EndCursor   string
    63  				}
    64  			} `graphql:"teams(first: 100, orderBy: {field: NAME, direction: ASC}, after: $endCursor)"`
    65  		} `graphql:"organization(login: $owner)"`
    66  	}
    67  
    68  	variables := map[string]interface{}{
    69  		"owner":     githubv4.String(repo.RepoOwner()),
    70  		"endCursor": (*githubv4.String)(nil),
    71  	}
    72  
    73  	gql := graphQLClient(client.http, repo.RepoHost())
    74  
    75  	var teams []OrgTeam
    76  	for {
    77  		var query responseData
    78  		err := gql.QueryNamed(context.Background(), "OrganizationTeamList", &query, variables)
    79  		if err != nil {
    80  			return nil, err
    81  		}
    82  
    83  		teams = append(teams, query.Organization.Teams.Nodes...)
    84  		if !query.Organization.Teams.PageInfo.HasNextPage {
    85  			break
    86  		}
    87  		variables["endCursor"] = githubv4.String(query.Organization.Teams.PageInfo.EndCursor)
    88  	}
    89  
    90  	return teams, nil
    91  }