Skip to content

Commit fd065cc

Browse files
authored
Merge pull request #81 from aquasecurity/saas-25129-add-gitlab-jenkins
feat(saas-25129): add option to get values from outside
2 parents 8041b3d + a432ae6 commit fd065cc

File tree

7 files changed

+39
-12
lines changed

7 files changed

+39
-12
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.18
55
require (
66
github.com/argonsecurity/go-environments v0.1.44
77
github.com/google/go-github/v44 v44.1.0
8+
github.com/rs/zerolog v1.33.0
89
github.com/samber/lo v1.37.0
910
github.com/urfave/cli/v2 v2.8.1
1011
golang.org/x/oauth2 v0.1.0
@@ -14,13 +15,16 @@ require (
1415
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
1516
github.com/golang/protobuf v1.5.2 // indirect
1617
github.com/google/go-querystring v1.1.0 // indirect
18+
github.com/mattn/go-colorable v0.1.13 // indirect
19+
github.com/mattn/go-isatty v0.0.19 // indirect
1720
github.com/pkg/errors v0.9.1 // indirect
1821
github.com/russross/blackfriday/v2 v2.1.0 // indirect
1922
github.com/thoas/go-funk v0.9.2 // indirect
2023
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
2124
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
2225
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d // indirect
2326
golang.org/x/net v0.1.0 // indirect
27+
golang.org/x/sys v0.12.0 // indirect
2428
google.golang.org/appengine v1.6.7 // indirect
2529
google.golang.org/protobuf v1.28.0 // indirect
2630
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
github.com/argonsecurity/go-environments v0.1.44 h1:iyQfMp6QS9qwjYsWr4N+ZMiwA5eKfSa/QHsM6pgG+2I=
22
github.com/argonsecurity/go-environments v0.1.44/go.mod h1:gf8wUiuP2DU6NN5nvfBy3h48R2JaPB7HAMag/DC0JUo=
3+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
34
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
45
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
56
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
67
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
79
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
810
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
911
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
@@ -15,11 +17,19 @@ github.com/google/go-github/v44 v44.1.0 h1:shWPaufgdhr+Ad4eo/pZv9ORTxFpsxPEPEuuX
1517
github.com/google/go-github/v44 v44.1.0/go.mod h1:iWn00mWcP6PRWHhXm0zuFJ8wbEjE5AGO5D5HXYM4zgw=
1618
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
1719
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
20+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
21+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
22+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
23+
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
24+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
1825
github.com/otiai10/copy v1.7.0 h1:hVoPiN+t+7d2nzzwMiDHPSOogsWAStewq3TwU05+clE=
1926
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2027
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2128
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2229
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
30+
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
31+
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
32+
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
2333
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
2434
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
2535
github.com/samber/lo v1.37.0 h1:XjVcB8g6tgUp8rsPsJ2CvhClfImrpL04YpQHXeHPhRw=
@@ -44,6 +54,10 @@ golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
4454
golang.org/x/oauth2 v0.1.0 h1:isLCZuhj4v+tYv7eskaN4v/TM+A1begWWgyVJDdl1+Y=
4555
golang.org/x/oauth2 v0.1.0/go.mod h1:G9FE4dLTsbXUu90h/Pf85g4w1D+SSAgR+q46nJZ8M4A=
4656
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
57+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
58+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
59+
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
60+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
4761
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4862
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
4963
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

pkg/app/commenter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Action(ctx *cli.Context) (err error) {
2828
case "gitlab":
2929
token := os.Getenv("GITLAB_TOKEN")
3030
r, err := gitlab.NewGitlab(
31-
token)
31+
token, "", "", "")
3232
if err != nil {
3333
return err
3434
}

pkg/commenter/azure/azure.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"net/http"
109
"os"
1110
"strconv"
@@ -141,7 +140,7 @@ func (c *Azure) WriteMultiLineComment(file, comment string, startLine, endLine i
141140
return err
142141
}
143142
if resp.StatusCode != http.StatusOK {
144-
b, _ := ioutil.ReadAll(resp.Body)
143+
b, _ := io.ReadAll(resp.Body)
145144
return fmt.Errorf("failed write azure line comment: %s", string(b))
146145
}
147146

pkg/commenter/bitbucket/bitbucket.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"net/http"
109
"os"
1110
"strconv"
@@ -115,7 +114,7 @@ func (c *Bitbucket) WriteLineComment(file, comment string, line int) error {
115114
}
116115

117116
if resp.StatusCode != http.StatusCreated {
118-
b, _ := ioutil.ReadAll(resp.Body)
117+
b, _ := io.ReadAll(resp.Body)
119118
return fmt.Errorf("failed write bitbucket line comment: %s", string(b))
120119
}
121120

pkg/commenter/gitlab/gitlab.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7-
"io/ioutil"
87
"net/http"
98
"net/url"
109
"os"
@@ -52,12 +51,12 @@ type Gitlab struct {
5251

5352
var lockFiles = []string{"package.json", "yarn.lock"}
5453

55-
func NewGitlab(token string) (b *Gitlab, err error) {
54+
func NewGitlab(token, apiUrl, repoName, mergeRequestID string) (b *Gitlab, err error) {
5655
return &Gitlab{
57-
ApiURL: os.Getenv("CI_API_V4_URL"),
56+
ApiURL: lo.Ternary(apiUrl == "", os.Getenv("CI_API_V4_URL"), apiUrl),
5857
Token: token,
59-
Repo: os.Getenv("CI_PROJECT_ID"),
60-
PrNumber: os.Getenv("CI_MERGE_REQUEST_IID"),
58+
Repo: lo.Ternary(repoName == "", os.Getenv("CI_PROJECT_ID"), repoName),
59+
PrNumber: lo.Ternary(mergeRequestID == "", os.Getenv("CI_MERGE_REQUEST_IID"), mergeRequestID),
6160
}, nil
6261
}
6362

@@ -138,7 +137,7 @@ func (c *Gitlab) WriteLineComment(file, comment string, line int) error {
138137
return nil
139138
}
140139
}
141-
b, _ := ioutil.ReadAll(resp.Body)
140+
b, _ := io.ReadAll(resp.Body)
142141
return fmt.Errorf("failed to write comment to file: %s, on line: %d, with gitlab error: %s", file, line, string(b))
143142
}
144143

@@ -184,7 +183,7 @@ func (c *Gitlab) getLatestVersion() (v Version, err error) {
184183
return v, err
185184
}
186185
if resp.StatusCode != http.StatusOK {
187-
b, _ := ioutil.ReadAll(resp.Body)
186+
b, _ := io.ReadAll(resp.Body)
188187
return v, fmt.Errorf("failed get gitlab PR version: %s", string(b))
189188
}
190189
defer func() { _ = resp.Body.Close() }()

pkg/commenter/jenkins/jenkins.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package jenkins
22

33
import (
44
"fmt"
5+
"net/url"
56
"os"
67
"strconv"
78
"strings"
89

910
"github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/github"
11+
"github.com/aquasecurity/go-git-pr-commenter/pkg/commenter/gitlab"
12+
1013
"github.com/argonsecurity/go-environments/enums"
1114
"github.com/argonsecurity/go-environments/environments/jenkins"
1215
env_utils "github.com/argonsecurity/go-environments/environments/utils"
@@ -66,6 +69,15 @@ func NewJenkins(baseRef string) (commenter.Repository, error) {
6669
return github.NewGithubServer(scmApiUrl, token, org, repoName, prNumberInt)
6770
}
6871

72+
} else if scmSource == enums.GitlabServer || scmSource == enums.Gitlab {
73+
_, org, repoName, _, err := env_utils.ParseDataFromCloneUrl(cloneUrl, scmApiUrl, scmSource)
74+
if err != nil {
75+
return nil, fmt.Errorf("failed parsing url with error: %s", err.Error())
76+
}
77+
token := os.Getenv("GITLAB_TOKEN")
78+
prNumber := os.Getenv("CHANGE_ID")
79+
80+
return gitlab.NewGitlab(token, scmApiUrl, url.PathEscape(fmt.Sprintf("%s/%s", org, repoName)), prNumber)
6981
}
7082
return nil, nil
7183
}

0 commit comments

Comments
 (0)