#!/bin/bash # Load environment if [ -f .env ]; then export $(cat .env | grep -v '^#' | xargs) else echo "❌ .env file not found!" exit 1 fi # Colors GREEN='\033[0;32m' BLUE='\033[0;34m' RED='\033[0;31m' NC='\033[0m' log_info() { echo -e "${BLUE}[INFO]${NC} $1"; } log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; } log_error() { echo -e "${RED}[ERROR]${NC} $1"; } # Get all labels with IDs declare -A LABEL_IDS get_labels() { response=$(curl -s "$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/labels" \ -H "Authorization: token $GITEA_TOKEN") while IFS= read -r line; do name=$(echo "$line" | jq -r '.name') id=$(echo "$line" | jq -r '.id') LABEL_IDS["$name"]="$id" done < <(echo "$response" | jq -c '.[]') } # Add comment to issue add_comment() { local issue_number="$1" local comment="$2" # Use jq for proper JSON escaping local json_payload=$(jq -n --arg body "$comment" '{body: $body}') response=$(curl -s -w "\n%{http_code}" -X POST \ "$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/issues/$issue_number/comments" \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ -d "$json_payload") http_code=$(echo "$response" | tail -n1) if [ "$http_code" = "201" ]; then log_success "Comment added to issue #$issue_number" else log_error "Failed to add comment (HTTP: $http_code)" fi } # Update issue labels - FIXED VERSION update_labels() { local issue_number="$1" local labels_string="$2" get_labels # Convert to ID array local valid_label_ids=() IFS=',' read -ra LABEL_ARRAY <<< "$labels_string" for label in "${LABEL_ARRAY[@]}"; do label=$(echo "$label" | xargs) if [ -n "${LABEL_IDS[$label]}" ]; then valid_label_ids+=("${LABEL_IDS[$label]}") else log_error "Label '$label' not found!" return 1 fi done # Build ID array JSON local labels_json="[" for i in "${!valid_label_ids[@]}"; do if [ $i -gt 0 ]; then labels_json="${labels_json}," fi labels_json="${labels_json}${valid_label_ids[$i]}" done labels_json="${labels_json}]" response=$(curl -s -w "\n%{http_code}" -X PUT \ "$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/issues/$issue_number/labels" \ -H "Authorization: token $GITEA_TOKEN" \ -H "Content-Type: application/json" \ -d "$labels_json") http_code=$(echo "$response" | tail -n1) if [ "$http_code" = "200" ]; then log_success "Labels updated for issue #$issue_number" else log_error "Failed to update labels (HTTP: $http_code)" fi } case "${1:-help}" in "comment") if [ -z "$2" ] || [ -z "$3" ]; then echo "Usage: $0 comment ISSUE_NUMBER \"COMMENT_TEXT\"" exit 1 fi add_comment "$2" "$3" ;; "labels") if [ -z "$2" ] || [ -z "$3" ]; then echo "Usage: $0 labels ISSUE_NUMBER \"label1,label2,label3\"" exit 1 fi update_labels "$2" "$3" ;; "progress") if [ -z "$2" ]; then echo "Usage: $0 progress ISSUE_NUMBER" exit 1 fi add_comment "$2" "📊 **Progress Update:** Arbeit an dieser Analyse läuft. Erste Quellen werden gesammelt und Framework-Relevanz geprüft." update_labels "$2" "work-in-progress" ;; "review") if [ -z "$2" ]; then echo "Usage: $0 review ISSUE_NUMBER" exit 1 fi add_comment "$2" "👀 **Ready for Review:** Erste Analyse abgeschlossen. Bitte um Peer-Review der Quellen und Framework-Integration." update_labels "$2" "needs-review" ;; "fact-check") if [ -z "$2" ]; then echo "Usage: $0 fact-check ISSUE_NUMBER" exit 1 fi add_comment "$2" "🔍 **Fact-Check Required:** Kritische Behauptungen gefunden die zusätzliche Quellen-Verifikation benötigen." update_labels "$2" "fact-check-needed" ;; *) echo "🔧 Issue Update Tool (FIXED VERSION)" echo "" echo "Usage: $0 COMMAND ISSUE_NUMBER [OPTIONS]" echo "" echo "Commands:" echo " comment NUM \"TEXT\" Add comment to issue" echo " labels NUM \"l1,l2\" Update issue labels (using IDs)" echo " progress NUM Mark as work-in-progress" echo " review NUM Mark as ready for review" echo " fact-check NUM Mark as needing fact-check" echo "" echo "Examples:" echo " $0 comment 5 \"Erste Quellen gefunden\"" echo " $0 labels 3 \"regional-case,work-in-progress\"" echo " $0 progress 7" ;; esac