NewProject creates a project based on the owner and project number if canPrompt is false, number is required as we cannot prompt for it if number is 0 it will prompt the user to select a project interactively otherwise it will make a request to get the project by number set `fields“ to true to get t
(canPrompt bool, o *Owner, number int32, fields bool)
| 1490 | // otherwise it will make a request to get the project by number |
| 1491 | // set `fields“ to true to get the project's field data |
| 1492 | func (c *Client) NewProject(canPrompt bool, o *Owner, number int32, fields bool) (*Project, error) { |
| 1493 | if number != 0 { |
| 1494 | variables := map[string]interface{}{ |
| 1495 | "number": githubv4.Int(number), |
| 1496 | "firstItems": githubv4.Int(0), |
| 1497 | "afterItems": (*githubv4.String)(nil), |
| 1498 | "firstFields": githubv4.Int(0), |
| 1499 | "afterFields": (*githubv4.String)(nil), |
| 1500 | } |
| 1501 | |
| 1502 | if fields { |
| 1503 | variables["firstFields"] = githubv4.Int(LimitMax) |
| 1504 | } |
| 1505 | if o.Type == UserOwner { |
| 1506 | var query userOwner |
| 1507 | variables["login"] = githubv4.String(o.Login) |
| 1508 | err := c.doQueryWithProgressIndicator("UserProject", &query, variables) |
| 1509 | return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err |
| 1510 | } else if o.Type == OrgOwner { |
| 1511 | variables["login"] = githubv4.String(o.Login) |
| 1512 | var query orgOwner |
| 1513 | err := c.doQueryWithProgressIndicator("OrgProject", &query, variables) |
| 1514 | return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err |
| 1515 | } else if o.Type == ViewerOwner { |
| 1516 | var query viewerOwner |
| 1517 | err := c.doQueryWithProgressIndicator("ViewerProject", &query, variables) |
| 1518 | return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err |
| 1519 | } |
| 1520 | return nil, errors.New("unknown owner type") |
| 1521 | } |
| 1522 | |
| 1523 | if !canPrompt { |
| 1524 | return nil, fmt.Errorf("project number is required when not running interactively") |
| 1525 | } |
| 1526 | |
| 1527 | projects, err := c.Projects(o.Login, o.Type, 0, fields) |
| 1528 | if err != nil { |
| 1529 | return nil, err |
| 1530 | } |
| 1531 | |
| 1532 | if len(projects.Nodes) == 0 { |
| 1533 | return nil, fmt.Errorf("no projects found for %s", o.Login) |
| 1534 | } |
| 1535 | |
| 1536 | options := make([]string, 0, len(projects.Nodes)) |
| 1537 | for _, p := range projects.Nodes { |
| 1538 | title := fmt.Sprintf("%s (#%d)", p.Title, p.Number) |
| 1539 | options = append(options, title) |
| 1540 | } |
| 1541 | |
| 1542 | answerIndex, err := c.prompter.Select("Which project would you like to use?", "", options) |
| 1543 | if err != nil { |
| 1544 | return nil, err |
| 1545 | } |
| 1546 | |
| 1547 | return &projects.Nodes[answerIndex], nil |
| 1548 | } |
| 1549 |