diff --git a/.github/release-targets.yaml b/.github/release-targets.yaml new file mode 100644 index 000000000..70878f838 --- /dev/null +++ b/.github/release-targets.yaml @@ -0,0 +1,3 @@ +major: "11.0.0" +minor: "10.2.0" +bug_fix: "10.1.1" \ No newline at end of file diff --git a/.github/workflows/pr_milestone.yaml b/.github/workflows/pr_milestone.yaml new file mode 100644 index 000000000..91a51c405 --- /dev/null +++ b/.github/workflows/pr_milestone.yaml @@ -0,0 +1,164 @@ +# This reusable workflow sets the milestone based on the +# current PR labels. + +name: PR Milestone + +on: + workflow_call: + inputs: + pr_number: + required: true + type: number + +permissions: + pull-requests: write + contents: read + +jobs: + set-milestone: + name: Set PR milestone + runs-on: ubuntu-latest + + steps: + - name: Set milestone + uses: actions/github-script@v8 + with: + script: | + const prNumber = ${{ inputs.pr_number }}; + + /* + * Get the base branch of the PR. + */ + const { data: pullRequest } = + await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: prNumber + }); + const baseBranch = pullRequest.base.ref; + + /* + * Return early if base is main branch. Milestone is already + * managed and probably done when merging to main. + */ + if (baseBranch === 'main') { + core.info('PR targets main. Milestone is managed separately.'); + return; + } + + /* + * Read release targets through the GitHub API + * from the base branch of PR. + */ + const { data: file } = + await github.rest.repos.getContent({ + owner: context.repo.owner, + repo: context.repo.repo, + path: '.github/release-targets.yaml', + ref: baseBranch + }); + + const releaseTargetsFile = + Buffer.from(file.content, 'base64').toString('utf8'); + + const getTarget = (name) => { + const match = releaseTargetsFile.match( + new RegExp(`^${name}:\\s*["']?([^"'\\s]+)["']?`, 'm') + ); + + if (!match) { + core.setFailed( + `Release target "${name}" was not found.` + ); + return null; + } + + return match[1]; + }; + + const major = getTarget('major'); + const minor = getTarget('minor'); + const bugFix = getTarget('bug_fix'); + + if (!major || !minor || !bugFix) { + return; + } + + /* + * Get current PR labels. + */ + const { data: labels } = + await github.rest.issues.listLabelsOnIssue({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber + }); + + const labelNames = + labels.map(label => label.name); + + /* + * Determine the milestone. + * + * Breaking API has highest priority. + */ + let targetMilestone; + + if (labelNames.includes('Breaking API')) { + targetMilestone = major; + } else if (labelNames.includes('Feature')) { + targetMilestone = minor; + } else if ( + labelNames.includes('Bug Fix') || + labelNames.includes('Infrastructure') + ) { + targetMilestone = bugFix; + } else { + core.info( + 'No milestone-related labels found.' + ); + return; + } + + core.info( + `Target milestone: ${targetMilestone}` + ); + + /* + * Find the milestone. + */ + const { data: milestones } = + await github.rest.issues.listMilestones({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open' + }); + + const milestone = milestones.find( + milestone => milestone.title === targetMilestone + ); + + if (!milestone) { + core.setFailed( + `Milestone "${targetMilestone}" was not found.` + ); + return; + } else { + core.info( + `Found milestone "${targetMilestone}".` + ); + } + + /* + * Set the milestone. + */ + await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + milestone: milestone.number + }); + + core.info( + `Milestone set to "${targetMilestone}".` + ); diff --git a/.github/workflows/pr_validation.yaml b/.github/workflows/pr_validation.yaml new file mode 100644 index 000000000..13382ffad --- /dev/null +++ b/.github/workflows/pr_validation.yaml @@ -0,0 +1,218 @@ +# This workflow is triggered when opening/reopening PR or editing the Pr description or changing labels. It ignores unrelated label changes. It validates the PR type and Breaking API, then sets the labels. It also runs the PR milestone workflow to set the milestone based on the PR type label. +name: PR Validation + +on: + pull_request: + types: + - opened + - reopened + - edited + - labeled + - unlabeled + +permissions: + pull-requests: write + contents: read + +jobs: + validate-pr: + name: Validate PR + runs-on: ubuntu-latest + outputs: + set_milestone: ${{ steps.validate.outputs.set_milestone }} + + steps: + - name: Validate PR type and Breaking API + id: validate + uses: actions/github-script@v8 + with: + script: | + /* + * Set it to run milestone at the end by default + */ + core.setOutput('set_milestone', 'true'); + + /* + * If triggered by label change, ignore unrelated labels. + */ + const action = context.payload.action; + if (action === 'labeled' || action === 'unlabeled') { + const changedLabel = context.payload.label?.name; + + const relevantLabels = [ + 'Feature', + 'Bug Fix', + 'Infrastructure', + 'Breaking API' + ]; + + if (!relevantLabels.includes(changedLabel)) { + core.info( + `Ignoring unrelated label change: ${changedLabel}` + ); + core.setOutput('set_milestone', 'false'); + return; + } + } + + /* + * Read the body contents of PR + */ + const prNumber = context.payload.pull_request?.number; + + if (!prNumber) { + core.setFailed('Unable to determine PR number.'); + return; + } + + const { data: pr } = + await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: prNumber + }); + + const body = pr.body || ''; + + /* + * Check which primary PR type is selected. + */ + const feature = + /-\s*\[[xX]\]\s*Feature\b/.test(body); + + const bugFix = + /-\s*\[[xX]\]\s*Bug Fix\b/.test(body); + + const infrastructure = + /-\s*\[[xX]\]\s*Infrastructure\b/.test(body); + + const primaryTypes = [ + feature && 'Feature', + bugFix && 'Bug Fix', + infrastructure && 'Infrastructure' + ].filter(Boolean); + + /* + * Exactly one primary PR type must be selected. + */ + if (primaryTypes.length !== 1) { + core.setFailed( + 'Exactly one primary PR type must be selected. Select exactly one of: Feature, Bug Fix, Infrastructure in the description.' + ); + return; + } + + const primaryType = primaryTypes[0]; + + /* + * Infrastructure PRs cannot be Breaking API. + */ + const breakingApi = + /-\s*\[[xX]\]\s*This is a Breaking API change\b/.test(body); + + if (infrastructure && breakingApi) { + core.setFailed( + 'Infrastructure PRs cannot be Breaking API changes. Deselect Breaking API or change the PR type to Feature or Bug Fix in the description.' + ); + return; + } + + core.info(`Primary PR type: ${primaryType}`); + core.info( + `Breaking API: ${breakingApi ? 'yes' : 'no'}` + ); + + /* + * Get the labels currently on the PR. + */ + const { data: currentLabels } = + await github.rest.issues.listLabelsOnIssue({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber + }); + + const currentLabelNames = + currentLabels.map(label => label.name); + + const primaryLabels = [ + 'Feature', + 'Bug Fix', + 'Infrastructure' + ]; + + const selectedPrimary = + primaryLabels.filter(label => + currentLabelNames.includes(label) + ); + + if (selectedPrimary.length > 1) { + core.setFailed( + `Invalid PR label state: multiple primary labels are present: ${selectedPrimary.join(', ')}. Remove these primary type labels 'Feature, Bug Fix, Infrastructure' and let the workflow set the correct label based on the PR description.` + ); + return; + } + + if (selectedPrimary.length === 1 && selectedPrimary[0] !== primaryType) { + core.setFailed( + `Invalid PR label state: the PR body selects ${primaryType}, but the existing label is ${selectedPrimary[0]}. Remove the existing label and let the workflow set the correct label based on the PR description.` + ); + return; + } + + if ( + currentLabelNames.includes('Breaking API') && + infrastructure + ) { + core.setFailed( + 'Infrastructure PRs cannot have the Breaking API label.' + ); + return; + } + + if ( + !breakingApi && + currentLabelNames.includes('Breaking API') + ) { + core.setFailed( + 'Breaking API label is present but the PR body does not mark the checkbox. Remove the label or check the checkbox in the PR body.' + ); + return; + } + + if ( + breakingApi && + !currentLabelNames.includes('Breaking API') + ) { + await github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + labels: ['Breaking API'] + }); + + core.info('Added label: Breaking API'); + } + + if (!currentLabelNames.includes(primaryType)) { + await github.rest.issues.addLabels({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: prNumber, + labels: [primaryType] + }); + + core.info(`Added label: ${primaryType}`); + } + + core.info('PR validation passed.'); + + milestone: + needs: validate-pr + if: ${{ needs.validate-pr.outputs.set_milestone == 'true' }} + uses: ./.github/workflows/pr_milestone.yaml + with: + pr_number: ${{ github.event.pull_request.number }} + permissions: + pull-requests: write + contents: read \ No newline at end of file