build01: building h8hl13ncyrpq0rwjaxws9mm0gx1hsfgg-treefmt-check.drv treefmt v2.5.0traversed 204 files emitted 159 files for processing formatted 159 files (1 changed) in 521ms M nix/packages/update-go-modules/update-go-modules.sh diff --git a/nix/packages/update-go-modules/update-go-modules.sh b/nix/packages/update-go-modules/update-go-modules.sh index 6f88c09..146e90c 100755 --- a/nix/packages/update-go-modules/update-go-modules.sh +++ b/nix/packages/update-go-modules/update-go-modules.sh @@ -22,198 +22,198 @@ YELLOW='\033[1;33m' NC='\033[0m' # No Color log() { - echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $*" + echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $*" } error() { - echo -e "${RED}[ERROR]${NC} $*" >&2 + echo -e "${RED}[ERROR]${NC} $*" >&2 } warning() { - echo -e "${YELLOW}[WARNING]${NC} $*" + echo -e "${YELLOW}[WARNING]${NC} $*" } # Validate required environment variables validate_env() { - local missing=() + local missing=() - if [[ -z ${GITEA_TOKEN:-} ]]; then - missing+=("GITEA_TOKEN") - fi + if [[ -z ${GITEA_TOKEN:-} ]]; then + missing+=("GITEA_TOKEN") + fi - if [[ -z ${GITEA_URL:-} ]]; then - missing+=("GITEA_URL") - fi + if [[ -z ${GITEA_URL:-} ]]; then + missing+=("GITEA_URL") + fi - if [[ -z ${REPO_OWNER:-} ]]; then - missing+=("REPO_OWNER") - fi + if [[ -z ${REPO_OWNER:-} ]]; then + missing+=("REPO_OWNER") + fi - if [[ -z ${REPO_NAME:-} ]]; then - missing+=("REPO_NAME") - fi + if [[ -z ${REPO_NAME:-} ]]; then + missing+=("REPO_NAME") + fi - if [[ ${#missing[@]} -gt 0 ]]; then - error "Missing required environment variables: ${missing[*]}" - error "Please set these variables before running the script" - exit 1 - fi + if [[ ${#missing[@]} -gt 0 ]]; then + error "Missing required environment variables: ${missing[*]}" + error "Please set these variables before running the script" + exit 1 + fi } # Clean up worktrees on exit cleanup() { - if [[ -d $WORKTREE_BASE ]]; then - log "Cleaning up worktrees..." - find "$WORKTREE_BASE" -mindepth 1 -maxdepth 1 -type d | while read -r worktree; do - git worktree remove --force "$worktree" 2>/dev/null || true - done - rm -rf "$WORKTREE_BASE" - fi + if [[ -d $WORKTREE_BASE ]]; then + log "Cleaning up worktrees..." + find "$WORKTREE_BASE" -mindepth 1 -maxdepth 1 -type d | while read -r worktree; do + git worktree remove --force "$worktree" 2>/dev/null || true + done + rm -rf "$WORKTREE_BASE" + fi } trap cleanup EXIT # Find all Go modules in the repository find_go_modules() { - find "$REPO_ROOT" -name "go.mod" -type f | while read -r mod_file; do - dirname "$mod_file" - done + find "$REPO_ROOT" -name "go.mod" -type f | while read -r mod_file; do + dirname "$mod_file" + done } # Get list of outdated dependencies for a module get_outdated_dependencies() { - local module_dir="$1" + local module_dir="$1" - cd "$module_dir" + cd "$module_dir" - # Get list of all direct dependencies and their available updates - go list -u -f '{{if (and (not (or .Main .Indirect)) .Update)}}{{.Path}}|{{.Version}}|{{.Update.Version}}{{end}}' -m all 2>/dev/null | grep -v '^$' || true + # Get list of all direct dependencies and their available updates + go list -u -f '{{if (and (not (or .Main .Indirect)) .Update)}}{{.Path}}|{{.Version}}|{{.Update.Version}}{{end}}' -m all 2>/dev/null | grep -v '^$' || true } # Create pull request using curl API create_pull_request() { - local branch_name="$1" - local pr_title="$2" - local pr_body="$3" + local branch_name="$1" + local pr_title="$2" + local pr_body="$3" - log "Creating pull request for branch: $branch_name" + log "Creating pull request for branch: $branch_name" - # Push the branch - git push origin "+HEAD:${branch_name}" + # Push the branch + git push origin "+HEAD:${branch_name}" - # Create PR using curl - local resp - resp=$(curl -s -X POST \ - -H "Authorization: token $GITEA_TOKEN" \ - -H "Content-Type: application/json" \ - -d "{ + # Create PR using curl + local resp + resp=$(curl -s -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d "{ \"head\": \"${branch_name}\", \"base\": \"${BASE_BRANCH}\", \"title\": \"${pr_title}\", \"body\": \"${pr_body}\" }" \ - "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/pulls") + "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/pulls") - local pr_number - pr_number=$(echo "$resp" | jq -r '.number') + local pr_number + pr_number=$(echo "$resp" | jq -r '.number') - # Check if PR already exists - local error_msg - error_msg=$(echo "$resp" | jq -r '.message // empty') + # Check if PR already exists + local error_msg + error_msg=$(echo "$resp" | jq -r '.message // empty') - if [[ $error_msg == *"pull request already exists"* ]]; then - log "Pull request already exists for branch: $branch_name" - return 0 - fi + if [[ $error_msg == *"pull request already exists"* ]]; then + log "Pull request already exists for branch: $branch_name" + return 0 + fi - if [[ $pr_number == "null" ]]; then - error "Error creating pull request:" - echo "$resp" | jq . >&2 - return 1 - fi + if [[ $pr_number == "null" ]]; then + error "Error creating pull request:" + echo "$resp" | jq . >&2 + return 1 + fi - local pr_url="${GITEA_URL}/${REPO_OWNER}/${REPO_NAME}/pulls/${pr_number}" - log "Created pull request #$pr_number: $pr_url" + local pr_url="${GITEA_URL}/${REPO_OWNER}/${REPO_NAME}/pulls/${pr_number}" + log "Created pull request #$pr_number: $pr_url" - # Enable auto-merge for the newly created PR - enable_auto_merge "$pr_number" + # Enable auto-merge for the newly created PR + enable_auto_merge "$pr_number" } # Enable auto-merge for a pull request enable_auto_merge() { - local pr_number="$1" + local pr_number="$1" - log "Enabling auto-merge for PR #$pr_number" + log "Enabling auto-merge for PR #$pr_number" - # Merge when checks succeed - while true; do - local resp - resp=$(curl -s -X POST \ - -H "Authorization: token $GITEA_TOKEN" \ - -H "Content-Type: application/json" \ - -d '{ + # Merge when checks succeed + while true; do + local resp + resp=$(curl -s -X POST \ + -H "Authorization: token $GITEA_TOKEN" \ + -H "Content-Type: application/json" \ + -d '{ "Do": "merge", "merge_when_checks_succeed": true, "delete_branch_after_merge": true }' \ - "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/pulls/$pr_number/merge") - - local msg - msg=$(echo "$resp" | jq -r '.message') - if [[ $msg != "Please try again later" ]]; then - break - fi - log "Retrying auto-merge in 2 seconds..." - sleep 2 - done - - log "Pull request #$pr_number merge initiated" + "${GITEA_URL}/api/v1/repos/${REPO_OWNER}/${REPO_NAME}/pulls/$pr_number/merge") + + local msg + msg=$(echo "$resp" | jq -r '.message') + if [[ $msg != "Please try again later" ]]; then + break + fi + log "Retrying auto-merge in 2 seconds..." + sleep 2 + done + + log "Pull request #$pr_number merge initiated" } # Update a single dependency update_single_dependency() { - local module_dir="$1" - local dep_path="$2" - local current_version="$3" - local new_version="$4" - local module_name="$5" - - # Create safe branch name (replace special characters) - local safe_dep_name="${dep_path//\//-}" - safe_dep_name="${safe_dep_name//./-}" - local branch_name - branch_name="update-go-dep-${safe_dep_name}-${new_version}" - local worktree_dir="$WORKTREE_BASE/$branch_name" - - log "Updating $dep_path from $current_version to $new_version" - - # Create worktree - mkdir -p "$WORKTREE_BASE" - git worktree add -b "$branch_name" "$worktree_dir" "$BASE_BRANCH" - - # Change to module directory in worktree - # module_dir is an absolute path - make it relative to repo root - local relative_module_dir="${module_dir#"$REPO_ROOT"}" - relative_module_dir="${relative_module_dir#/}" # Remove leading slash if present - cd "$worktree_dir/${relative_module_dir:-.}" # Default to . if at repo root - - # Update specific dependency - log "Updating dependency $dep_path to $new_version..." - go get "${dep_path}@${new_version}" - go mod tidy - - # Check if there are any changes - if ! git diff --quiet; then - # Update vendor hash if needed - nix run .#data-mesher.update-vendor-hash -- "$worktree_dir" - - # Commit changes - git add -A - git commit -m "fix(deps): update module $dep_path to $new_version" - - # Create pull request - local pr_title="fix(deps): update module $dep_path to $new_version" - local pr_body="## Automated Go Dependency Update + local module_dir="$1" + local dep_path="$2" + local current_version="$3" + local new_version="$4" + local module_name="$5" + + # Create safe branch name (replace special characters) + local safe_dep_name="${dep_path//\//-}" + safe_dep_name="${safe_dep_name//./-}" + local branch_name + branch_name="update-go-dep-${safe_dep_name}-${new_version}" + local worktree_dir="$WORKTREE_BASE/$branch_name" + + log "Updating $dep_path from $current_version to $new_version" + + # Create worktree + mkdir -p "$WORKTREE_BASE" + git worktree add -b "$branch_name" "$worktree_dir" "$BASE_BRANCH" + + # Change to module directory in worktree + # module_dir is an absolute path - make it relative to repo root + local relative_module_dir="${module_dir#"$REPO_ROOT"}" + relative_module_dir="${relative_module_dir#/}" # Remove leading slash if present + cd "$worktree_dir/${relative_module_dir:-.}" # Default to . if at repo root + + # Update specific dependency + log "Updating dependency $dep_path to $new_version..." + go get "${dep_path}@${new_version}" + go mod tidy + + # Check if there are any changes + if ! git diff --quiet; then + # Update vendor hash if needed + nix run .#data-mesher.update-vendor-hash -- "$worktree_dir" + + # Commit changes + git add -A + git commit -m "fix(deps): update module $dep_path to $new_version" + + # Create pull request + local pr_title="fix(deps): update module $dep_path to $new_version" + local pr_body="## Automated Go Dependency Update This pull request updates a single Go dependency for the module \`$module_name\`: @@ -227,86 +227,86 @@ https://pkg.go.dev/$dep_path?tab=versions This is an automated update generated by the update-go-modules script." - create_pull_request "$branch_name" "$pr_title" "$pr_body" + create_pull_request "$branch_name" "$pr_title" "$pr_body" - log "Successfully processed $dep_path update" - else - log "No changes detected for $dep_path update" - fi + log "Successfully processed $dep_path update" + else + log "No changes detected for $dep_path update" + fi - # Clean up worktree - cd "$REPO_ROOT" - git worktree remove --force "$worktree_dir" + # Clean up worktree + cd "$REPO_ROOT" + git worktree remove --force "$worktree_dir" } # Process a single Go module process_go_module() { - local module_dir="$1" - local module_name - module_name=$(cd "$module_dir" && go list -m) - - log "Processing Go module: $module_name in $module_dir" - - # Get outdated dependencies - local outdated_deps - outdated_deps=$(get_outdated_dependencies "$module_dir") - - if [[ -z $outdated_deps ]]; then - log "No outdated dependencies found for $module_name" - return - fi - - # Count dependencies - local dep_count - dep_count=$(echo "$outdated_deps" | wc -l) - log "Found $dep_count outdated dependencies for $module_name" - - # Process each outdated dependency - while IFS='|' read -r dep_path current_version new_version; do - if [[ -n $dep_path ]]; then - update_single_dependency "$module_dir" "$dep_path" "$current_version" "$new_version" "$module_name" - - # Add a small delay between PRs to avoid overwhelming the API - sleep 2 - fi - done <<<"$outdated_deps" + local module_dir="$1" + local module_name + module_name=$(cd "$module_dir" && go list -m) + + log "Processing Go module: $module_name in $module_dir" + + # Get outdated dependencies + local outdated_deps + outdated_deps=$(get_outdated_dependencies "$module_dir") + + if [[ -z $outdated_deps ]]; then + log "No outdated dependencies found for $module_name" + return + fi + + # Count dependencies + local dep_count + dep_count=$(echo "$outdated_deps" | wc -l) + log "Found $dep_count outdated dependencies for $module_name" + + # Process each outdated dependency + while IFS='|' read -r dep_path current_version new_version; do + if [[ -n $dep_path ]]; then + update_single_dependency "$module_dir" "$dep_path" "$current_version" "$new_version" "$module_name" + + # Add a small delay between PRs to avoid overwhelming the API + sleep 2 + fi + done <<<"$outdated_deps" } # Main execution main() { - log "Starting Go module update process" + log "Starting Go module update process" - # Validate environment - validate_env + # Validate environment + validate_env - # Ensure we're in a git repository - if ! git rev-parse --git-dir >/dev/null 2>&1; then - error "Not in a git repository" - exit 1 - fi + # Ensure we're in a git repository + if ! git rev-parse --git-dir >/dev/null 2>&1; then + error "Not in a git repository" + exit 1 + fi - # Fetch latest changes - git fetch origin + # Fetch latest changes + git fetch origin - # Find and process all Go modules - local modules=() - while IFS= read -r module; do - modules+=("$module") - done < <(find_go_modules) + # Find and process all Go modules + local modules=() + while IFS= read -r module; do + modules+=("$module") + done < <(find_go_modules) - if [[ ${#modules[@]} -eq 0 ]]; then - warning "No Go modules found in the repository" - exit 0 - fi + if [[ ${#modules[@]} -eq 0 ]]; then + warning "No Go modules found in the repository" + exit 0 + fi - log "Found ${#modules[@]} Go module(s)" + log "Found ${#modules[@]} Go module(s)" - # Process each module - for module in "${modules[@]}"; do - process_go_module "$module" - done + # Process each module + for module in "${modules[@]}"; do + process_go_module "$module" + done - log "Go module update process completed" + log "Go module update process completed" } # Run main function