Check that the PR has a changeset #3133
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Fails if a PR doesn't have a changeset and is not labeled | |
# as "no changeset needed" | |
name: Check that the PR has a changeset | |
on: | |
merge_group: | |
pull_request: | |
branches: | |
- main | |
- v-next | |
types: | |
- opened | |
- synchronize | |
- reopened | |
- labeled | |
- unlabeled | |
jobs: | |
check-if-changeset: | |
name: Check that PR has a changeset | |
runs-on: ubuntu-latest | |
# don't run this check in the changesets PR | |
if: github.head_ref != 'changeset-release/main' | |
steps: | |
- uses: actions/github-script@v7 | |
with: | |
script: | | |
const isMergeGroup = context.eventName === "merge_group"; | |
// Merge group context | |
if (isMergeGroup) { | |
console.log("Running in merge_group context..."); | |
const { data: mergeQueue } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
commit_sha: context.sha | |
}); | |
if (mergeQueue.length === 0) { | |
console.log("No associated PRs found for this merge_group commit."); | |
process.exit(1); | |
} | |
console.log(`Found ${mergeQueue.length} PR(s) in the merge group.`); | |
for (const pr of mergeQueue) { | |
const pullNumber = context.issue.number; | |
console.log(`Checking PR #${pr.number}...`); | |
const { data: files } = await github.rest.pulls.listFiles({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: pullNumber | |
}); | |
const changeset = files.find( | |
file => file.status === "added" && file.filename.startsWith(".changeset/") | |
); | |
if (changeset !== undefined) { | |
console.log(`PR #${pr.number} has a changeset: ${changeset.filename}`); | |
continue; | |
} | |
console.log(`PR #${pr.number} has no changeset.`); | |
const { data: prDetails } = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: pullNumber | |
}); | |
const noChangesetNeededLabel = prDetails.labels.some(l => l.name === "no changeset needed"); | |
if (noChangesetNeededLabel) { | |
console.log(`PR #${pr.number} is labeled as "no changeset needed".`); | |
continue; | |
} | |
console.log(`PR #${pr.number} is not labeled as "no changeset needed". Failing job.`); | |
process.exit(1); | |
} | |
console.log("All PRs in the merge group have a changeset or are labeled correctly."); | |
return; | |
} | |
// Single PR context | |
const pullNumber = context.issue.number; | |
const { data: files } = await github.rest.pulls.listFiles({ | |
...context.issue, | |
pull_number: pullNumber | |
}); | |
const changeset = files.find( | |
file => file.status === "added" && file.filename.startsWith(".changeset/") | |
); | |
if (changeset !== undefined) { | |
console.log("Changeset found:", changeset.filename); | |
return; | |
} | |
console.log("No changeset found"); | |
const { data: pull } = await github.rest.pulls.get({ | |
...context.issue, | |
pull_number: pullNumber | |
}); | |
const noChangesetNeededLabel = pull.labels | |
.some(l => l.name === "no changeset needed"); | |
if (noChangesetNeededLabel) { | |
console.log('The PR is labeled as "no changeset needed"'); | |
return; | |
} | |
console.log('The PR is not labeled as "no changeset needed"'); | |
process.exit(1); |