From f15ebf021e698db8688413d4ae7a8d247e7ef947 Mon Sep 17 00:00:00 2001 From: Ludwig Nussel Date: Thu, 2 Mar 2023 15:41:42 +0100 Subject: [PATCH] pulls --- gitea-pulls | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100755 gitea-pulls diff --git a/gitea-pulls b/gitea-pulls new file mode 100755 index 0000000..ff01582 --- /dev/null +++ b/gitea-pulls @@ -0,0 +1,155 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# SPDX-FileCopyrightText: Copyright 2023 SUSE LLC +set -e + +# config options +PR_SRC_USER= +PR_PROJECT= +PR_REPO= +TOKEN= + +# command line only +verbose=0 +cfg_file= + +################### + +requestfile=$(mktemp gitea-pulls.XXXXXX) +tmpfile=$(mktemp gitea-pulls.XXXXXX) +cleanup() +{ + rm -f "$requestfile" "$tmpfile" +} +trap cleanup EXIT + +helpandquit() +{ + cat <<-EOF + Usage: $0 [OPTIONS] + OPTIONS: + --verbsoe verbose + -h help screen + EOF + exit 0 +} + +log_info() +{ + [ "$verbose" -gt 0 ] || return 0 + echo "$@" +} + +d(){ + local retval=0 + # Bash makes it a bit annoying to read the output of a different FD into a variable, it + # only supports reading stdout by itself. So redirect 3 to stdout and 1 to the real stdout. + exec {stdoutfd}>&1 + result="$(dialog --backtitle "Gita Pull requests" --output-fd 3 "$@" 3>&1 1>&${stdoutfd})" || retval=$? + # Word splitting makes it necessary to use eval here. + eval "exec ${stdoutfd}>&-" + return "$retval" +} + +# Given the number of total item pairs, outputs the number of items to display at once +menuheight() { + local height=$(($1 / 2)) + [ "$height" -le "$dh_menu" ] || height="$dh_menu" + echo "$height" +} + +stty_size() { + set -- $(stty size) + LINES="$1" + COLUMNS="$2" + # stty size can return zero when not ready or + # its a serial console + if [ "$COLUMNS" = "0" ] || [ "$LINES" = "0" ]; then + LINES=24 + COLUMNS=80 + fi + + dh_menu=$((LINES-15)) + dh_text=$((LINES-5)) +} +stty_size + +getopttmp=$(getopt -o hc:v --long help,config:,verbose -n "${0##*/}" -- "$@") +eval set -- "$getopttmp" + +while true ; do + case "$1" in + -h|--help) helpandquit; shift ;; + -v|--verbose) verbose=$((++verbose)); shift ;; + -c|--config) cfg_file="$2"; shift 2 ;; + --) shift ; break ;; + *) echo "Internal error!" ; exit 1 ;; + esac +done + +# shellcheck disable=SC1090 +. "${cfg_file:-.settings}" + +needed=(PR_SRC_USER PR_PROJECT PR_REPO TOKEN) +for i in "${needed[@]}"; do + eval test -n "\"\$$i\"" || { echo "The following settings are mandatory: ${needed[*]}"; exit 1; } +done + +request() +{ + local urlpart="$1" + shift + curl -s -f "https://gitea.opensuse.org/api/v1/repos/$PR_PROJECT/$PR_REPO$urlpart" \ + -H "accept: application/json" \ + -H "Authorization: token $TOKEN" \ + -H "Content-Type: application/json" "$@" +} + +fetch_requests() +{ + request "/pulls?state=open" > "$requestfile" +} + +fetch_requests + +while true; do + list=() + while read -r number _branch _cid owner title; do + list+=("$number" "$owner: $title") + done < <(jq '.[]|[((.number|tostring)), .head.ref, .head.sha, .head.repo.owner.login, .title]|join(" ")' -r < "$requestfile") + if [ "${#list}" = 0 ]; then + d --msgbox "No pull requests in $PR_PROJECT/$PR_REPO" 0 0 + exit 0 + fi + d --no-hot-list --menu "$PR_PROJECT/$PR_REPO pull requests" 0 0 "$(menuheight ${#list[@]})" "${list[@]}" || exit 1 + prno="$result" + + while true; do + list=(raw raw diff diff merge merge close close) + d --no-tags --menu "$PR_PROJECT/$PR_REPO #$prno" 0 0 "$(menuheight ${#list[@]})" "${list[@]}" || break + action="$result" + + case "$action" in + raw) + jq ".[]|select(.number==$prno)" < "$requestfile" > "$tmpfile" + d --textbox "$tmpfile" 0 0 + ;; + diff) + request "/pulls/$prno.diff" > "$tmpfile" + d --textbox "$tmpfile" 0 0 + ;; + merge) + request "/pulls/$prno/merge" -d '{ "do": "merge", "delete_branch_after_merge": true}' | jq > "$tmpfile" + d --textbox "$tmpfile" 0 0 + fetch_requests + break + ;; + close) + request "/pulls/$prno" -d '{ "state": "closed"}' -X PATCH | jq > "$tmpfile" + d --textbox "$tmpfile" 0 0 + fetch_requests + break + ;; + esac + done +done