This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
/
query.go
97 lines (83 loc) 路 2.16 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package server
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"github.com/bobheadxi/timelines/config"
"github.com/bobheadxi/timelines/db"
"github.com/bobheadxi/timelines/graphql/go/timelines"
"github.com/bobheadxi/timelines/graphql/go/timelines/models"
"github.com/bobheadxi/timelines/host"
)
type queryResolver struct {
db *db.Database
l *zap.SugaredLogger
build string
deployed time.Time
}
func newQueryResolver(
l *zap.SugaredLogger,
database *db.Database,
build string,
) timelines.QueryResolver {
return &queryResolver{database, l.Named("query"), build, time.Now()}
}
func (q *queryResolver) ServiceStatus(context.Context) (*models.ServiceStatus, error) {
return &models.ServiceStatus{
Build: q.build,
Deployed: q.deployed,
}, nil
}
func (q *queryResolver) Repo(
ctx context.Context,
owner, name string, h *models.RepositoryHost,
) (*models.RepositoryAnalytics, error) {
var l = q.l.With(config.LogKeyRID, requestID(ctx),
"owner", owner, "name", name)
hostService, err := modelToHost(h)
if err != nil {
return nil, err
}
repo, err := q.db.Repos().GetRepository(ctx, hostService, owner, name)
if err != nil {
if !db.IsNotFound(err) {
l.Errorw(err.Error())
}
return nil, fmt.Errorf("could not find repository for '%s/%s'", owner, name)
}
return &models.RepositoryAnalytics{
Repository: repo,
}, nil
}
func (q *queryResolver) Repos(
ctx context.Context,
owner string, h *models.RepositoryHost,
) ([]*models.Repository, error) {
var l = q.l.With(config.LogKeyRID, requestID(ctx),
"owner", owner)
hostService, err := modelToHost(h)
if err != nil {
return nil, err
}
repos, err := q.db.Repos().GetRepositories(ctx, hostService, owner)
if err != nil {
if !db.IsNotFound(err) {
l.Errorw(err.Error())
}
return nil, fmt.Errorf("could not find repositories for '%s'", owner)
}
return repos, nil
}
func modelToHost(h *models.RepositoryHost) (host.Host, error) {
if h == nil {
return host.HostGitHub, nil
}
hostService := host.Host(h.String())
switch hostService {
case host.HostGitHub, host.HostGitLab, host.HostBitbucket:
return hostService, nil
default:
return "", fmt.Errorf("unknown host '%v'", h)
}
}