Skip to content

Instantly share code, notes, and snippets.

@arifbalik
Last active August 7, 2024 20:27
Show Gist options
  • Save arifbalik/69a40e218cc5c0b5558bda31f0866576 to your computer and use it in GitHub Desktop.
Save arifbalik/69a40e218cc5c0b5558bda31f0866576 to your computer and use it in GitHub Desktop.
PR Size Watcher Action with actions/github-script@v7
name: PR Watcher
on:
pull_request:
types: [opened, synchronize]
jobs:
check-pr-size:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/github-script@v7
with:
script: |
const MAX_PR_SIZE = parseInt(process.env.MAX_PR_SIZE);
const pr = context.payload.pull_request;
if (pr.additions > MAX_PR_SIZE) {
github.rest.issues.createComment({
issue_number: pr.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `:x: PR size is too large. Please keep PR size under ${MAX_PR_SIZE} additions.`
});
core.setFailed(`PR size is too large. Please keep PR size under ${MAX_PR_SIZE} additions.`);
} else { core.notice(`PR size is within limits.`); }
env:
MAX_PR_SIZE: 200
@arifbalik
Copy link
Author

No need for marketplace actions, github-script has got you covered!

@arifbalik
Copy link
Author

With file extension exclusion

name: PR Watcher

on:
  pull_request:
    types: [opened, synchronize]

permissions:
  contents: read
  pull-requests: write

jobs:
  check-pr-size:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v7
        env:
          MAX_PR_SIZE: 500
        with:
          script: |
            const MAX_PR_SIZE = parseInt(process.env.MAX_PR_SIZE);
            const pr = context.payload.pull_request;
            const exludeExtensions = ['.yaml', '.yml', '.json', '.md', '.txt'];

            const changedFiles = await github.rest.pulls.listFiles({
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: pr.number
            });

            const excludedFiles = changedFiles.data.filter(file => {
              return exludeExtensions.some(ext => file.filename.endsWith(ext));
            });

            const totalAdditions = changedFiles.data.reduce((acc, file) => {
              if (!excludedFiles.includes(file)) {
                return acc + file.additions;
              }
              return acc;
            }, 0);

            core.notice(`Total additions: ${totalAdditions}`);

            if (totalAdditions > MAX_PR_SIZE) {
              github.rest.issues.createComment({
              issue_number: pr.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: `:x: PR size is too large. Please keep PR size under ${MAX_PR_SIZE} additions.`
              });
              core.setFailed(`PR size is too large. Please keep PR size under ${MAX_PR_SIZE} additions.`);
            } else { core.notice(`PR size is within limits.`); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment