Skip to content

Commit

Permalink
in: handle private fork
Browse files Browse the repository at this point in the history
if private fork is used, send note with info buld
limitation due to this issues
https://gitlab.com/gitlab-org/gitlab/-/issues/23648

Signed-off-by: Jonathan  Monnet <jmonnet@baylibre.com>
  • Loading branch information
Baylibrejmonnet committed May 11, 2022
1 parent eba7e30 commit 3b48abe
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ Updates the merge request's `merge_status` which displays nicely in the GitLab U
#### Parameters

* `repository`: The path of the repository of the merge request's source branch (required)
* `name_builder`: Useful in the case where the ressource can't access to private fork and update pipeline status, in this case the ressource will write a notes on MR like this:


Hi, i'm <name_builder> builder due to this [limitations](https://gitlab.com/gitlab-org/gitlab/-/issues/23648), i can't update pipeline status.<br>
*Build State*: **<status>**<br>
*Build URL*: [here](%s)`
Default: "Concourse"

* `status`: The new status of the merge request (required, can be either `pending`, `running`, `success`, `failed`, or `canceled`)
* `labels`(string[]): The labels you want add to your merge request
* `comment`: Add a comment for MR. Could be an object with `text`/`file` fields. If just the `file` or `text` is specified it is used to populate the field, if both `file` and `text` are specified then the file is substituted in to replace $FILE_CONTENT in the text.
Expand Down
3 changes: 2 additions & 1 deletion pkg/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func (source *Source) GetPipelineName() string {
if source.PipelineName != "" {
return source.PipelineName
} else {
return os.Getenv("BUILD_PIPELINE_NAME")
jobName := os.Getenv("BUILD_PIPELINE_NAME") + ":" + os.Getenv("BUILD_JOB_NAME")
return jobName
}

}
Expand Down
41 changes: 37 additions & 4 deletions pkg/out/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package out

import (
"encoding/json"
"github.com/DrummyFloyd/gitlab-merge-request-resource/pkg"
"github.com/xanzy/go-gitlab"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/DrummyFloyd/gitlab-merge-request-resource/pkg"
"github.com/xanzy/go-gitlab"
)

type Command struct {
Expand Down Expand Up @@ -76,7 +78,7 @@ func (command *Command) createNote(destination string, request Request, mr gitla

if body != "" {
options := gitlab.CreateMergeRequestNoteOptions{Body: &body}
_, _, err := command.client.Notes.CreateMergeRequestNote(mr.SourceProjectID, mr.IID, &options)
_, _, err := command.client.Notes.CreateMergeRequestNote(mr.TargetProjectID, mr.IID, &options)
if err != nil {
return err
}
Expand Down Expand Up @@ -105,7 +107,37 @@ func (command *Command) updateLabels(request Request, mr gitlab.MergeRequest) er
}
return nil
}

func (command *Command) updateStatusWithNote(request Request, mr gitlab.MergeRequest) error {

body := fmt.Sprintf(`Hi, i'm **%s** builder due to this [limitations](https://gitlab.com/gitlab-org/gitlab/-/issues/23648), i can't update pipeline status.<br>
*Build Name:* %s<br>
*Build State*: **%s**<br>
*Build URL*: [here](%s)`, request.Params.GetBuilderName(), os.Getenv("BUILD_JOB_NAME"), request.Params.Status, request.Source.GetTargetURL())
path := ".git/comment.json"
var noteRes *gitlab.Note
var exist bool
var noteID int
noteLists, _, err := command.client.Notes.ListMergeRequestNotes(mr.TargetProjectID, mr.IID, nil)
if err != nil {
fmt.Println("ListMergeRequestNotes error:", err)
} else {
for _, note := range noteLists {
if strings.Contains(note.Body, request.Source.GetTargetURL()) {
exist = true
noteID = note.ID
break
}
}
}
if exist {
noteRes, _, err = command.client.Notes.UpdateMergeRequestNote(mr.TargetProjectID, mr.IID, noteID, &gitlab.UpdateMergeRequestNoteOptions{Body: &body})
} else {
noteRes, _, err = command.client.Notes.CreateMergeRequestNote(mr.TargetProjectID, mr.IID, &gitlab.CreateMergeRequestNoteOptions{Body: &body})
}
file, _ := json.MarshalIndent(&noteRes, "", " ")
_ = ioutil.WriteFile(path, file, 0644)
return err
}
func (command *Command) updateCommitStatus(request Request, mr gitlab.MergeRequest) error {
if request.Params.Status != "" {
state := gitlab.BuildState(gitlab.BuildStateValue(request.Params.Status))
Expand All @@ -119,6 +151,7 @@ func (command *Command) updateCommitStatus(request Request, mr gitlab.MergeReque

_, _, err := command.client.Commits.SetCommitStatus(mr.SourceProjectID, mr.SHA, &options)
if err != nil {
err := command.updateStatusWithNote(request, mr)
return err
}
}
Expand Down
19 changes: 14 additions & 5 deletions pkg/out/models.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package out

import (
"github.com/DrummyFloyd/gitlab-merge-request-resource/pkg"
"io/ioutil"
"path"
"strings"

"github.com/DrummyFloyd/gitlab-merge-request-resource/pkg"
)

type Request struct {
Expand All @@ -18,17 +19,25 @@ type Response struct {
}

type Params struct {
Repository string `json:"repository"`
Status string `json:"status"`
Labels []string `json:"labels"`
Comment Comment `json:"comment"`
Repository string `json:"repository"`
Status string `json:"status"`
Labels []string `json:"labels"`
NameBuilder string `json:"name_builder"`
Comment Comment `json:"comment"`
}

type Comment struct {
FilePath string `json:"file"`
Text string `json:"text"`
}

func (params *Params) GetBuilderName() string {
if params.NameBuilder != "" {
return params.NameBuilder
}
return "Concourse"
}

func (comment Comment) ReadContent(folder string) (string, error) {
var (
commentContent string
Expand Down

0 comments on commit 3b48abe

Please sign in to comment.