diff --git a/flake.nix b/flake.nix index a698a65..a7b1006 100644 --- a/flake.nix +++ b/flake.nix @@ -56,7 +56,7 @@ printobstacle = pkgs.callPackage ./package/printobstacle.nix {}; printprogress = pkgs.callPackage ./package/printprogress.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 { inherit (self.lib) cargoToml; nativeBuildInputs = [ @@ -75,6 +75,7 @@ buildInputs = (with self.packages.${system}; [ nvim-alias prettify-log + nvim-pager ]) ++ (with pkgs; [ jq yq-go diff --git a/fufu b/fufu new file mode 100644 index 0000000..5c14896 --- /dev/null +++ b/fufu @@ -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"} \ No newline at end of file diff --git a/package/github/gh-tl.nix b/package/github/gh-tl.nix new file mode 100644 index 0000000..bd70ad5 --- /dev/null +++ b/package/github/gh-tl.nix @@ -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] " + 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." +'' diff --git a/package/github/gh_translabeles.nix b/package/github/gh_translabeles.nix deleted file mode 100644 index 5a1d0a9..0000000 --- a/package/github/gh_translabeles.nix +++ /dev/null @@ -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] " - 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." -''