Skip to content

feat: Configure GitHub artifact provider to filter by workflow and name #262

feat: Configure GitHub artifact provider to filter by workflow and name

feat: Configure GitHub artifact provider to filter by workflow and name #262

name: Changelog Preview
on:
# Allow this workflow to be called from other repositories
#
# USAGE REQUIREMENTS:
# When calling this workflow from another repository, you must:
#
# 1. Grant required permissions:
# - contents: read (to checkout repo and read git history)
# - pull-requests: write (to post/update PR comments)
#
# 2. Inherit secrets:
# - secrets: inherit (ensures caller's GITHUB_TOKEN is used)
#
# Example caller workflow:
#
# on:
# pull_request:
# types: [opened, synchronize, reopened, edited, labeled]
#
# permissions:
# contents: read
# pull-requests: write
#
# jobs:
# changelog-preview:
# uses: getsentry/craft/.github/workflows/changelog-preview.yml@v2
# secrets: inherit
#
workflow_call:
inputs:
craft-version:
description: 'Version of Craft to use (tag or "latest")'
required: false
type: string
# Also run on PRs in this repository (for dogfooding)
# Includes 'edited' and 'labeled' to update when PR title/description/labels change
pull_request:
types: [opened, synchronize, reopened, edited, labeled]
permissions:
contents: read
pull-requests: write
jobs:
preview:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
# Install Craft from release
# Note: Dogfooding (using build artifact from this PR) is not feasible here because:
# 1. The build workflow runs separately and artifacts are scoped to that workflow run
# 2. Cross-workflow artifact fetching would require additional complexity
# 3. The changelog preview is less critical than the main release action
# For now, we always use the released version for changelog previews.
- name: Install Craft
shell: bash
run: |
set -euo pipefail
CRAFT_VERSION="${{ inputs.craft-version || 'latest' }}"
if [[ "$CRAFT_VERSION" == "latest" || -z "$CRAFT_VERSION" ]]; then
echo "Downloading latest Craft release..."
CRAFT_URL=$(curl -fsSL "https://api.github.com/repos/getsentry/craft/releases/latest" \
| jq -r '.assets[] | select(.name == "craft") | .browser_download_url')
else
CRAFT_URL="https://github.com/getsentry/craft/releases/download/${CRAFT_VERSION}/craft"
echo "Downloading Craft ${CRAFT_VERSION}..."
# Fallback to latest if specified version doesn't exist
if ! curl -sfI "$CRAFT_URL" >/dev/null 2>&1; then
echo "Release not found for version '${CRAFT_VERSION}', falling back to latest..."
CRAFT_URL=$(curl -fsSL "https://api.github.com/repos/getsentry/craft/releases/latest" \
| jq -r '.assets[] | select(.name == "craft") | .browser_download_url')
fi
fi
# Verify we have a valid URL
if [[ -z "$CRAFT_URL" ]]; then
echo "::error::Failed to determine Craft download URL"
exit 1
fi
echo "Installing Craft from: ${CRAFT_URL}"
sudo curl -fsSL -o /usr/local/bin/craft "$CRAFT_URL"
sudo chmod +x /usr/local/bin/craft
# Verify installation
if [[ ! -s /usr/local/bin/craft ]]; then
echo "::error::Downloaded Craft binary is empty or missing"
exit 1
fi
craft --version
- name: Generate Changelog Preview
shell: bash
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
CRAFT_LOG_LEVEL: Warn
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
# Generate changelog with current PR injected and highlighted (JSON format)
echo "Running craft changelog --pr $PR_NUMBER --format json..."
RESULT=$(craft changelog --pr "$PR_NUMBER" --format json 2>/dev/null || echo '{"changelog":"","bumpType":null}')
# Extract fields from JSON
CHANGELOG=$(echo "$RESULT" | jq -r '.changelog // ""')
BUMP_TYPE=$(echo "$RESULT" | jq -r '.bumpType // "none"')
PR_SKIPPED=$(echo "$RESULT" | jq -r '.prSkipped // false')
if [[ "$PR_SKIPPED" == "true" ]]; then
CHANGELOG="_This PR will not appear in the changelog._"
elif [[ -z "$CHANGELOG" ]]; then
CHANGELOG="_No changelog entries will be generated from this PR._"
fi
# Format bump type for display
case "$BUMP_TYPE" in
major) BUMP_BADGE="πŸ”΄ **Major** (breaking changes)" ;;
minor) BUMP_BADGE="🟑 **Minor** (new features)" ;;
patch) BUMP_BADGE="🟒 **Patch** (bug fixes)" ;;
*) BUMP_BADGE="βšͺ **None** (no version bump detected)" ;;
esac
# Build comment body using a temp file (safer than heredoc)
COMMENT_FILE=$(mktemp)
cat > "$COMMENT_FILE" << CRAFT_CHANGELOG_COMMENT_END
<!-- craft-changelog-preview -->
## Semver Impact of This PR
${BUMP_BADGE}
## πŸ“‹ Changelog Preview
This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).
---
${CHANGELOG}
---
<sub>πŸ€– This preview updates automatically when you update the PR.</sub>
CRAFT_CHANGELOG_COMMENT_END
# Find existing comment with our marker
COMMENT_ID=$(gh api \
"repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" \
--jq '.[] | select(.body | contains("<!-- craft-changelog-preview -->")) | .id' \
| head -1)
if [[ -n "$COMMENT_ID" ]]; then
echo "Updating existing comment $COMMENT_ID..."
gh api -X PATCH \
"repos/$GITHUB_REPOSITORY/issues/comments/$COMMENT_ID" \
-F body=@"$COMMENT_FILE"
else
echo "Creating new comment..."
gh api -X POST \
"repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" \
-F body=@"$COMMENT_FILE"
fi
rm -f "$COMMENT_FILE"