@@ -5,11 +5,14 @@ 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"
13
16
"code.gitea.io/gitea/modules/structs"
14
17
15
18
"github.com/stretchr/testify/assert"
@@ -60,3 +63,55 @@ func TestGetCommitStatuses(t *testing.T) {
60
63
assert .Equal (t , int (maxResults ), 5 )
61
64
assert .Empty (t , statuses )
62
65
}
66
+
67
+ func TestFindRepoRecentCommitStatusContexts (t * testing.T ) {
68
+ assert .NoError (t , unittest .PrepareTestDatabase ())
69
+
70
+ repo2 := unittest .AssertExistsAndLoadBean (t , & repo_model.Repository {ID : 2 })
71
+ user2 := unittest .AssertExistsAndLoadBean (t , & user_model.User {ID : 2 })
72
+ gitRepo , err := git .OpenRepository (git .DefaultContext , repo2 .RepoPath ())
73
+ assert .NoError (t , err )
74
+ defer gitRepo .Close ()
75
+
76
+ commit , err := gitRepo .GetBranchCommit (repo2 .DefaultBranch )
77
+ assert .NoError (t , err )
78
+
79
+ defer func () {
80
+ _ , err := db .DeleteByBean (db .DefaultContext , & git_model.CommitStatus {
81
+ RepoID : repo2 .ID ,
82
+ CreatorID : user2 .ID ,
83
+ SHA : commit .ID .String (),
84
+ })
85
+ assert .NoError (t , err )
86
+ }()
87
+
88
+ err = git_model .NewCommitStatus (db .DefaultContext , git_model.NewCommitStatusOptions {
89
+ Repo : repo2 ,
90
+ Creator : user2 ,
91
+ SHA : commit .ID .String (),
92
+ CommitStatus : & git_model.CommitStatus {
93
+ State : structs .CommitStatusFailure ,
94
+ TargetURL : "https://example.com/tests/" ,
95
+ Context : "compliance/lint-backend" ,
96
+ },
97
+ })
98
+ assert .NoError (t , err )
99
+
100
+ err = git_model .NewCommitStatus (db .DefaultContext , git_model.NewCommitStatusOptions {
101
+ Repo : repo2 ,
102
+ Creator : user2 ,
103
+ SHA : commit .ID .String (),
104
+ CommitStatus : & git_model.CommitStatus {
105
+ State : structs .CommitStatusSuccess ,
106
+ TargetURL : "https://example.com/tests/" ,
107
+ Context : "compliance/lint-backend" ,
108
+ },
109
+ })
110
+ assert .NoError (t , err )
111
+
112
+ contexts , err := git_model .FindRepoRecentCommitStatusContexts (db .DefaultContext , repo2 .ID , time .Hour )
113
+ assert .NoError (t , err )
114
+ if assert .Len (t , contexts , 1 ) {
115
+ assert .Equal (t , "compliance/lint-backend" , contexts [0 ])
116
+ }
117
+ }
0 commit comments