Skip to content

Instantly share code, notes, and snippets.

@PandelisZ
Last active May 8, 2019 14:54
Show Gist options
  • Save PandelisZ/14fb13ffd61e6d60319dc1d838dcb9ec to your computer and use it in GitHub Desktop.
Save PandelisZ/14fb13ffd61e6d60319dc1d838dcb9ec to your computer and use it in GitHub Desktop.
autoMergeRequest.sh
job:autoupdate:
image: php:7.3-cli
only:
- schedules
before_script:
# Install composer dependencies
- apt-get update -y >/dev/null && apt-get install -y libpng-dev git zip unzip >/dev/null
- docker-php-ext-install calendar gd >/dev/null
- php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
- php composer-setup.php >/dev/null
- php -r "unlink('composer-setup.php');"
- mv composer.phar /usr/local/bin/composer
- git config --global user.email '*****@substrakt.com'
- git config --global user.name 'Hero Bot'
script:
- export MERGE_DESCRIPTION=`composer update --no-ansi 2>&1 | php -r 'echo str_replace("\"", "", json_encode(file_get_contents("php://stdin")));'`
- if [ $(git diff | wc -l) -eq "0" ]; then echo "No updates found, exiting"; exit; fi
- git remote set-url origin https://gitlab-ci:${CI_PUSH_TOKEN}@gitlab.com/${CI_PROJECT_PATH}.git
- git checkout -B update
- "git commit -am 'UPDATE: Composer Update\n\n🤖'"
- git push --follow-tags origin update -f
- ./.gitlab/autoMergeRequest.sh
#!/usr/bin/env bash
# The description of our new MR, we want to remove the branch after the MR has
# been closed
BODY="{
\"id\": ${CI_PROJECT_ID},
\"source_branch\": \"update\",
\"target_branch\": \"master\",
\"remove_source_branch\": true,
\"title\": \"UPDATE: Composer Update\",
\"description\": \"\`\`\`sh\r\n
${MERGE_DESCRIPTION}
\`\`\`\"}";
# Require a list of all the merge request and take a look if there is already
# one with the same source branch
LISTMR=`curl --silent "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/merge_requests?state=opened" --header "PRIVATE-TOKEN:${CI_PUSH_TOKEN}"`;
COUNTBRANCHES=`echo ${LISTMR} | grep -o "\"source_branch\":\"update\"" | wc -l`;
# No MR found, let's create a new one
if [ ${COUNTBRANCHES} -eq "0" ]; then
RESPONSE=`echo ${BODY} | curl -d@- -X POST "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/merge_requests" \
--header "PRIVATE-TOKEN:${CI_PUSH_TOKEN}" \
--header "Content-Type: application/json; charset=utf-8" \
--silent`;
echo ${RESPONSE}
MERGE_URL=`echo ${RESPONSE} | grep -oP 'https*:\/\/[^"]*merge_requests/\d.'`;
curl -X POST --silent --data-urlencode "payload={
\"text\": \"Beep boop @php there are updates available for BaseProject.\n\n Please review and merge:\n${MERGE_URL}\",
\"mrkdwn\": true,
\"parse\": \"full\"
}" ${SLACK_WEBHOOK}?parse=full;
echo "Opened a new merge request: ${MERGE_URL} and posted in slack";
exit;
fi
echo "No new merge request opened";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment