Skip to content

Commit

Permalink
fix: use merge request internal ID for GitLab API (#463)
Browse files Browse the repository at this point in the history
There are two types of ID's for merge request, `CI_MERGE_REQUEST_ID` for
instance-level and `CI_MERGE_REQUEST_IID` for project-level.

According to GitLab's predefined variables documentation, 
> The project-level IID...is the number used in the merge request URL
  • Loading branch information
gjonathanhong authored Dec 17, 2024
1 parent 38cb11a commit 92618eb
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions pkg/platform/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ type gitLabConfig struct {
GuardianGitLabToken string
GitLabBaseURL string

GitLabProjectID int
GitLabMergeRequestID int
GitLabProjectID int
GitLabMergeRequestIID int
}

type gitLabPredefinedConfig struct {
CIJobToken string
CIServerHost string
CIProjectID int
CIMergeRequestID int
CIJobToken string
CIServerHost string
CIProjectID int
// The merge request IID is the number used in the GitLab API, and not ID.
// See https://docs.gitlab.com/ee/ci/variables/predefined_variables.html.
CIMergeRequestIID int
}

// Load retrieves the predefined GitLab CI/CD variables from environment. See
Expand All @@ -92,8 +94,8 @@ func (c *gitLabPredefinedConfig) Load() {
c.CIProjectID = v
}

if v, err := strconv.Atoi(os.Getenv("CI_MERGE_REQUEST_ID")); err == nil {
c.CIMergeRequestID = v
if v, err := strconv.Atoi(os.Getenv("CI_MERGE_REQUEST_IID")); err == nil {
c.CIMergeRequestIID = v
}
}

Expand Down Expand Up @@ -132,11 +134,11 @@ func (c *gitLabConfig) RegisterFlags(set *cli.FlagSet) {
})

f.IntVar(&cli.IntVar{
Name: "gitlab-merge-request-id",
EnvVar: "GITLAB_MERGE_REQUEST_ID",
Target: &c.GitLabMergeRequestID,
Default: cfgDefaults.CIMergeRequestID,
Usage: "The GitLab merge request ID.",
Name: "gitlab-merge-request-iid",
EnvVar: "GITLAB_MERGE_REQUEST_IID",
Target: &c.GitLabMergeRequestIID,
Default: cfgDefaults.CIMergeRequestIID,
Usage: "The GitLab project-level merge request internal ID.",
Hidden: true,
})
}
Expand Down Expand Up @@ -210,7 +212,7 @@ func (g *GitLab) ListReports(ctx context.Context, opts *ListReportsOptions) (*Li
var pagination *Pagination

if err := g.withRetries(ctx, func(ctx context.Context) error {
notes, resp, err := g.client.Notes.ListMergeRequestNotes(g.cfg.GitLabProjectID, g.cfg.GitLabMergeRequestID, opts.GitLab)
notes, resp, err := g.client.Notes.ListMergeRequestNotes(g.cfg.GitLabProjectID, g.cfg.GitLabMergeRequestIID, opts.GitLab)
if err != nil {
if resp != nil {
if _, ok := gitLabIgnoredStatusCodes[resp.StatusCode]; !ok {
Expand Down Expand Up @@ -246,7 +248,7 @@ func (g *GitLab) DeleteReport(ctx context.Context, id any) error {
}

if err := g.withRetries(ctx, func(ctx context.Context) error {
resp, err := g.client.Notes.DeleteMergeRequestNote(g.cfg.GitLabProjectID, g.cfg.GitLabMergeRequestID, noteID)
resp, err := g.client.Notes.DeleteMergeRequestNote(g.cfg.GitLabProjectID, g.cfg.GitLabMergeRequestIID, noteID)
if err != nil {
if resp != nil {
if _, ok := gitLabIgnoredStatusCodes[resp.StatusCode]; !ok {
Expand Down Expand Up @@ -345,9 +347,9 @@ func (g *GitLab) createMergeRequestNote(ctx context.Context, msg string) error {

logger.DebugContext(ctx, "creating merge request note",
"project", g.cfg.GitLabProjectID,
"merge_request", g.cfg.GitLabMergeRequestID)
"merge_request", g.cfg.GitLabMergeRequestIID)

if _, _, err := g.client.Notes.CreateMergeRequestNote(g.cfg.GitLabProjectID, g.cfg.GitLabMergeRequestID, &gitlab.CreateMergeRequestNoteOptions{
if _, _, err := g.client.Notes.CreateMergeRequestNote(g.cfg.GitLabProjectID, g.cfg.GitLabMergeRequestIID, &gitlab.CreateMergeRequestNoteOptions{
Body: &msg,
}); err != nil {
return fmt.Errorf("failed to create merge request note: %w", err)
Expand All @@ -362,7 +364,7 @@ func validateGitLabReporterInputs(cfg *gitLabConfig) error {
merr = errors.Join(merr, fmt.Errorf("gitlab project id is required"))
}

if cfg.GitLabMergeRequestID <= 0 {
if cfg.GitLabMergeRequestIID <= 0 {
merr = errors.Join(merr, fmt.Errorf("gitlab merge request id is required"))
}

Expand Down

0 comments on commit 92618eb

Please sign in to comment.