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)
| 1443 | // otherwise it will make a request to get the project by number |
| 1444 | // set `fields“ to true to get the project's field data |
| 1445 | func (c *Client) NewProject(canPrompt bool, o *Owner, number int32, fields bool) (*Project, error) { |
| 1446 | if number != 0 { |
| 1447 | variables := map[string]interface{}{ |
| 1448 | "number": githubv4.Int(number), |
| 1449 | "firstItems": githubv4.Int(0), |
| 1450 | "afterItems": (*githubv4.String)(nil), |
| 1451 | "firstFields": githubv4.Int(0), |
| 1452 | "afterFields": (*githubv4.String)(nil), |
| 1453 | } |
| 1454 | |
| 1455 | if fields { |
| 1456 | variables["firstFields"] = githubv4.Int(LimitMax) |
| 1457 | } |
| 1458 | if o.Type == UserOwner { |
| 1459 | var query userOwner |
| 1460 | variables["login"] = githubv4.String(o.Login) |
| 1461 | err := c.doQueryWithProgressIndicator("UserProject", &query, variables) |
| 1462 | return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err |
| 1463 | } else if o.Type == OrgOwner { |
| 1464 | variables["login"] = githubv4.String(o.Login) |
| 1465 | var query orgOwner |
| 1466 | err := c.doQueryWithProgressIndicator("OrgProject", &query, variables) |
| 1467 | return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err |
| 1468 | } else if o.Type == ViewerOwner { |
| 1469 | var query viewerOwner |
| 1470 | err := c.doQueryWithProgressIndicator("ViewerProject", &query, variables) |
| 1471 | return newProjectFromQueryWithoutItemsQuery(query.Owner.Project), err |
| 1472 | } |
| 1473 | return nil, errors.New("unknown owner type") |
| 1474 | } |
| 1475 | |
| 1476 | if !canPrompt { |
| 1477 | return nil, fmt.Errorf("project number is required when not running interactively") |
| 1478 | } |
| 1479 | |
| 1480 | projects, err := c.Projects(o.Login, o.Type, 0, fields) |
| 1481 | if err != nil { |
| 1482 | return nil, err |
| 1483 | } |
| 1484 | |
| 1485 | if len(projects.Nodes) == 0 { |
| 1486 | return nil, fmt.Errorf("no projects found for %s", o.Login) |
| 1487 | } |
| 1488 | |
| 1489 | options := make([]string, 0, len(projects.Nodes)) |
| 1490 | for _, p := range projects.Nodes { |
| 1491 | title := fmt.Sprintf("%s (#%d)", p.Title, p.Number) |
| 1492 | options = append(options, title) |
| 1493 | } |
| 1494 | |
| 1495 | answerIndex, err := c.prompter.Select("Which project would you like to use?", "", options) |
| 1496 | if err != nil { |
| 1497 | return nil, err |
| 1498 | } |
| 1499 | |
| 1500 | return &projects.Nodes[answerIndex], nil |
| 1501 | } |
| 1502 |