refactor(gh-tl): impruve

This commit is contained in:
2025-02-02 00:32:28 +00:00
parent 90a0437755
commit 6f3af5f205
4 changed files with 111 additions and 103 deletions

View File

@@ -56,7 +56,7 @@
printobstacle = pkgs.callPackage ./package/printobstacle.nix {}; printobstacle = pkgs.callPackage ./package/printobstacle.nix {};
printprogress = pkgs.callPackage ./package/printprogress.nix {}; printprogress = pkgs.callPackage ./package/printprogress.nix {};
colorize = pkgs.callPackage ./package/colorize.nix {}; colorize = pkgs.callPackage ./package/colorize.nix {};
gh_translabeles = pkgs.callPackage ./package/github/gh_translabeles.nix {}; github.gh-tl = pkgs.callPackage ./package/github/gh-tl.nix {};
prettify-log = pkgs.callPackage ./package/prettify-log/default.nix { prettify-log = pkgs.callPackage ./package/prettify-log/default.nix {
inherit (self.lib) cargoToml; inherit (self.lib) cargoToml;
nativeBuildInputs = [ nativeBuildInputs = [
@@ -75,6 +75,7 @@
buildInputs = (with self.packages.${system}; [ buildInputs = (with self.packages.${system}; [
nvim-alias nvim-alias
prettify-log prettify-log
nvim-pager
]) ++ (with pkgs; [ ]) ++ (with pkgs; [
jq jq
yq-go yq-go

1
fufu Normal file
View File

@@ -0,0 +1 @@
{"id":7937906597,"node_id":"LA_kwDONjR0ys8AAAAB2SLXpQ","url":"https://api.github.com/repos/hectic-lab/util.nix/labels/bug","name":"bug","color":"d73a4a","default":true,"description":"bla_bla"}

108
package/github/gh-tl.nix Normal file
View File

@@ -0,0 +1,108 @@
# FIXME: very unstable (on every request opens pager) but works somehow
{ pkgs, ... }:
pkgs.writeShellScriptBin "gh-tl" ''
set -euo pipefail
export GH_PAGER=cat
alias gh="${pkgs.gh}/bin/gh"
alias jq="${pkgs.jq}/bin/gh"
usage() {
echo "Usage: $0 [--force] <source_repo> <target_repo>"
echo "Options:"
echo " --force Replace existing labels in the target repository."
echo "Example:"
echo " $0 owner/source-repo owner/target-repo"
echo " $0 --force owner/source-repo owner/target-repo"
exit 1
}
FORCE=0
while [[ "$#" -gt 0 ]]; do
case "$1" in
--force)
FORCE=1
shift
;;
-h|--help)
usage
;;
*)
break
;;
esac
done
if [ "$#" -ne 2 ]; then
usage
fi
SOURCE_REPO="$1"
TARGET_REPO="$2"
TEMP_FILE="$(mktemp)"
trap 'rm -f "$TEMP_FILE"' EXIT
# Check if required commands are available
for cmd in gh jq; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: '$cmd' is not installed or not in PATH."
exit 1
fi
done
# Fetch all labels from the source repository with pagination
echo "Fetching labels from $SOURCE_REPO..."
LABELS_JSON=$(gh api -H "Accept: application/vnd.github.v3+json" \
/repos/"$SOURCE_REPO"/labels --paginate | jq -s 'add')
if [ -z "$LABELS_JSON" ] || [ "$LABELS_JSON" == "[]" ]; then
echo "No labels found or an error occurred." >&2
exit 1
fi
echo "$LABELS_JSON" > "$TEMP_FILE"
# Create or update labels in the target repository
echo "Processing labels for $TARGET_REPO..."
jq -c '.[]' "$TEMP_FILE" | while IFS= read -r label; do
name=$(echo "$label" | jq -r '.name')
encoded_name=$(printf "%s" "$name" | jq -s -R -r @uri)
color=$(echo "$label" | jq -r '.color')
description=$(echo "$label" | jq -r '.description // ""')
if [ "$FORCE" -eq 1 ]; then
if update_output=$(gh api -X PATCH -H "Accept: application/vnd.github.v3+json" \
/repos/"$TARGET_REPO"/labels/"$encoded_name" \
-f name="$name" -f color="$color" -f description="$description" 2>&1); then
echo "Label '$name' updated in $TARGET_REPO."
else
if echo "$update_output" | grep -q '"status": *"404"'; then
echo "Label '$name' not found, creating..."
if gh api -X POST -H "Accept: application/vnd.github.v3+json" \
/repos/"$TARGET_REPO"/labels \
-f name="$name" -f color="$color" -f description="$description"; then
echo "Label '$name' created in $TARGET_REPO."
else
echo "Error: Failed to create label '$name'."
fi
else
echo "Error: Failed to update label '$name'."
fi
fi
else
echo "Creating label '$name' in $TARGET_REPO..."
if gh api -X POST -H "Accept: application/vnd.github.v3+json" \
/repos/"$TARGET_REPO"/labels \
-f name="$name" -f color="$color" -f description="$description" 1>/dev/null 2>&1; then
echo "Label '$name' created in $TARGET_REPO."
else
echo "Warning: Label '$name' already exists or creation failed. Skipping."
fi
fi
done
echo "Label transfer completed successfully."
''

View File

@@ -1,102 +0,0 @@
# FIXME: very unstable (on every request opens pager) but works somehow
{ pkgs, ... }:
pkgs.writeShellScriptBin "gh_translabeles" ''
set -euo pipefail
# Function to display usage information
usage() {
echo "Usage: $0 [--force] <source_repo> <target_repo>"
echo "Options:"
echo " --force Replace existing labels in the target repository."
echo "Example:"
echo " $0 owner/source-repo owner/target-repo"
echo " $0 --force owner/source-repo owner/target-repo"
exit 1
}
# Initialize variables
FORCE=0
# Parse options
while [[ "$#" -gt 0 ]]; do
case "$1" in
--force)
FORCE=1
shift
;;
-h|--help)
usage
;;
*)
break
;;
esac
done
# Check for required arguments
if [ "$#" -ne 2 ]; then
usage
fi
SOURCE_REPO="$1"
TARGET_REPO="$2"
TEMP_FILE="$(mktemp)"
trap 'rm -f "$TEMP_FILE"' EXIT
# Check if required commands are available
for cmd in ${pkgs.gh}/bin/gh ${pkgs.jq}/bin/jq; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: '$cmd' is not installed or not in PATH."
exit 1
fi
done
# Fetch all labels from the source repository with pagination
echo "Fetching labels from $SOURCE_REPO..."
LABELS_JSON=$(${pkgs.gh}/bin/gh api -H "Accept: application/vnd.github.v3+json" \
/repos/"$SOURCE_REPO"/labels --paginate | ${pkgs.jq}/bin/jq -s 'add')
if [ -z "$LABELS_JSON" ] || [ "$LABELS_JSON" == "[]" ]; then
echo "No labels found or an error occurred." >&2
exit 1
fi
echo "$LABELS_JSON" > "$TEMP_FILE"
# Create or update labels in the target repository
echo "Processing labels for $TARGET_REPO..."
${pkgs.jq}/bin/jq -c '.[]' "$TEMP_FILE" | while IFS= read -r label; do
name=$(echo "$label" | ${pkgs.jq}/bin/jq -r '.name')
encoded_name=$(echo "$name" | ${pkgs.jq}/bin/jq -s -R -r @uri)
color=$(echo "$label" | ${pkgs.jq}/bin/jq -r '.color')
description=$(echo "$label" | ${pkgs.jq}/bin/jq -r '.description // ""')
if [ "$FORCE" -eq 1 ]; then
echo "Creating or updating label '$name' in $TARGET_REPO..."
if ! ${pkgs.gh}/bin/gh api -X PATCH -H "Accept: application/vnd.github.v3+json" \
/repos/"$TARGET_REPO"/labels/"$encoded_name" \
-f name="$name" -f color="$color" -f description="$description"; then
echo "Error: Failed to create/update label '$name'. Skipping."
else
echo "Label '$name' has been created/updated in $TARGET_REPO."
fi
else
if ${pkgs.gh}/bin/gh api -H "Accept: application/vnd.github.v3+json" \
/repos/"$TARGET_REPO"/labels/"$encoded_name" &>/dev/null; then
echo "Label '$name' already exists in $TARGET_REPO. Skipping."
else
echo "Creating label '$name' in $TARGET_REPO..."
if ! ${pkgs.gh}/bin/gh api -X POST -H "Accept: application/vnd.github.v3+json" \
/repos/"$TARGET_REPO"/labels \
-f name="$name" -f color="$color" -f description="$description"; then
echo "Warning: Label '$name' already exists or failed to create. Skipping."
else
echo "Label '$name' has been created in $TARGET_REPO."
fi
fi
fi
done
echo "Label transfer completed successfully."
''