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