|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# this executes renovate against the local repository. |
| 5 | +# NB this uses a temporary gitea instance because running renovate against a |
| 6 | +# local directory not (yet?) supported. |
| 7 | +# see https://github.com/renovatebot/renovate/issues/3609 |
| 8 | + |
| 9 | +export RENOVATE_USERNAME='renovate' |
| 10 | +export RENOVATE_NAME='Renovate Bot' |
| 11 | +export RENOVATE_PASSWORD='password' |
| 12 | +gitea_container_name="$(basename "$(dirname "$(realpath "${BASH_SOURCE[0]}")")")-renovate-gitea" |
| 13 | + |
| 14 | +# see https://hub.docker.com/r/gitea/gitea/tags |
| 15 | +# renovate: datasource=docker depName=gitea/gitea |
| 16 | +gitea_version='1.22.0' |
| 17 | + |
| 18 | +# see https://hub.docker.com/r/renovate/renovate/tags |
| 19 | +# renovate: datasource=docker depName=renovate/renovate extractVersion=(?<version>.+)-slim$ |
| 20 | +renovate_version='37.414.1' |
| 21 | + |
| 22 | +# clean. |
| 23 | +echo 'Deleting existing Gitea...' |
| 24 | +docker rm --force "$gitea_container_name" >/dev/null 2>&1 |
| 25 | +echo 'Deleting existing temporary files...' |
| 26 | +rm -f tmp/renovate-* |
| 27 | +install -d tmp |
| 28 | + |
| 29 | +# start gitea in background. |
| 30 | +# see https://docs.gitea.io/en-us/config-cheat-sheet/ |
| 31 | +# see https://github.com/go-gitea/gitea/releases |
| 32 | +# see https://github.com/go-gitea/gitea/blob/v1.22.0/docker/root/etc/s6/gitea/setup |
| 33 | +echo 'Starting Gitea...' |
| 34 | +docker run \ |
| 35 | + --detach \ |
| 36 | + --name "$gitea_container_name" \ |
| 37 | + -v /etc/timezone:/etc/timezone:ro \ |
| 38 | + -v /etc/localtime:/etc/localtime:ro \ |
| 39 | + -e SECRET_KEY=abracadabra \ |
| 40 | + -p 3000 \ |
| 41 | + "gitea/gitea:$gitea_version" \ |
| 42 | + >/dev/null |
| 43 | +gitea_addr="$(docker port "$gitea_container_name" 3000 | head -1)" |
| 44 | +gitea_url="http://$gitea_addr" |
| 45 | +export RENOVATE_ENDPOINT="$gitea_url" |
| 46 | +export GIT_PUSH_REPOSITORY="http://$RENOVATE_USERNAME:$RENOVATE_PASSWORD@$gitea_addr/$RENOVATE_USERNAME/test.git" |
| 47 | + |
| 48 | +# wait for gitea to be ready. |
| 49 | +echo "Waiting for Gitea to be ready at $gitea_url..." |
| 50 | +GITEA_URL="$gitea_url" bash -euc 'while [ -z "$(wget -qO- "$GITEA_URL/api/v1/version" | jq -r ".version | select(.!=null)")" ]; do sleep 5; done' |
| 51 | + |
| 52 | +# create user in gitea. |
| 53 | +echo "Creating Gitea $RENOVATE_USERNAME user..." |
| 54 | +docker exec --user git "$gitea_container_name" gitea admin user create \ |
| 55 | + --admin \ |
| 56 | + --email "$RENOVATE_USERNAME@example.com" \ |
| 57 | + --username "$RENOVATE_USERNAME" \ |
| 58 | + --password "$RENOVATE_PASSWORD" |
| 59 | +curl \ |
| 60 | + --silent \ |
| 61 | + --show-error \ |
| 62 | + --fail-with-body \ |
| 63 | + -u "$RENOVATE_USERNAME:$RENOVATE_PASSWORD" \ |
| 64 | + -X 'PATCH' \ |
| 65 | + -H 'Accept: application/json' \ |
| 66 | + -H 'Content-Type: application/json' \ |
| 67 | + -d "{\"full_name\":\"$RENOVATE_NAME\"}" \ |
| 68 | + "$gitea_url/api/v1/user/settings" \ |
| 69 | + | jq \ |
| 70 | + > /dev/null |
| 71 | + |
| 72 | +# create the user personal access token. |
| 73 | +# see https://docs.gitea.io/en-us/api-usage/ |
| 74 | +# see https://docs.gitea.io/en-us/oauth2-provider/#scopes |
| 75 | +# see https://try.gitea.io/api/swagger#/user/userCreateToken |
| 76 | +echo "Creating Gitea $RENOVATE_USERNAME user personal access token..." |
| 77 | +curl \ |
| 78 | + --silent \ |
| 79 | + --show-error \ |
| 80 | + --fail-with-body \ |
| 81 | + -u "$RENOVATE_USERNAME:$RENOVATE_PASSWORD" \ |
| 82 | + -X POST \ |
| 83 | + -H "Content-Type: application/json" \ |
| 84 | + -d '{"name": "renovate", "scopes": ["read:user", "write:issue", "write:repository"]}' \ |
| 85 | + "$gitea_url/api/v1/users/$RENOVATE_USERNAME/tokens" \ |
| 86 | + | jq -r .sha1 \ |
| 87 | + >tmp/renovate-gitea-token.txt |
| 88 | + |
| 89 | +# try the token. |
| 90 | +echo "Trying the Gitea $RENOVATE_USERNAME user personal access token..." |
| 91 | +RENOVATE_TOKEN="$(cat tmp/renovate-gitea-token.txt)" |
| 92 | +export RENOVATE_TOKEN |
| 93 | +curl \ |
| 94 | + --silent \ |
| 95 | + --show-error \ |
| 96 | + --fail-with-body \ |
| 97 | + -H "Authorization: token $RENOVATE_TOKEN" \ |
| 98 | + -H 'Accept: application/json' \ |
| 99 | + "$gitea_url/api/v1/version" \ |
| 100 | + | jq \ |
| 101 | + > /dev/null |
| 102 | + |
| 103 | +# create remote repository in gitea. |
| 104 | +echo "Creating Gitea $RENOVATE_USERNAME test repository..." |
| 105 | +curl \ |
| 106 | + --silent \ |
| 107 | + --show-error \ |
| 108 | + --fail-with-body \ |
| 109 | + -u "$RENOVATE_USERNAME:$RENOVATE_PASSWORD" \ |
| 110 | + -X POST \ |
| 111 | + -H 'Accept: application/json' \ |
| 112 | + -H 'Content-Type: application/json' \ |
| 113 | + -d '{"name": "test"}' \ |
| 114 | + "$gitea_url/api/v1/user/repos" \ |
| 115 | + | jq \ |
| 116 | + > /dev/null |
| 117 | + |
| 118 | +# push the code to local gitea repository. |
| 119 | +# NB running renovate locally is not yet supported. |
| 120 | +# see https://github.com/renovatebot/renovate/issues/3609 |
| 121 | +echo "Pushing local repository to Gitea $RENOVATE_USERNAME test repository..." |
| 122 | +git push --force "$GIT_PUSH_REPOSITORY" |
| 123 | + |
| 124 | +# see https://docs.renovatebot.com/modules/platform/gitea/ |
| 125 | +# see https://docs.renovatebot.com/self-hosted-configuration/#dryrun |
| 126 | +# see https://github.com/renovatebot/renovate/blob/main/docs/usage/examples/self-hosting.md |
| 127 | +# see https://github.com/renovatebot/renovate/tree/main/lib/modules/datasource |
| 128 | +# see https://github.com/renovatebot/renovate/tree/main/lib/modules/versioning |
| 129 | +RENOVATE_TOKEN="$(cat tmp/renovate-gitea-token.txt)" |
| 130 | +export RENOVATE_TOKEN |
| 131 | +# NB these can also be passed as raw positional arguments to docker run. |
| 132 | +export RENOVATE_REPOSITORIES="$RENOVATE_USERNAME/test" |
| 133 | +# see https://docs.github.com/en/rest/rate-limit#get-rate-limit-status-for-the-authenticated-user |
| 134 | +# see https://github.com/settings/tokens |
| 135 | +# NB this is only used for authentication. the token should not have any scope enabled. |
| 136 | +#export GITHUB_COM_TOKEN='TODO-YOUR-TOKEN' |
| 137 | +# let renovate create all the required pull requests. |
| 138 | +# see https://docs.renovatebot.com/configuration-options/#prhourlylimit |
| 139 | +# see https://docs.renovatebot.com/configuration-options/#prconcurrentlimit |
| 140 | +export RENOVATE_PR_HOURLY_LIMIT='0' |
| 141 | +export RENOVATE_PR_CONCURRENT_LIMIT='0' |
| 142 | +echo 'Running renovate...' |
| 143 | +# NB use --dry-run=lookup for not modifying the repository (e.g. for not |
| 144 | +# creating pull requests). |
| 145 | +docker run \ |
| 146 | + --rm \ |
| 147 | + --tty \ |
| 148 | + --interactive \ |
| 149 | + --net host \ |
| 150 | + --env GITHUB_COM_TOKEN \ |
| 151 | + --env RENOVATE_ENDPOINT \ |
| 152 | + --env RENOVATE_TOKEN \ |
| 153 | + --env RENOVATE_REPOSITORIES \ |
| 154 | + --env RENOVATE_PR_HOURLY_LIMIT \ |
| 155 | + --env RENOVATE_PR_CONCURRENT_LIMIT \ |
| 156 | + --env LOG_LEVEL=debug \ |
| 157 | + --env LOG_FORMAT=json \ |
| 158 | + "renovate/renovate:$renovate_version-slim" \ |
| 159 | + --platform=gitea \ |
| 160 | + --git-url=endpoint \ |
| 161 | + >tmp/renovate-log.json |
| 162 | + |
| 163 | +echo 'Getting results...' |
| 164 | +# extract the errors. |
| 165 | +jq 'select(.err)' tmp/renovate-log.json >tmp/renovate-errors.json |
| 166 | +# extract the result from the renovate log. |
| 167 | +jq 'select(.msg == "packageFiles with updates") | .config' tmp/renovate-log.json >tmp/renovate-result.json |
| 168 | +# extract all the dependencies. |
| 169 | +jq 'to_entries[].value[] | {packageFile,dep:.deps[]}' tmp/renovate-result.json >tmp/renovate-dependencies.json |
| 170 | +# extract the dependencies that have updates. |
| 171 | +jq 'select((.dep.updates | length) > 0)' tmp/renovate-dependencies.json >tmp/renovate-dependencies-updates.json |
| 172 | + |
| 173 | +# helpers. |
| 174 | +function show-title { |
| 175 | + echo |
| 176 | + echo '#' |
| 177 | + echo "# $1" |
| 178 | + echo '#' |
| 179 | + echo |
| 180 | +} |
| 181 | + |
| 182 | +# show errors. |
| 183 | +if [ "$(jq --slurp length tmp/renovate-errors.json)" -ne '0' ]; then |
| 184 | + show-title errors |
| 185 | + jq . tmp/renovate-errors.json |
| 186 | +fi |
| 187 | + |
| 188 | +# show dependencies. |
| 189 | +function show-dependencies { |
| 190 | + show-title "$1" |
| 191 | + ( |
| 192 | + printf 'packageFile\tdatasource\tdepName\tcurrentValue\tnewVersions\tskipReason\twarnings\n' |
| 193 | + jq \ |
| 194 | + -r \ |
| 195 | + '[ |
| 196 | + .packageFile, |
| 197 | + .dep.datasource, |
| 198 | + .dep.depName, |
| 199 | + .dep.currentValue, |
| 200 | + (.dep | select(.updates) | .updates | map(.newVersion) | join(" | ")), |
| 201 | + .dep.skipReason, |
| 202 | + (.dep | select(.warnings) | .warnings | map(.message) | join(" | ")) |
| 203 | + ] | @tsv' \ |
| 204 | + "$2" \ |
| 205 | + | sort |
| 206 | + ) | column -t -s "$(printf \\t)" |
| 207 | +} |
| 208 | +show-dependencies 'Dependencies' tmp/renovate-dependencies.json |
| 209 | +show-dependencies 'Dependencies Updates' tmp/renovate-dependencies-updates.json |
| 210 | + |
| 211 | +# show the gitea project. |
| 212 | +show-title "See PRs at $gitea_url/$RENOVATE_USERNAME/test/pulls (you can login as $RENOVATE_USERNAME:$RENOVATE_PASSWORD)" |
0 commit comments