@@ -5,11 +5,15 @@ package git_test
5
5
6
6
import (
7
7
"testing"
8
+ "time"
8
9
9
10
"code.gitea.io/gitea/models/db"
10
11
git_model "code.gitea.io/gitea/models/git"
11
12
repo_model "code.gitea.io/gitea/models/repo"
12
13
"code.gitea.io/gitea/models/unittest"
14
+ user_model "code.gitea.io/gitea/models/user"
15
+ "code.gitea.io/gitea/modules/git"
16
+ "code.gitea.io/gitea/modules/gitrepo"
13
17
"code.gitea.io/gitea/modules/structs"
14
18
15
19
"github.com/stretchr/testify/assert"
@@ -175,3 +179,55 @@ func Test_CalcCommitStatus(t *testing.T) {
175
179
assert .Equal (t , kase .expected , git_model .CalcCommitStatus (kase .statuses ))
176
180
}
177
181
}
182
+
183
+ func TestFindRepoRecentCommitStatusContexts (t * testing.T ) {
184
+ assert .NoError (t , unittest .PrepareTestDatabase ())
185
+
186
+ repo2 := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 2 })
187
+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
188
+ gitRepo , err := gitrepo .OpenRepository (git .DefaultContext , repo2 )
189
+ assert .NoError (t , err )
190
+ defer gitRepo .Close ()
191
+
192
+ commit , err := gitRepo .GetBranchCommit (repo2 .DefaultBranch )
193
+ assert .NoError (t , err )
194
+
195
+ defer func () {
196
+ _ , err := db .DeleteByBean (db .DefaultContext , & git_model.CommitStatus {
197
+ RepoID : repo2 .ID ,
198
+ CreatorID : user2 .ID ,
199
+ SHA : commit .ID .String (),
200
+ })
201
+ assert .NoError (t , err )
202
+ }()
203
+
204
+ err = git_model .NewCommitStatus (db .DefaultContext , git_model.NewCommitStatusOptions {
205
+ Repo : repo2 ,
206
+ Creator : user2 ,
207
+ SHA : commit .ID ,
208
+ CommitStatus : & git_model.CommitStatus {
209
+ State : structs .CommitStatusFailure ,
210
+ TargetURL : "https://example.com/tests/" ,
211
+ Context : "compliance/lint-backend" ,
212
+ },
213
+ })
214
+ assert .NoError (t , err )
215
+
216
+ err = git_model .NewCommitStatus (db .DefaultContext , git_model.NewCommitStatusOptions {
217
+ Repo : repo2 ,
218
+ Creator : user2 ,
219
+ SHA : commit .ID ,
220
+ CommitStatus : & git_model.CommitStatus {
221
+ State : structs .CommitStatusSuccess ,
222
+ TargetURL : "https://example.com/tests/" ,
223
+ Context : "compliance/lint-backend" ,
224
+ },
225
+ })
226
+ assert .NoError (t , err )
227
+
228
+ contexts , err := git_model .FindRepoRecentCommitStatusContexts (db .DefaultContext , repo2 .ID , time .Hour )
229
+ assert .NoError (t , err )
230
+ if assert .Len (t , contexts , 1 ) {
231
+ assert .Equal (t , "compliance/lint-backend" , contexts [0 ])
232
+ }
233
+ }
0 commit comments