├── .prettierignore ├── .prettierrc.js ├── .eslintrc.js ├── .github ├── workflows │ ├── check-fixup.yml │ ├── eslint.yml │ └── node.yml └── dependabot.yml ├── action.yml ├── .gitignore ├── package.json ├── LICENSE ├── src └── main.js └── README.md /.prettierignore: -------------------------------------------------------------------------------- 1 | dist/ 2 | lib/ 3 | docs-dist/ 4 | node_modules/ 5 | -------------------------------------------------------------------------------- /.prettierrc.js: -------------------------------------------------------------------------------- 1 | const fabric = require('@umijs/fabric'); 2 | 3 | module.exports = { 4 | ...fabric.prettier, 5 | arrowParens: 'avoid', 6 | }; 7 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | browser: true, 4 | commonjs: true, 5 | es2021: true, 6 | }, 7 | extends: 'airbnb-base', 8 | overrides: [ 9 | ], 10 | parserOptions: { 11 | ecmaVersion: 'latest', 12 | }, 13 | rules: { 14 | }, 15 | }; 16 | -------------------------------------------------------------------------------- /.github/workflows/check-fixup.yml: -------------------------------------------------------------------------------- 1 | name: Pull request checks 2 | 3 | on: pull_request 4 | 5 | jobs: 6 | commit-message-check: 7 | name: Block fixup and squash commits 8 | 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Run check 13 | uses: skjnldsv/block-fixup-merge-action@main 14 | with: 15 | repo-token: ${{ secrets.GITHUB_TOKEN }} 16 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "npm" # See documentation for possible values 9 | directory: "/" # Location of package manifests 10 | schedule: 11 | interval: "weekly" 12 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions 2 | name: 'Block squash and fixup commits' 3 | description: 'A Github Action to prevent merging pull requests containing autosquash commit messages.' 4 | author: 'skjnldsv' 5 | 6 | # https://actions-cool.github.io/github-action-branding/ 7 | branding: 8 | icon: 'slash' 9 | color: 'blue' 10 | 11 | inputs: 12 | repo-token: 13 | description: "Token for the repo. Can be passed in using {{ secrets.GITHUB_TOKEN }}" 14 | required: true 15 | 16 | runs: 17 | using: 'node20' 18 | main: 'dist/index.js' 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # @source: https://github.com/xrkffgg/gitignore/blob/master/.gitignore 2 | 3 | # production 4 | # /dist 5 | /docs-dist 6 | /lib 7 | 8 | # Log file 9 | *.log 10 | npm-debug.log* 11 | yarn-debug.log* 12 | yarn-error.log* 13 | 14 | # Private 15 | 16 | # misc 17 | .DS_Store 18 | 19 | # dependencies 20 | node_modules 21 | 22 | # local env files 23 | .env.local 24 | .env.*.local 25 | 26 | # Compiled file 27 | *.class 28 | *.css.map 29 | *.sass.map 30 | *.scss.map 31 | 32 | # Editor directories and files 33 | .idea 34 | .vscode 35 | *.suo 36 | *.ntvs* 37 | *.njsproj 38 | *.sln 39 | *.sw? 40 | ~$*.* 41 | 42 | # umi 43 | .umi 44 | .umi-production 45 | .umi-test 46 | .env.local 47 | 48 | # cache 49 | .sass-cache/ 50 | 51 | # test 52 | coverage 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "main": "src/main.js", 3 | "scripts": { 4 | "build": "ncc build", 5 | "lint": "eslint src", 6 | "lint:fix": "eslint --fix src", 7 | "prettier": "prettier --check src/*.js", 8 | "prettier:fix": "prettier --write src/*.js" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/skjnldsv/block-fixup-merge-action", 13 | "branch": "main" 14 | }, 15 | "author": "skjnldsv", 16 | "license": "MIT", 17 | "dependencies": { 18 | "@actions/core": "^1.10.1", 19 | "@actions/github": "^6.0.0", 20 | "@octokit/rest": "^20.1.0", 21 | "actions-util": "^1.1.4" 22 | }, 23 | "devDependencies": { 24 | "@umijs/fabric": "^4.0.1", 25 | "@vercel/ncc": "^0.38.1", 26 | "eslint": "^8.57.0", 27 | "eslint-config-airbnb-base": "^15.0.0", 28 | "eslint-plugin-import": "^2.29.1", 29 | "prettier": "^3.2.5" 30 | }, 31 | "engines": { 32 | "node": "^20.0.0", 33 | "npm": "^10.0.0" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /.github/workflows/eslint.yml: -------------------------------------------------------------------------------- 1 | name: Lint 2 | 3 | on: pull_request 4 | 5 | permissions: 6 | contents: read 7 | 8 | concurrency: 9 | group: eslint-${{ github.head_ref || github.run_id }} 10 | cancel-in-progress: true 11 | 12 | jobs: 13 | eslint: 14 | runs-on: ubuntu-latest 15 | 16 | name: eslint 17 | steps: 18 | - name: Checkout 19 | uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 20 | 21 | - name: Read package.json node and npm engines version 22 | uses: skjnldsv/read-package-engines-version-actions@8205673bab74a63eb9b8093402fd9e0e018663a1 # v2.2 23 | id: versions 24 | with: 25 | fallbackNode: '^20' 26 | fallbackNpm: '^10' 27 | 28 | - name: Set up node ${{ steps.versions.outputs.nodeVersion }} 29 | uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 30 | with: 31 | node-version: ${{ steps.versions.outputs.nodeVersion }} 32 | 33 | - name: Install dependencies 34 | run: npm ci 35 | 36 | - name: Lint 37 | run: npm run lint 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 John Molakvoæ 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | const { 2 | debug, error, getInput, setFailed, 3 | } = require('@actions/core'); 4 | 5 | const { context, getOctokit } = require('@actions/github'); 6 | 7 | class PullRequestChecker { 8 | constructor(repoToken) { 9 | this.client = getOctokit(repoToken); 10 | } 11 | 12 | async process() { 13 | const commits = await this.client.paginate( 14 | 'GET /repos/{owner}/{repo}/pulls/{pull_number}/commits', 15 | { 16 | ...context.repo, 17 | pull_number: context.issue.number, 18 | per_page: 100, 19 | }, 20 | ); 21 | 22 | debug(`${commits.length} commit(s) in the pull request`); 23 | 24 | let blockedCommits = 0; 25 | commits.forEach(({ commit: { message }, sha, url }) => { 26 | const isAutosquash = message.startsWith('fixup!') || message.startsWith('squash!'); 27 | 28 | if (isAutosquash) { 29 | error(`Commit ${sha} is an autosquash commit: ${url}`); 30 | 31 | blockedCommits += 1; 32 | } 33 | }); 34 | 35 | if (blockedCommits) { 36 | throw Error(`${blockedCommits} commit(s) need to be squashed`); 37 | } 38 | } 39 | } 40 | 41 | async function run() { 42 | try { 43 | await new PullRequestChecker(getInput('repo-token', { required: true })).process(); 44 | } catch (err) { 45 | setFailed(err.message); 46 | } 47 | } 48 | 49 | run(); 50 | -------------------------------------------------------------------------------- /.github/workflows/node.yml: -------------------------------------------------------------------------------- 1 | name: Node 2 | 3 | on: 4 | pull_request: 5 | push: 6 | branches: 7 | - main 8 | 9 | permissions: 10 | contents: read 11 | 12 | concurrency: 13 | group: node-${{ github.head_ref || github.run_id }} 14 | cancel-in-progress: true 15 | 16 | jobs: 17 | build: 18 | runs-on: ubuntu-latest 19 | 20 | name: build 21 | steps: 22 | - name: Checkout 23 | uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 24 | 25 | - name: Read package.json node and npm engines version 26 | uses: skjnldsv/read-package-engines-version-actions@8205673bab74a63eb9b8093402fd9e0e018663a1 # v2.2 27 | id: versions 28 | with: 29 | fallbackNode: '^20' 30 | fallbackNpm: '^10' 31 | 32 | - name: Set up node ${{ steps.versions.outputs.nodeVersion }} 33 | uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2 34 | with: 35 | node-version: ${{ steps.versions.outputs.nodeVersion }} 36 | 37 | - name: Install dependencies 38 | run: npm ci 39 | 40 | - name: Build 41 | run: npm run build 42 | 43 | - name: Check build changes 44 | # See https://github.com/skjnldsv/check-actor-permission/blob/main/action.yml#L27 45 | if: matrix.node-version == 16 46 | run: | 47 | bash -c "[[ ! \"`git status --porcelain `\" ]] || (echo 'Please recompile and commit the assets, see the section \"Show changes on failure\" for details' && exit 1)" 48 | 49 | - name: Show changes on failure 50 | if: failure() 51 | run: | 52 | git status 53 | git --no-pager diff 54 | exit 1 # make it red to grab attention 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Block Autosquash Commits Action 2 | 3 | A Github Action to prevent merging pull requests containing [autosquash](https://git-scm.com/docs/git-rebase#git-rebase---autosquash) commit messages. 4 | 5 | ## How it works 6 | 7 | If any commit message in the pull request starts with `fixup!` or `squash!` the check status will be set to `error`. 8 | 9 | >⚠️ GitHub's API only returns the first 250 commits of a PR so if you're working on a really large PR your fixup commits might not be detected. 10 | 11 | ## Usage 12 | 13 | ```yaml 14 | on: pull_request 15 | 16 | name: Pull Requests 17 | 18 | jobs: 19 | message-check: 20 | name: Block Autosquash Commits 21 | 22 | runs-on: ubuntu-latest 23 | 24 | steps: 25 | - name: Block Autosquash Commits 26 | uses: skjnldsv/block-autosquash-commits-action@v1 27 | with: 28 | repo-token: ${{ secrets.GITHUB_TOKEN }} 29 | ``` 30 | 31 | You'll also need to add a [required status check](https://help.github.com/en/articles/enabling-required-status-checks) rule for your action to block merging if it detects any `fixup!` or `squash!` commits. 32 | 33 | ### Control Permissions 34 | 35 | If your repository is using [control permissions](https://github.blog/changelog/2021-04-20-github-actions-control-permissions-for-github_token/) you'll need to set `pull-request: read` on either the workflow or the job. 36 | 37 | #### Workflow Config 38 | 39 | ```yaml 40 | on: pull_request 41 | 42 | name: Pull Request 43 | 44 | permissions: 45 | pull-requests: read 46 | 47 | jobs: 48 | message-check: 49 | name: Block Autosquash Commits 50 | 51 | runs-on: ubuntu-latest 52 | 53 | steps: 54 | - name: Block Autosquash Commits 55 | uses: skjnldsv/block-autosquash-commits-action@v1 56 | with: 57 | repo-token: ${{ secrets.GITHUB_TOKEN }} 58 | ``` 59 | 60 | #### Job Config 61 | 62 | ```yaml 63 | on: pull_request 64 | 65 | name: Pull Request 66 | 67 | jobs: 68 | message-check: 69 | name: Block Autosquash Commits 70 | 71 | runs-on: ubuntu-latest 72 | 73 | permissions: 74 | pull-requests: read 75 | 76 | steps: 77 | - name: Block Autosquash Commits 78 | uses: skjnldsv/block-autosquash-commits-action@v1 79 | with: 80 | repo-token: ${{ secrets.GITHUB_TOKEN }} 81 | ``` --------------------------------------------------------------------------------