Skip to content

Instantly share code, notes, and snippets.

@dschulten
Created September 20, 2020 15:49
Show Gist options
  • Save dschulten/795ca9d962fea21393aa167b2047806e to your computer and use it in GitHub Desktop.
Save dschulten/795ca9d962fea21393aa167b2047806e to your computer and use it in GitHub Desktop.
Portably capture curl exit status, http status code and response body when creating gitlab merge request
#!/bin/sh
# see https://superuser.com/a/862395/1001553 if you can use bash and want to avoid temp file
# let curl fail on error and print response code while redirecting body to file
# read curl stdout into variable
http_status=$(curl --request POST \
--fail \
--silent --write-out '%{http_code}' \
--output response.json \
--url http://gitlab.dzbw.de/api/v4/projects/4560/merge_requests \
--header 'content-type: application/json' \
--header 'private-token: the-application-token' \
--data '{
"id": "4560",
"source_branch": "develop",
"target_branch": "master",
"title": "Deploye auf Stage Kons",
"assignee_id" : 129,
"allow_collaboration": true,
"squash": true
}')
exit_status="$?"
echo http_status: $http_status
if [ $http_status -eq 409 ]; then
echo "Merge request already exists, nothing to do"
exit_status=0
else
if [ $exit_status -eq 0 ]; then
echo "Merge request created"
else
echo "HTTP ${http_status}: Failed to create merge request"
fi
cat response.json
echo
fi
exit $exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment