@@ -211,51 +211,26 @@ func (c CommitPolicyConfig) IsEmpty() bool {
211211 return string (c1 ) == string (c2 )
212212}
213213
214- type gitEnv struct {
215- EnvName string
216- URL string
217- Token string
218- ProjectID string
219- PMRequestID string
220- }
221-
222- type gitEnvVars struct {
223- EnvName string
224- ApiUrl string
225- ApiToken string
226- ProjectID string
227- RequestID string
228- }
229-
230214var ErrGitEnvironment = errors .New ("git environment error" )
231215
232- func readGitEnvironment () (* gitEnv , error ) {
233- knownVars := []gitEnvVars {
234- {GITHUB , "GITHUB_API_URL" , "API_TOKEN" , "GITHUB_REPOSITORY" , "GITHUB_SHA" },
235- {GITLAB , "CI_API_V4_URL" , "CI_JOB_TOKEN" , "CI_MERGE_REQUEST_PROJECT_ID" , "CI_MERGE_REQUEST_ID" },
236- }
237-
238- for _ , vars := range knownVars {
239- url := os .Getenv (vars .ApiUrl )
240- token := os .Getenv (vars .ApiToken )
241- project := os .Getenv (vars .ProjectID )
242- request := os .Getenv (vars .RequestID )
216+ func readGitEnvironment () (string , error ) {
217+ url := os .Getenv ("GITHUB_API_URL" )
218+ if url != "" {
219+ log .Printf ("detected %s environment\n " , GITHUB )
220+ log .Printf ("using api url '%s'\n " , url )
243221
244- if ! (url == "" && token == "" && project == "" && request == "" ) {
245- log .Printf ("detected %s environment\n " , vars .EnvName )
222+ return GITHUB , nil
223+ } else {
224+ url = os .Getenv ("CI_API_V4_URL" )
225+ if url != "" {
226+ log .Printf ("detected %s environment\n " , GITLAB )
246227 log .Printf ("using api url '%s'\n " , url )
247228
248- return & gitEnv {
249- EnvName : vars .EnvName ,
250- URL : url ,
251- Token : token ,
252- ProjectID : project ,
253- PMRequestID : request ,
254- }, nil
229+ return GITLAB , nil
230+ } else {
231+ return "" , fmt .Errorf ("no suitable git environment variables found: %w" , ErrGitEnvironment )
255232 }
256233 }
257-
258- return nil , fmt .Errorf ("no suitable git environment variables found: %w" , ErrGitEnvironment )
259234}
260235
261236func LoadCommitPolicy (filename string ) (CommitPolicyConfig , error ) {
@@ -278,7 +253,12 @@ func LoadCommitPolicy(filename string) (CommitPolicyConfig, error) {
278253 return commitPolicy , nil
279254}
280255
281- func getGithubCommitSubjects (token string , repo string , sha string ) ([]string , error ) {
256+ func getGithubCommitSubjects () ([]string , error ) {
257+ token := os .Getenv ("API_TOKEN" )
258+ repo := os .Getenv ("GITHUB_REPOSITORY" )
259+ ref := os .Getenv ("GITHUB_REF" )
260+ event := os .Getenv ("GITHUB_EVENT_NAME" )
261+
282262 ctx := context .Background ()
283263
284264 ts := oauth2 .StaticTokenSource (
@@ -287,53 +267,62 @@ func getGithubCommitSubjects(token string, repo string, sha string) ([]string, e
287267 tc := oauth2 .NewClient (ctx , ts )
288268 githubClient := github .NewClient (tc )
289269
290- repoSlice := strings .SplitN (repo , "/" , 2 )
270+ if event == "pull_request" {
271+ repoSlice := strings .SplitN (repo , "/" , 2 )
272+ if len (repoSlice ) < 2 {
273+ return nil , fmt .Errorf ("error fetching owner and project from repo %s" , repo )
274+ }
275+ owner := repoSlice [0 ]
276+ project := repoSlice [1 ]
291277
292- prs , _ , err := githubClient .PullRequests .ListPullRequestsWithCommit (ctx , repoSlice [0 ], repoSlice [1 ], sha , & github.PullRequestListOptions {})
293- if err != nil {
294- return nil , fmt .Errorf ("error fetching prs for commit %s: %w" , sha , err )
295- }
278+ refSlice := strings .SplitN (ref , "/" , 4 )
279+ if len (refSlice ) < 3 {
280+ return nil , fmt .Errorf ("error fetching pr from ref %s" , ref )
281+ }
282+ prNo , err := strconv .Atoi (refSlice [2 ])
283+ if err != nil {
284+ return nil , fmt .Errorf ("Error fetching pr number from %s: %w" , refSlice [2 ], err )
285+ }
296286
297- subjects := []string {}
298- if len (prs ) > 0 {
299- // Check the latest PR with this commit
300- prNo := prs [0 ].GetNumber ()
301- commits , _ , err := githubClient .PullRequests .ListCommits (ctx , repoSlice [0 ], repoSlice [1 ], prNo , & github.ListOptions {})
287+ commits , _ , err := githubClient .PullRequests .ListCommits (ctx , owner , project , prNo , & github.ListOptions {})
302288 if err != nil {
303289 return nil , fmt .Errorf ("error fetching commits: %w" , err )
304290 }
291+
292+ subjects := []string {}
305293 for _ , c := range commits {
306294 l := strings .SplitN (c .Commit .GetMessage (), "\n " , 2 )
307295 if len (l ) > 0 {
308296 subjects = append (subjects , l [0 ])
309297 }
310298 }
299+ return subjects , nil
311300 } else {
312- // no PRs, event was a direct push, check only latest commit
313- c , _ , err := githubClient .Repositories .GetCommit (ctx , repoSlice [0 ], repoSlice [1 ], sha )
314- if err != nil {
315- return nil , fmt .Errorf ("error fetching commit %s: %w" , sha , err )
316- }
317- l := strings .SplitN (c .Commit .GetMessage (), "\n " , 2 )
318- if len (l ) > 0 {
319- subjects = append (subjects , l [0 ])
320- }
301+ return nil , fmt .Errorf ("unsupported event name: %s" , event )
321302 }
322-
323- return subjects , nil
324303}
325304
326- func gitGitlabCommitSubjects (url string , token string , project string , mr string ) ([]string , error ) {
327- gitlabClient , err := gitlab .NewClient (token , gitlab .WithBaseURL (url ))
305+ func gitGitlabCommitSubjects () ([]string , error ) {
306+ gitlab_url := os .Getenv ("CI_API_V4_URL" )
307+ token := os .Getenv ("API_TOKEN" )
308+ mri := os .Getenv ("CI_MERGE_REQUEST_IID" )
309+ project := os .Getenv ("CI_MERGE_REQUEST_PROJECT_ID" )
310+
311+ gitlabClient , err := gitlab .NewClient (token , gitlab .WithBaseURL (gitlab_url ))
328312 if err != nil {
329313 log .Fatalf ("Failed to create gitlab client: %v" , err )
330314 }
331315
332- mrID , err := strconv .Atoi (mr )
316+ mrIID , err := strconv .Atoi (mri )
317+ if err != nil {
318+ return nil , fmt .Errorf ("invalid merge request id %s" , mri )
319+ }
320+
321+ projectID , err := strconv .Atoi (project )
333322 if err != nil {
334- return nil , fmt .Errorf ("invalid merge request id %s" , mr )
323+ return nil , fmt .Errorf ("invalid project id %s" , project )
335324 }
336- commits , _ , err := gitlabClient .MergeRequests .GetMergeRequestCommits (project , mrID , & gitlab.GetMergeRequestCommitsOptions {})
325+ commits , _ , err := gitlabClient .MergeRequests .GetMergeRequestCommits (projectID , mrIID , & gitlab.GetMergeRequestCommitsOptions {})
337326 if err != nil {
338327 return nil , fmt .Errorf ("error fetching commits: %w" , err )
339328 }
@@ -349,13 +338,13 @@ func gitGitlabCommitSubjects(url string, token string, project string, mr string
349338 return subjects , nil
350339}
351340
352- func getCommitSubjects (repoEnv * gitEnv ) ([]string , error ) {
353- if repoEnv . EnvName == GITHUB {
354- return getGithubCommitSubjects (repoEnv . Token , repoEnv . ProjectID , repoEnv . PMRequestID )
355- } else if repoEnv . EnvName == GITLAB {
356- return gitGitlabCommitSubjects (repoEnv . URL , repoEnv . Token , repoEnv . ProjectID , repoEnv . PMRequestID )
341+ func getCommitSubjects (repoEnv string ) ([]string , error ) {
342+ if repoEnv == GITHUB {
343+ return getGithubCommitSubjects ()
344+ } else if repoEnv == GITLAB {
345+ return gitGitlabCommitSubjects ()
357346 }
358- return nil , fmt .Errorf ("unrecognized git environment %s" , repoEnv . EnvName )
347+ return nil , fmt .Errorf ("unrecognized git environment %s" , repoEnv )
359348}
360349
361350var ErrSubjectList = errors .New ("subjects contain errors" )
0 commit comments