diff --git a/.github/workflows/github-actions-check-labels.yml b/.github/workflows/github-actions-check-labels.yml new file mode 100644 index 0000000000..e369390c95 --- /dev/null +++ b/.github/workflows/github-actions-check-labels.yml @@ -0,0 +1,35 @@ +name: Check Labels +run-name: ${{ github.actor }} is checking labels 🚀 +on: + pull_request: + types: [ labeled, unlabeled ] +jobs: + Checking-Labels: + runs-on: ubuntu-latest + steps: + - name: Check for Semver label + run: | + LABELS=$(jq -r '.pull_request.labels[].name' "$GITHUB_EVENT_PATH") + SEMVER_PATTERN="^(major|minor|patch)$" + + SEMVER_LABELS=$(echo "$LABELS" | grep -iE "$SEMVER_PATTERN" || true) + + # Check if SEMVER_LABELS is empty + if [ -z "$SEMVER_LABELS" ]; then + echo "Error: No Semver label found. Please add exactly one of: major, minor, patch (case-insensitive)" + exit 1 + fi + + SEMVER_LABEL_COUNT=$(echo "$SEMVER_LABELS" | wc -l) + + if [ "$SEMVER_LABEL_COUNT" -eq 0 ]; then + echo "Error: No Semver label found. Please add exactly one of: major, minor, patch (case-insensitive)" + exit 1 + elif [ "$SEMVER_LABEL_COUNT" -gt 1 ]; then + echo "Error: Multiple Semver labels found. Please ensure only one is present:" + echo "$SEMVER_LABELS" + exit 1 + else + NORMALIZED_LABEL=$(echo "$SEMVER_LABELS" | tr '[:upper:]' '[:lower:]') + echo "Valid Semver label found: $NORMALIZED_LABEL" + fi \ No newline at end of file diff --git a/.github/workflows/github-actions-release-candidate.yml b/.github/workflows/github-actions-release-candidate.yml index 0df9bed6c3..6f5a90f9da 100644 --- a/.github/workflows/github-actions-release-candidate.yml +++ b/.github/workflows/github-actions-release-candidate.yml @@ -4,9 +4,18 @@ on: push: branches: - master + workflow_dispatch: + inputs: + pr_number: + description: 'Pull Request Number' + required: true + pull_request: + types: [labeled] jobs: Creating-Release-Candidate: + # Only run release creation on master push or workflow_dispatch + # if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest defaults: run: @@ -30,29 +39,89 @@ jobs: with: bundler-cache: true - name: Python Setup - uses: actions/setup-python@v4 - with: + uses: actions/setup-python@v4 + with: python-version: '3.9' - name: Set Git Config run: | git config --local user.name "${{ github.actor }}" git config --local user.email "${{ github.actor }}@users.noreply.github.com" + + - name: Get Semver Label + id: get-label + run: | + PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" 2>/dev/null || echo "") + if [ ! -z "$PR_NUMBER" ] && [ "$PR_NUMBER" != "null" ]; then + echo "✅ Successfully found PR number: $PR_NUMBER" + else + echo "❌ Unable to find PR number" + fi + + echo "Fetching labels for PR #$PR_NUMBER..." + LABELS=$(gh pr view $PR_NUMBER --json labels -q '.labels[].name' || echo "Failed to fetch labels") + echo "Found labels: $LABELS" + + if [ -z "$LABELS" ]; then + echo "⛔ Error: Failed to fetch PR labels" + exit 1 + fi + + SEMVER_LABEL=$(echo "$LABELS" | grep -iE '^(major|minor|patch)$' || true) + echo "Found Semver labels: $SEMVER_LABEL" + + if [ -z "$SEMVER_LABEL" ]; then + echo "⛔ Error: No valid Semver label (major, minor, patch) found on PR #$PR_NUMBER." + exit 1 + fi + + LABEL_COUNT=$(echo "$SEMVER_LABEL" | wc -l) + echo "Number of Semver labels found: $LABEL_COUNT" + + if [ "$LABEL_COUNT" -ne 1 ]; then + echo "⛔ Error: Expected exactly one Semver label, found $LABEL_COUNT on PR #$PR_NUMBER." + exit 1 + fi + + echo "SEMVER_LABEL=$SEMVER_LABEL" >> $GITHUB_ENV + echo "✅ Successfully found Semver label: $SEMVER_LABEL" + + echo "SEMVER_LABEL=$SEMVER_LABEL" >> $GITHUB_ENV + echo "Semver label found: $SEMVER_LABEL" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Grab Current Version and Set New RC Version id: set-version run: | current_npm_version=$(node -pe "require('./package.json').version") - + if [[ $current_npm_version == *"-rc."* ]]; then new_npm_version=$(yarn version --prerelease --preid rc --no-git-tag-version | grep "New version:" | awk '{print $4}') else - new_npm_version=$(yarn version --preminor --preid rc --no-git-tag-version | grep "New version:" | awk '{print $4}') + case ${{ env.SEMVER_LABEL }} in + Major) + new_npm_version=$(yarn version --premajor --preid rc --no-git-tag-version | grep "New version:" | awk '{print $4}') + ;; + Minor) + new_npm_version=$(yarn version --preminor --preid rc --no-git-tag-version | grep "New version:" | awk '{print $4}') + ;; + Patch) + new_npm_version=$(yarn version --prepatch --preid rc --no-git-tag-version | grep "New version:" | awk '{print $4}') + ;; + *) + echo "Error: Invalid Semver label: ${{ env.SEMVER_LABEL }}" + exit 1 + ;; + esac fi - + new_npm_version=${new_npm_version#v} new_ruby_version=$(echo $new_npm_version | sed 's/-rc\./.pre.rc./') - + echo "new_npm_version=${new_npm_version}" >> $GITHUB_ENV echo "new_ruby_version=${new_ruby_version}" >> $GITHUB_ENV + - name: Check if version exists and increment if necessary run: | max_attempts=100 @@ -76,74 +145,3 @@ jobs: echo "new_npm_version=${current_version}" >> $GITHUB_ENV new_ruby_version=$(echo $current_version | sed 's/-rc\./.pre.rc./') echo "new_ruby_version=${new_ruby_version}" >> $GITHUB_ENV - - name: Update Version.rb - run: | - gem install bundler - bundle - bin/rails pb_release:action[${{env.new_ruby_version}}] - - name: Distribute and Publish (NPM) - run: | - yarn install - bundle - yarn release - npm pack - npm publish --registry https://registry.npmjs.org playbook-ui-${{ env.new_npm_version }}.tgz --tag rc - - name: Distribute and Publish (RubyGems) - run: | - bin/build_gem - gem build lib/playbook_ui_docs.gemspec - rm -rf dist/playbook-doc.js dist/playbook-rails.js dist/app dist/pb_doc_helper.rb dist/menu.yml - gem push playbook_ui-${{ env.new_ruby_version }}.gem --host https://rubygems.org/ --key ${{ env.GEM_HOST_API_KEY }} - gem push playbook_ui_docs-${{ env.new_ruby_version }}.gem --host https://rubygems.org/ --key ${{ env.GEM_HOST_API_KEY }} - - name: Create GitHub Release - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: | - gh release create v${{ env.new_npm_version }} \ - --title "Release Candidate v${{ env.new_npm_version }}" \ - --notes "This is a release candidate version. Please test thoroughly before promoting to a stable release." \ - --prerelease - - name: Leave PR comment - uses: actions/github-script@v6 - env: - PR_NUMBER: ${{ github.event.pull_request.number }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - RUBY_GEM_VERSION: ${{ env.new_ruby_version }} - RUBY_GEM_LINK: https://rubygems.org/gems/playbook_ui/versions/${{env.new_ruby_version}} - NPM_VERSION: ${{ env.new_npm_version }} - NPM_LINK: https://www.npmjs.com/package/playbook-ui/v/${{env.new_npm_version}} - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const commitSha = context.payload.head_commit.id; - const commitMessage = context.payload.head_commit.message; - const commitAuthor = context.payload.head_commit.author.name; - const commitUrl = context.payload.head_commit.url; - - console.log(`Commit message: ${commitMessage}`); - console.log(`Commit author: ${commitAuthor}`); - console.log(`Commit URL: ${commitUrl}`); - - // Get the PR related to this commit using octokit API - const pullRequests = await github.rest.repos.listPullRequestsAssociatedWithCommit({ - owner: context.repo.owner, - repo: context.repo.repo, - commit_sha: commitSha - }); - - if (pullRequests.data.length > 0) { - const pullRequestNumber = pullRequests.data[0].number; - console.log(`Found pull request #${pullRequestNumber} for commit ${commitSha}`); - - // Add a comment to the pull request - await github.rest.issues.createComment({ - issue_number: pullRequestNumber, - owner: context.repo.owner, - repo: context.repo.repo, - body: `You merged this pr to master branch: - - Ruby Gem: [${{env.RUBY_GEM_VERSION}}](${{env.RUBY_GEM_LINK}}) - - NPM: [${{env.NPM_VERSION}}](${{env.NPM_LINK}})` - }); - } else { - console.log('No pull request found for this commit'); - }