From 9532f1b237bbd5f74f6a4ef137dbdcbd1c58d0ba Mon Sep 17 00:00:00 2001
From: Matthias Piepkorn <mpiepk@gmail.com>
Date: Sat, 08 Sep 2018 19:48:49 +0000
Subject: [PATCH] add travis script to check for Keycloak updates + PR them automatically

---
 .travis.yml |   29 ++++++++------
 update.sh   |   53 ++++++++++++++++++++++++++
 2 files changed, 69 insertions(+), 13 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index e442f4b..f79a31b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -16,21 +16,24 @@
   - KEYCLOAK_VERSION=4.1.0.Final
 
 before_install:
-  - docker pull jboss/keycloak:$KEYCLOAK_VERSION
+  - if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then docker pull jboss/keycloak:$KEYCLOAK_VERSION; fi
 
 script:
-  - mvn test package -B &&
-    mkdir target/docker &&
-    cp target/*.jar target/docker/keycloak-protocol-cas.jar &&
-    echo "FROM jboss/keycloak:$KEYCLOAK_VERSION" > target/docker/Dockerfile &&
-    echo "ADD keycloak-protocol-cas.jar /opt/jboss/keycloak/standalone/deployments/" >> target/docker/Dockerfile &&
-    cd target/docker && docker build -t doccrazy/keycloak-cas . && cd ../.. &&
-    docker run -d -p 127.0.0.1:8080:8080 --name keycloak -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin doccrazy/keycloak-cas && sleep 30 &&
-    docker logs keycloak &&
-    docker exec -t keycloak /opt/jboss/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080/auth --realm master --user admin --password admin &&
-    docker exec -t keycloak /opt/jboss/keycloak/bin/kcadm.sh create clients -r master -s clientId=test -s protocol=cas -s enabled=true -s publicClient=true -s 'redirectUris=["http://localhost/*"]' -s baseUrl=http://localhost -s adminUrl=http://localhost &&
-    docker exec -t keycloak /opt/jboss/keycloak/bin/kcadm.sh get serverinfo -r master --fields "providers(login-protocol(providers(cas)))" | grep cas &&
-    integrationTest/suite.sh
+  - if [ "$TRAVIS_EVENT_TYPE" != "cron" ]; then
+      mvn test package -B &&
+      mkdir target/docker &&
+      cp target/*.jar target/docker/keycloak-protocol-cas.jar &&
+      echo "FROM jboss/keycloak:$KEYCLOAK_VERSION" > target/docker/Dockerfile &&
+      echo "ADD keycloak-protocol-cas.jar /opt/jboss/keycloak/standalone/deployments/" >> target/docker/Dockerfile &&
+      cd target/docker && docker build -t doccrazy/keycloak-cas . && cd ../.. &&
+      docker run -d -p 127.0.0.1:8080:8080 --name keycloak -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin doccrazy/keycloak-cas && sleep 30 &&
+      docker logs keycloak &&
+      docker exec -t keycloak /opt/jboss/keycloak/bin/kcadm.sh config credentials --server http://localhost:8080/auth --realm master --user admin --password admin &&
+      docker exec -t keycloak /opt/jboss/keycloak/bin/kcadm.sh create clients -r master -s clientId=test -s protocol=cas -s enabled=true -s publicClient=true -s 'redirectUris=["http://localhost/*"]' -s baseUrl=http://localhost -s adminUrl=http://localhost &&
+      docker exec -t keycloak /opt/jboss/keycloak/bin/kcadm.sh get serverinfo -r master --fields "providers(login-protocol(providers(cas)))" | grep cas &&
+      integrationTest/suite.sh
+    ; fi
+  - if [ "$TRAVIS_EVENT_TYPE" = "cron" ]; then ./update.sh; fi
 
 before_deploy:
   - "mvn -DskipTests package"
diff --git a/update.sh b/update.sh
new file mode 100755
index 0000000..13216f5
--- /dev/null
+++ b/update.sh
@@ -0,0 +1,53 @@
+#!/bin/bash
+set -e
+
+function docker_tag_exists() {
+    TOKEN=$(curl --silent -f "https://auth.docker.io/token?service=registry.docker.io&scope=repository:$1:pull" | sed 's/.*"token":"\([^"]*\)".*/\1/')
+    REGISTRY_URL="https://registry-1.docker.io/v2/$1/manifests/$2"
+    curl -fsSLI -H "Authorization: Bearer $TOKEN" "$REGISTRY_URL" > /dev/null
+}
+
+setup_git() {
+    git config --global user.email "updater@travis-ci.org"
+    git config --global user.name "Updater Bot"
+    git remote add origin-auth https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git > /dev/null 2>&1
+}
+
+pull_request() {
+    curl -fsSL -H "Authorization: token ${GH_TOKEN}" -X POST -d "{\"head\":\"$1\",\"base\":\"master\",\"title\":\"$2\",\"body\":\"$3\"}" "https://api.github.com/repos/${TRAVIS_REPO_SLUG}/pulls"
+}
+
+
+KEYCLOAK_VERSION=$(mvn versions:display-property-updates -DincludeProperties=keycloak.version | grep "keycloak.version" | sed -n "s/.*->\s*\(.*\)\.Final$/\1/p")
+KEYCLOAK_VERSION_FULL=$KEYCLOAK_VERSION.Final
+if [ -z "$KEYCLOAK_VERSION" ]; then
+    echo "No Keycloak update found."
+    exit
+fi
+echo "Keycloak version $KEYCLOAK_VERSION available; updating..."
+
+BRANCH=feature/keycloak-update-$KEYCLOAK_VERSION
+if git ls-remote -q --exit-code origin $BRANCH; then
+    echo "Branch $BRANCH already exists."
+    exit
+fi
+
+if ! docker_tag_exists jboss/keycloak $KEYCLOAK_VERSION_FULL; then
+    echo "Docker image for Keycloak $KEYCLOAK_VERSION_FULL not found, not updating."
+    exit
+fi
+echo "Found updated docker image, proceeding"
+
+mvn versions:set -DnewVersion=$KEYCLOAK_VERSION -DgenerateBackupPoms=false
+sed -i "s/KEYCLOAK_VERSION=.*/KEYCLOAK_VERSION=$KEYCLOAK_VERSION_FULL/" .travis.yml
+
+setup_git
+git checkout -b $BRANCH
+git add pom.xml .travis.yml
+git commit -m "Update to Keycloak $KEYCLOAK_VERSION_FULL"
+git push --quiet --set-upstream origin-auth $BRANCH
+
+PR_TITLE="Update to Keycloak $KEYCLOAK_VERSION_FULL"
+PR_BODY="Updates Keycloak dependency, CI test image and project version for Keycloak release $KEYCLOAK_VERSION_FULL\\n\\n*(automated pull request after upstream release)*"
+pull_request $BRANCH "$PR_TITLE" "$PR_BODY"
+echo "Created pull request '$PR_TITLE'"

--
Gitblit v1.9.1