diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f1fc419..b94666a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -11,6 +11,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: write + pull-requests: write steps: - uses: actions/checkout@v4 with: @@ -24,6 +25,16 @@ jobs: fi echo "Action file validated" + - name: Test PR Features + if: github.event_name == 'pull_request' + id: pr-version + uses: ./ + with: + major-version: "1" + enable-prerelease: "true" + comment-on-pr: "true" + add-to-summary: "true" + - name: Create Release Version if: github.event_name == 'push' && github.ref == 'refs/heads/main' id: version diff --git a/README.md b/README.md index b188757..6c8e7bf 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Automatically version your releases based on branch naming conventions. Works on main branch merges with optional pre-release support. +> Testing PR comment and job summary features. + ## Quick Start ```yaml @@ -95,7 +97,7 @@ When you merge to main, it drops the suffix and creates the final version. | `enable-prerelease` | `false` | Enable pre-release tags on feature branches | | `github-token` | `${{ github.token }}` | Token for API access | | `branch-patterns` | `""` | Custom branch patterns (JSON format, optional) | -| `comment-on-pr` | `false` | Add version preview comment to PRs | +| `comment-on-pr` | `false` | Add version comment to PRs (requires `enable-prerelease: true`) | | `add-to-summary` | `false` | Add version to GitHub Actions job summary | ## Outputs @@ -104,6 +106,39 @@ When you merge to main, it drops the suffix and creates the final version. |--------|-------------| | `version` | The version tag that was created (e.g., `v1.2.3`) | | `is-prerelease` | `true` if it's a pre-release, `false` otherwise | +| `branch` | Branch name used for versioning | +| `previous-version` | Previous version tag | +| `bump-type` | Type of version bump (Minor or Patch) | + +## PR Comments & Job Summaries + +Enable helpful version information directly in your pull requests and workflow summaries: + +```yaml +- uses: TheKathan/semantic-versioning@v1 + with: + major-version: "1" + enable-prerelease: "true" + comment-on-pr: "true" # Adds comment to PRs + add-to-summary: "true" # Adds to workflow summary +``` + +Both features display the same information in a clean table format: + +``` +## 📦 Version: v1.2.0-alpha.1 + +| Field | Value | +|-------|-------| +| Type | Pre-release | +| Branch | feature/new-feature | +| Bump | Minor | +``` + +**Notes:** +- PR comments only appear when `enable-prerelease` is `true` (versions are created on feature branches) +- Job summaries appear on main branch merges or when `enable-prerelease` is `true` +- PR comments are updated in place (no duplicate comments) ## Creating Releases diff --git a/action.yml b/action.yml index 583418f..a98d076 100644 --- a/action.yml +++ b/action.yml @@ -35,6 +35,15 @@ outputs: is-prerelease: description: "Whether this is a pre-release version" value: ${{ steps.version.outputs.is_prerelease }} + branch: + description: "Branch name used for versioning" + value: ${{ steps.version.outputs.branch }} + previous-version: + description: "Previous version tag" + value: ${{ steps.version.outputs.previous_version }} + bump-type: + description: "Type of version bump (Minor or Patch)" + value: ${{ steps.version.outputs.bump_type }} runs: using: "composite" @@ -66,6 +75,12 @@ runs: IFS='.' read -r MAJ MIN PAT <<< "${LATEST#v}" echo "Version base: $MAJ.$MIN.$PAT" + # Determine bump type based on branch + BUMP_TYPE="Patch" + case "$BRANCH" in + feature/*|feat/*) BUMP_TYPE="Minor" ;; + esac + if [[ "$BRANCH" == "main" ]]; then echo "On main branch, checking PR source" SOURCE=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN" \ @@ -131,46 +146,62 @@ runs: fi echo "version=$VERSION" >> $GITHUB_OUTPUT + echo "branch=$BRANCH" >> $GITHUB_OUTPUT + echo "previous_version=$LATEST" >> $GITHUB_OUTPUT + echo "bump_type=$BUMP_TYPE" >> $GITHUB_OUTPUT - # Add to job summary if enabled - if [[ "${{ inputs.add-to-summary }}" == "true" ]]; then - echo "## 📦 Version: \`$VERSION\`" >> $GITHUB_STEP_SUMMARY - echo "" >> $GITHUB_STEP_SUMMARY - echo "- **Branch**: \`$BRANCH\`" >> $GITHUB_STEP_SUMMARY - if [[ "${{ github.event_name }}" == "pull_request" ]]; then - echo "- **Type**: Pre-release" >> $GITHUB_STEP_SUMMARY - else - echo "- **Type**: Release" >> $GITHUB_STEP_SUMMARY - fi + + - name: Add to Job Summary + if: inputs.add-to-summary == 'true' && (github.ref == 'refs/heads/main' || inputs.enable-prerelease == 'true') + shell: bash + run: | + VERSION="${{ steps.version.outputs.version }}" + IS_PRERELEASE="${{ steps.version.outputs.is_prerelease }}" + BRANCH="${{ steps.version.outputs.branch }}" + PREVIOUS="${{ steps.version.outputs.previous_version }}" + BUMP="${{ steps.version.outputs.bump_type }}" + + if [[ "$IS_PRERELEASE" == "true" ]]; then + TYPE="Pre-release" + else + TYPE="Release" fi + echo "## 📦 Version: **$VERSION**" >> $GITHUB_STEP_SUMMARY + echo "" >> $GITHUB_STEP_SUMMARY + echo "| Field | Value |" >> $GITHUB_STEP_SUMMARY + echo "|-------|-------|" >> $GITHUB_STEP_SUMMARY + echo "| Type | $TYPE |" >> $GITHUB_STEP_SUMMARY + echo "| Branch | $BRANCH |" >> $GITHUB_STEP_SUMMARY + echo "| Bump | $BUMP |" >> $GITHUB_STEP_SUMMARY + - name: Comment on PR - if: inputs.comment-on-pr == 'true' && github.event_name == 'pull_request' + if: inputs.comment-on-pr == 'true' && github.event_name == 'pull_request' && inputs.enable-prerelease == 'true' shell: bash env: GH_TOKEN: ${{ inputs.github-token }} run: | VERSION="${{ steps.version.outputs.version }}" IS_PRERELEASE="${{ steps.version.outputs.is_prerelease }}" + BRANCH="${{ steps.version.outputs.branch }}" + PREVIOUS="${{ steps.version.outputs.previous_version }}" + BUMP="${{ steps.version.outputs.bump_type }}" if [[ "$IS_PRERELEASE" == "true" ]]; then TYPE="Pre-release" - NOTE="Will be published as pre-release tag" else TYPE="Release" - NOTE="Will be published when merged to main" fi - cat > comment.md << EOF - ## 📦 Version Preview + printf "## 📦 Version: **%s**\n\n| Field | Value |\n|-------|-------|\n| Type | %s |\n| Branch | %s |\n| Bump | %s |\n" "$VERSION" "$TYPE" "$BRANCH" "$BUMP" > comment-body.txt - This PR will create version: **$VERSION** + EXISTING_COMMENT=$(gh pr view ${{ github.event.pull_request.number }} --json comments --jq '.comments[] | select(.author.login == "github-actions[bot]" and (.body | contains("📦 Version"))) | .id' | head -1) - - Type: **$TYPE** - - $NOTE - EOF - - gh pr comment ${{ github.event.pull_request.number }} --body-file comment.md + if [[ -n "$EXISTING_COMMENT" ]]; then + gh api -X PATCH "/repos/${{ github.repository }}/issues/comments/$EXISTING_COMMENT" -F body=@comment-body.txt + else + gh pr comment ${{ github.event.pull_request.number }} --body-file comment-body.txt + fi - name: Push Tag if: steps.version.outputs.skip_tag != 'true' @@ -179,6 +210,12 @@ runs: VERSION="${{ steps.version.outputs.version }}" IS_PRERELEASE="${{ steps.version.outputs.is_prerelease }}" + # Check if tag already exists + if git rev-parse "$VERSION" >/dev/null 2>&1; then + echo "Tag $VERSION already exists, skipping" + exit 0 + fi + git tag "$VERSION" git push origin "$VERSION"