@@ -45,8 +45,10 @@ func createGithubCommit(sha, message string) *github.RepositoryCommit {
45
45
return & github.RepositoryCommit {SHA : & sha , Commit : & github.Commit {Message : & message }}
46
46
}
47
47
48
- var commitType = "commit"
49
- var tagType = "tag"
48
+ var (
49
+ commitType = "commit"
50
+ tagType = "tag"
51
+ )
50
52
51
53
func createGithubRef (ref , sha string ) * github.Reference {
52
54
return & github.Reference {Ref : & ref , Object : & github.GitObject {SHA : & sha , Type : & commitType }}
@@ -57,19 +59,19 @@ func createGithubRefWithTag(ref, sha string) *github.Reference {
57
59
}
58
60
59
61
var (
60
- GITHUB_REPO_PRIVATE = true
61
- GITHUB_DEFAULTBRANCH = "master"
62
- GITHUB_REPO_NAME = "test-repo"
63
- GITHUB_OWNER_LOGIN = "owner"
64
- GITHUB_REPO = github.Repository {
65
- DefaultBranch : & GITHUB_DEFAULTBRANCH ,
66
- Private : & GITHUB_REPO_PRIVATE ,
62
+ githubRepoPrivate = true
63
+ githubDefaultBranch = "master"
64
+ githubRepoName = "test-repo"
65
+ githubOwnerLogin = "owner"
66
+ githubRepo = github.Repository {
67
+ DefaultBranch : & githubDefaultBranch ,
68
+ Private : & githubRepoPrivate ,
67
69
Owner : & github.User {
68
- Login : & GITHUB_OWNER_LOGIN ,
70
+ Login : & githubOwnerLogin ,
69
71
},
70
- Name : & GITHUB_REPO_NAME ,
72
+ Name : & githubRepoName ,
71
73
}
72
- GITHUB_COMMITS = []* github.RepositoryCommit {
74
+ githubCommits = []* github.RepositoryCommit {
73
75
createGithubCommit ("abcd" , "feat(app): new new feature" ),
74
76
createGithubCommit ("1111" , "feat: to" ),
75
77
createGithubCommit ("abcd" , "feat(app): new feature" ),
@@ -79,62 +81,64 @@ var (
79
81
createGithubCommit ("2222" , "feat: from" ),
80
82
createGithubCommit ("beef" , "fix: test" ),
81
83
}
82
- GITHUB_TAGS = []* github.Reference {
84
+ githubTags = []* github.Reference {
83
85
createGithubRef ("refs/tags/test-tag" , "deadbeef" ),
84
- createGithubRef ("refs/tags/v1.0.0" , "deadbeef " ),
86
+ createGithubRef ("refs/tags/v1.0.0" , "beefdead " ),
85
87
createGithubRef ("refs/tags/v2.0.0" , "deadbeef" ),
86
- createGithubRef ("refs/tags/v2.1.0-beta" , "deadbeef " ),
88
+ createGithubRef ("refs/tags/v2.1.0-beta" , "beefdead " ),
87
89
createGithubRef ("refs/tags/v3.0.0-beta.2" , "deadbeef" ),
88
- createGithubRef ("refs/tags/v3.0.0-beta.1" , "deadbeef " ),
90
+ createGithubRef ("refs/tags/v3.0.0-beta.1" , "beefdead " ),
89
91
createGithubRef ("refs/tags/2020.04.19" , "deadbeef" ),
90
92
createGithubRefWithTag ("refs/tags/v1.1.1" , "12345678" ),
91
93
}
92
94
)
93
95
94
96
//nolint:errcheck
97
+ //gocyclo:ignore
95
98
func githubHandler (w http.ResponseWriter , r * http.Request ) {
96
99
if r .Header .Get ("Authorization" ) != "Bearer token" {
97
100
http .Error (w , "unauthorized" , http .StatusUnauthorized )
98
101
return
99
102
}
100
- if r .Method == "GET" && r .URL .Path == "/repos/owner/test-repo" {
101
- json .NewEncoder (w ).Encode (GITHUB_REPO )
103
+ if r .Method == http . MethodGet && r .URL .Path == "/repos/owner/test-repo" {
104
+ json .NewEncoder (w ).Encode (githubRepo )
102
105
return
103
106
}
104
- if r .Method == "GET" && strings .HasPrefix (r .URL .Path , "/repos/owner/test-repo/compare/" ) {
107
+
108
+ if r .Method == http .MethodGet && strings .HasPrefix (r .URL .Path , "/repos/owner/test-repo/compare/" ) {
105
109
li := strings .LastIndex (r .URL .Path , "/" )
106
110
shaRange := strings .Split (r .URL .Path [li + 1 :], "..." )
107
111
fromSha := shaRange [0 ]
108
112
toSha := shaRange [1 ]
109
113
start := 0
110
114
end := 0
111
- for i , commit := range GITHUB_COMMITS {
115
+ for i , commit := range githubCommits {
112
116
if commit .GetSHA () == toSha {
113
117
start = i
114
118
} else if commit .GetSHA () == fromSha {
115
119
end = i
116
120
}
117
121
}
118
- json .NewEncoder (w ).Encode (github.CommitsComparison {Commits : GITHUB_COMMITS [start :end ]})
122
+ json .NewEncoder (w ).Encode (github.CommitsComparison {Commits : githubCommits [start :end ]})
119
123
return
120
124
}
121
- if r .Method == "GET" && r .URL .Path == "/repos/owner/test-repo/commits" {
125
+ if r .Method == http . MethodGet && r .URL .Path == "/repos/owner/test-repo/commits" {
122
126
toSha := r .URL .Query ().Get ("sha" )
123
127
skip := 0
124
- for i , commit := range GITHUB_COMMITS {
128
+ for i , commit := range githubCommits {
125
129
if commit .GetSHA () == toSha {
126
130
skip = i
127
131
break
128
132
}
129
133
}
130
- json .NewEncoder (w ).Encode (GITHUB_COMMITS [skip :])
134
+ json .NewEncoder (w ).Encode (githubCommits [skip :])
131
135
return
132
136
}
133
- if r .Method == "GET" && r .URL .Path == "/repos/owner/test-repo/git/matching-refs/tags" {
134
- json .NewEncoder (w ).Encode (GITHUB_TAGS )
137
+ if r .Method == http . MethodGet && r .URL .Path == "/repos/owner/test-repo/git/matching-refs/tags" {
138
+ json .NewEncoder (w ).Encode (githubTags )
135
139
return
136
140
}
137
- if r .Method == "POST" && r .URL .Path == "/repos/owner/test-repo/git/refs" {
141
+ if r .Method == http . MethodPost && r .URL .Path == "/repos/owner/test-repo/git/refs" {
138
142
var data map [string ]string
139
143
json .NewDecoder (r .Body ).Decode (& data )
140
144
r .Body .Close ()
@@ -145,7 +149,7 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
145
149
fmt .Fprint (w , "{}" )
146
150
return
147
151
}
148
- if r .Method == "POST" && r .URL .Path == "/repos/owner/test-repo/releases" {
152
+ if r .Method == http . MethodPost && r .URL .Path == "/repos/owner/test-repo/releases" {
149
153
var data map [string ]string
150
154
json .NewDecoder (r .Body ).Decode (& data )
151
155
r .Body .Close ()
@@ -156,7 +160,7 @@ func githubHandler(w http.ResponseWriter, r *http.Request) {
156
160
fmt .Fprint (w , "{}" )
157
161
return
158
162
}
159
- if r .Method == "GET" && r .URL .Path == "/repos/owner/test-repo/git/tags/12345678" {
163
+ if r .Method == http . MethodGet && r .URL .Path == "/repos/owner/test-repo/git/tags/12345678" {
160
164
sha := "deadbeef"
161
165
json .NewEncoder (w ).Encode (github.Tag {
162
166
Object : & github.GitObject {SHA : & sha , Type : & commitType },
@@ -183,9 +187,9 @@ func TestGithubGetInfo(t *testing.T) {
183
187
defer ts .Close ()
184
188
repoInfo , err := repo .GetInfo ()
185
189
require .NoError (t , err )
186
- require .Equal (t , GITHUB_DEFAULTBRANCH , repoInfo .DefaultBranch )
187
- require .Equal (t , GITHUB_OWNER_LOGIN , repoInfo .Owner )
188
- require .Equal (t , GITHUB_REPO_NAME , repoInfo .Repo )
190
+ require .Equal (t , githubDefaultBranch , repoInfo .DefaultBranch )
191
+ require .Equal (t , githubOwnerLogin , repoInfo .Owner )
192
+ require .Equal (t , githubRepoName , repoInfo .Repo )
189
193
require .True (t , repoInfo .Private )
190
194
}
191
195
@@ -198,8 +202,8 @@ func TestGithubGetCommits(t *testing.T) {
198
202
199
203
for i , c := range commits {
200
204
idxOff := i + 1
201
- require .Equal (t , c .SHA , GITHUB_COMMITS [idxOff ].GetSHA ())
202
- require .Equal (t , c .RawMessage , GITHUB_COMMITS [idxOff ].Commit .GetMessage ())
205
+ require .Equal (t , c .SHA , githubCommits [idxOff ].GetSHA ())
206
+ require .Equal (t , c .RawMessage , githubCommits [idxOff ].Commit .GetMessage ())
203
207
}
204
208
}
205
209
@@ -213,8 +217,8 @@ func TestGithubGetCommitsWithCompare(t *testing.T) {
213
217
214
218
for i , c := range commits {
215
219
idxOff := i + 1
216
- require .Equal (t , c .SHA , GITHUB_COMMITS [idxOff ].GetSHA ())
217
- require .Equal (t , c .RawMessage , GITHUB_COMMITS [idxOff ].Commit .GetMessage ())
220
+ require .Equal (t , c .SHA , githubCommits [idxOff ].GetSHA ())
221
+ require .Equal (t , c .RawMessage , githubCommits [idxOff ].Commit .GetMessage ())
218
222
}
219
223
}
220
224
0 commit comments