From 4784024c00c371492d63a9b63f5087c2c7c2276d Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sun, 10 May 2020 22:40:36 +0000
Subject: [PATCH] Use GitHub Actions to automatically create PR on Keycloak release

---
 /dev/null                         |   51 -----------------
 .github/workflows/update-deps.yml |  101 +++++++++++++++++++++++++++++++++
 2 files changed, 101 insertions(+), 51 deletions(-)

diff --git a/.github/workflows/update-deps.yml b/.github/workflows/update-deps.yml
new file mode 100644
index 0000000..704a7ae
--- /dev/null
+++ b/.github/workflows/update-deps.yml
@@ -0,0 +1,101 @@
+on:
+  schedule:
+    - cron:  '41 8 * * *'
+
+name: Update dependencies
+
+jobs:
+  update:
+    name: Update dependencies
+    runs-on: ubuntu-latest
+    steps:
+      - id: checkout
+        name: Checkout code
+        uses: actions/checkout@v2
+
+      - id: java
+        name: Install Java and Maven
+        uses: actions/setup-java@v1
+        with:
+          java-version: 8
+
+      - id: update_keycloak
+        name: Update Keycloak
+        run: |
+          mvn -B versions:update-property -Dproperty=keycloak.version
+
+      - id: vars
+        name: Get project variables
+        run: |
+          echo -n "::set-output name=versionUpdated::"
+          [ -f pom.xml.versionsBackup ] && echo 1 || echo 0
+          echo -n "::set-output name=keycloakVersion::"
+          mvn -q help:evaluate -Dexpression=keycloak.version -DforceStdout 2> /dev/null | grep -E '^[0-9a-zA-Z.-]+$'
+
+      - id: check_branch
+        name: Check if branch exists
+        run: |
+          echo -n "::set-output name=commit::"
+          if [ '${{ steps.vars.outputs.versionUpdated }}' == '1' ]; then
+            git ls-remote origin 'feature/keycloak-update-${{ steps.vars.outputs.keycloakVersion }}'
+          else
+            git rev-parse HEAD
+          fi
+
+      - id: reset_repo
+        name: Reset repository
+        if: steps.check_branch.outputs.commit == ''
+        run: |
+          git reset --hard
+
+      - id: update_deps
+        name: Update dependencies
+        if: steps.check_branch.outputs.commit == ''
+        run: |
+          mvn versions:set -DnewVersion='${{ steps.vars.outputs.keycloakVersion }}'
+          mvn versions:compare-dependencies \
+            -DremotePom='org.keycloak:keycloak-parent:${{ steps.vars.outputs.keycloakVersion }}' \
+            -DupdateDependencies=true -DupdatePropertyVersions=true
+          mvn versions:use-latest-versions -DallowMajorUpdates=false
+
+      - id: create_commit
+        name: Create commit
+        if: steps.check_branch.outputs.commit == ''
+        run: |
+          git add pom.xml
+          git config user.name 'github-actions'
+          git config user.email '41898282+github-actions[bot]@users.noreply.github.com'
+          git commit -m 'Update to Keycloak ${{ steps.vars.outputs.keycloakVersion }}'
+
+      - id: create_branch
+        name: Create branch
+        if: steps.check_branch.outputs.commit == ''
+        run: |
+          git push origin 'HEAD:feature/keycloak-update-${{ steps.vars.outputs.keycloakVersion }}'
+
+      - id: set_token
+        name: Set access token
+        if: steps.check_branch.outputs.commit == ''
+        run: |
+          echo -n "::set-env name=GH_TOKEN::"
+          if [ '${{ secrets.GH_TOKEN }}' != '' ]; then
+            echo '${{ secrets.GH_TOKEN }}'
+          else
+            echo '${{ secrets.GITHUB_TOKEN }}'
+          fi
+
+      - id: create_pull_request_default_token
+        name: Create pull request
+        if: steps.check_branch.outputs.commit == ''
+        uses: actions/github-script@0.9.0
+        with:
+          github-token: ${{ env.GH_TOKEN }}
+          script: |
+            github.pulls.create({
+              owner: context.repo.owner,
+              repo: context.repo.repo,
+              head: 'feature/keycloak-update-${{ steps.vars.outputs.keycloakVersion }}',
+              base: 'master',
+              title: 'Update to Keycloak ${{ steps.vars.outputs.keycloakVersion }}',
+              body: 'Automatic dependency bump due to release of Keycloak ${{ steps.vars.outputs.keycloakVersion }}'
+            })
diff --git a/update.sh b/update.sh
deleted file mode 100755
index 29da39d..0000000
--- a/update.sh
+++ /dev/null
@@ -1,51 +0,0 @@
-#!/bin/bash
-set -e
-
-function docker_tag_exists() {
-    REGISTRY_URL="https://quay.io/v2/$1/manifests/$2"
-    curl -fsSLI "$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 -nr "s/.*->\s*([0-9]+\.[0-9]+\.[0-9])$/\1/p")
-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 keycloak/keycloak $KEYCLOAK_VERSION; then
-    echo "Docker image for Keycloak $KEYCLOAK_VERSION 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/" .travis.yml
-
-setup_git
-git checkout -b $BRANCH
-git add pom.xml .travis.yml
-git commit -m "Update to Keycloak $KEYCLOAK_VERSION"
-git push --quiet --set-upstream origin-auth $BRANCH
-
-PR_TITLE="Update to Keycloak $KEYCLOAK_VERSION"
-PR_BODY="Updates Keycloak dependency, CI test image and project version for Keycloak release $KEYCLOAK_VERSION\\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