diff options
author | Michael Muré <batolettre@gmail.com> | 2020-06-21 22:12:04 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2020-06-27 23:03:05 +0200 |
commit | 2ab6381a94d55fa22b80acdbb18849d6b24951f9 (patch) | |
tree | 99942b000955623ea7466b9fa4cc7dab37645df6 /api/graphql/models | |
parent | 5f72b04ef8e84b1c367ca6874519706318e351f5 (diff) | |
download | git-bug-2ab6381a94d55fa22b80acdbb18849d6b24951f9.tar.gz git-bug-2ab6381a94d55fa22b80acdbb18849d6b24951f9.zip |
Reorganize the webUI and API code
Included in the changes:
- create a new /api root package to hold all API code, migrate /graphql in there
- git API handlers all use the cache instead of the repo directly
- git API handlers are now tested
- git API handlers now require a "repo" mux parameter
- lots of untangling of API/handlers/middleware
- less code in commands/webui.go
Diffstat (limited to 'api/graphql/models')
-rw-r--r-- | api/graphql/models/edges.go | 31 | ||||
-rw-r--r-- | api/graphql/models/gen_models.go | 324 | ||||
-rw-r--r-- | api/graphql/models/lazy_bug.go | 215 | ||||
-rw-r--r-- | api/graphql/models/lazy_identity.go | 180 | ||||
-rw-r--r-- | api/graphql/models/models.go | 23 |
5 files changed, 773 insertions, 0 deletions
diff --git a/api/graphql/models/edges.go b/api/graphql/models/edges.go new file mode 100644 index 00000000..6a331e3e --- /dev/null +++ b/api/graphql/models/edges.go @@ -0,0 +1,31 @@ +package models + +// GetCursor return the cursor entry of an edge +func (e OperationEdge) GetCursor() string { + return e.Cursor +} + +// GetCursor return the cursor entry of an edge +func (e BugEdge) GetCursor() string { + return e.Cursor +} + +// GetCursor return the cursor entry of an edge +func (e CommentEdge) GetCursor() string { + return e.Cursor +} + +// GetCursor return the cursor entry of an edge +func (e TimelineItemEdge) GetCursor() string { + return e.Cursor +} + +// GetCursor return the cursor entry of an edge +func (e IdentityEdge) GetCursor() string { + return e.Cursor +} + +// GetCursor return the cursor entry of an edge +func (e LabelEdge) GetCursor() string { + return e.Cursor +} diff --git a/api/graphql/models/gen_models.go b/api/graphql/models/gen_models.go new file mode 100644 index 00000000..cbece6fe --- /dev/null +++ b/api/graphql/models/gen_models.go @@ -0,0 +1,324 @@ +// Code generated by github.com/99designs/gqlgen, DO NOT EDIT. + +package models + +import ( + "fmt" + "io" + "strconv" + + "github.com/MichaelMure/git-bug/bug" + "github.com/MichaelMure/git-bug/util/git" +) + +// An object that has an author. +type Authored interface { + IsAuthored() +} + +type AddCommentInput struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // "The name of the repository. If not set, the default repository is used. + RepoRef *string `json:"repoRef"` + // The bug ID's prefix. + Prefix string `json:"prefix"` + // The first message of the new bug. + Message string `json:"message"` + // The collection of file's hash required for the first message. + Files []git.Hash `json:"files"` +} + +type AddCommentPayload struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // The affected bug. + Bug BugWrapper `json:"bug"` + // The resulting operation. + Operation *bug.AddCommentOperation `json:"operation"` +} + +// The connection type for Bug. +type BugConnection struct { + // A list of edges. + Edges []*BugEdge `json:"edges"` + Nodes []BugWrapper `json:"nodes"` + // Information to aid in pagination. + PageInfo *PageInfo `json:"pageInfo"` + // Identifies the total count of items in the connection. + TotalCount int `json:"totalCount"` +} + +// An edge in a connection. +type BugEdge struct { + // A cursor for use in pagination. + Cursor string `json:"cursor"` + // The item at the end of the edge. + Node BugWrapper `json:"node"` +} + +type ChangeLabelInput struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // "The name of the repository. If not set, the default repository is used. + RepoRef *string `json:"repoRef"` + // The bug ID's prefix. + Prefix string `json:"prefix"` + // The list of label to add. + Added []string `json:"added"` + // The list of label to remove. + Removed []string `json:"Removed"` +} + +type ChangeLabelPayload struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // The affected bug. + Bug BugWrapper `json:"bug"` + // The resulting operation. + Operation *bug.LabelChangeOperation `json:"operation"` + // The effect each source label had. + Results []*bug.LabelChangeResult `json:"results"` +} + +type CloseBugInput struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // "The name of the repository. If not set, the default repository is used. + RepoRef *string `json:"repoRef"` + // The bug ID's prefix. + Prefix string `json:"prefix"` +} + +type CloseBugPayload struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // The affected bug. + Bug BugWrapper `json:"bug"` + // The resulting operation. + Operation *bug.SetStatusOperation `json:"operation"` +} + +type CommentConnection struct { + Edges []*CommentEdge `json:"edges"` + Nodes []*bug.Comment `json:"nodes"` + PageInfo *PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +type CommentEdge struct { + Cursor string `json:"cursor"` + Node *bug.Comment `json:"node"` +} + +type IdentityConnection struct { + Edges []*IdentityEdge `json:"edges"` + Nodes []IdentityWrapper `json:"nodes"` + PageInfo *PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +type IdentityEdge struct { + Cursor string `json:"cursor"` + Node IdentityWrapper `json:"node"` +} + +type LabelConnection struct { + Edges []*LabelEdge `json:"edges"` + Nodes []bug.Label `json:"nodes"` + PageInfo *PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +type LabelEdge struct { + Cursor string `json:"cursor"` + Node bug.Label `json:"node"` +} + +type NewBugInput struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // "The name of the repository. If not set, the default repository is used. + RepoRef *string `json:"repoRef"` + // The title of the new bug. + Title string `json:"title"` + // The first message of the new bug. + Message string `json:"message"` + // The collection of file's hash required for the first message. + Files []git.Hash `json:"files"` +} + +type NewBugPayload struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // The created bug. + Bug BugWrapper `json:"bug"` + // The resulting operation. + Operation *bug.CreateOperation `json:"operation"` +} + +type OpenBugInput struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // "The name of the repository. If not set, the default repository is used. + RepoRef *string `json:"repoRef"` + // The bug ID's prefix. + Prefix string `json:"prefix"` +} + +type OpenBugPayload struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // The affected bug. + Bug BugWrapper `json:"bug"` + // The resulting operation. + Operation *bug.SetStatusOperation `json:"operation"` +} + +// The connection type for an Operation +type OperationConnection struct { + Edges []*OperationEdge `json:"edges"` + Nodes []bug.Operation `json:"nodes"` + PageInfo *PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +// Represent an Operation +type OperationEdge struct { + Cursor string `json:"cursor"` + Node bug.Operation `json:"node"` +} + +// Information about pagination in a connection. +type PageInfo struct { + // When paginating forwards, are there more items? + HasNextPage bool `json:"hasNextPage"` + // When paginating backwards, are there more items? + HasPreviousPage bool `json:"hasPreviousPage"` + // When paginating backwards, the cursor to continue. + StartCursor string `json:"startCursor"` + // When paginating forwards, the cursor to continue. + EndCursor string `json:"endCursor"` +} + +type SetTitleInput struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // "The name of the repository. If not set, the default repository is used. + RepoRef *string `json:"repoRef"` + // The bug ID's prefix. + Prefix string `json:"prefix"` + // The new title. + Title string `json:"title"` +} + +type SetTitlePayload struct { + // A unique identifier for the client performing the mutation. + ClientMutationID *string `json:"clientMutationId"` + // The affected bug. + Bug BugWrapper `json:"bug"` + // The resulting operation + Operation *bug.SetTitleOperation `json:"operation"` +} + +// The connection type for TimelineItem +type TimelineItemConnection struct { + Edges []*TimelineItemEdge `json:"edges"` + Nodes []bug.TimelineItem `json:"nodes"` + PageInfo *PageInfo `json:"pageInfo"` + TotalCount int `json:"totalCount"` +} + +// Represent a TimelineItem +type TimelineItemEdge struct { + Cursor string `json:"cursor"` + Node bug.TimelineItem `json:"node"` +} + +type LabelChangeStatus string + +const ( + LabelChangeStatusAdded LabelChangeStatus = "ADDED" + LabelChangeStatusRemoved LabelChangeStatus = "REMOVED" + LabelChangeStatusDuplicateInOp LabelChangeStatus = "DUPLICATE_IN_OP" + LabelChangeStatusAlreadyExist LabelChangeStatus = "ALREADY_EXIST" + LabelChangeStatusDoesntExist LabelChangeStatus = "DOESNT_EXIST" +) + +var AllLabelChangeStatus = []LabelChangeStatus{ + LabelChangeStatusAdded, + LabelChangeStatusRemoved, + LabelChangeStatusDuplicateInOp, + LabelChangeStatusAlreadyExist, + LabelChangeStatusDoesntExist, +} + +func (e LabelChangeStatus) IsValid() bool { + switch e { + case LabelChangeStatusAdded, LabelChangeStatusRemoved, LabelChangeStatusDuplicateInOp, LabelChangeStatusAlreadyExist, LabelChangeStatusDoesntExist: + return true + } + return false +} + +func (e LabelChangeStatus) String() string { + return string(e) +} + +func (e *LabelChangeStatus) UnmarshalGQL(v interface{}) error { + str, ok := v.(string) + if !ok { + return fmt.Errorf("enums must be strings") + } + + *e = LabelChangeStatus(str) + if !e.IsValid() { + return fmt.Errorf("%s is not a valid LabelChangeStatus", str) + } + return nil +} + +func (e LabelChangeStatus) MarshalGQL(w io.Writer) { + fmt.Fprint(w, strconv.Quote(e.String())) +} + +type Status string + +const ( + StatusOpen Status = "OPEN" + StatusClosed Status = "CLOSED" +) + +var AllStatus = []Status{ + StatusOpen, + StatusClosed, +} + +func (e Status) IsValid() bool { + switch e { + case StatusOpen, StatusClosed: + return true + } + return false +} + +func (e Status) String() string { + return string(e) +} + +func (e *Status) UnmarshalGQL(v interface{}) error { + str, ok := v.(string) + if !ok { + return fmt.Errorf("enums must be strings") + } + + *e = Status(str) + if !e.IsValid() { + return fmt.Errorf("%s is not a valid Status", str) + } + return nil +} + +func (e Status) MarshalGQL(w io.Writer) { + fmt.Fprint(w, strconv.Quote(e.String())) +} diff --git a/api/graphql/models/lazy_bug.go b/api/graphql/models/lazy_bug.go new file mode 100644 index 00000000..a7840df2 --- /dev/null +++ b/api/graphql/models/lazy_bug.go @@ -0,0 +1,215 @@ +package models + +import ( + "sync" + "time" + + "github.com/MichaelMure/git-bug/bug" + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/entity" +) + +// BugWrapper is an interface used by the GraphQL resolvers to handle a bug. +// Depending on the situation, a Bug can already be fully loaded in memory or not. +// This interface is used to wrap either a lazyBug or a loadedBug depending on the situation. +type BugWrapper interface { + Id() entity.Id + LastEdit() time.Time + Status() bug.Status + Title() string + Comments() ([]bug.Comment, error) + Labels() []bug.Label + Author() (IdentityWrapper, error) + Actors() ([]IdentityWrapper, error) + Participants() ([]IdentityWrapper, error) + CreatedAt() time.Time + Timeline() ([]bug.TimelineItem, error) + Operations() ([]bug.Operation, error) + + IsAuthored() +} + +var _ BugWrapper = &lazyBug{} + +// lazyBug is a lazy-loading wrapper that fetch data from the cache (BugExcerpt) in priority, +// and load the complete bug and snapshot only when necessary. +type lazyBug struct { + cache *cache.RepoCache + excerpt *cache.BugExcerpt + + mu sync.Mutex + snap *bug.Snapshot +} + +func NewLazyBug(cache *cache.RepoCache, excerpt *cache.BugExcerpt) *lazyBug { + return &lazyBug{ + cache: cache, + excerpt: excerpt, + } +} + +func (lb *lazyBug) load() error { + if lb.snap != nil { + return nil + } + + lb.mu.Lock() + defer lb.mu.Unlock() + + b, err := lb.cache.ResolveBug(lb.excerpt.Id) + if err != nil { + return err + } + + lb.snap = b.Snapshot() + return nil +} + +func (lb *lazyBug) identity(id entity.Id) (IdentityWrapper, error) { + i, err := lb.cache.ResolveIdentityExcerpt(id) + if err != nil { + return nil, err + } + return &lazyIdentity{cache: lb.cache, excerpt: i}, nil +} + +// Sign post method for gqlgen +func (lb *lazyBug) IsAuthored() {} + +func (lb *lazyBug) Id() entity.Id { + return lb.excerpt.Id +} + +func (lb *lazyBug) LastEdit() time.Time { + return lb.excerpt.EditTime() +} + +func (lb *lazyBug) Status() bug.Status { + return lb.excerpt.Status +} + +func (lb *lazyBug) Title() string { + return lb.excerpt.Title +} + +func (lb *lazyBug) Comments() ([]bug.Comment, error) { + err := lb.load() + if err != nil { + return nil, err + } + return lb.snap.Comments, nil +} + +func (lb *lazyBug) Labels() []bug.Label { + return lb.excerpt.Labels +} + +func (lb *lazyBug) Author() (IdentityWrapper, error) { + return lb.identity(lb.excerpt.AuthorId) +} + +func (lb *lazyBug) Actors() ([]IdentityWrapper, error) { + result := make([]IdentityWrapper, len(lb.excerpt.Actors)) + for i, actorId := range lb.excerpt.Actors { + actor, err := lb.identity(actorId) + if err != nil { + return nil, err + } + result[i] = actor + } + return result, nil +} + +func (lb *lazyBug) Participants() ([]IdentityWrapper, error) { + result := make([]IdentityWrapper, len(lb.excerpt.Participants)) + for i, participantId := range lb.excerpt.Participants { + participant, err := lb.identity(participantId) + if err != nil { + return nil, err + } + result[i] = participant + } + return result, nil +} + +func (lb *lazyBug) CreatedAt() time.Time { + return lb.excerpt.CreateTime() +} + +func (lb *lazyBug) Timeline() ([]bug.TimelineItem, error) { + err := lb.load() + if err != nil { + return nil, err + } + return lb.snap.Timeline, nil +} + +func (lb *lazyBug) Operations() ([]bug.Operation, error) { + err := lb.load() + if err != nil { + return nil, err + } + return lb.snap.Operations, nil +} + +var _ BugWrapper = &loadedBug{} + +type loadedBug struct { + *bug.Snapshot +} + +func NewLoadedBug(snap *bug.Snapshot) *loadedBug { + return &loadedBug{Snapshot: snap} +} + +func (l *loadedBug) LastEdit() time.Time { + return l.Snapshot.EditTime() +} + +func (l *loadedBug) Status() bug.Status { + return l.Snapshot.Status +} + +func (l *loadedBug) Title() string { + return l.Snapshot.Title +} + +func (l *loadedBug) Comments() ([]bug.Comment, error) { + return l.Snapshot.Comments, nil +} + +func (l *loadedBug) Labels() []bug.Label { + return l.Snapshot.Labels +} + +func (l *loadedBug) Author() (IdentityWrapper, error) { + return NewLoadedIdentity(l.Snapshot.Author), nil +} + +func (l *loadedBug) Actors() ([]IdentityWrapper, error) { + res := make([]IdentityWrapper, len(l.Snapshot.Actors)) + for i, actor := range l.Snapshot.Actors { + res[i] = NewLoadedIdentity(actor) + } + return res, nil +} + +func (l *loadedBug) Participants() ([]IdentityWrapper, error) { + res := make([]IdentityWrapper, len(l.Snapshot.Participants)) + for i, participant := range l.Snapshot.Participants { + res[i] = NewLoadedIdentity(participant) + } + return res, nil +} + +func (l *loadedBug) CreatedAt() time.Time { + return l.Snapshot.CreateTime +} + +func (l *loadedBug) Timeline() ([]bug.TimelineItem, error) { + return l.Snapshot.Timeline, nil +} + +func (l *loadedBug) Operations() ([]bug.Operation, error) { + return l.Snapshot.Operations, nil +} diff --git a/api/graphql/models/lazy_identity.go b/api/graphql/models/lazy_identity.go new file mode 100644 index 00000000..344bb5f0 --- /dev/null +++ b/api/graphql/models/lazy_identity.go @@ -0,0 +1,180 @@ +package models + +import ( + "fmt" + "sync" + + "github.com/MichaelMure/git-bug/cache" + "github.com/MichaelMure/git-bug/entity" + "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/util/lamport" + "github.com/MichaelMure/git-bug/util/timestamp" +) + +// IdentityWrapper is an interface used by the GraphQL resolvers to handle an identity. +// Depending on the situation, an Identity can already be fully loaded in memory or not. +// This interface is used to wrap either a lazyIdentity or a loadedIdentity depending on the situation. +type IdentityWrapper interface { + Id() entity.Id + Name() string + Email() (string, error) + Login() (string, error) + AvatarUrl() (string, error) + Keys() ([]*identity.Key, error) + ValidKeysAtTime(time lamport.Time) ([]*identity.Key, error) + DisplayName() string + IsProtected() (bool, error) + LastModificationLamport() (lamport.Time, error) + LastModification() (timestamp.Timestamp, error) +} + +var _ IdentityWrapper = &lazyIdentity{} + +type lazyIdentity struct { + cache *cache.RepoCache + excerpt *cache.IdentityExcerpt + + mu sync.Mutex + id *cache.IdentityCache +} + +func NewLazyIdentity(cache *cache.RepoCache, excerpt *cache.IdentityExcerpt) *lazyIdentity { + return &lazyIdentity{ + cache: cache, + excerpt: excerpt, + } +} + +func (li *lazyIdentity) load() (*cache.IdentityCache, error) { + if li.id != nil { + return li.id, nil + } + + li.mu.Lock() + defer li.mu.Unlock() + + id, err := li.cache.ResolveIdentity(li.excerpt.Id) + if err != nil { + return nil, fmt.Errorf("cache: missing identity %v", li.excerpt.Id) + } + li.id = id + return id, nil +} + +func (li *lazyIdentity) Id() entity.Id { + return li.excerpt.Id +} + +func (li *lazyIdentity) Name() string { + return li.excerpt.Name +} + +func (li *lazyIdentity) Email() (string, error) { + id, err := li.load() + if err != nil { + return "", err + } + return id.Email(), nil +} + +func (li *lazyIdentity) Login() (string, error) { + id, err := li.load() + if err != nil { + return "", err + } + return id.Login(), nil +} + +func (li *lazyIdentity) AvatarUrl() (string, error) { + id, err := li.load() + if err != nil { + return "", err + } + return id.AvatarUrl(), nil +} + +func (li *lazyIdentity) Keys() ([]*identity.Key, error) { + id, err := li.load() + if err != nil { + return nil, err + } + return id.Keys(), nil +} + +func (li *lazyIdentity) ValidKeysAtTime(time lamport.Time) ([]*identity.Key, error) { + id, err := li.load() + if err != nil { + return nil, err + } + return id.ValidKeysAtTime(time), nil +} + +func (li *lazyIdentity) DisplayName() string { + return li.excerpt.DisplayName() +} + +func (li *lazyIdentity) IsProtected() (bool, error) { + id, err := li.load() + if err != nil { + return false, err + } + return id.IsProtected(), nil +} + +func (li *lazyIdentity) LastModificationLamport() (lamport.Time, error) { + id, err := li.load() + if err != nil { + return 0, err + } + return id.LastModificationLamport(), nil +} + +func (li *lazyIdentity) LastModification() (timestamp.Timestamp, error) { + id, err := li.load() + if err != nil { + return 0, err + } + return id.LastModification(), nil +} + +var _ IdentityWrapper = &loadedIdentity{} + +type loadedIdentity struct { + identity.Interface +} + +func NewLoadedIdentity(id identity.Interface) *loadedIdentity { + return &loadedIdentity{Interface: id} +} + +func (l loadedIdentity) Email() (string, error) { + return l.Interface.Email(), nil +} + +func (l loadedIdentity) Login() (string, error) { + return l.Interface.Login(), nil +} + +func (l loadedIdentity) AvatarUrl() (string, error) { + return l.Interface.AvatarUrl(), nil +} + +func (l loadedIdentity) Keys() ([]*identity.Key, error) { + return l.Interface.Keys(), nil +} + +func (l loadedIdentity) ValidKeysAtTime(time lamport.Time) ([]*identity.Key, error) { + return l.Interface.ValidKeysAtTime(time), nil +} + +func (l loadedIdentity) IsProtected() (bool, error) { + return l.Interface.IsProtected(), nil +} + +func (l loadedIdentity) LastModificationLamport() (lamport.Time, error) { + return l.Interface.LastModificationLamport(), nil +} + +func (l loadedIdentity) LastModification() (timestamp.Timestamp, error) { + return l.Interface.LastModification(), nil +} diff --git a/api/graphql/models/models.go b/api/graphql/models/models.go new file mode 100644 index 00000000..816a04a8 --- /dev/null +++ b/api/graphql/models/models.go @@ -0,0 +1,23 @@ +// Package models contains the various GraphQL data models +package models + +import ( + "github.com/MichaelMure/git-bug/cache" +) + +type ConnectionInput struct { + After *string + Before *string + First *int + Last *int +} + +type Repository struct { + Cache *cache.MultiRepoCache + Repo *cache.RepoCache +} + +type RepositoryMutation struct { + Cache *cache.MultiRepoCache + Repo *cache.RepoCache +} |