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

Method Create

pkg/cmd/discussion/client/client.go:969–1024  ·  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

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

Tested by

no test coverage detected