Set squash boolean when creating a merge request from a url - gitlab

When creating a merge request in gitlab, you can add parameters to the url to have them automatically filled out in the MR form. For example, merge_request[source_branch]=new-feature will set the source branch as new-feature, and merge_request[title]=add+new+feature will set the MR's title to "add new feature". A final MR url could look something like:
https://my-gitlab/group/project/-/merge_requests/new?merge_request%5Bsource_branch%5D=new-feature&merge_request%5Btarget_branch%5D=master&merge_request%5Btitle%5D=add+new+feature
One setting that I'd like to be able to automatically set via the url is whether or not to squash commits upon merge. According to the gitlab API, one of the available params for an MR POST request is a boolean squash. However, this doesn't seem to apply to a URL request. When I add &merge_request%5Bsquash%5D=false to my URL, the MR is created with the default squash enabled.
Is there a way to get the effect I'm looking for using the merge_requests/new?... URL? Am I formatting something wrong?

The link you posted is a link to Gitlab page (not Gitlab API) which redirects you to create MR page with preset source and target branch and it creates MR with your current project settings.
To make all your MRs have Squash option set by default go to:
Project -> Settings -> General -> Merge requests and in section Squash commits when merging set Encourage.
Using Gitlab API you can create MR with proper settings regardless of project settings.
I tried the API with Squash parameter on public Gitlab and it works like a charm:
curl -X POST \
--fail \
--header 'PRIVATE-TOKEN: <YOUR PERSONAL API TOKEN>' \
-F source_branch=<source branch name> \
-F target_branch=master \
-F title=FromCurlApi \
-F squash=true \
https://gitlab.com/api/v4/projects/14385369/merge_requests

Related

Continuous deployment gitlab

Line of my .gitlab-ci.yml
IID=$(curl --verbose --request GET --header "Content-Type: application/json" --header "PRIVATE-TOKEN: ${CI_PUSH_TOKEN}" ${APP_REPO_URL}/merge_requests?author_id=xxxxxxxx\&search=${CI_COMMIT_SHORT_SHA} | jq '.[0].iid')
output jq: error (at :0): Cannot index object with number
Please refer to the documentation on Merge requests API and How to use the API to construct a properly formed cURL request with GitLab API before piping the result into jq.
There are a few things going on with your query.
First of all, according to the documentation, the /merge_requests endpoint will
Get all merge requests the authenticated user has access to.
This means that APP_REPO_URL needs to be the GitLab instance URL followed by /api/v4 and not the URL of the specific project.
curl "https://gitlab.example.com/api/v4/merge_requests"
If you need all merge requests for a given project, then you can use
curl "https://gitlab.example.com/api/v4/projects/:id/merge_requests"
where :id is the id of your project. See more at List project merge requests
Then, the search attribute is expecting either a title or a description and not a commit SHA:
Search merge requests against their title and description

GitLab api to know the target branch in a merge request

Need to know the target branch from the merge request created, i tried
curl -X GET http://gitlab.com/api/v4/projects/4889/merge_requests/25?target_branch&private_token=************
getting message {"message":"404 Project Not Found"} all the time.

Incoming webhook trigger for Azure Devops pipeline : "Cannot find webhook for the given webHookId"

I try to setup a incoming webhook in a project to trigger a yaml pipeline.
Setup service connection with incoming webhook.
Added yaml snippet to pipeline like this:
resources:
webhooks:
- webhook: testtrigger ### Webhook alias
connection: testconnection ### Incoming webhook service connection
When calling the url via curl : curl -X POST 'https://dev.azure.com/<my org>/_apis/public/distributedtask/webhooks/testtrigger?api-version=6.0-preview' -d '{ "tags": {} }' -H "Content-Type: application/json", I always get the error message :
{"$id":"1","innerException":null,"message":"Cannot find webhook for the given webHookId testtrigger. Try enabling CD trigger for this artifact.","typeName":"Microsoft.TeamFoundation.DistributedTask.Pipelines.Artifacts.WebHooks.WebHookException, Microsoft.TeamFoundation.DistributedTask.Orchestration.Server","typeKey":"WebHookException","errorCode":0,"eventId":3000}
Tried with different names on triggers/connection but nothing worked.
Any ideas what I am missing ?
Echoing #FrankBaumfalk's comment, you need to ensure the pipeline's default branch is the same as the one that youre developing on. (You may be like me, developing and testing this yaml on a branch of the same repo it is built for.)
To indicate your default branch:
Edit yer Pipeline
Choose Triggers from the kabob menu
Choose the YAML tab, see below 👇
Notice the default branch field; save
Also, yes the error referring to webHookId is the WebHook Name in the Incoming WebHook in Services Connections. Yay consistent naming.
Based on my test, I could reproduce this issue.
The root cause of this issue is that you are using the incorrect webhook name.
To find the correct webhook name, you could navigate to Project Settings -> Service Connection -> testconnection incoming webhook.
Or you could go to the target Webhook and check the request url:
https://dev.azure.com/<ADO Organization>/_apis/public/distributedtask/webhooks/<WebHook Name>?api-version=6.0-preview
The testtrigger in yaml pipeline is the name of the webhook resource. This is a custom name instead of the correct webhook name.

Automatically close outdated GitLab merge request

Is there any option to automatically close GitLab merge request after 1 day?
Manually close all the merge requests is not an option since there are many each day.
Thanks in advance
You can create a custom script that uses GitLab API either directly or via client
You'll need to generate private token with Scope: api
Example
Fetch Merge Requests updated before a specific date:
$ PRIVATE_TOKEN=****
$ curl -s -X GET -H "PRIVATE-TOKEN: $PRIVATE_TOKEN" \
"https://gitlab.com/api/v4/merge_requests?state=opened&view=simple&updated_before=2020-02-01T14:09:18.679Z" |\
jq '.[].id'
34520388
33038903
20988416
(put your token instead of ****)
Call Update request for each id in a loop, specifying state_event=close

Gitlab API adding members to a project

I'm experiencing errors when trying to add users to a Gitlab project using the Gitlab API. I'm using Gitlab version 8.11.0 on CentOS 6.8 and I'm not sure if it is a bug or something I am doing wrong.
The following works fine and gives me a list of project members:
curl --header "PRIVATE-TOKEN: top_secret" "https://thegitlabserver.com/api/v3/projects/1/members"
but when trying to add a members with the following, I get an error message:
curl --request POST --header "PRIVATE-TOKEN: top_secret" "https://thegitlabserver.com/api/v3/projects/1/members/myusername?access_level=30"
{"error":"405 Not Allowed"}
Using the web interface I am able to add the user to the project.
It looks like the Gitlab API documentation was incorrect.
As it's a create action, you should send both the user ID and access levels as named parameters (like you would for creating any other resource).
So something like: https://thegitlabserver.com/api/v3/projects/1/members/?user_id=299&access_level=40

Resources