From aefe1ef53f3ad20ce59e549fc080a8c928d7f401 Mon Sep 17 00:00:00 2001
From: SuperEwald <code@superewald.net>
Date: Wed, 4 Oct 2023 13:33:11 +0200
Subject: [PATCH] feat: use CI_JOB_TOKEN if no PAT provided

---
 pkg/provider/gitlab.go | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/pkg/provider/gitlab.go b/pkg/provider/gitlab.go
index 75487da..1052048 100644
--- a/pkg/provider/gitlab.go
+++ b/pkg/provider/gitlab.go
@@ -30,10 +30,15 @@ func (repo *GitLabRepository) Init(config map[string]string) error {
 		gitlabBaseURL = os.Getenv("CI_SERVER_URL")
 	}
 
+	useJobToken := false
 	token := config["token"]
 	if token == "" {
 		token = os.Getenv("GITLAB_TOKEN")
 	}
+	if token == "" {
+		token = os.Getenv("CI_JOB_TOKEN")
+		useJobToken = true
+	}
 	if token == "" {
 		return errors.New("gitlab token missing")
 	}
@@ -62,11 +67,16 @@ func (repo *GitLabRepository) Init(config map[string]string) error {
 	repo.projectID = projectID
 	repo.branch = branch
 
-	var client *gitlab.Client
+	gitlabClientOpts := []gitlab.ClientOptionFunc{}
 	if gitlabBaseURL != "" {
-		client, err = gitlab.NewClient(token, gitlab.WithBaseURL(gitlabBaseURL))
+		gitlabClientOpts = append(gitlabClientOpts, gitlab.WithBaseURL(gitlabBaseURL))
+	}
+
+	var client *gitlab.Client
+	if useJobToken {
+		client, err = gitlab.NewJobClient(token, gitlabClientOpts...)
 	} else {
-		client, err = gitlab.NewClient(token)
+		client, err = gitlab.NewClient(token, gitlabClientOpts...)
 	}
 
 	if err != nil {
-- 
GitLab