mirror of
https://code.forgejo.org/actions/cascading-pr.git
synced 2025-07-21 23:08:23 +02:00
chore: upgrade forgejo-curl.sh
This commit is contained in:
parent
2b01e2dc4f
commit
d7e60d3ba3
4 changed files with 125 additions and 125 deletions
|
@ -27,6 +27,7 @@ jobs:
|
|||
runner_config=/tmp/runner-config.yaml
|
||||
sed -e 's|file: .runner|file: ${{ steps.forgejo.outputs.runner-file }}|' < tests/runner-config.yaml > $runner_config
|
||||
FORGEJO_RUNNER_CONFIG=$runner_config forgejo-runner.sh reload
|
||||
export PATH=$(pwd):$PATH
|
||||
tests/run.sh --verbose --host_port ${{ steps.forgejo.outputs.host-port }} --url ${{ steps.forgejo.outputs.url }} --token ${{ steps.forgejo.outputs.token }}
|
||||
|
||||
- name: full logs
|
||||
|
|
|
@ -14,7 +14,7 @@ function repo_login() {
|
|||
local direction="$1"
|
||||
local repo=${options[${direction}_repo]}
|
||||
(
|
||||
export DOT=$TMPDIR/$repo
|
||||
export DOT_FORGEJO_CURL=$TMPDIR/$repo
|
||||
forgejo-curl.sh logout
|
||||
forgejo-curl.sh --token "${options[${direction}_token]}" login "${options[${direction}_url]}"
|
||||
)
|
||||
|
@ -23,7 +23,7 @@ function repo_login() {
|
|||
function repo_curl() {
|
||||
local repo=$1
|
||||
shift
|
||||
DOT=$TMPDIR/$repo forgejo-curl.sh "$@"
|
||||
DOT_FORGEJO_CURL=$TMPDIR/$repo forgejo-curl.sh "$@"
|
||||
}
|
||||
|
||||
function default_branch() {
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
#!/bin/bash
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
VERSION=1.0.0
|
||||
VERSION=1.0.1
|
||||
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
VERBOSE=false
|
||||
DEBUG=false
|
||||
: ${EXIT_ON_ERROR:=true}
|
||||
: ${TOKEN_NAME:=forgejo-curl}
|
||||
: ${DOT:=$HOME/.forgejo-curl}
|
||||
: ${DOT_FORGEJO_CURL:=$HOME/.forgejo-curl}
|
||||
: ${DOT:=$DOT_FORGEJO_CURL}
|
||||
|
||||
function debug() {
|
||||
DEBUG=true
|
||||
|
@ -47,12 +48,12 @@ function fatal_error() {
|
|||
}
|
||||
|
||||
function dot_ensure() {
|
||||
mkdir -p $DOT
|
||||
mkdir -p $DOT_FORGEJO_CURL
|
||||
}
|
||||
|
||||
HEADER_JSON='-H Content-Type:application/json'
|
||||
HEADER_TOKEN="-H @$DOT/header-token"
|
||||
HEADER_CSRF="-H @$DOT/header-csrf"
|
||||
HEADER_TOKEN="-H @$DOT_FORGEJO_CURL/header-token"
|
||||
HEADER_CSRF="-H @$DOT_FORGEJO_CURL/header-csrf"
|
||||
|
||||
function api() {
|
||||
client $HEADER_TOKEN "$@"
|
||||
|
@ -66,7 +67,7 @@ function login_api() {
|
|||
local user="$1" password="$2" token="$3" scopes="${4:-[\"all\"]}" url="$5"
|
||||
|
||||
dot_ensure
|
||||
if test -s $DOT/token; then
|
||||
if test -s $DOT_FORGEJO_CURL/token ; then
|
||||
log_info "already logged in, ignored"
|
||||
return
|
||||
fi
|
||||
|
@ -82,16 +83,13 @@ function login_api() {
|
|||
token=$(client $HEADER_JSON --user "${basic}" --data-raw '{"name":"'${TOKEN_NAME}'","scopes":'${scopes}'}' "${url}/api/v1/users/${user}/tokens" | jq --raw-output .sha1)
|
||||
fi
|
||||
if [[ "$token" =~ ^@ ]] ; then
|
||||
cp "${token##@}" $DOT/token
|
||||
cp "${token##@}" $DOT_FORGEJO_CURL/token
|
||||
else
|
||||
echo "$token" >$DOT/token
|
||||
echo "$token" > $DOT_FORGEJO_CURL/token
|
||||
fi
|
||||
(
|
||||
echo -n "Authorization: token "
|
||||
cat $DOT/token
|
||||
) >$DOT/header-token
|
||||
( echo -n "Authorization: token " ; cat $DOT_FORGEJO_CURL/token ) > $DOT_FORGEJO_CURL/header-token
|
||||
#
|
||||
# Verify it works
|
||||
# Verify the token works
|
||||
#
|
||||
local status=$(api -w "%{http_code}" -o /dev/null "${url}/api/v1/user")
|
||||
if test "${status}" != 200 ; then
|
||||
|
@ -101,8 +99,8 @@ function login_api() {
|
|||
}
|
||||
|
||||
function client() {
|
||||
log_verbose curl --cookie $DOT/cookies -f -sS "$@"
|
||||
if ! curl --cookie $DOT/cookies -f -sS "$@"; then
|
||||
log_verbose curl --cookie $DOT_FORGEJO_CURL/cookies -f -sS "$@"
|
||||
if ! curl --cookie $DOT_FORGEJO_CURL/cookies -f -sS "$@" ; then
|
||||
fatal_error
|
||||
fi
|
||||
}
|
||||
|
@ -112,8 +110,8 @@ function web() {
|
|||
}
|
||||
|
||||
function client_update_cookies() {
|
||||
log_verbose curl --cookie-jar $DOT/cookies --cookie $DOT/cookies -w "%{http_code}" -f -sS "$@"
|
||||
local status=$(curl --cookie-jar $DOT/cookies --cookie $DOT/cookies -w "%{http_code}" -f -sS "$@")
|
||||
log_verbose curl --cookie-jar $DOT_FORGEJO_CURL/cookies --cookie $DOT_FORGEJO_CURL/cookies -w "%{http_code}" -f -sS "$@"
|
||||
local status=$(curl --cookie-jar $DOT_FORGEJO_CURL/cookies --cookie $DOT_FORGEJO_CURL/cookies -w "%{http_code}" -f -sS "$@")
|
||||
if ! test "${status}" = 200 -o "${status}" = 303 ; then
|
||||
fatal_error
|
||||
fi
|
||||
|
@ -135,21 +133,21 @@ function login_client() {
|
|||
#
|
||||
# The login stores a cookie
|
||||
#
|
||||
client_update_cookies -X POST --data "user_name=${user}" --data "password=${password}" "${url}/user/login" -o $DOT/login.html
|
||||
client_update_cookies -X POST --data "user_name=${user}" --data "password=${password}" "${url}/user/login" -o $DOT_FORGEJO_CURL/login.html
|
||||
#
|
||||
# Get the CSRF for reuse by other requests
|
||||
#
|
||||
client_update_cookies -o /dev/null "${url}/user/login"
|
||||
local csrf=$(sed -n -e '/csrf/s/.*csrf\t//p' $DOT/cookies)
|
||||
echo "X-Csrf-Token: $csrf" >$DOT/header-csrf
|
||||
local csrf=$(sed -n -e '/csrf/s/.*csrf\t//p' $DOT_FORGEJO_CURL/cookies)
|
||||
echo "X-Csrf-Token: $csrf" > $DOT_FORGEJO_CURL/header-csrf
|
||||
#
|
||||
# Verify it works
|
||||
#
|
||||
local status=$(web -o /dev/null -w "%{http_code}" "${url}/user/settings")
|
||||
if test "${status}" != 200 ; then
|
||||
grep -C 1 flash-error $DOT/login.html
|
||||
grep -C 1 flash-error $DOT_FORGEJO_CURL/login.html
|
||||
if ${DEBUG} ; then
|
||||
cat $DOT/login.html
|
||||
cat $DOT_FORGEJO_CURL/login.html
|
||||
fi
|
||||
fatal_error login failed, the user or password are probably incorrect, try again with --verbose
|
||||
fi
|
||||
|
@ -162,12 +160,13 @@ function login() {
|
|||
}
|
||||
|
||||
function logout() {
|
||||
rm -f $DOT/*
|
||||
if test -d $DOT; then
|
||||
rmdir $DOT
|
||||
rm -f $DOT_FORGEJO_CURL/*
|
||||
if test -d $DOT_FORGEJO_CURL ; then
|
||||
rmdir $DOT_FORGEJO_CURL
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function usage() {
|
||||
cat >&2 <<EOF
|
||||
forgejo-curl.sh - thin curl wrapper that helps with Forgejo authentication
|
||||
|
@ -187,9 +186,9 @@ LOGIN AND TOKEN
|
|||
The web endpoints that require authentication will be given a cookie
|
||||
and CSRF token created using the the --user and --password credentials
|
||||
|
||||
On a successful login the credentials are stored in the $DOT
|
||||
On a successful login the credentials are stored in the $DOT_FORGEJO_CURL
|
||||
directory to be used by the web, api, api_json commands. The logout
|
||||
command removes the $DOT directory.
|
||||
command removes the $DOT_FORGEJO_CURL directory.
|
||||
|
||||
If the argument of --token starts with @, it is used as a filename
|
||||
from which the token will be read.
|
||||
|
|
|
@ -18,7 +18,7 @@ function push_self() {
|
|||
function user_login() {
|
||||
local username=$1
|
||||
(
|
||||
export DOT=$TMPDIR/$username
|
||||
export DOT_FORGEJO_CURL=$TMPDIR/$username
|
||||
forgejo-curl.sh logout
|
||||
forgejo-curl.sh --user $username --password "${options[password]}" login ${options[url]}
|
||||
)
|
||||
|
@ -27,7 +27,7 @@ function user_login() {
|
|||
function user_curl() {
|
||||
local username=$1
|
||||
shift
|
||||
DOT=$TMPDIR/$username forgejo-curl.sh "$@"
|
||||
DOT_FORGEJO_CURL=$TMPDIR/$username forgejo-curl.sh "$@"
|
||||
}
|
||||
|
||||
function user_token() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue