Merging a merge request with API and waiting for its pipeline to finish in Gitlab Ci - gitlab

i am trying to write a ci.yml to automatically create a merge request and merge it and wait until pipeline succeed. In order to do that, i need the pipeline Id of the merge request. but the only pipeline Id that i recieve from the response of merge call is .head_pipeline.id which is the last pipeline id of the project not the one after merging.
i need the actual pipeline id to poll the status. if the status is "success", job will be ended.
i have tried :
`
PIPELINE_ID=`curl --silent -X PUT "${GITLAB_BASE_URL}/${SERVICE_PROJECT_ID}/merge_requests/${MERGE_REQUEST_IID}/merge" --header "Private-Token: ${PRIVATE_TOKEN}" --header "Content-Type: application/json" | jq .head_pipeline.id`
`

here is the solution, that i found. After merge, we can find "merge_commit_sha" as an attribute in rspBody. then we should get all pipelines and find the one that match with that attribute.
MERGE_COMMIT_SHA=`curl --silent -X GET "${GITLAB_BASE_URL}/${SERVICE_PROJECT_ID}/merge_requests/${MERGE_REQUEST_IID}" --header "Private-Token: ${PRIVATE_TOKEN}" --header "Content-Type: application/json" | jq -r .merge_commit_sha`
PIPELINE_ID=$(curl --silent -X GET "${GITLAB_BASE_URL}/${SERVICE_PROJECT_ID}/pipelines" --header "Private-Token: ${PRIVATE_TOKEN}" | jq '.[] | select(.ref=='\"$TARGET_BRANCH\"') | select (.sha=='\"$MERGE_COMMIT_SHA\"') | .id')

Related

How to chain curl such that the output of one become another's input?

Could someone tell me how to pass the output of one CURL GET to another CURL POST? I mean something like this:
curl --header "Content-Type: application/json" --request POST --data "{\"specification\": curl --header "Content-Type: application/json" --request GET "https://user:pass#anotherhost"}" "https://user:pass#localhost"
Use jq to craft JSON in the shell.
response="$(curl --header "Content-Type: application/json" --request GET "https://user:pass#anotherhost")"
data="$(jq "{specification: .}" <<< "$response")"
curl --header "Content-Type: application/json" --request POST --data "$data" "https://user:pass#localhost"
Keep in mind, that passwords in command line arguments are public to the host.
It looks like you need to use command substitution. The result would be something like this.
curl --header "Content-Type: application/json" --request POST --data "{\"specification\": $(curl --header "Content-Type: application/json" --request GET "https://user:pass#anotherhost")}" "https://user:pass#localhost"
$() is used for command substitution and it invokes a subshell. The command in the parentheses (a.k.a. round brackets) of $() is executed in a subshell and the output is then placed in the original command.
So the curl GET will be executed in a subshell and the result will be passed to the curl POST
You can read more about it at the link below:
https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html

What are the REST calls to import a CSV file into Shopware 6?

We saw https://developer.shopware.com/docs/guides/integrations-api but couldn't find a detailed documentation of the APIs.
EDIT: I found https://my-shop-host.example/api/v2/_info/swagger.html which describes all available APIs and import-export-file endpoints, but it is not clearly described how to use them (and which ones to use).
I believe we need to call (extracted from the admin panel work flow)
/api/v2/_action/import-export/prepare
and then
/api/v2/_action/import-export/process
to trigger an import.
But how are the files uploaded?
Is there an easier way, for example in one call?
I start with the authentication
curl 'http://example.com/api/oauth/token'\
-H 'Accept: pplication/vnd.api+json'\
-H 'Content-Type: application/json' \
--data '{
"grant_type": "client_credentials",
"client_id": "<my client ID from the integration>",
"client_secret": "<my secret>"
}' | jq .access_token
This returns and auth token which is valid for 10 minutes.
I store it to the variable $B
Next I upload the file and set the expiry date to 10 days in the future:
curl 'http://example.com/api/v2/_action/import-export/prepare' \
-H 'Accept: application/vnd.api+json' \
-H "Authorization: Bearer $B"
--form "file=#\"products.csv\"" --form "profileId=d4ec3999a33242a690ca5c213a7145cd" --form "expireDate=+10 days" | jq .log.id
This returns a log.id field
This field must be passed to the process call
curl 'http://example.com/api/v2/_action/import-export/process' \
-H 'Accept: application/vnd.api+json' \
-H 'Content-type: application/json' \
-H "Authorization: Bearer $B" --data '{"logId":"1410eaa1ca434744a8c211b60f65a3c5","offset":0}'
EDIT: Each chunk has to be triggered separately by using the last returned offset and pass it again to the process call.

Gitlab-ci won't replace my variables in curl

I have this script in my gitlab yml file:
script:
- "curl -X POST -H 'Content-type: application/json' --data '{\"tag\":\"${CI_COMMIT_TAG}\", \"projectId\":\"${PROJECT_ID}\"}' http://localhost:1337/slack"**
Let's say when I create a tag named 'testing-v1' on gitlab, the variables $CI_COMMIT_TAG should be 'testing-v1', but instead it is showing as it is.
It's not replacing.
If I hard code the tag name and project id like this
- "curl -X POST -H 'Content-type: application/json' --data '{\"tag\":\"testing-v1\", \"projectId\":\"1111111\"}' http://localhost:1337/slack"**
It's working.
If you copy-paste your curl line (without the first and last double-quote) in shellcheck.net, you will be pointed to SC2016
Expressions don't expand in single quotes, use double quotes for that.
Single quotes prevent expansion of everything, including variables and command substitution.
If you want to use the values of variables and such, use double quotes instead.
Note that if you have other items that needs single quoting, you can use both in a single word:
echo '$1 USD is '"$rate GBP"
See an example of a working gitlab-ci.yml with curl here:
image: ruby:2.3
stages:
- clear-cache
CLOUDFLARE_URL: https://api.cloudflare.com/client/v4/zones/$zone/purge_cache
EMAIL: "X-Auth-Email: $email"
AUTH: "X-Auth-Key: $key"
CONTENT: 'Content-Type: application/json'
clear-cache:
stage: clear-cache
script:
- curl -X POST "${CLOUDFLARE_URL}" -H "${EMAIL}" -H "${AUTH}" -H "${CONTENT}" --data '{"purge_everything":true}'
only:
- master

Can you curl files from GitLab?

Is it possible to download a file from GitLab using the API? I am using CentOS 6 commandline. The documentation for the API says "Get file from repository" but it is only to get the metadata and not the file itself. The example they give is:
curl --request GET --header 'PRIVATE-TOKEN: <your_access_token>' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/test%2Epy/raw?ref=master'
If I use the raw option, it gives me the contents of the file, but it saves the name with as test%2Epy/raw?ref=master
How do I get it to save as test.py?
Append > test.py to curl as below:
curl --request GET --header 'PRIVATE-TOKEN: ' 'https://gitlab.example.com/api/v4/projects/13083/repository/files/test%2Epy/raw?ref=master' > test.py
It's also possible to use the group and project name instead of the project-id:
response=$(curl "$GITLAB_URL/api/v4/projects/<group>%2F<project>/repository/files/<folder>%2Ftest%2E.py/raw?ref=master" \
--silent \
-o "test.py" \
--header "PRIVATE-TOKEN: $GITLAB_TOKEN")
if [[ $response == 4* ]] || [[ $response == 5* ]]; then
echo ERROR - Http status: "$response"
exit 1
fi
It's important to URL encode the group + project path and the file path as well.

Gitlab API, Merge When Build Succeeds

I'd like to set the merge_when_build_succeeds attribute of a merge request with the Gitlab API. The docs says, that when the optional merged_when_build_succeeds parameter is true, the MR'll be accepted only after the build succeeds.
How should I provide this merged_when_build_succeeds parameter to the API? I tried the following curl commands without any success:
# In the request's body
vilmosnagy#vnagy-dell:~$ curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" -d merged_when_build_succeeds=true "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge"
{"message":"405 Method Not Allowed"}
# In the URL
vilmosnagy#vnagy-dell:~$ curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge?merged_when_build_succeeds=true"
{"message":"405 Method Not Allowed"}
# I tried this also, maybe the __merged__ is a mistake in the docs
vilmosnagy#vnagy-dell:~$ curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" -d merge_when_build_succeeds=true "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge"
{"message":"405 Method Not Allowed"}
# In the URL
vilmosnagy#vnagy-dell:~$ curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge?merge_when_build_succeeds=true"
{"message":"405 Method Not Allowed"}
# Some other CURL parameters
vilmosnagy#vnagy-dell:~$ curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" -H "Content-Type: multipart/form-data;" -F "merge_when_build_succeeds=true" "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge"
{"message":"405 Method Not Allowed"}
# Some other CURL parameters
vilmosnagy#vnagy-dell:~$ curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" -H "Content-Type: multipart/form-data;" -F "merged_when_build_succeeds=true" "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge"
{"message":"405 Method Not Allowed"}
The new feature, 'Only allow merge requests to be merged if the build succeeds' is turned on. When I turn off this feature any call of the previous calls merge the MR before the build finished, see: https://snag.gy/06nOEm.jpg
The private token, and the URL is right. I can query the MR's info:
vilmosnagy#vnagy-dell:~$ curl -X GET --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" "http://localhost:8080/api/v3/projects/1/merge_requests/9"
{"id":9,"iid":9,"project_id":1,"title":"enters","description":"","state":"opened","created_at":"2016-06-29T15:36:15.235Z","updated_at":"2016-06-29T15:36:15.235Z","target_branch":"master","source_branch":"features/long_build_05","upvotes":0,"downvotes":0,"author":{"name":"Vilmos Nagy","username":"vilmos.nagy","id":2,"state":"active","avatar_url":"http://www.gravatar.com/avatar/4f94d9571ec83f42a85651291296f503?s=80\u0026d=identicon","web_url":"http://172.21.0.3/u/vilmos.nagy"},"assignee":null,"source_project_id":2,"target_project_id":1,"labels":[],"work_in_progress":false,"milestone":null,"merge_when_build_succeeds":false,"merge_status":"can_be_merged","subscribed":false,"user_notes_count":0}
And I can accept the MR after the build succeeds:
vilmosnagy#vnagy-dell:~$ curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge"
{"id":9,"iid":9,"project_id":1,"title":"enters","description":"","state":"merged","created_at":"2016-06-29T15:36:15.235Z","updated_at":"2016-06-29T16:13:41.242Z","target_branch":"master","source_branch":"features/long_build_05","upvotes":0,"downvotes":0,"author":{"name":"Vilmos Nagy","username":"vilmos.nagy","id":2,"state":"active","avatar_url":"http://www.gravatar.com/avatar/4f94d9571ec83f42a85651291296f503?s=80\u0026d=identicon","web_url":"http://172.21.0.3/u/vilmos.nagy"},"assignee":null,"source_project_id":2,"target_project_id":1,"labels":[],"work_in_progress":false,"milestone":null,"merge_when_build_succeeds":false,"merge_status":"can_be_merged","subscribed":true,"user_notes_count":0}
Could you help me, and give me the correct call of the given API?
Thanks!
Vilmos
It seems the documentation is wrong, the parameter is actually called merge_when_build_succeeds (without the "d" in "merge").
curl -X PUT --header "PRIVATE-TOKEN: zvzK7CNzx9WviV5iChyg" -d merge_when_build_succeeds=true "http://localhost:8080/api/v3/projects/1/merge_requests/9/merge" should work ("405 Method Not Allowed" was probably because of something else, like the MR was already merged or had conflicts).
I created an issue you can follow if you want to know when the documentation is fixed: https://gitlab.com/gitlab-org/gitlab-ce/issues/19448

Resources