1package resolvers
2
3import (
4 "context"
5 "time"
6
7 "github.com/MichaelMure/git-bug/bug"
8 "github.com/MichaelMure/git-bug/cache"
9 "github.com/MichaelMure/git-bug/graphql/graph"
10 "github.com/MichaelMure/git-bug/graphql/graphqlidentity"
11 "github.com/MichaelMure/git-bug/graphql/models"
12)
13
14var _ graph.MutationResolver = &mutationResolver{}
15
16type mutationResolver struct {
17 cache *cache.MultiRepoCache
18}
19
20func (r mutationResolver) getRepo(ref *string) (*cache.RepoCache, error) {
21 if ref != nil {
22 return r.cache.ResolveRepo(*ref)
23 }
24
25 return r.cache.DefaultRepo()
26}
27
28func (r mutationResolver) getBug(repoRef *string, bugPrefix string) (*cache.RepoCache, *cache.BugCache, error) {
29 repo, err := r.getRepo(repoRef)
30 if err != nil {
31 return nil, nil, err
32 }
33
34 bug, err := repo.ResolveBugPrefix(bugPrefix)
35 if err != nil {
36 return nil, nil, err
37 }
38 return repo, bug, nil
39}
40
41func (r mutationResolver) NewBug(ctx context.Context, input models.NewBugInput) (*models.NewBugPayload, error) {
42 repo, err := r.getRepo(input.RepoRef)
43 if err != nil {
44 return nil, err
45 }
46
47 id, err := graphqlidentity.ForContext(ctx, repo)
48 if err != nil {
49 return nil, err
50 }
51
52 b, op, err := repo.NewBugRaw(id, time.Now().Unix(), input.Title, input.Message, input.Files, nil)
53 if err != nil {
54 return nil, err
55 }
56
57 return &models.NewBugPayload{
58 ClientMutationID: input.ClientMutationID,
59 Bug: models.NewLoadedBug(b.Snapshot()),
60 Operation: op,
61 }, nil
62}
63
64func (r mutationResolver) AddComment(ctx context.Context, input models.AddCommentInput) (*models.AddCommentPayload, error) {
65 repo, b, err := r.getBug(input.RepoRef, input.Prefix)
66 if err != nil {
67 return nil, err
68 }
69
70 id, err := graphqlidentity.ForContext(ctx, repo)
71 if err != nil {
72 return nil, err
73 }
74
75 op, err := b.AddCommentRaw(id, time.Now().Unix(), input.Message, input.Files, nil)
76 if err != nil {
77 return nil, err
78 }
79
80 err = b.Commit()
81 if err != nil {
82 return nil, err
83 }
84
85 return &models.AddCommentPayload{
86 ClientMutationID: input.ClientMutationID,
87 Bug: models.NewLoadedBug(b.Snapshot()),
88 Operation: op,
89 }, nil
90}
91
92func (r mutationResolver) ChangeLabels(ctx context.Context, input *models.ChangeLabelInput) (*models.ChangeLabelPayload, error) {
93 repo, b, err := r.getBug(input.RepoRef, input.Prefix)
94 if err != nil {
95 return nil, err
96 }
97
98 id, err := graphqlidentity.ForContext(ctx, repo)
99 if err != nil {
100 return nil, err
101 }
102
103 results, op, err := b.ChangeLabelsRaw(id, time.Now().Unix(), input.Added, input.Removed, nil)
104 if err != nil {
105 return nil, err
106 }
107
108 err = b.Commit()
109 if err != nil {
110 return nil, err
111 }
112
113 resultsPtr := make([]*bug.LabelChangeResult, len(results))
114 for i, result := range results {
115 resultsPtr[i] = &result
116 }
117
118 return &models.ChangeLabelPayload{
119 ClientMutationID: input.ClientMutationID,
120 Bug: models.NewLoadedBug(b.Snapshot()),
121 Operation: op,
122 Results: resultsPtr,
123 }, nil
124}
125
126func (r mutationResolver) OpenBug(ctx context.Context, input models.OpenBugInput) (*models.OpenBugPayload, error) {
127 repo, b, err := r.getBug(input.RepoRef, input.Prefix)
128 if err != nil {
129 return nil, err
130 }
131
132 id, err := graphqlidentity.ForContext(ctx, repo)
133 if err != nil {
134 return nil, err
135 }
136
137 op, err := b.OpenRaw(id, time.Now().Unix(), nil)
138 if err != nil {
139 return nil, err
140 }
141
142 err = b.Commit()
143 if err != nil {
144 return nil, err
145 }
146
147 return &models.OpenBugPayload{
148 ClientMutationID: input.ClientMutationID,
149 Bug: models.NewLoadedBug(b.Snapshot()),
150 Operation: op,
151 }, nil
152}
153
154func (r mutationResolver) CloseBug(ctx context.Context, input models.CloseBugInput) (*models.CloseBugPayload, error) {
155 repo, b, err := r.getBug(input.RepoRef, input.Prefix)
156 if err != nil {
157 return nil, err
158 }
159
160 id, err := graphqlidentity.ForContext(ctx, repo)
161 if err != nil {
162 return nil, err
163 }
164
165 op, err := b.CloseRaw(id, time.Now().Unix(), nil)
166 if err != nil {
167 return nil, err
168 }
169
170 err = b.Commit()
171 if err != nil {
172 return nil, err
173 }
174
175 return &models.CloseBugPayload{
176 ClientMutationID: input.ClientMutationID,
177 Bug: models.NewLoadedBug(b.Snapshot()),
178 Operation: op,
179 }, nil
180}
181
182func (r mutationResolver) SetTitle(ctx context.Context, input models.SetTitleInput) (*models.SetTitlePayload, error) {
183 repo, b, err := r.getBug(input.RepoRef, input.Prefix)
184 if err != nil {
185 return nil, err
186 }
187
188 id, err := graphqlidentity.ForContext(ctx, repo)
189 if err != nil {
190 return nil, err
191 }
192
193 op, err := b.SetTitleRaw(id, time.Now().Unix(), input.Title, nil)
194 if err != nil {
195 return nil, err
196 }
197
198 err = b.Commit()
199 if err != nil {
200 return nil, err
201 }
202
203 return &models.SetTitlePayload{
204 ClientMutationID: input.ClientMutationID,
205 Bug: models.NewLoadedBug(b.Snapshot()),
206 Operation: op,
207 }, nil
208}