From a50ac8c135803b7571573ce03d50d1032bdc4298 Mon Sep 17 00:00:00 2001 From: Juanjo Presa Date: Fri, 18 Oct 2024 02:30:39 +0200 Subject: [PATCH] Update _postDiffToGitHubSnippet.nix --- .../blockTypes/_postDiffToGitHubSnippet.nix | 71 ++----------------- 1 file changed, 4 insertions(+), 67 deletions(-) diff --git a/src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix b/src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix index ed9edede..e78c6532 100644 --- a/src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix +++ b/src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix @@ -1,59 +1,27 @@ _: path: cmd: script: '' - # GitHub Diff Posting Script - Centralized Comment with Entry Replacement and Collapsible Diffs - - echo "Starting GitHub Diff Posting Script" if [[ -v CI ]] && [[ -v BRANCH ]] && [[ -v OWNER_AND_REPO ]] && command -v gh > /dev/null ; then - echo "CI environment detected. Proceeding with posting diff to GitHub." - - # Extract owner/repo using gh repo view - echo "Extracting owner/repo from OWNER_AND_REPO: $OWNER_AND_REPO" OWNER_REPO_NAME=$(gh repo view "$OWNER_AND_REPO" --json nameWithOwner --jq '.nameWithOwner') - echo "Resolved OWNER_REPO_NAME: $OWNER_REPO_NAME" - # Check if PR exists - echo "Checking if PR exists for branch: $BRANCH" if ! gh pr view "$BRANCH" --repo "$OWNER_REPO_NAME" >/dev/null 2>&1; then - echo "No PR found for branch '$BRANCH'. Exiting." exit 0 fi - echo "PR exists for branch '$BRANCH'. Proceeding." - - # Generate diff - echo "Generating diff using script: ${script}" - set +e # Allow the diff command to fail without exiting DIFF_OUTPUT=$(${script}) - set -e # Re-enable exit on error - - # Output the diff output for debugging - echo "Generated DIFF_OUTPUT:" - echo "$DIFF_OUTPUT" - - # Exit if no diff output if [[ -z "$DIFF_OUTPUT" ]]; then - echo "No diff output generated. Exiting." exit 0 fi - # Define markers CENTRAL_COMMENT_HEADER="" - ENTRY_START_MARKER="" - ENTRY_END_MARKER="" + ENTRY_START_MARKER="" + ENTRY_END_MARKER="" - echo "Defined markers:" - echo "CENTRAL_COMMENT_HEADER: $CENTRAL_COMMENT_HEADER" - echo "ENTRY_START_MARKER: $ENTRY_START_MARKER" - echo "ENTRY_END_MARKER: $ENTRY_END_MARKER" - - # Construct the diff entry with unique markers and collapsible markdown - echo "Constructing DIFF_ENTRY" DIFF_ENTRY=$(cat < -Diff for \`${path}\` (\`${cmd}\`) +Diff for \`$path\` (\`$cmd\`) \`\`\`diff $DIFF_OUTPUT @@ -64,51 +32,25 @@ $ENTRY_END_MARKER EOF ) - echo "Constructed DIFF_ENTRY:" - echo "$DIFF_ENTRY" - - # Fetch PR number - echo "Fetching PR number for branch '$BRANCH'" PR_NUMBER=$(gh pr view "$BRANCH" --repo "$OWNER_REPO_NAME" --json number --jq '.number') - echo "Fetched PR_NUMBER: $PR_NUMBER" - # Try to find existing central comment - echo "Looking for existing central comment with header: $CENTRAL_COMMENT_HEADER" - EXISTING_COMMENT_ID=$(gh pr view "$PR_NUMBER" --repo "$OWNER_REPO_NAME" --json comments --jq '.comments[] | select(.body | contains("'"$CENTRAL_COMMENT_HEADER"'")) | .id' | head -n 1) - echo "EXISTING_COMMENT_ID: $EXISTING_COMMENT_ID" + EXISTING_COMMENT_ID=$(gh api "repos/$OWNER_REPO_NAME/issues/$PR_NUMBER/comments" --jq ".[] | select(.body | contains(\"$CENTRAL_COMMENT_HEADER\")) | .id" | head -n 1) if [[ -n "$EXISTING_COMMENT_ID" ]]; then - echo "Existing central comment found with ID: $EXISTING_COMMENT_ID" - - # Fetch existing comment body - echo "Fetching existing comment body" EXISTING_BODY=$(gh api "repos/$OWNER_REPO_NAME/issues/comments/$EXISTING_COMMENT_ID" --jq '.body') - echo "Fetched EXISTING_BODY:" - echo "$EXISTING_BODY" - # Check if entry for this path exists if echo "$EXISTING_BODY" | grep -q "$ENTRY_START_MARKER"; then - echo "Existing entry for path '$path' found. Removing it." - # Remove existing entry UPDATED_BODY=$(echo "$EXISTING_BODY" | sed -e "/$ENTRY_START_MARKER/,/$ENTRY_END_MARKER/d") else - echo "No existing entry for path '$path'." UPDATED_BODY="$EXISTING_BODY" fi - # Append new diff entry - echo "Appending new DIFF_ENTRY to UPDATED_BODY" UPDATED_BODY="$UPDATED_BODY $DIFF_ENTRY" - # Update the comment - echo "Updating central comment with ID: $EXISTING_COMMENT_ID" gh api --method PATCH "repos/$OWNER_REPO_NAME/issues/comments/$EXISTING_COMMENT_ID" -f body="$UPDATED_BODY" else - echo "No existing central comment found. Creating new one." - - # Create new central comment NEW_COMMENT=$(cat <