Skip to content

Commit

Permalink
Update _postDiffToGitHubSnippet.nix
Browse files Browse the repository at this point in the history
  • Loading branch information
oneingan authored Oct 18, 2024
1 parent ad5ad09 commit a50ac8c
Showing 1 changed file with 4 additions and 67 deletions.
71 changes: 4 additions & 67 deletions src/std/fwlib/blockTypes/_postDiffToGitHubSnippet.nix
Original file line number Diff line number Diff line change
@@ -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="<!-- Unified Diff Comment -->"
ENTRY_START_MARKER="<!-- Start Diff for \`${path}\` -->"
ENTRY_END_MARKER="<!-- End Diff for \`${path}\` -->"
ENTRY_START_MARKER="<!-- Start Diff for \`$path\` -->"
ENTRY_END_MARKER="<!-- End Diff for \`$path\` -->"
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 <<EOF
$ENTRY_START_MARKER
<details>
<summary>Diff for \`${path}\` (\`${cmd}\`)</summary>
<summary>Diff for \`$path\` (\`$cmd\`)</summary>
\`\`\`diff
$DIFF_OUTPUT
Expand All @@ -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 <<EOF
$CENTRAL_COMMENT_HEADER
Expand All @@ -117,15 +59,10 @@ $DIFF_ENTRY
EOF
)
echo "Posting new central comment to PR #$PR_NUMBER"
gh pr comment "$PR_NUMBER" --repo "$OWNER_REPO_NAME" --body "$NEW_COMMENT"
fi
echo "Successfully posted diff to PR '$BRANCH'."
exit 0
else
echo "CI environment variables not set or 'gh' command not found. Skipping posting diff to GitHub."
fi
''

0 comments on commit a50ac8c

Please sign in to comment.