@@ -6,16 +6,14 @@ package models
6
6
7
7
import (
8
8
"container/list"
9
+ "crypto/sha1"
9
10
"fmt"
10
11
"strings"
11
12
12
- "code.gitea.io/gitea/modules/git"
13
13
"code.gitea.io/gitea/modules/log"
14
14
"code.gitea.io/gitea/modules/setting"
15
15
api "code.gitea.io/gitea/modules/structs"
16
16
"code.gitea.io/gitea/modules/util"
17
-
18
- "github.com/go-xorm/xorm"
19
17
)
20
18
21
19
// CommitStatusState holds the state of a Status
@@ -61,6 +59,7 @@ type CommitStatus struct {
61
59
SHA string `xorm:"VARCHAR(64) NOT NULL INDEX UNIQUE(repo_sha_index)"`
62
60
TargetURL string `xorm:"TEXT"`
63
61
Description string `xorm:"TEXT"`
62
+ ContextHash string `xorm:"char(40) index"`
64
63
Context string `xorm:"TEXT"`
65
64
Creator * User `xorm:"-"`
66
65
CreatorID int64
@@ -146,7 +145,7 @@ func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitSta
146
145
Table (& CommitStatus {}).
147
146
Where ("repo_id = ?" , repo .ID ).And ("sha = ?" , sha ).
148
147
Select ("max( id ) as id" ).
149
- GroupBy ("context " ).OrderBy ("max( id ) desc" ).Find (& ids )
148
+ GroupBy ("context_hash " ).OrderBy ("max( id ) desc" ).Find (& ids )
150
149
if err != nil {
151
150
return nil , err
152
151
}
@@ -157,27 +156,6 @@ func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitSta
157
156
return statuses , x .In ("id" , ids ).Find (& statuses )
158
157
}
159
158
160
- // GetCommitStatus populates a given status for a given commit.
161
- // NOTE: If ID or Index isn't given, and only Context, TargetURL and/or Description
162
- // is given, the CommitStatus created _last_ will be returned.
163
- func GetCommitStatus (repo * Repository , sha string , status * CommitStatus ) (* CommitStatus , error ) {
164
- conds := & CommitStatus {
165
- Context : status .Context ,
166
- State : status .State ,
167
- TargetURL : status .TargetURL ,
168
- Description : status .Description ,
169
- }
170
- has , err := x .Where ("repo_id = ?" , repo .ID ).And ("sha = ?" , sha ).Desc ("created_unix" ).Get (conds )
171
- if err != nil {
172
- return nil , fmt .Errorf ("GetCommitStatus[%s, %s]: %v" , repo .RepoPath (), sha , err )
173
- }
174
- if ! has {
175
- return nil , fmt .Errorf ("GetCommitStatus[%s, %s]: not found" , repo .RepoPath (), sha )
176
- }
177
-
178
- return conds , nil
179
- }
180
-
181
159
// NewCommitStatusOptions holds options for creating a CommitStatus
182
160
type NewCommitStatusOptions struct {
183
161
Repo * Repository
@@ -186,31 +164,31 @@ type NewCommitStatusOptions struct {
186
164
CommitStatus * CommitStatus
187
165
}
188
166
189
- func newCommitStatus (sess * xorm.Session , opts NewCommitStatusOptions ) error {
190
- opts .CommitStatus .Description = strings .TrimSpace (opts .CommitStatus .Description )
191
- opts .CommitStatus .Context = strings .TrimSpace (opts .CommitStatus .Context )
192
- opts .CommitStatus .TargetURL = strings .TrimSpace (opts .CommitStatus .TargetURL )
193
- opts .CommitStatus .SHA = opts .SHA
194
- opts .CommitStatus .CreatorID = opts .Creator .ID
195
-
167
+ // NewCommitStatus save commit statuses into database
168
+ func NewCommitStatus (opts NewCommitStatusOptions ) error {
196
169
if opts .Repo == nil {
197
- return fmt .Errorf ("newCommitStatus [nil, %s]: no repository specified" , opts .SHA )
170
+ return fmt .Errorf ("NewCommitStatus [nil, %s]: no repository specified" , opts .SHA )
198
171
}
199
- opts .CommitStatus .RepoID = opts .Repo .ID
200
- repoPath := opts .Repo .repoPath (sess )
201
172
173
+ repoPath := opts .Repo .RepoPath ()
202
174
if opts .Creator == nil {
203
- return fmt .Errorf ("newCommitStatus [%s, %s]: no user specified" , repoPath , opts .SHA )
175
+ return fmt .Errorf ("NewCommitStatus [%s, %s]: no user specified" , repoPath , opts .SHA )
204
176
}
205
177
206
- gitRepo , err := git .OpenRepository (repoPath )
207
- if err != nil {
208
- return fmt .Errorf ("OpenRepository[%s]: %v" , repoPath , err )
209
- }
210
- if _ , err := gitRepo .GetCommit (opts .SHA ); err != nil {
211
- return fmt .Errorf ("GetCommit[%s]: %v" , opts .SHA , err )
178
+ sess := x .NewSession ()
179
+ defer sess .Close ()
180
+
181
+ if err := sess .Begin (); err != nil {
182
+ return fmt .Errorf ("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v" , opts .Repo .ID , opts .Creator .ID , opts .SHA , err )
212
183
}
213
184
185
+ opts .CommitStatus .Description = strings .TrimSpace (opts .CommitStatus .Description )
186
+ opts .CommitStatus .Context = strings .TrimSpace (opts .CommitStatus .Context )
187
+ opts .CommitStatus .TargetURL = strings .TrimSpace (opts .CommitStatus .TargetURL )
188
+ opts .CommitStatus .SHA = opts .SHA
189
+ opts .CommitStatus .CreatorID = opts .Creator .ID
190
+ opts .CommitStatus .RepoID = opts .Repo .ID
191
+
214
192
// Get the next Status Index
215
193
var nextIndex int64
216
194
lastCommitStatus := & CommitStatus {
@@ -220,46 +198,25 @@ func newCommitStatus(sess *xorm.Session, opts NewCommitStatusOptions) error {
220
198
has , err := sess .Desc ("index" ).Limit (1 ).Get (lastCommitStatus )
221
199
if err != nil {
222
200
if err := sess .Rollback (); err != nil {
223
- log .Error ("newCommitStatus : sess.Rollback: %v" , err )
201
+ log .Error ("NewCommitStatus : sess.Rollback: %v" , err )
224
202
}
225
- return fmt .Errorf ("newCommitStatus [%s, %s]: %v" , repoPath , opts .SHA , err )
203
+ return fmt .Errorf ("NewCommitStatus [%s, %s]: %v" , repoPath , opts .SHA , err )
226
204
}
227
205
if has {
228
- log .Debug ("newCommitStatus [%s, %s]: found" , repoPath , opts .SHA )
206
+ log .Debug ("NewCommitStatus [%s, %s]: found" , repoPath , opts .SHA )
229
207
nextIndex = lastCommitStatus .Index
230
208
}
231
209
opts .CommitStatus .Index = nextIndex + 1
232
- log .Debug ("newCommitStatus[%s, %s]: %d" , repoPath , opts .SHA , opts .CommitStatus .Index )
210
+ log .Debug ("NewCommitStatus[%s, %s]: %d" , repoPath , opts .SHA , opts .CommitStatus .Index )
211
+
212
+ opts .CommitStatus .ContextHash = hashCommitStatusContext (opts .CommitStatus .Context )
233
213
234
214
// Insert new CommitStatus
235
215
if _ , err = sess .Insert (opts .CommitStatus ); err != nil {
236
216
if err := sess .Rollback (); err != nil {
237
- log .Error ("newCommitStatus : sess.Rollback: %v" , err )
217
+ log .Error ("Insert CommitStatus : sess.Rollback: %v" , err )
238
218
}
239
- return fmt .Errorf ("newCommitStatus[%s, %s]: %v" , repoPath , opts .SHA , err )
240
- }
241
-
242
- return nil
243
- }
244
-
245
- // NewCommitStatus creates a new CommitStatus given a bunch of parameters
246
- // NOTE: All text-values will be trimmed from whitespaces.
247
- // Requires: Repo, Creator, SHA
248
- func NewCommitStatus (repo * Repository , creator * User , sha string , status * CommitStatus ) error {
249
- sess := x .NewSession ()
250
- defer sess .Close ()
251
-
252
- if err := sess .Begin (); err != nil {
253
- return fmt .Errorf ("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v" , repo .ID , creator .ID , sha , err )
254
- }
255
-
256
- if err := newCommitStatus (sess , NewCommitStatusOptions {
257
- Repo : repo ,
258
- Creator : creator ,
259
- SHA : sha ,
260
- CommitStatus : status ,
261
- }); err != nil {
262
- return fmt .Errorf ("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v" , repo .ID , creator .ID , sha , err )
219
+ return fmt .Errorf ("Insert CommitStatus[%s, %s]: %v" , repoPath , opts .SHA , err )
263
220
}
264
221
265
222
return sess .Commit ()
@@ -295,3 +252,8 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List
295
252
}
296
253
return newCommits
297
254
}
255
+
256
+ // hashCommitStatusContext hash context
257
+ func hashCommitStatusContext (context string ) string {
258
+ return fmt .Sprintf ("%x" , sha1 .Sum ([]byte (context )))
259
+ }
0 commit comments