|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Usage: |
| 4 | +# $0 PACKAGE_NAME VERSION |
| 5 | +# |
| 6 | +# Before calling this script, run the commands to update the dependency. If |
| 7 | +# there dependency shouldn't update then the script must not modify the repo. |
| 8 | +# |
| 9 | +# When the repo is in the updated state, run this script. It will commit |
| 10 | +# everything and create a PR. |
| 11 | +# |
| 12 | +# The PR title is `Update ${PACKAGE_NAME} to ${VERSION}` the script checks for |
| 13 | +# this string and only creates a new PR if the string isn't the title of an |
| 14 | +# existing PR. |
| 15 | +# |
| 16 | +# PACKAGE_NAME is an identifier of the package, no spaces. Doesn't need to be |
| 17 | +# the exact name of the dependency. |
| 18 | +# |
| 19 | +# VERSION an identifier of the next version of the package, no spaces. This |
| 20 | +# variable must be the same if the version if the same and different if |
| 21 | +# the version is different. However, it doesn't have to be a `x.y.z` it |
| 22 | +# could be a Git SHA, or something else. |
| 23 | + |
| 24 | +set -eu |
| 25 | + |
| 26 | +PACKAGE_NAME=$1 |
| 27 | +VERSION=$2 |
| 28 | +BRANCH=update-${PACKAGE_NAME}-${VERSION} |
| 29 | +COMMIT_MESSAGE="Update ${PACKAGE_NAME} to ${VERSION}" |
| 30 | + |
| 31 | +if [[ -z "${PACKAGE_NAME}" ]] |
| 32 | +then |
| 33 | + echo "Empty PACKAGE_NAME." |
| 34 | + exit -1 |
| 35 | +fi |
| 36 | + |
| 37 | +if [[ -z "${VERSION}" ]] |
| 38 | +then |
| 39 | + echo "Empty VERSION." |
| 40 | + exit -1 |
| 41 | +fi |
| 42 | + |
| 43 | + |
| 44 | +# NOTE: In a later runs of CI we will search for PR with this exact |
| 45 | +# title. Only if no such PR exists will the script create a |
| 46 | +# new PR. |
| 47 | +PR_TITLE="Update ${PACKAGE_NAME} to ${VERSION}" |
| 48 | + |
| 49 | +if [[ -z "$(git status --porcelain)" ]] |
| 50 | +then |
| 51 | + echo "No differences detected: ${PACKAGE_NAME} is up-to-date." |
| 52 | + exit 0 |
| 53 | +fi |
| 54 | + |
| 55 | +if [[ -z "$(gh pr list --state all --search "${PR_TITLE}")" ]] |
| 56 | +then |
| 57 | + |
| 58 | + git checkout -b $BRANCH |
| 59 | + git config user.name github-actions |
| 60 | + git config user.email github-actions@github.com |
| 61 | + git commit -a -m "${COMMIT_MESSAGE}" |
| 62 | + |
| 63 | + git push -u origin ${BRANCH} |
| 64 | + gh pr create \ |
| 65 | + --title "${PR_TITLE}" \ |
| 66 | + --body "This PR was generated by a Github Actions workflow." |
| 67 | + |
| 68 | +else |
| 69 | + echo "Old PR detected: didn't create a new one." |
| 70 | +fi |
0 commit comments