Get MergeRequests ID by API
@@ -334,15 +334,23 @@ func gitlabService() (gitlabservice *reviewdog.GitLabMergeRequest, isPR bool, er
if err != nil {
return nil, isPR, err
}
- // TODO: support commit build
- if !isPR {
- return nil, isPR, nil
- }
+
client, err := gitlabClient(token)
if err != nil {
return nil, isPR, err
}
+ if !isPR {
+ ok, prNr, err := fetchMergeRequestIDFromCommit(client, g.SHA)
+ if err != nil {
+ return nil, isPR, err
+ }
+ if !ok {
+ return nil, false, nil // TODO: support commit build
+ }
+ g.PullRequest = prNr
+ }
+
gitlabservice, err = reviewdog.NewGitLabMergeRequest(client, g.Owner, g.Repo, g.PullRequest, g.SHA)
if err != nil {
return nil, isPR, err
@@ -350,6 +358,24 @@ func gitlabService() (gitlabservice *reviewdog.GitLabMergeRequest, isPR bool, er
return gitlabservice, isPR, nil
}
+func fetchMergeRequestIDFromCommit(cli *gitlab.Client, sha string) (ok bool, id int, err error) {
+ // https://docs.gitlab.com/ce/api/merge_requests.html#list-merge-requests
+ opt := &gitlab.ListMergeRequestsOptions{
+ State: gitlab.String("open"),
+ OrderBy: gitlab.String("updated_at"),
+ }
+ mrs, _, err := cli.MergeRequests.ListMergeRequests(opt)
+ if err != nil {
+ return false, 0, err
+ }
+ for _, mr := range mrs {
+ if mr.SHA == sha {
+ return true, mr.IID, nil
+ }
+ }
+ return false, 0, nil
+}
+
func gitlabClient(token string) (*gitlab.Client, error) {
client := gitlab.NewClient(nil, token)
baseURL, err := gitlabBaseURL()
