feat: labeles transfer for github repositories

This commit is contained in:
2025-01-31 00:44:58 +00:00
parent 7890362285
commit 404e6a05ed
7 changed files with 191 additions and 80 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
.env
result

View File

@@ -45,90 +45,25 @@
forAllSystemsWithPkgs [] ({ system, pkgs }:
{
packages.${system} = {
# necessary to load every time .nvimrc
# makes some magic to shading nvim but still uses nvim that shaded
nvim-alias = pkgs.writeShellScriptBin "nvim" ''
# Source .env file
if [ -f .env ]; then
set -a
. .env
set +a
fi
# Get the directory of this script
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
# Remove the script's directory from PATH to avoid recursion
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v "$SCRIPT_DIR" | paste -sd ':' -)
# Find the system's nvim
SYSTEM_NVIM=$(command -v nvim)
if [ -z "$SYSTEM_NVIM" ]; then
echo "Error: nvim not found in PATH" >&2
exit 1
fi
# Execute the system's nvim with your custom arguments
exec "$SYSTEM_NVIM" --cmd 'lua vim.o.exrc = true' "$@"
'';
printobstacle =
let
name = "printobstacle";
in
pkgs.writeShellScriptBin "${name}" ''
printf "%s%s%s\n" "''${RED}" "$*" "''${RESET}"
'';
printprogress =
let
name = "printprogress";
in
pkgs.writeShellScriptBin "${name}" ''
printf "%s%s%s\n" "''${YELLOW}" "$*" "''${RESET}"
'';
colorize = pkgs.writeShellScriptBin "colorize" ''
awk '
BEGIN {
# Define color codes
RED = "\x1b[31m";
BLUE = "\x1b[34m";
GREEN = "\x1b[32m";
YELLOW = "\x1b[33m";
MAGENTA = "\x1b[35m";
CYAN = "\x1b[36m";
RESET = "\x1b[0m";
}
{
# Apply color based on keywords
gsub(/ERROR:/, RED "&" RESET, $0);
gsub(/DEBUG:/, BLUE "&" RESET, $0);
gsub(/INFO:/, GREEN "&" RESET, $0);
gsub(/LOG:/, GREEN "&" RESET, $0);
gsub(/EXCEPTION:/, MAGENTA "&" RESET, $0);
gsub(/WARNING:/, YELLOW "&" RESET, $0);
gsub(/NOTICE:/, CYAN "&" RESET, $0);
gsub(/HINT:/, CYAN "&" RESET, $0);
gsub(/FATAL:/, MAGENTA "&" RESET, $0);
gsub(/DETAIL:/, CYAN "&" RESET, $0);
gsub(/STATEMENT:/, CYAN "&" RESET, $0);
print;
}
'
'';
nvim-alias = pkgs.callPackage ./package/nvim-alias.nix {};
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 {};
};
nixosModules.${system} = {
"hetzner.hardware" = {
boot.loader.grub.device = "/dev/sda";
boot.initrd.availableKernelModules = [
"ata_piix"
"uhci_hcd"
"xen_blkfront"
"vmw_pvscsi"
];
boot.initrd.kernelModules = [ "nvme" ];
fileSystems."/" = { device = "/dev/sda1"; fsType = "ext4"; };
};
boot.loader.grub.device = "/dev/sda";
boot.initrd.availableKernelModules = [
"ata_piix"
"uhci_hcd"
"xen_blkfront"
"vmw_pvscsi"
];
boot.initrd.kernelModules = [ "nvme" ];
fileSystems."/" = { device = "/dev/sda1"; fsType = "ext4"; };
};
};
}) // {
lib = {

31
package/colorize.nix Normal file
View File

@@ -0,0 +1,31 @@
{ pkgs, ... }:
pkgs.writeShellScriptBin "colorize" ''
awk '
BEGIN {
# Define color codes
RED = "\x1b[31m";
BLUE = "\x1b[34m";
GREEN = "\x1b[32m";
YELLOW = "\x1b[33m";
MAGENTA = "\x1b[35m";
CYAN = "\x1b[36m";
RESET = "\x1b[0m";
IGNORECASE = 1;
}
{
line = $0;
gsub(/(^|[^A-Za-z])ERROR:/, RED "&" RESET, line);
gsub(/(^|[^A-Za-z])DEBUG:/, BLUE "&" RESET, line);
gsub(/(^|[^A-Za-z])INFO:/, GREEN "&" RESET, line);
gsub(/(^|[^A-Za-z])LOG:/, GREEN "&" RESET, line);
gsub(/(^|[^A-Za-z])EXCEPTION:/, MAGENTA "&" RESET, line);
gsub(/(^|[^A-Za-z])WARNING:/, YELLOW "&" RESET, line);
gsub(/(^|[^A-Za-z])NOTICE:/, CYAN "&" RESET, line);
gsub(/(^|[^A-Za-z])HINT:/, CYAN "&" RESET, line);
gsub(/(^|[^A-Za-z])FATAL:/, MAGENTA "&" RESET, line);
gsub(/(^|[^A-Za-z])DETAIL:/, CYAN "&" RESET, line);
gsub(/(^|[^A-Za-z])STATEMENT:/, CYAN "&" RESET, line);
print line;
}
'
''

View File

@@ -0,0 +1,102 @@
# 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."
''

28
package/nvim-alias.nix Normal file
View File

@@ -0,0 +1,28 @@
# necessary to load every time .nvimrc
# makes some magic to shading nvim but still uses nvim that shaded
{ pkgs, ... }:
pkgs.writeShellScriptBin "nvim" ''
# Source .env file
if [ -f .env ]; then
set -a
. .env
set +a
fi
# Get the directory of this script
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
# Remove the script's directory from PATH to avoid recursion
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v "$SCRIPT_DIR" | paste -sd ':' -)
# Find the system's nvim
SYSTEM_NVIM=$(command -v nvim)
if [ -z "$SYSTEM_NVIM" ]; then
echo "Error: nvim not found in PATH" >&2
exit 1
fi
# Execute the system's nvim with your custom arguments
exec "$SYSTEM_NVIM" --cmd 'lua vim.o.exrc = true' "$@"
''

View File

@@ -0,0 +1,7 @@
{ pkgs, ... }:
let
name = "printobstacle";
in
pkgs.writeShellScriptBin "${name}" ''
printf "%s%s%s\n" "''${RED}" "$*" "''${RESET}"
''

View File

@@ -0,0 +1,7 @@
{ pkgs, ... }:
let
name = "printprogress";
in
pkgs.writeShellScriptBin "${name}" ''
printf "%s%s%s\n" "''${YELLOW}" "$*" "''${RESET}"
''