Skip to content

Commit eae0e37

Browse files
committed
Let snapshot only hold identity IDs
1 parent a740229 commit eae0e37

File tree

19 files changed

+233
-146
lines changed

19 files changed

+233
-146
lines changed

api/graphql/graph/gen_graph.go

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/graphql/models/lazy_bug.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ type BugWrapper interface {
2121
Description() (*commentary.Comment, error)
2222
Comments() ([]commentary.Comment, error)
2323
Labels() []bug.Label
24-
Author() (IdentityWrapper, error)
25-
Actors() ([]IdentityWrapper, error)
26-
Participants() ([]IdentityWrapper, error)
24+
Author() entity.Id
25+
Actors() []entity.Id
26+
Participants() []entity.Id
2727
CreatedAt() time.Time
2828
Timeline() ([]bug.TimelineItem, error)
2929
Operations() ([]bug.Operation, error)
@@ -115,32 +115,16 @@ func (lb *lazyBug) Labels() []bug.Label {
115115
return lb.excerpt.Labels
116116
}
117117

118-
func (lb *lazyBug) Author() (IdentityWrapper, error) {
119-
return lb.identity(lb.excerpt.AuthorId)
118+
func (lb *lazyBug) Author() entity.Id {
119+
return lb.excerpt.AuthorId
120120
}
121121

122-
func (lb *lazyBug) Actors() ([]IdentityWrapper, error) {
123-
result := make([]IdentityWrapper, len(lb.excerpt.Actors))
124-
for i, actorId := range lb.excerpt.Actors {
125-
actor, err := lb.identity(actorId)
126-
if err != nil {
127-
return nil, err
128-
}
129-
result[i] = actor
130-
}
131-
return result, nil
122+
func (lb *lazyBug) Actors() []entity.Id {
123+
return lb.excerpt.Actors
132124
}
133125

134-
func (lb *lazyBug) Participants() ([]IdentityWrapper, error) {
135-
result := make([]IdentityWrapper, len(lb.excerpt.Participants))
136-
for i, participantId := range lb.excerpt.Participants {
137-
participant, err := lb.identity(participantId)
138-
if err != nil {
139-
return nil, err
140-
}
141-
result[i] = participant
142-
}
143-
return result, nil
126+
func (lb *lazyBug) Participants() []entity.Id {
127+
return lb.excerpt.Participants
144128
}
145129

146130
func (lb *lazyBug) CreatedAt() time.Time {
@@ -198,24 +182,16 @@ func (l *loadedBug) Labels() []bug.Label {
198182
return l.Snapshot.Labels()
199183
}
200184

201-
func (l *loadedBug) Author() (IdentityWrapper, error) {
202-
return NewLoadedIdentity(l.Snapshot.Author()), nil
185+
func (l *loadedBug) Author() entity.Id {
186+
return l.Snapshot.Author()
203187
}
204188

205-
func (l *loadedBug) Actors() ([]IdentityWrapper, error) {
206-
res := make([]IdentityWrapper, len(l.Snapshot.Actors()))
207-
for i, actor := range l.Snapshot.Actors() {
208-
res[i] = NewLoadedIdentity(actor)
209-
}
210-
return res, nil
189+
func (l *loadedBug) Actors() []entity.Id {
190+
return l.Snapshot.Actors()
211191
}
212192

213-
func (l *loadedBug) Participants() ([]IdentityWrapper, error) {
214-
res := make([]IdentityWrapper, len(l.Snapshot.Participants()))
215-
for i, participant := range l.Snapshot.Participants() {
216-
res[i] = NewLoadedIdentity(participant)
217-
}
218-
return res, nil
193+
func (l *loadedBug) Participants() []entity.Id {
194+
return l.Snapshot.Participants()
219195
}
220196

221197
func (l *loadedBug) CreatedAt() time.Time {

api/graphql/resolvers/bug.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,31 @@ import (
88
"github.com/MichaelMure/git-bug/api/graphql/models"
99
"github.com/MichaelMure/git-bug/bug"
1010
"github.com/MichaelMure/git-bug/commentary"
11+
"github.com/MichaelMure/git-bug/entity"
1112
)
1213

1314
var _ graph.BugResolver = &bugResolver{}
1415

15-
type bugResolver struct{}
16+
type bugResolver struct {
17+
identityResolver
18+
}
19+
20+
// resolveIdentity is a helper function to call the identityResolver
21+
func (r bugResolver) resolveIdentity(id entity.Id) (models.IdentityWrapper, error) {
22+
return r.identityResolver.resolveIdentity(id)
23+
}
24+
25+
func (r bugResolver) resolveAllIdentities(ids []entity.Id) ([]models.IdentityWrapper, error) {
26+
result := make([]models.IdentityWrapper, len(ids))
27+
for i, id := range ids {
28+
identity, err := r.resolveIdentity(id)
29+
if err != nil {
30+
return nil, err
31+
}
32+
result[i] = identity
33+
}
34+
return result, nil
35+
}
1636

1737
func (bugResolver) ID(_ context.Context, obj models.BugWrapper) (string, error) {
1838
return obj.Id().String(), nil
@@ -22,6 +42,10 @@ func (bugResolver) HumanID(_ context.Context, obj models.BugWrapper) (string, er
2242
return obj.Id().Human(), nil
2343
}
2444

45+
func (r bugResolver) Author(_ context.Context, obj models.BugWrapper) (models.IdentityWrapper, error) {
46+
return r.resolveIdentity(obj.Author())
47+
}
48+
2549
func (bugResolver) Status(_ context.Context, obj models.BugWrapper) (models.Status, error) {
2650
return convertStatus(obj.Status())
2751
}
@@ -126,7 +150,7 @@ func (bugResolver) Timeline(_ context.Context, obj models.BugWrapper, after *str
126150
return connections.TimelineItemCon(timeline, edger, conMaker, input)
127151
}
128152

129-
func (bugResolver) Actors(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
153+
func (r bugResolver) Actors(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
130154
input := models.ConnectionInput{
131155
Before: before,
132156
After: after,
@@ -150,15 +174,15 @@ func (bugResolver) Actors(_ context.Context, obj models.BugWrapper, after *strin
150174
}, nil
151175
}
152176

153-
actors, err := obj.Actors()
177+
actors, err := r.resolveAllIdentities(obj.Actors())
154178
if err != nil {
155179
return nil, err
156180
}
157181

158182
return connections.IdentityCon(actors, edger, conMaker, input)
159183
}
160184

161-
func (bugResolver) Participants(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
185+
func (r bugResolver) Participants(_ context.Context, obj models.BugWrapper, after *string, before *string, first *int, last *int) (*models.IdentityConnection, error) {
162186
input := models.ConnectionInput{
163187
Before: before,
164188
After: after,
@@ -182,7 +206,7 @@ func (bugResolver) Participants(_ context.Context, obj models.BugWrapper, after
182206
}, nil
183207
}
184208

185-
participants, err := obj.Participants()
209+
participants, err := r.resolveAllIdentities(obj.Participants())
186210
if err != nil {
187211
return nil, err
188212
}

api/graphql/resolvers/identity.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,40 @@ import (
55

66
"github.com/MichaelMure/git-bug/api/graphql/graph"
77
"github.com/MichaelMure/git-bug/api/graphql/models"
8+
"github.com/MichaelMure/git-bug/cache"
9+
"github.com/MichaelMure/git-bug/entity"
810
)
911

1012
var _ graph.IdentityResolver = &identityResolver{}
1113

12-
type identityResolver struct{}
14+
type identityResolver struct {
15+
cache *cache.MultiRepoCache
16+
}
17+
18+
func (r identityResolver) resolveIdentity(id entity.Id) (models.IdentityWrapper, error) {
19+
var repo, err = r.cache.DefaultRepo()
20+
if err != nil {
21+
return nil, err
22+
}
23+
24+
excerpt, err := repo.ResolveIdentityExcerpt(id)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
return models.NewLazyIdentity(repo, excerpt), nil
30+
31+
//var repo, err = r.cache.DefaultRepo()
32+
//if err != nil {
33+
// return nil, err
34+
//}
35+
36+
//identity, err := repo.ResolveIdentity(id)
37+
//if err != nil {
38+
// return nil, err
39+
//}
40+
//return models.NewLoadedIdentity(identity.Identity), nil
41+
}
1342

1443
func (identityResolver) ID(ctx context.Context, obj models.IdentityWrapper) (string, error) {
1544
return obj.Id().String(), nil

api/graphql/resolvers/root.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ func (RootResolver) Repository() graph.RepositoryResolver {
3434
return &repoResolver{}
3535
}
3636

37-
func (RootResolver) Bug() graph.BugResolver {
38-
return &bugResolver{}
37+
func (r RootResolver) Bug() graph.BugResolver {
38+
return &bugResolver{
39+
identityResolver{
40+
cache: r.MultiRepoCache,
41+
},
42+
}
3943
}
4044

4145
func (RootResolver) Color() graph.ColorResolver {
@@ -52,8 +56,10 @@ func (RootResolver) Label() graph.LabelResolver {
5256
return &labelResolver{}
5357
}
5458

55-
func (RootResolver) Identity() graph.IdentityResolver {
56-
return &identityResolver{}
59+
func (r RootResolver) Identity() graph.IdentityResolver {
60+
return &identityResolver{
61+
cache: r.MultiRepoCache,
62+
}
5763
}
5864

5965
func (RootResolver) Alteration() graph.AlterationResolver {

bridge/github/export.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, out
203203

204204
// first operation is always createOp
205205
createOp := snapshot.Operations[0].(*bug.CreateOperation)
206-
author := snapshot.Author()
206+
authorID := snapshot.Author()
207207

208208
// skip bug if origin is not allowed
209209
origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
@@ -242,7 +242,7 @@ func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, out
242242

243243
} else {
244244
// check that we have a token for operation author
245-
client, err := ge.getClientForIdentity(author.Id())
245+
client, err := ge.getClientForIdentity(authorID)
246246
if err != nil {
247247
// if bug is still not exported and we do not have the author stop the execution
248248
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))

bridge/gitlab/export.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, out
171171

172172
// first operation is always createOp
173173
createOp := snapshot.Operations[0].(*bug.CreateOperation)
174-
author := snapshot.Author()
174+
authorID := snapshot.Author()
175175

176176
// get gitlab bug ID
177177
gitlabID, ok := snapshot.GetCreateMetadata(metaKeyGitlabId)
@@ -204,7 +204,7 @@ func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, out
204204

205205
} else {
206206
// check that we have a token for operation author
207-
client, err := ge.getIdentityClient(author.Id())
207+
client, err := ge.getIdentityClient(authorID)
208208
if err != nil {
209209
// if bug is still not exported and we do not have the author stop the execution
210210
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))

bridge/jira/export.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (je *jiraExporter) exportBug(ctx context.Context, b *cache.BugCache, out ch
200200

201201
// first operation is always createOp
202202
createOp := snapshot.Operations[0].(*bug.CreateOperation)
203-
author := snapshot.Author()
203+
authorID := snapshot.Author()
204204

205205
// skip bug if it was imported from some other bug system
206206
origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
@@ -226,13 +226,13 @@ func (je *jiraExporter) exportBug(ctx context.Context, b *cache.BugCache, out ch
226226
bugJiraID = jiraID
227227
} else {
228228
// check that we have credentials for operation author
229-
client, err := je.getClientForIdentity(author.Id())
229+
client, err := je.getClientForIdentity(authorID)
230230
if err != nil {
231231
// if bug is not yet exported and we do not have the author's credentials
232232
// then there is nothing we can do, so just skip this bug
233233
out <- core.NewExportNothing(
234234
b.Id(), fmt.Sprintf("missing author credentials for user %.8s",
235-
author.Id().String()))
235+
authorID.String()))
236236
return err
237237
}
238238

@@ -314,7 +314,7 @@ func (je *jiraExporter) exportBug(ctx context.Context, b *cache.BugCache, out ch
314314
if err != nil {
315315
out <- core.NewExportError(
316316
fmt.Errorf("missing operation author credentials for user %.8s",
317-
author.Id().String()), op.Id())
317+
authorID.String()), op.Id())
318318
continue
319319
}
320320

bug/op_add_comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (op *AddCommentOperation) Id() entity.Id {
2929
}
3030

3131
func (op *AddCommentOperation) Apply(snapshot *Snapshot) {
32-
snapshot.addActor(op.Author_)
33-
snapshot.addParticipant(op.Author_)
32+
snapshot.addActor(op.Author_.Id())
33+
snapshot.addParticipant(op.Author_.Id())
3434
var comment, _ = commentary.UnsafeComment{
3535
Id: entity.CombineIds(snapshot.Id(), op.Id()),
3636
Message: op.Message,

bug/op_create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) {
5050
}
5151

5252
snapshot.id = op.Id()
53-
snapshot.addActor(op.Author_)
54-
snapshot.addParticipant(op.Author_)
53+
snapshot.addActor(op.Author_.Id())
54+
snapshot.addParticipant(op.Author_.Id())
5555
snapshot.changeTitleTo(op.Title)
5656

5757
var desc, _ = commentary.UnsafeComment{
@@ -62,7 +62,7 @@ func (op *CreateOperation) Apply(snapshot *Snapshot) {
6262
}.Validate()
6363
if desc != nil {
6464
snapshot.description = *desc
65-
snapshot.author = op.Author_
65+
snapshot.authorID = op.Author_.Id()
6666
snapshot.CreateTime = op.Time()
6767
snapshot.comments = []commentary.Comment{}
6868
snapshot.timeline = []TimelineItem{}

0 commit comments

Comments
 (0)