Skip to content

Commit f8ea51c

Browse files
committed
MINOR: allow reading tokens from different env variables
1 parent 7e7a87e commit f8ea51c

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

.aspell.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ allowed:
3737
- english
3838
- lang
3939
- junit
40+
- deps
41+
- alternative

check.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func LoadCommitPolicy(filename string) (CommitPolicyConfig, error) {
284284
}
285285

286286
func getGithubCommitData(junitSuite junit.Interface) ([]string, []string, []map[string]string, error) {
287-
token := os.Getenv("API_TOKEN")
287+
token := getAPIToken("GITHUB_TOKEN")
288288
repo := os.Getenv("GITHUB_REPOSITORY")
289289
ref := os.Getenv("GITHUB_REF")
290290
event := os.Getenv("GITHUB_EVENT_NAME")
@@ -484,12 +484,12 @@ func cleanGitPatch(patch string) string {
484484
}
485485

486486
func getGitlabCommitData(junitSuite junit.Interface) ([]string, []string, []map[string]string, error) {
487-
gitlab_url := os.Getenv("CI_API_V4_URL")
488-
token := os.Getenv("API_TOKEN")
487+
gitlabURL := os.Getenv("CI_API_V4_URL")
488+
token := getAPIToken("GITLAB_TOKEN")
489489
mri := os.Getenv("CI_MERGE_REQUEST_IID")
490490
project := os.Getenv("CI_MERGE_REQUEST_PROJECT_ID")
491491

492-
gitlabClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(gitlab_url))
492+
gitlabClient, err := gitlab.NewClient(token, gitlab.WithBaseURL(gitlabURL))
493493
if err != nil {
494494
junitSuite.AddMessageFailed("", "failed to create gitlab client", err.Error())
495495
return nil, nil, nil, fmt.Errorf("failed to create gitlab client: %w", err)
@@ -506,7 +506,7 @@ func getGitlabCommitData(junitSuite junit.Interface) ([]string, []string, []map[
506506
junitSuite.AddMessageFailed("", "invalid project id", err.Error())
507507
return nil, nil, nil, fmt.Errorf("invalid project id %s", project)
508508
}
509-
commits, _, err := gitlabClient.MergeRequests.GetMergeRequestCommits(projectID, mrIID, &gitlab.GetMergeRequestCommitsOptions{})
509+
commits, _, err := gitlabClient.MergeRequests.GetMergeRequestCommits(projectID, int64(mrIID), &gitlab.GetMergeRequestCommitsOptions{})
510510
if err != nil {
511511
junitSuite.AddMessageFailed("", "error fetching commits", err.Error())
512512
return nil, nil, nil, fmt.Errorf("error fetching commits: %w", err)
@@ -528,7 +528,7 @@ func getGitlabCommitData(junitSuite junit.Interface) ([]string, []string, []map[
528528
log.Printf("detected message %s from commit %s", l[0], hash)
529529
subjects = append(subjects, l[0])
530530
messages = append(messages, c.Message)
531-
diff, _, err := gitlabClient.MergeRequests.ListMergeRequestDiffs(projectID, mrIID, &gitlab.ListMergeRequestDiffsOptions{})
531+
diff, _, err := gitlabClient.MergeRequests.ListMergeRequestDiffs(projectID, int64(mrIID), &gitlab.ListMergeRequestDiffsOptions{})
532532
if err != nil {
533533
junitSuite.AddMessageFailed("", "error fetching commit changes", err.Error())
534534
return nil, nil, nil, fmt.Errorf("error fetching commit changes: %w", err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/haproxytech/check-commit/v5
22

3-
go 1.24.0
3+
go 1.25.0
44

55
require (
66
github.com/fatih/camelcase v1.0.0

token.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import "os"
4+
5+
func getAPIToken(alterative string) string {
6+
token := os.Getenv("API_TOKEN")
7+
if token == "" {
8+
token = os.Getenv(alterative)
9+
}
10+
11+
return token
12+
}

0 commit comments

Comments
 (0)