Opening merge request in Gitlab via Curl

Asked

Viewed 259 times

3

Where I work, we use a variation of Gitflow in which, when a hotfix enters the product code, there is only the merge to the master which, when approved, we propagate to the develop through the branch master 2 develop (or simply m2d).

I’m making a requisition curl to open the merge request. Currently I have the following (already with the creation of the branch m2d as up to date as possible):

git fetch
git push origin +origin/master:refs/heads/m2d

curl --request POST https://gitlab.com/api/v4/projects/${project_id}/merge_requests --header "PRIVATE-TOKEN: ${mytoken}" --data "{
                \"id\": \"${project_id}\",
                \"title\": \"m2d\",
                \"source_branch\": \"m2d\",
                \"target_branch\": \"develop\"
        }"

However, I get the following output from curl:

{"error":"title is missing, source_branch is missing, target_branch is missing"}

I tried to follow the tips of that question no Stack Overflow international, putting the relevant information/JSON in the shipping body, but I couldn’t figure out what I’m doing wrong.

I can, using the same token, successfully make the following request:

curl https://gitlab.com/api/v4/projects/${project_id} --header "PRIVATE-TOKEN: ${mytoken}"
  • 1

    I don’t know if you’ve seen this their blog post, there’s an example there.

  • It’s not five minutes since I saw it, and I got it working. But if you want to write down the answer and show me what I forgot to send in the requisition, I’ll be grateful

  • Otherwise, tomorrow I answer

  • 1

    Can answer hehe I do not understand much what it says there rsrs, also I have no time to translate.

  • @Noobsaibot, I replied, I believe it was a little more than the publication of the site

  • 1

    Tip: whenever you are going to execute something that fails, use the parameter --verbose (abbreviated I believe it to be -v), being like this curl --verbose --request ...

Show 1 more comment

1 answer

1


As well indicated by the user @Noobsaibot in comment on the question, there is a publication on Gitlab blog just on that subject. Specific like that. Look at the title in free translation:

How to automatically create a new merge request in Gitlab with Gitlab-CI

There he places several examples of the use of the command curl.

After reading the aforementioned publication, I noticed that there are 2 paths to correct the command.

Solution using JSON

Simply inform the MIME-type of the sending of my content. Simply set the content-type in the header:

git fetch
git push origin +origin/master:refs/heads/m2d

curl https://gitlab.com/api/v4/projects/${project_id}/merge_requests --header "PRIVATE-TOKEN: ${mytoken}" \
  --header 'Content-Type: application/json' \
  --data "{
            \"id\": \"${project_id}\",
            \"title\": \"m2d\",
            \"source_branch\": \"m2d\",
            \"target_branch\": \"develop\"
    }"

I just added the seguitne header in the request: Content-Type: application/json

Solution using form

Basically, using the directive of curl for sending forms, the CLI flag --form.

curl https://gitlab.com/api/v4/projects/${project_id}/merge_requests \
    --header "PRIVATE-TOKEN: ${mytoken}" \
    --form "id=${project_id}" \
    --form 'title=m2d' \
    --form 'source_branch=m2d' \
    --form 'target_branch=develop'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.