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

Method Create

pkg/cmd/discussion/client/client.go:966–1021  ·  view source on GitHub ↗

Create creates a discussion and optionally assigns labels. If the discussion is created successfully but the label mutation fails, the returned discussion is non-nil (reflecting the created state without labels) and err describes the label failure.

(repo ghrepo.Interface, input CreateDiscussionInput)

Source from the content-addressed store, hash-verified

964// is non-nil (reflecting the created state without labels) and err describes
965// the label failure.
966func (c *discussionClient) Create(repo ghrepo.Interface, input CreateDiscussionInput) (*Discussion, error) {
967 meta, err := c.getRepositoryMeta(repo)
968 if err != nil {
969 return nil, err
970 }
971 if !meta.HasDiscussionsEnabled {
972 return nil, fmt.Errorf("the '%s/%s' repository has discussions disabled", repo.RepoOwner(), repo.RepoName())
973 }
974
975 var mutation struct {
976 CreateDiscussion struct {
977 Discussion struct {
978 discussionListNode
979 }
980 } `graphql:"createDiscussion(input: $input)"`
981 }
982
983 variables := map[string]any{
984 "input": githubv4.CreateDiscussionInput{
985 RepositoryID: githubv4.ID(meta.ID),
986 CategoryID: githubv4.ID(input.CategoryID),
987 Title: githubv4.String(input.Title),
988 Body: githubv4.String(input.Body),
989 },
990 }
991
992 if err := c.gql.Mutate(repo.RepoHost(), "CreateDiscussion", &mutation, variables); err != nil {
993 return nil, err
994 }
995
996 node := &mutation.CreateDiscussion.Discussion.discussionListNode
997
998 var secondaryErrs []error
999 if len(input.LabelIDs) > 0 {
1000 labelNode, err := c.editDiscussionLabels(repo, node.ID, input.LabelIDs, nil)
1001 if err != nil {
1002 secondaryErrs = append(secondaryErrs, err)
1003 } else {
1004 node = labelNode
1005 }
1006 }
1007
1008 d := mapDiscussionFromListNode(*node)
1009
1010 for _, rg := range node.ReactionGroups {
1011 d.ReactionGroups = append(d.ReactionGroups, ReactionGroup{
1012 Content: rg.Content,
1013 TotalCount: rg.Users.TotalCount,
1014 })
1015 }
1016
1017 if len(secondaryErrs) > 0 {
1018 return &d, fmt.Errorf("discussion created but some mutations failed: %w", errors.Join(secondaryErrs...))
1019 }
1020 return &d, nil
1021}
1022
1023// Update updates a discussion's fields and/or labels. If field updates succeed

Callers

nothing calls this directly

Calls 11

getRepositoryMetaMethod · 0.95
editDiscussionLabelsMethod · 0.95
JoinMethod · 0.80
ErrorfMethod · 0.65
RepoOwnerMethod · 0.65
RepoNameMethod · 0.65
IDMethod · 0.65
StringMethod · 0.65
MutateMethod · 0.65
RepoHostMethod · 0.65

Tested by

no test coverage detected