mirror of
https://code.forgejo.org/actions/cascading-pr.git
synced 2025-07-22 07:18:26 +02:00
Reviewed-on: https://code.forgejo.org/actions/cascading-pr/pulls/36 Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
parent
98b48e57d7
commit
06248c771c
5 changed files with 526 additions and 518 deletions
212
forgejo-curl.sh
212
forgejo-curl.sh
|
@ -2,7 +2,7 @@
|
|||
# SPDX-License-Identifier: MIT
|
||||
|
||||
VERSION=1.0.0
|
||||
SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
||||
SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
VERBOSE=false
|
||||
DEBUG=false
|
||||
: ${EXIT_ON_ERROR:=true}
|
||||
|
@ -28,8 +28,8 @@ function log_error() {
|
|||
}
|
||||
|
||||
function log_verbose() {
|
||||
if $VERBOSE ; then
|
||||
log "$@"
|
||||
if $VERBOSE; then
|
||||
log "$@"
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -39,10 +39,10 @@ function log_info() {
|
|||
|
||||
function fatal_error() {
|
||||
log_error "$@"
|
||||
if $EXIT_ON_ERROR ; then
|
||||
exit 1
|
||||
if $EXIT_ON_ERROR; then
|
||||
exit 1
|
||||
else
|
||||
return 1
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -66,41 +66,44 @@ function login_api() {
|
|||
local user="$1" password="$2" token="$3" scopes="${4:-[\"all\"]}" url="$5"
|
||||
|
||||
dot_ensure
|
||||
if test -s $DOT/token ; then
|
||||
log_info "already logged in, ignored"
|
||||
return
|
||||
if test -s $DOT/token; then
|
||||
log_info "already logged in, ignored"
|
||||
return
|
||||
fi
|
||||
|
||||
if test -z "$token" ; then
|
||||
log_verbose curl -sS -X DELETE --user "${user}:${password}" "${url}/api/v1/users/$user/tokens/${TOKEN_NAME}" -o /dev/null -w "%{http_code}"
|
||||
local basic="${user:-unknown}:${password:-unknown}"
|
||||
local status=$(curl -sS -X DELETE --user "${basic}" "${url}/api/v1/users/$user/tokens/${TOKEN_NAME}" -o /dev/null -w "%{http_code}")
|
||||
if test "${status}" != 404 -a "${status}" != 204 ; then
|
||||
fatal_error permission denied, the user or password are probably incorrect, try again with --verbose
|
||||
return 1
|
||||
fi
|
||||
token=$(client $HEADER_JSON --user "${basic}" --data-raw '{"name":"'${TOKEN_NAME}'","scopes":'${scopes}'}' "${url}/api/v1/users/${user}/tokens" | jq --raw-output .sha1)
|
||||
if test -z "$token"; then
|
||||
log_verbose curl -sS -X DELETE --user "${user}:${password}" "${url}/api/v1/users/$user/tokens/${TOKEN_NAME}" -o /dev/null -w "%{http_code}"
|
||||
local basic="${user:-unknown}:${password:-unknown}"
|
||||
local status=$(curl -sS -X DELETE --user "${basic}" "${url}/api/v1/users/$user/tokens/${TOKEN_NAME}" -o /dev/null -w "%{http_code}")
|
||||
if test "${status}" != 404 -a "${status}" != 204; then
|
||||
fatal_error permission denied, the user or password are probably incorrect, try again with --verbose
|
||||
return 1
|
||||
fi
|
||||
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
|
||||
if [[ "$token" =~ ^@ ]]; then
|
||||
cp "${token##@}" $DOT/token
|
||||
else
|
||||
echo "$token" > $DOT/token
|
||||
echo "$token" >$DOT/token
|
||||
fi
|
||||
( echo -n "Authorization: token " ; cat $DOT/token ) > $DOT/header-token
|
||||
(
|
||||
echo -n "Authorization: token "
|
||||
cat $DOT/token
|
||||
) >$DOT/header-token
|
||||
#
|
||||
# Verify it works
|
||||
#
|
||||
local status=$(api -w "%{http_code}" -o /dev/null "${url}/api/v1/user")
|
||||
if test "${status}" != 200 ; then
|
||||
fatal_error "${url}/api/v1/user returns status code '${status}', the token is invalid, $0 logout and login again"
|
||||
return 1
|
||||
if test "${status}" != 200; then
|
||||
fatal_error "${url}/api/v1/user returns status code '${status}', the token is invalid, $0 logout and login again"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
function client() {
|
||||
log_verbose curl --cookie $DOT/cookies -f -sS "$@"
|
||||
if ! curl --cookie $DOT/cookies -f -sS "$@" ; then
|
||||
fatal_error
|
||||
if ! curl --cookie $DOT/cookies -f -sS "$@"; then
|
||||
fatal_error
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -111,17 +114,17 @@ 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 "$@")
|
||||
if ! test "${status}" = 200 -o "${status}" = 303 ; then
|
||||
fatal_error
|
||||
if ! test "${status}" = 200 -o "${status}" = 303; then
|
||||
fatal_error
|
||||
fi
|
||||
}
|
||||
|
||||
function login_client() {
|
||||
local user="$1" password="$2" url="$3"
|
||||
|
||||
if test -z "$password" ; then
|
||||
log_verbose "no password, web will not be authenticated"
|
||||
return
|
||||
if test -z "$password"; then
|
||||
log_verbose "no password, web will not be authenticated"
|
||||
return
|
||||
fi
|
||||
|
||||
dot_ensure
|
||||
|
@ -138,17 +141,17 @@ function login_client() {
|
|||
#
|
||||
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
|
||||
echo "X-Csrf-Token: $csrf" >$DOT/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
|
||||
if ${DEBUG} ; then
|
||||
cat $DOT/login.html
|
||||
fi
|
||||
fatal_error login failed, the user or password are probably incorrect, try again with --verbose
|
||||
if test "${status}" != 200; then
|
||||
grep -C 1 flash-error $DOT/login.html
|
||||
if ${DEBUG}; then
|
||||
cat $DOT/login.html
|
||||
fi
|
||||
fatal_error login failed, the user or password are probably incorrect, try again with --verbose
|
||||
fi
|
||||
}
|
||||
|
||||
|
@ -160,12 +163,11 @@ function login() {
|
|||
|
||||
function logout() {
|
||||
rm -f $DOT/*
|
||||
if test -d $DOT ; then
|
||||
rmdir $DOT
|
||||
if test -d $DOT; then
|
||||
rmdir $DOT
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function usage() {
|
||||
cat >&2 <<EOF
|
||||
forgejo-curl.sh - thin curl wrapper that helps with Forgejo authentication
|
||||
|
@ -269,69 +271,69 @@ function main() {
|
|||
local command=login user password token scopes
|
||||
|
||||
while true; do
|
||||
case "$1" in
|
||||
--verbose)
|
||||
shift
|
||||
verbose
|
||||
;;
|
||||
--debug)
|
||||
shift
|
||||
debug
|
||||
;;
|
||||
--user)
|
||||
shift
|
||||
user="$1"
|
||||
shift
|
||||
;;
|
||||
--password)
|
||||
shift
|
||||
password="$1"
|
||||
shift
|
||||
;;
|
||||
--token)
|
||||
shift
|
||||
token="$1"
|
||||
shift
|
||||
;;
|
||||
--scopes)
|
||||
shift
|
||||
scopes="$1"
|
||||
shift
|
||||
;;
|
||||
login)
|
||||
shift
|
||||
login "$user" "$password" "$token" "$scopes" "$1"
|
||||
return 0
|
||||
;;
|
||||
logout)
|
||||
shift
|
||||
logout
|
||||
return 0
|
||||
;;
|
||||
web)
|
||||
shift
|
||||
web "$@"
|
||||
return 0
|
||||
;;
|
||||
api)
|
||||
shift
|
||||
api "$@"
|
||||
return 0
|
||||
;;
|
||||
api_json)
|
||||
shift
|
||||
api_json "$@"
|
||||
return 0
|
||||
;;
|
||||
--version)
|
||||
echo "forgejo-curl.sh version $VERSION"
|
||||
return 0
|
||||
;;
|
||||
--help|*)
|
||||
usage
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
case "$1" in
|
||||
--verbose)
|
||||
shift
|
||||
verbose
|
||||
;;
|
||||
--debug)
|
||||
shift
|
||||
debug
|
||||
;;
|
||||
--user)
|
||||
shift
|
||||
user="$1"
|
||||
shift
|
||||
;;
|
||||
--password)
|
||||
shift
|
||||
password="$1"
|
||||
shift
|
||||
;;
|
||||
--token)
|
||||
shift
|
||||
token="$1"
|
||||
shift
|
||||
;;
|
||||
--scopes)
|
||||
shift
|
||||
scopes="$1"
|
||||
shift
|
||||
;;
|
||||
login)
|
||||
shift
|
||||
login "$user" "$password" "$token" "$scopes" "$1"
|
||||
return 0
|
||||
;;
|
||||
logout)
|
||||
shift
|
||||
logout
|
||||
return 0
|
||||
;;
|
||||
web)
|
||||
shift
|
||||
web "$@"
|
||||
return 0
|
||||
;;
|
||||
api)
|
||||
shift
|
||||
api "$@"
|
||||
return 0
|
||||
;;
|
||||
api_json)
|
||||
shift
|
||||
api_json "$@"
|
||||
return 0
|
||||
;;
|
||||
--version)
|
||||
echo "forgejo-curl.sh version $VERSION"
|
||||
return 0
|
||||
;;
|
||||
--help | *)
|
||||
usage
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue