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