Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(repo): Add major version check workflow #4269

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/moody-otters-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
100 changes: 100 additions & 0 deletions .github/workflows/major-version-check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
name: Major Version Check

on:
pull_request:
types: [opened, edited, synchronize]
issue_comment:
types: [created, edited]

permissions:
contents: read
issues: read
pull-requests: read

jobs:
check-major-bump:
runs-on: ubuntu-latest
steps:
- name: Check for major changesets
id: check_major
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;

// Get list of files changed in the PR
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});

// Check if any changeset files indicate a major bump
let hasMajorChangeset = false;
for (const file of files) {
if (file.filename.startsWith('.changeset/') && file.status !== 'removed') {
// Fetch the contents of the changeset file
const { data: changesetContent } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: file.filename,
ref: context.payload.pull_request.head.sha,
});

const content = Buffer.from(changesetContent.content, changesetContent.encoding).toString();

if (/major/.test(content)) {
hasMajorChangeset = true;
break;
}
}
}

core.setOutput('has_major_changeset', hasMajorChangeset);

- name: Check if major version bump is allowed
if: steps.check_major.outputs.has_major_changeset == 'true'
id: check_approval
uses: actions/github-script@v7
with:
script: |
const prNumber = context.payload.pull_request.number;
const org = context.repo.owner;

// Get all comments on the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
nikosdouvlis marked this conversation as resolved.
Show resolved Hide resolved

let approvalFound = false;
for (const comment of comments) {
if (comment.body.trim().toLowerCase() === '!allow-major') {
const username = comment.user.login;

// Check if the user is a public member of the organization
try {
const { status } = await github.rest.orgs.checkMembershipForUser({
org,
username,
});

if (status === 204) {
approvalFound = true;
break;
}
} catch (error) {
if (error.status !== 204) {
// User is not a public member
continue;
}
}
}
}

if (!approvalFound) {
core.setFailed('Major version bump requires approval from an organization member by commenting "!allow-major" on the PR.');
} else {
console.log('Major version bump approved by an organization member.');
}
Loading