Projects returns all the projects for an Owner. If the OwnerType is VIEWER, no login is required. If limit is 0, the default limit is used.
(login string, t OwnerType, limit int, fields bool)
| 1503 | // Projects returns all the projects for an Owner. If the OwnerType is VIEWER, no login is required. |
| 1504 | // If limit is 0, the default limit is used. |
| 1505 | func (c *Client) Projects(login string, t OwnerType, limit int, fields bool) (Projects, error) { |
| 1506 | projects := Projects{ |
| 1507 | Nodes: make([]Project, 0), |
| 1508 | } |
| 1509 | cursor := (*githubv4.String)(nil) |
| 1510 | hasNextPage := false |
| 1511 | |
| 1512 | if limit == 0 { |
| 1513 | limit = LimitDefault |
| 1514 | } |
| 1515 | |
| 1516 | // set first to the min of limit and LimitMax |
| 1517 | first := LimitMax |
| 1518 | if limit < first { |
| 1519 | first = limit |
| 1520 | } |
| 1521 | |
| 1522 | variables := map[string]interface{}{ |
| 1523 | "first": githubv4.Int(first), |
| 1524 | "after": cursor, |
| 1525 | "firstItems": githubv4.Int(0), |
| 1526 | "afterItems": (*githubv4.String)(nil), |
| 1527 | "firstFields": githubv4.Int(0), |
| 1528 | "afterFields": (*githubv4.String)(nil), |
| 1529 | } |
| 1530 | |
| 1531 | if fields { |
| 1532 | variables["firstFields"] = githubv4.Int(LimitMax) |
| 1533 | } |
| 1534 | |
| 1535 | if t != ViewerOwner { |
| 1536 | variables["login"] = githubv4.String(login) |
| 1537 | } |
| 1538 | // loop until we get all the projects |
| 1539 | for { |
| 1540 | // the code below is very repetitive, the only real difference being the type of the query |
| 1541 | // and the query variables. I couldn't figure out a way to make this cleaner that was worth |
| 1542 | // the cost. |
| 1543 | if t == UserOwner { |
| 1544 | var query userProjects |
| 1545 | if err := c.doQueryWithProgressIndicator("UserProjects", &query, variables); err != nil { |
| 1546 | return projects, err |
| 1547 | } |
| 1548 | for _, p := range query.Owner.Projects.Nodes { |
| 1549 | projects.Nodes = append(projects.Nodes, *newProjectFromQueryWithoutItemsQuery(p)) |
| 1550 | } |
| 1551 | hasNextPage = query.Owner.Projects.PageInfo.HasNextPage |
| 1552 | cursor = &query.Owner.Projects.PageInfo.EndCursor |
| 1553 | projects.TotalCount = query.Owner.Projects.TotalCount |
| 1554 | } else if t == OrgOwner { |
| 1555 | var query orgProjects |
| 1556 | if err := c.doQueryWithProgressIndicator("OrgProjects", &query, variables); err != nil { |
| 1557 | return projects, err |
| 1558 | } |
| 1559 | for _, p := range query.Owner.Projects.Nodes { |
| 1560 | projects.Nodes = append(projects.Nodes, *newProjectFromQueryWithoutItemsQuery(p)) |
| 1561 | } |
| 1562 | hasNextPage = query.Owner.Projects.PageInfo.HasNextPage |