-
Notifications
You must be signed in to change notification settings - Fork 124
/
repository.go
118 lines (104 loc) · 2.43 KB
/
repository.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package model
import (
"strings"
"time"
"github.com/evergreen-ci/evergreen/db"
"github.com/mongodb/anser/bsonutil"
adb "github.com/mongodb/anser/db"
"go.mongodb.org/mongo-driver/bson"
)
// Repository contains fields used to track projects.
type Repository struct {
Project string `bson:"_id"`
LastRevision string `bson:"last_revision"`
RevisionOrderNumber int `bson:"last_commit_number"`
}
var (
// BSON fields for the Repository struct
RepoProjectKey = bsonutil.MustHaveTag(Repository{},
"Project")
RepoLastRevisionKey = bsonutil.MustHaveTag(Repository{},
"LastRevision")
RepositoryOrderNumberKey = bsonutil.MustHaveTag(Repository{},
"RevisionOrderNumber")
)
const (
RepositoriesCollection = "repo_revisions"
)
const (
GithubRepoType = "github"
)
// valid repositories - currently only github supported
var (
ValidRepoTypes = []string{GithubRepoType}
)
type Revision struct {
Author string
AuthorID string
AuthorGithubUID int
AuthorEmail string
RevisionMessage string
Revision string
CreateTime time.Time
}
type GitTag struct {
Tag string
Pusher string
}
type GitTags []GitTag
func (tags GitTags) String() string {
tagNames := []string{}
for _, t := range tags {
tagNames = append(tagNames, t.Tag)
}
return strings.Join(tagNames, ", ")
}
// FindRepository gets the repository object of a project.
func FindRepository(projectId string) (*Repository, error) {
repository := &Repository{}
q := db.Query(bson.M{RepoProjectKey: projectId})
err := db.FindOneQ(RepositoriesCollection, q, repository)
if adb.ResultsNotFound(err) {
return nil, nil
}
return repository, err
}
// UpdateLastRevision updates the last created revision of a project.
func UpdateLastRevision(projectId, revision string) error {
return db.Update(
RepositoriesCollection,
bson.M{
RepoProjectKey: projectId,
},
bson.M{
"$set": bson.M{
RepoLastRevisionKey: revision,
},
},
)
}
// GetNewRevisionOrderNumber gets a new revision order number for a project.
func GetNewRevisionOrderNumber(projectId string) (int, error) {
repo := &Repository{}
_, err := db.FindAndModify(
RepositoriesCollection,
bson.M{
RepoProjectKey: projectId,
},
nil,
adb.Change{
Update: bson.M{
"$inc": bson.M{
RepositoryOrderNumberKey: 1,
},
},
Upsert: true,
ReturnNew: true,
},
repo,
)
if err != nil {
return 0, err
}
return repo.RevisionOrderNumber, nil
}