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)
| 967 | // is non-nil (reflecting the created state without labels) and err describes |
| 968 | // the label failure. |
| 969 | func (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 |
nothing calls this directly
no test coverage detected