2023-03-29 09:23:09 -04:00
|
|
|
#!/bin/bash
|
2023-04-01 05:08:06 -04:00
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-29 09:23:09 -04:00
|
|
|
|
|
|
|
set -ex
|
|
|
|
|
|
|
|
: ${LOOPS:=40}
|
|
|
|
: ${LOOP_DELAY:=10}
|
|
|
|
DIR=$(mktemp -d)
|
|
|
|
|
|
|
|
trap "rm -fr $DIR" EXIT
|
|
|
|
|
2023-04-02 10:02:16 -04:00
|
|
|
function api() {
|
|
|
|
method=$1
|
|
|
|
shift
|
|
|
|
url=$1
|
|
|
|
shift
|
|
|
|
path=$1
|
|
|
|
shift
|
|
|
|
token=$1
|
|
|
|
|
|
|
|
if ! which jq curl > /dev/null ; then
|
|
|
|
apt-get -qq install -y jq curl
|
|
|
|
fi
|
|
|
|
|
|
|
|
curl --fail -X $method -sS -H "Content-Type: application/json" -H "Authorization: token $token" "$@" $url/api/v1/$path
|
|
|
|
}
|
|
|
|
|
2023-03-29 09:23:09 -04:00
|
|
|
function check_status() {
|
|
|
|
local url="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local sha="$3"
|
|
|
|
|
2023-04-02 10:02:16 -04:00
|
|
|
local state=$(api GET $url repos/$repo/commits/$sha/status | jq --raw-output .state)
|
2023-03-29 09:23:09 -04:00
|
|
|
echo $state
|
|
|
|
test "$state" != "" -a "$state" != "pending" -a "$state" != "running" -a "$state" != "null"
|
|
|
|
}
|
|
|
|
|
|
|
|
function wait_success() {
|
|
|
|
local url="$1"
|
|
|
|
local repo="$2"
|
|
|
|
local sha="$3"
|
|
|
|
|
|
|
|
for i in $(seq $LOOPS); do
|
|
|
|
if check_status "$url" "$repo" "$sha"; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
test "$FORGEJO_RUNNER_LOGS" && tail $FORGEJO_RUNNER_LOGS
|
|
|
|
sleep $LOOP_DELAY
|
|
|
|
done
|
|
|
|
if ! test "$(check_status "$url" "$repo" "$sha")" = "success" ; then
|
|
|
|
test "$FORGEJO_RUNNER_LOGS" && cat $FORGEJO_RUNNER_LOGS
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function push() {
|
|
|
|
local directory="$1"
|
|
|
|
local url="$2"
|
|
|
|
local owner="$3"
|
|
|
|
local project="$4"
|
|
|
|
local self_action="$5"
|
2023-03-30 10:11:04 -04:00
|
|
|
local token="$6"
|
2023-03-29 09:23:09 -04:00
|
|
|
|
|
|
|
local dir="$DIR/$project"
|
2023-04-03 03:07:46 -04:00
|
|
|
rsync -a --exclude .git $directory/ $dir/
|
2023-03-29 10:50:46 -04:00
|
|
|
|
|
|
|
local workflows=$dir/.forgejo/workflows/*.yml
|
|
|
|
if test "$(echo $workflows)" != "$workflows"; then
|
2023-03-30 03:11:45 -04:00
|
|
|
sed -i \
|
2023-03-30 10:11:04 -04:00
|
|
|
-e "s|SELF|$url/$owner/$self_action|g" \
|
|
|
|
-e "s|FORGEJO_TOKEN|$token|g" \
|
2023-03-30 03:11:45 -04:00
|
|
|
$dir/.forgejo/workflows/*.yml
|
2023-03-29 10:50:46 -04:00
|
|
|
fi
|
2023-03-29 09:23:09 -04:00
|
|
|
(
|
|
|
|
cd $dir
|
|
|
|
git init
|
|
|
|
git checkout -b main
|
|
|
|
git config user.email root@example.com
|
|
|
|
git config user.name username
|
|
|
|
git add .
|
|
|
|
git commit -m 'initial commit'
|
|
|
|
git remote add origin $url/$owner/$project
|
|
|
|
git push --force -u origin main
|
2023-03-29 17:33:49 -04:00
|
|
|
sha=$(git rev-parse HEAD)
|
|
|
|
echo $sha > SHA
|
|
|
|
echo sha=$sha
|
2023-03-29 09:23:09 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function run_workflow() {
|
|
|
|
local directory="$1"
|
|
|
|
local url="$2"
|
|
|
|
local owner="$3"
|
|
|
|
local project="$4"
|
|
|
|
local self_action="$5"
|
2023-03-30 10:11:04 -04:00
|
|
|
local token="$6"
|
2023-03-29 09:23:09 -04:00
|
|
|
|
2023-04-02 09:30:29 -04:00
|
|
|
if test -z "$token"; then
|
|
|
|
echo missing token argument
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-03-30 10:11:04 -04:00
|
|
|
push "$directory" "$url" "$owner" "$project" "$self_action" "$token"
|
2023-03-29 09:23:09 -04:00
|
|
|
wait_success "$url" "$owner/$project" $(cat $DIR/$project/SHA)
|
|
|
|
}
|
|
|
|
|
|
|
|
function push_self_action() {
|
|
|
|
local url="$1"
|
|
|
|
local owner="$2"
|
|
|
|
local self_action="$3"
|
|
|
|
local tag="$4"
|
|
|
|
|
|
|
|
local dir="$DIR/self"
|
|
|
|
git clone . $dir
|
|
|
|
(
|
|
|
|
cd $dir
|
|
|
|
rm -fr .forgejo .git
|
|
|
|
git init
|
|
|
|
git checkout -b main
|
|
|
|
git remote add origin "$url/$owner/$self_action"
|
|
|
|
git config user.email root@example.com
|
|
|
|
git config user.name username
|
|
|
|
git add .
|
|
|
|
git commit -m 'initial commit'
|
|
|
|
git push --force origin main
|
|
|
|
git tag --force $tag HEAD
|
|
|
|
git push --force origin $tag
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
"$@"
|