forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commits.go
202 lines (177 loc) · 5.71 KB
/
commits.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package database
import (
"time"
. "github.com/drone/drone/pkg/model"
"github.com/russross/meddler"
)
// Name of the Commit table in the database
const commitTable = "commits"
// SQL Queries to retrieve a list of all Commits belonging to a Repo.
const commitStmt = `
SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits
WHERE repo_id = ? AND branch = ?
ORDER BY created DESC, id DESC
LIMIT 10
`
// SQL Queries to retrieve the latest Commit.
const commitLatestStmt = `
SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits
WHERE repo_id = ? AND branch = ?
ORDER BY created DESC, id DESC
LIMIT 1
`
// SQL Queries to retrieve a Commit by id.
const commitFindStmt = `
SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits
WHERE id = ?
`
// SQL Queries to retrieve a Commit by hash and repo id.
const commitFindHashStmt = `
SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits
WHERE hash = ? AND repo_id = ?
LIMIT 1
`
// SQL Queries to retrieve a Commit by branch, hash, and repo id.
const commitFindBranchHashStmt = `
SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits
WHERE branch = ? AND hash = ? AND repo_id = ?
LIMIT 1
`
// SQL Query to retrieve a list of recent commits by user.
const userCommitRecentStmt = `
SELECT r.slug, r.host, r.owner, r.name,
c.status, c.started, c.finished, c.duration, c.hash, c.branch, c.pull_request,
c.author, c.gravatar, c.timestamp, c.message, c.created, c.updated
FROM repos r, commits c
WHERE r.user_id = ?
AND r.team_id = 0
AND r.id = c.repo_id
AND c.status IN ('Success', 'Failure')
ORDER BY c.created desc, c.id desc
LIMIT 10
`
// SQL Query to retrieve a list of recent commits by team.
const teamCommitRecentStmt = `
SELECT r.slug, r.host, r.owner, r.name,
c.status, c.started, c.finished, c.duration, c.hash, c.branch, c.pull_request,
c.author, c.gravatar, c.timestamp, c.message, c.created, c.updated
FROM repos r, commits c
WHERE r.team_id = ?
AND r.id = c.repo_id
AND c.status IN ('Success', 'Failure')
ORDER BY c.created desc, c.id desc
LIMIT 10
`
// SQL Queries to delete a Commit.
const commitDeleteStmt = `
DELETE FROM commits WHERE id = ?
`
// SQL Queries to retrieve the latest Commits for each branch.
const commitBranchesStmt = `
SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits
WHERE id IN (
SELECT MAX(id)
FROM commits
WHERE repo_id = ?
GROUP BY branch)
ORDER BY branch ASC
`
// SQL Queries to retrieve the latest Commits for each branch.
const commitBranchStmt = `
SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits
WHERE id IN (
SELECT MAX(id)
FROM commits
WHERE repo_id = ?
AND branch = ?
GROUP BY branch)
LIMIT 1
`
// SQL Queries to fail all commits that are currently building
const commitFailUnfinishedStmt = `
UPDATE commits
SET status = 'Failure'
WHERE status IN ('Started', 'Pending')
`
// Returns the Commit with the given ID.
func GetCommit(id int64) (*Commit, error) {
commit := Commit{}
err := meddler.QueryRow(db, &commit, commitFindStmt, id)
return &commit, err
}
// Returns the Commit with the given hash.
func GetCommitHash(hash string, repo int64) (*Commit, error) {
commit := Commit{}
err := meddler.QueryRow(db, &commit, commitFindHashStmt, hash, repo)
return &commit, err
}
// Returns the Commit on the given branch with the given hash.
func GetCommitBranchHash(branch string, hash string, repo int64) (*Commit, error) {
commit := Commit{}
err := meddler.QueryRow(db, &commit, commitFindBranchHashStmt, branch, hash, repo)
return &commit, err
}
// Returns the most recent Commit for the given branch.
func GetBranch(repo int64, branch string) (*Commit, error) {
commit := Commit{}
err := meddler.QueryRow(db, &commit, commitBranchStmt, repo, branch)
return &commit, err
}
// Creates a new Commit.
func SaveCommit(commit *Commit) error {
if commit.ID == 0 {
commit.Created = time.Now().UTC()
}
commit.Updated = time.Now().UTC()
return meddler.Save(db, commitTable, commit)
}
// Deletes an existing Commit.
func DeleteCommit(id int64) error {
_, err := db.Exec(commitDeleteStmt, id)
return err
}
// Returns a list of all Commits associated
// with the specified Repo ID.
func ListCommits(repo int64, branch string) ([]*Commit, error) {
var commits []*Commit
err := meddler.QueryAll(db, &commits, commitStmt, repo, branch)
return commits, err
}
// Returns a list of recent Commits associated
// with the specified User ID
func ListCommitsUser(user int64) ([]*RepoCommit, error) {
var commits []*RepoCommit
err := meddler.QueryAll(db, &commits, userCommitRecentStmt, user)
return commits, err
}
// Returns a list of recent Commits associated
// with the specified Team ID
func ListCommitsTeam(team int64) ([]*RepoCommit, error) {
var commits []*RepoCommit
err := meddler.QueryAll(db, &commits, teamCommitRecentStmt, team)
return commits, err
}
// Returns a list of the most recent commits for each branch.
func ListBranches(repo int64) ([]*Commit, error) {
var commits []*Commit
err := meddler.QueryAll(db, &commits, commitBranchesStmt, repo)
return commits, err
}
func FailUnfinishedCommits() error {
_, err := db.Exec(commitFailUnfinishedStmt)
return err
}