MCPcopy Create free account
hub / github.com/cli/cli / NewProject

Method NewProject

pkg/cmd/project/shared/queries/queries.go:1485–1541  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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

Callers 15

runCloseFunction · 0.80
runLinkFunction · 0.80
runCopyFunction · 0.80
runMarkTemplateFunction · 0.80
runListFunction · 0.80
runDeleteFunction · 0.80
runViewFunction · 0.80
runCreateFieldFunction · 0.80
runDeleteItemFunction · 0.80
runAddItemFunction · 0.80
runListFunction · 0.80
TestNewProject_nonTTYFunction · 0.80

Calls 6

ProjectsMethod · 0.95
StringMethod · 0.65
ErrorfMethod · 0.65
SelectMethod · 0.65

Tested by 1

TestNewProject_nonTTYFunction · 0.64