├── .env.example ├── .github └── workflows │ └── ci.yml ├── .gitignore ├── .nvmrc ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── __tests__ ├── handle-pull-request-change.js └── is-semantic-message.js ├── assets ├── icon.png └── icon.sketch ├── index.js ├── lib ├── handle-pull-request-change.js └── is-semantic-message.js ├── package-lock.json ├── package.json └── scripts └── deploy.sh /.env.example: -------------------------------------------------------------------------------- 1 | APP_ID=9517 2 | WEBHOOK_SECRET= 3 | WEBHOOK_PROXY_URL= -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: ci 5 | 6 | on: [push, pull_request] 7 | 8 | jobs: 9 | build: 10 | 11 | runs-on: ubuntu-latest 12 | 13 | strategy: 14 | matrix: 15 | node-version: [10.x, 12.x] 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Use Node.js ${{ matrix.node-version }} 20 | uses: actions/setup-node@v1 21 | with: 22 | node-version: ${{ matrix.node-version }} 23 | - run: npm ci 24 | - run: npm run build --if-present 25 | - run: npm test 26 | 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .npmrc 3 | coverage 4 | node_modules 5 | private-key.pem 6 | -------------------------------------------------------------------------------- /.nvmrc: -------------------------------------------------------------------------------- 1 | v8.17.0 -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at zeke@sikelianos.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2017 Gregor Martynus and other contributors. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ⚠️ THIS SERVICE IS NO LONGER MAINTAINED ⚠️ 2 | 3 | I maintained this project and kept the webservice running for several years, but it was often difficult, time-consuming, and generally low on my priority list compared to other things like work and family. The final blow was a [Heroku incident](https://status.heroku.com/incidents/2413) in April 2022 in which all of Heroku's GitHub-related secrets were compromised, and Heroku invalidated existing secrets and disabled support for all GitHub-related integrations. 4 | 5 | If you're looking for an alternative way of semantically checking your PRs, see the [Alternatives](#alternatives) section in the README below. GitHub Actions are the way to go! 6 | 7 | Thanks for your understanding. I wish you luck on your journey to semantic englightenment. 8 | 9 | Love, 10 | [@zeke](https://github.com/zeke) 11 | 12 | --- 13 | --- 14 | --- 15 | --- 16 | --- 17 | 18 | 19 | # Semantic Pull Requests 20 | 21 | > GitHub status check that ensures your pull requests follow the Conventional Commits spec 22 | 23 | Using [semantic-release](https://github.com/semantic-release/semantic-release) 24 | and [conventional commit messages](https://conventionalcommits.org)? Install this 25 | [Probot](https://probot.github.io/) app 26 | on your repos to ensure your pull requests are semantic before you merge them. 27 | 28 | :hand: Wanna check your pull requests using a GitHub Actions workflow instead? See [alternatives](#alternatives) below. 29 | 30 | ## How it works 31 | 32 | 👮 Note! The default behavior of this bot is not to police all commit messages, 33 | but rather to ensure that every PR has **just enough semantic information** to be 34 | able to trigger a release when appropriate. The goal is to gather this semantic 35 | information in a way that doesn't make life harder for project contributors, 36 | especially newcomers who may not know how to amend their git commit history. 37 | 38 | By default, only the PR title OR at least one 39 | commit message needs to have semantic prefix. If you wish to change this 40 | behavior, see [configuration](#configuration) section below. 41 | 42 | Scenario | Status | Status Check Message 43 | -------- | ------ | ------- 44 | PR title is [semantic][conventional commit type] | 💚 | `ready to be squashed` 45 | any commit is semantic | 💚 | `ready to be merged or rebased` 46 | nothing is semantic | 💛 | `add a semantic commit or PR title` 47 | 48 | ## Example Scenario 49 | 50 | Take this PR for example. None of the commit messages are semantic, nor is the PR title, so the status remains yellow: 51 | 52 | screen shot 2018-07-14 at 6 22 58 pm 53 | 54 | screen shot 2018-07-14 at 6 22 10 pm 55 | 56 | --- 57 | 58 | Edit the PR title by adding a semantic prefix like `fix: ` or `feat: ` or any other 59 | [conventional commit type]. Now use `Squash and Merge` to squash the branch onto master and write a standardized commit message while doing so: 60 | 61 | --- 62 | 63 | screen shot 2018-07-14 at 6 23 11 pm 64 | 65 | screen shot 2018-07-14 at 6 23 23 pm 66 | 67 | 68 | ## Installation 69 | 70 | 👉 [github.com/apps/semantic-pull-requests](https://github.com/apps/semantic-pull-requests) 71 | 72 | ## Configuration 73 | 74 | By default, no configuration is necessary. 75 | 76 | If you wish to override some 77 | behaviors, you can add a `semantic.yml` file to your `.github` directory with 78 | the following optional settings: 79 | 80 | ```yml 81 | # Disable validation, and skip status check creation 82 | enabled: false 83 | ``` 84 | 85 | ```yml 86 | # Always validate the PR title, and ignore the commits 87 | titleOnly: true 88 | ``` 89 | 90 | ```yml 91 | # Always validate all commits, and ignore the PR title 92 | commitsOnly: true 93 | ``` 94 | 95 | ```yml 96 | # Always validate the PR title AND all the commits 97 | titleAndCommits: true 98 | ``` 99 | 100 | ```yml 101 | # Require at least one commit to be valid 102 | # this is only relevant when using commitsOnly: true or titleAndCommits: true, 103 | # which validate all commits by default 104 | anyCommit: true 105 | ``` 106 | 107 | ```yml 108 | # You can define a list of valid scopes 109 | scopes: 110 | - scope1 111 | - scope2 112 | ... 113 | ``` 114 | 115 | ```yml 116 | # By default types specified in commitizen/conventional-commit-types is used. 117 | # See: https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json 118 | # You can override the valid types 119 | types: 120 | - feat 121 | - fix 122 | - docs 123 | - style 124 | - refactor 125 | - perf 126 | - test 127 | - build 128 | - ci 129 | - chore 130 | - revert 131 | ``` 132 | 133 | ```yml 134 | # Allow use of Merge commits (eg on github: "Merge branch 'master' into feature/ride-unicorns") 135 | # this is only relevant when using commitsOnly: true (or titleAndCommits: true) 136 | allowMergeCommits: true 137 | ``` 138 | 139 | ```yml 140 | # Allow use of Revert commits (eg on github: "Revert "feat: ride unicorns"") 141 | # this is only relevant when using commitsOnly: true (or titleAndCommits: true) 142 | allowRevertCommits: true 143 | ``` 144 | 145 | ## Alternatives 146 | 147 | This project is a GitHub App that you can install on one or many repositories, making it a convenient choice if you want to use it on lots of different repos, or even an entire GitHub organization full of repos. Now that this GitHub App is no longer available, [Semantic PRs](https://github.com/marketplace/semantic-prs) can be used as a drop-in replacement GitHub App. 148 | 149 | If, however, you want more control over exactly how and when your pull requests are semantically checked, consider writing your own custom Actions workflow using a GitHub Action like [amannn/action-semantic-pull-request](https://github.com/amannn/action-semantic-pull-request). 150 | 151 | See also https://github.com/squash-commit-app/squash-commit-app, a GitHub App which adds an empty commit to an open pull request with a single commit when the title was changed 152 | 153 | ## License 154 | 155 | [Apache 2.0](LICENSE) 156 | 157 | [conventional commit type]: https://github.com/commitizen/conventional-commit-types/blob/master/index.json 158 | -------------------------------------------------------------------------------- /__tests__/handle-pull-request-change.js: -------------------------------------------------------------------------------- 1 | const handlePullRequestChange = require('../lib/handle-pull-request-change') 2 | const nock = require('nock') 3 | const github = require('@octokit/rest')() 4 | 5 | // prevent all network activity to ensure mocks are used 6 | nock.disableNetConnect() 7 | 8 | describe('handlePullRequestChange', () => { 9 | test('it is a function', () => { 10 | expect(typeof handlePullRequestChange).toBe('function') 11 | }) 12 | 13 | test('sets `failure` status if PR has no semantic commits and no semantic title', async () => { 14 | const context = buildContext() 15 | context.payload.pull_request.title = 'do a thing' 16 | const expectedBody = { 17 | state: 'failure', 18 | target_url: 'https://github.com/probot/semantic-pull-requests', 19 | description: 'add a semantic commit or PR title', 20 | context: 'Semantic Pull Request' 21 | } 22 | 23 | const mock = nock('https://api.github.com') 24 | .get('/repos/sally/project-x/pulls/123/commits') 25 | .reply(200, unsemanticCommits()) 26 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 27 | .reply(200) 28 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 29 | .reply(200, getConfigResponse()) 30 | 31 | await handlePullRequestChange(context) 32 | expect(mock.isDone()).toBe(true) 33 | }) 34 | 35 | describe('when `enabled` is set to `false` in config', () => { 36 | test('sets `success` status with a skipped message', async () => { 37 | const context = buildContext() 38 | context.payload.pull_request.title = 'do a thing' 39 | const expectedBody = { 40 | state: 'success', 41 | target_url: 'https://github.com/probot/semantic-pull-requests', 42 | description: 'skipped; check disabled in semantic.yml config', 43 | context: 'Semantic Pull Request' 44 | } 45 | 46 | const mock = nock('https://api.github.com') 47 | .get('/repos/sally/project-x/pulls/123/commits') 48 | .reply(200, unsemanticCommits()) 49 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 50 | .reply(200) 51 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 52 | .reply(200, getConfigResponse(`enabled: false`)) 53 | 54 | await handlePullRequestChange(context) 55 | expect(mock.isDone()).toBe(true) 56 | }) 57 | }) 58 | 59 | describe('custom scopes', () => { 60 | test('sets `success` status if PR has semantic title with available scope', async () => { 61 | const context = buildContext() 62 | context.payload.pull_request.title = 'fix(scope1): bananas' 63 | const expectedBody = { 64 | state: 'success', 65 | description: 'ready to be squashed', 66 | target_url: 'https://github.com/probot/semantic-pull-requests', 67 | context: 'Semantic Pull Request' 68 | } 69 | 70 | const mock = nock('https://api.github.com') 71 | .get('/repos/sally/project-x/pulls/123/commits') 72 | .reply(200, semanticCommits()) 73 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 74 | .reply(200) 75 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 76 | .reply(200, getConfigResponse(` 77 | titleOnly: true 78 | scopes: 79 | - scope1 80 | - scope2 81 | `)) 82 | 83 | await handlePullRequestChange(context) 84 | expect(mock.isDone()).toBe(true) 85 | }) 86 | 87 | test('sets `failure` status if PR has semantic title with invalid scope', async () => { 88 | const context = buildContext() 89 | context.payload.pull_request.title = 'fix(scope3): do a thing' 90 | const expectedBody = { 91 | state: 'failure', 92 | target_url: 'https://github.com/probot/semantic-pull-requests', 93 | description: 'add a semantic PR title', 94 | context: 'Semantic Pull Request' 95 | } 96 | 97 | const mock = nock('https://api.github.com') 98 | .get('/repos/sally/project-x/pulls/123/commits') 99 | .reply(200, semanticCommits()) 100 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 101 | .reply(200) 102 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 103 | .reply(200, getConfigResponse(` 104 | titleOnly: true 105 | scopes: 106 | - scope1 107 | - scope2 108 | `)) 109 | 110 | await handlePullRequestChange(context) 111 | expect(mock.isDone()).toBe(true) 112 | }) 113 | 114 | test('sets `failure` status if PR has semantic commit with invalid scope', async () => { 115 | const context = buildContext() 116 | context.payload.pull_request.title = 'fix(scope3): do a thing' 117 | const expectedBody = { 118 | state: 'failure', 119 | target_url: 'https://github.com/probot/semantic-pull-requests', 120 | description: 'make sure every commit is semantic', 121 | context: 'Semantic Pull Request' 122 | } 123 | 124 | const mock = nock('https://api.github.com') 125 | .get('/repos/sally/project-x/pulls/123/commits') 126 | .reply(200, semanticCommits()) 127 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 128 | .reply(200) 129 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 130 | .reply(200, getConfigResponse(` 131 | commitsOnly: true 132 | scopes: 133 | - scope3 134 | - scope4 135 | ` 136 | )) 137 | 138 | await handlePullRequestChange(context) 139 | expect(mock.isDone()).toBe(true) 140 | }) 141 | 142 | test('sets `success` status if PR has semantic title with available scope', async () => { 143 | const context = buildContext() 144 | context.payload.pull_request.title = 'fix(scope1): bananas' 145 | const expectedBody = { 146 | state: 'success', 147 | description: 'ready to be squashed', 148 | target_url: 'https://github.com/probot/semantic-pull-requests', 149 | context: 'Semantic Pull Request' 150 | } 151 | 152 | const mock = nock('https://api.github.com') 153 | .get('/repos/sally/project-x/pulls/123/commits') 154 | .reply(200, semanticCommits()) 155 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 156 | .reply(200) 157 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 158 | .reply(200, getConfigResponse(` 159 | titleOnly: true 160 | scopes: 161 | - scope1 162 | - scope2 163 | ` 164 | )) 165 | 166 | await handlePullRequestChange(context) 167 | expect(mock.isDone()).toBe(true) 168 | }) 169 | }) 170 | 171 | describe('when `commitsOnly` is set to `true` in config', () => { 172 | test('sets `failure` status if PR has no semantic commits', async () => { 173 | const context = buildContext() 174 | context.payload.pull_request.title = 'do a thing' 175 | const expectedBody = { 176 | state: 'failure', 177 | target_url: 'https://github.com/probot/semantic-pull-requests', 178 | description: 'make sure every commit is semantic', 179 | context: 'Semantic Pull Request' 180 | } 181 | 182 | const mock = nock('https://api.github.com') 183 | .get('/repos/sally/project-x/pulls/123/commits') 184 | .reply(200, unsemanticCommits()) 185 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 186 | .reply(200) 187 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 188 | .reply(200, getConfigResponse('commitsOnly: true')) 189 | 190 | await handlePullRequestChange(context) 191 | expect(mock.isDone()).toBe(true) 192 | }) 193 | 194 | test('sets `failure` status if PR has no semantic commits but has a semantic title', async () => { 195 | const context = buildContext() 196 | context.payload.pull_request.title = 'fix: do a thing' 197 | const expectedBody = { 198 | state: 'failure', 199 | target_url: 'https://github.com/probot/semantic-pull-requests', 200 | description: 'make sure every commit is semantic', 201 | context: 'Semantic Pull Request' 202 | } 203 | 204 | const mock = nock('https://api.github.com') 205 | .get('/repos/sally/project-x/pulls/123/commits') 206 | .reply(200, unsemanticCommits()) 207 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 208 | .reply(200) 209 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 210 | .reply(200, getConfigResponse('commitsOnly: true')) 211 | 212 | await handlePullRequestChange(context) 213 | expect(mock.isDone()).toBe(true) 214 | }) 215 | 216 | test('sets `failure` status if one or commits are not well formed', async () => { 217 | const context = buildContext() 218 | context.payload.pull_request.title = 'do a thing' 219 | const expectedBody = { 220 | state: 'failure', 221 | target_url: 'https://github.com/probot/semantic-pull-requests', 222 | description: 'make sure every commit is semantic', 223 | context: 'Semantic Pull Request' 224 | } 225 | 226 | const mock = nock('https://api.github.com') 227 | .get('/repos/sally/project-x/pulls/123/commits') 228 | .reply(200, [...unsemanticCommits(), ...semanticCommits()]) 229 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 230 | .reply(200) 231 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 232 | .reply(200, getConfigResponse('commitsOnly: true')) 233 | 234 | await handlePullRequestChange(context) 235 | expect(mock.isDone()).toBe(true) 236 | }) 237 | 238 | test('Only lints commits', async () => { 239 | const context = buildContext() 240 | context.payload.pull_request.title = 'bananas' 241 | const expectedBody = { 242 | state: 'success', 243 | target_url: 'https://github.com/probot/semantic-pull-requests', 244 | description: 'ready to be merged or rebased', 245 | context: 'Semantic Pull Request' 246 | } 247 | 248 | const mock = nock('https://api.github.com') 249 | .get('/repos/sally/project-x/pulls/123/commits') 250 | .reply(200, semanticCommits()) 251 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 252 | .reply(200) 253 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 254 | .reply(200, getConfigResponse('commitsOnly: true')) 255 | 256 | await handlePullRequestChange(context) 257 | expect(mock.isDone()).toBe(true) 258 | }) 259 | }) 260 | 261 | describe('when `titleOnly` is set to `true` in config', () => { 262 | test('sets `failure` status if PR has no semantic PR title', async () => { 263 | const context = buildContext() 264 | context.payload.pull_request.title = 'do a thing' 265 | const expectedBody = { 266 | state: 'failure', 267 | target_url: 'https://github.com/probot/semantic-pull-requests', 268 | description: 'add a semantic PR title', 269 | context: 'Semantic Pull Request' 270 | } 271 | 272 | const mock = nock('https://api.github.com') 273 | .get('/repos/sally/project-x/pulls/123/commits') 274 | .reply(200, unsemanticCommits()) 275 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 276 | .reply(200) 277 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 278 | .reply(200, getConfigResponse('titleOnly: true')) 279 | 280 | await handlePullRequestChange(context) 281 | expect(mock.isDone()).toBe(true) 282 | }) 283 | 284 | test('sets `failure` status if PR has no semantic PR title but has semantic commits', async () => { 285 | const context = buildContext() 286 | context.payload.pull_request.title = 'do a thing' 287 | const expectedBody = { 288 | state: 'failure', 289 | target_url: 'https://github.com/probot/semantic-pull-requests', 290 | description: 'add a semantic PR title', 291 | context: 'Semantic Pull Request' 292 | } 293 | 294 | const mock = nock('https://api.github.com') 295 | .get('/repos/sally/project-x/pulls/123/commits') 296 | .reply(200, semanticCommits()) 297 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 298 | .reply(200) 299 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 300 | .reply(200, getConfigResponse('titleOnly: true')) 301 | 302 | await handlePullRequestChange(context) 303 | expect(mock.isDone()).toBe(true) 304 | }) 305 | 306 | test('Only lints title', async () => { 307 | const context = buildContext() 308 | context.payload.pull_request.title = 'build: do a thing' 309 | const expectedBody = { 310 | state: 'success', 311 | target_url: 'https://github.com/probot/semantic-pull-requests', 312 | description: 'ready to be squashed', 313 | context: 'Semantic Pull Request' 314 | } 315 | 316 | const mock = nock('https://api.github.com') 317 | .get('/repos/sally/project-x/pulls/123/commits') 318 | .reply(200, unsemanticCommits()) 319 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 320 | .reply(200) 321 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 322 | .reply(200, getConfigResponse('titleOnly: true')) 323 | 324 | await handlePullRequestChange(context) 325 | expect(mock.isDone()).toBe(true) 326 | }) 327 | }) 328 | 329 | describe('when `titleAndCommits` is set to `true` in config', () => { 330 | test('sets `failure` status if PR has no semantic PR title', async () => { 331 | const context = buildContext() 332 | context.payload.pull_request.title = 'do a thing' 333 | const expectedBody = { 334 | state: 'failure', 335 | target_url: 'https://github.com/probot/semantic-pull-requests', 336 | description: 'add a semantic commit AND PR title', 337 | context: 'Semantic Pull Request' 338 | } 339 | 340 | const mock = nock('https://api.github.com') 341 | .get('/repos/sally/project-x/pulls/123/commits') 342 | .reply(200, unsemanticCommits()) 343 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 344 | .reply(200) 345 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 346 | .reply(200, getConfigResponse('titleAndCommits: true')) 347 | 348 | await handlePullRequestChange(context) 349 | expect(mock.isDone()).toBe(true) 350 | }) 351 | 352 | test('sets `failure` status if PR has no semantic PR title but has semantic commits', async () => { 353 | const context = buildContext() 354 | context.payload.pull_request.title = 'do a thing' 355 | const expectedBody = { 356 | state: 'failure', 357 | target_url: 'https://github.com/probot/semantic-pull-requests', 358 | description: 'add a semantic commit AND PR title', 359 | context: 'Semantic Pull Request' 360 | } 361 | 362 | const mock = nock('https://api.github.com') 363 | .get('/repos/sally/project-x/pulls/123/commits') 364 | .reply(200, semanticCommits()) 365 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 366 | .reply(200) 367 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 368 | .reply(200, getConfigResponse('titleAndCommits: true')) 369 | 370 | await handlePullRequestChange(context) 371 | expect(mock.isDone()).toBe(true) 372 | }) 373 | 374 | test('sets `failure` status if PR has no semantic PR title and has no semantic commits', async () => { 375 | const context = buildContext() 376 | context.payload.pull_request.title = 'do a thing' 377 | const expectedBody = { 378 | state: 'failure', 379 | target_url: 'https://github.com/probot/semantic-pull-requests', 380 | description: 'add a semantic commit AND PR title', 381 | context: 'Semantic Pull Request' 382 | } 383 | 384 | const mock = nock('https://api.github.com') 385 | .get('/repos/sally/project-x/pulls/123/commits') 386 | .reply(200, unsemanticCommits()) 387 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 388 | .reply(200) 389 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 390 | .reply(200, getConfigResponse('titleAndCommits: true')) 391 | 392 | await handlePullRequestChange(context) 393 | expect(mock.isDone()).toBe(true) 394 | }) 395 | 396 | test('lints title and commits', async () => { 397 | const context = buildContext() 398 | context.payload.pull_request.title = 'build: do a thing' 399 | const expectedBody = { 400 | state: 'success', 401 | target_url: 'https://github.com/probot/semantic-pull-requests', 402 | description: 'ready to be merged, squashed or rebased', 403 | context: 'Semantic Pull Request' 404 | } 405 | 406 | const mock = nock('https://api.github.com') 407 | .get('/repos/sally/project-x/pulls/123/commits') 408 | .reply(200, semanticCommits()) 409 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 410 | .reply(200) 411 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 412 | .reply(200, getConfigResponse('titleAndCommits: true')) 413 | 414 | await handlePullRequestChange(context) 415 | expect(mock.isDone()).toBe(true) 416 | }) 417 | }) 418 | 419 | describe('when `anyCommit` is set to `true` in config', () => { 420 | describe.each([ 421 | ['commitsOnly', 'add a semantic commit'], 422 | ['titleAndCommits', 'add a semantic commit AND PR title'] 423 | ])('when no commits are semantic and `%s` is set to `true` in config', (configOption, description) => { 424 | test(('sets `failure` status with description: ' + description), async () => { 425 | const context = buildContext() 426 | context.payload.pull_request.title = 'fix: bananas' 427 | const expectedBody = { 428 | state: 'failure', 429 | target_url: 'https://github.com/probot/semantic-pull-requests', 430 | description: description, 431 | context: 'Semantic Pull Request' 432 | } 433 | 434 | const mock = nock('https://api.github.com') 435 | .get('/repos/sally/project-x/pulls/123/commits') 436 | .reply(200, unsemanticCommits()) 437 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 438 | .reply(200) 439 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 440 | .reply(200, getConfigResponse(configOption + ': true\nanyCommit: true')) 441 | 442 | await handlePullRequestChange(context) 443 | expect(mock.isDone()).toBe(true) 444 | }) 445 | }) 446 | 447 | describe.each([ 448 | ['commitsOnly', 'ready to be merged or rebased'], 449 | ['titleAndCommits', 'ready to be merged, squashed or rebased'] 450 | ])('when all commits are semantic and `%s` is set to `true` in config', (configOption, description) => { 451 | test(('sets `success` status with description: ' + description), async () => { 452 | const context = buildContext() 453 | context.payload.pull_request.title = 'fix: bananas' 454 | const expectedBody = { 455 | state: 'success', 456 | target_url: 'https://github.com/probot/semantic-pull-requests', 457 | description: description, 458 | context: 'Semantic Pull Request' 459 | } 460 | 461 | const mock = nock('https://api.github.com') 462 | .get('/repos/sally/project-x/pulls/123/commits') 463 | .reply(200, semanticCommits()) 464 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 465 | .reply(200) 466 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 467 | .reply(200, getConfigResponse(configOption + ': true\nanyCommit: true')) 468 | 469 | await handlePullRequestChange(context) 470 | expect(mock.isDone()).toBe(true) 471 | }) 472 | }) 473 | 474 | describe.each([ 475 | ['commitsOnly', 'ready to be merged or rebased'], 476 | ['titleAndCommits', 'ready to be merged, squashed or rebased'] 477 | ])('when some commits are semantic and `%s` is set to `true` in config', (configOption, description) => { 478 | test(('sets `success` status with description: ' + description), async () => { 479 | const context = buildContext() 480 | context.payload.pull_request.title = 'fix: bananas' 481 | const expectedBody = { 482 | state: 'success', 483 | target_url: 'https://github.com/probot/semantic-pull-requests', 484 | description: description, 485 | context: 'Semantic Pull Request' 486 | } 487 | 488 | const mock = nock('https://api.github.com') 489 | .get('/repos/sally/project-x/pulls/123/commits') 490 | .reply(200, [...unsemanticCommits(), ...semanticCommits()]) 491 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 492 | .reply(200) 493 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 494 | .reply(200, getConfigResponse(configOption + ': true\nanyCommit: true')) 495 | 496 | await handlePullRequestChange(context) 497 | expect(mock.isDone()).toBe(true) 498 | }) 499 | }) 500 | }) 501 | 502 | describe('when `allowMergeCommits` is set to `true` AND `commitsOnly`is set to `true` too in config', () => { 503 | test('sets `success` status if PR has Merge commit', async () => { 504 | const context = buildContext() 505 | context.payload.pull_request.title = 'fix: bananas' 506 | const expectedBody = { 507 | state: 'success', 508 | description: 'ready to be merged or rebased', 509 | target_url: 'https://github.com/probot/semantic-pull-requests', 510 | context: 'Semantic Pull Request' 511 | } 512 | 513 | const mock = nock('https://api.github.com') 514 | .get('/repos/sally/project-x/pulls/123/commits') 515 | .reply(200, [ 516 | ...semanticCommits(), 517 | { commit: { message: 'Merge branch \'master\' into feature/logout' } } 518 | ]) 519 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 520 | .reply(200) 521 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 522 | .reply(200, getConfigResponse(` 523 | commitsOnly: true 524 | scopes: 525 | - scope1 526 | - scope2 527 | allowMergeCommits: true 528 | `)) 529 | 530 | await handlePullRequestChange(context) 531 | expect(mock.isDone()).toBe(true) 532 | }) 533 | }) 534 | 535 | describe('when `allowMergeCommits` is set to `false` AND `commitsOnly` is set to `true` in config', () => { 536 | test('sets `failure` status if PR has Merge commit', async () => { 537 | const context = buildContext() 538 | context.payload.pull_request.title = 'fix: bananas' 539 | const expectedBody = { 540 | state: 'failure', 541 | description: 'make sure every commit is semantic', 542 | target_url: 'https://github.com/probot/semantic-pull-requests', 543 | context: 'Semantic Pull Request' 544 | } 545 | 546 | const mock = nock('https://api.github.com') 547 | .get('/repos/sally/project-x/pulls/123/commits') 548 | .reply(200, [ 549 | { commit: { message: 'Merge branch \'master\' into feature/logout' } } 550 | ]) 551 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 552 | .reply(200) 553 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 554 | .reply(200, getConfigResponse(` 555 | commitsOnly: true 556 | allowMergeCommits: false 557 | `)) 558 | 559 | await handlePullRequestChange(context) 560 | expect(mock.isDone()).toBe(true) 561 | }) 562 | }) 563 | 564 | describe('when `allowRevertCommits` is set to `true` AND `commitsOnly`is set to `true` too in config', () => { 565 | test('sets `success` status if PR has Merge commit', async () => { 566 | const context = buildContext() 567 | context.payload.pull_request.title = 'fix: bananas' 568 | const expectedBody = { 569 | state: 'success', 570 | description: 'ready to be merged or rebased', 571 | target_url: 'https://github.com/probot/semantic-pull-requests', 572 | context: 'Semantic Pull Request' 573 | } 574 | 575 | const mock = nock('https://api.github.com') 576 | .get('/repos/sally/project-x/pulls/123/commits') 577 | .reply(200, [ 578 | ...semanticCommits(), 579 | { commit: { message: 'Revert "feat: ride unicorns"' } } 580 | ]) 581 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 582 | .reply(200) 583 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 584 | .reply(200, getConfigResponse(` 585 | commitsOnly: true 586 | scopes: 587 | - scope1 588 | - scope2 589 | allowRevertCommits: true 590 | `)) 591 | 592 | await handlePullRequestChange(context) 593 | expect(mock.isDone()).toBe(true) 594 | }) 595 | }) 596 | 597 | describe('when `allowRevertCommits` is set to `false` AND `commitsOnly` is set to `true` in config', () => { 598 | test('sets `failure` status if PR has Merge commit', async () => { 599 | const context = buildContext() 600 | context.payload.pull_request.title = 'fix: bananas' 601 | const expectedBody = { 602 | state: 'failure', 603 | description: 'make sure every commit is semantic', 604 | target_url: 'https://github.com/probot/semantic-pull-requests', 605 | context: 'Semantic Pull Request' 606 | } 607 | 608 | const mock = nock('https://api.github.com') 609 | .get('/repos/sally/project-x/pulls/123/commits') 610 | .reply(200, [ 611 | { commit: { message: 'Revert "feat: ride unicorns"' } } 612 | ]) 613 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 614 | .reply(200) 615 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 616 | .reply(200, getConfigResponse(` 617 | commitsOnly: true 618 | allowRevertCommits: false 619 | `)) 620 | 621 | await handlePullRequestChange(context) 622 | expect(mock.isDone()).toBe(true) 623 | }) 624 | }) 625 | 626 | describe('vanilla setup (no config)', () => { 627 | test('sets `success` status and `ready to be merged or squashed` description if PR has semantic commits but no semantic title', async () => { 628 | const context = buildContext() 629 | context.payload.pull_request.title = 'bananas' 630 | const expectedBody = { 631 | state: 'success', 632 | description: 'ready to be merged or rebased', 633 | target_url: 'https://github.com/probot/semantic-pull-requests', 634 | context: 'Semantic Pull Request' 635 | } 636 | 637 | const mock = nock('https://api.github.com') 638 | .get('/repos/sally/project-x/pulls/123/commits') 639 | .reply(200, semanticCommits()) 640 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 641 | .reply(200) 642 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 643 | .reply(200, getConfigResponse()) 644 | 645 | await handlePullRequestChange(context) 646 | expect(mock.isDone()).toBe(true) 647 | }) 648 | 649 | test('sets `failure` status and `PR has only one non-merge commit and it\'s not semantic` description if PR has has only one un-semantic commit', async () => { 650 | const context = buildContext() 651 | context.payload.pull_request.title = 'fix: I am a semantic PR title but there is only commit unsemantic commit that will not be squashed!' 652 | const expectedBody = { 653 | state: 'failure', 654 | description: 'PR has only one non-merge commit and it\'s not semantic; add another commit before squashing', 655 | target_url: 'https://github.com/probot/semantic-pull-requests', 656 | context: 'Semantic Pull Request' 657 | } 658 | 659 | const mock = nock('https://api.github.com') 660 | .get('/repos/sally/project-x/pulls/123/commits') 661 | .reply(200, [ 662 | { commit: { message: 'fix something' } }, 663 | { commit: { message: 'Merge something' } }, 664 | { commit: { message: 'Merge something else' } } 665 | ]) 666 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 667 | .reply(200) 668 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 669 | .reply(200, getConfigResponse()) 670 | 671 | await handlePullRequestChange(context) 672 | expect(mock.isDone()).toBe(true) 673 | }) 674 | 675 | test('encourages squashing when title is semantic but commits are not', async () => { 676 | const context = buildContext() 677 | context.payload.pull_request.title = 'fix: bananas' 678 | const expectedBody = { 679 | state: 'success', 680 | description: 'ready to be squashed', 681 | target_url: 'https://github.com/probot/semantic-pull-requests', 682 | context: 'Semantic Pull Request' 683 | } 684 | 685 | // since the title is semantic, no GET request for commits is needed 686 | const mock = nock('https://api.github.com') 687 | .get('/repos/sally/project-x/pulls/123/commits') 688 | .reply(200, unsemanticCommits()) 689 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 690 | .reply(200) 691 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 692 | .reply(200, getConfigResponse()) 693 | 694 | await handlePullRequestChange(context) 695 | expect(mock.isDone()).toBe(true) 696 | }) 697 | 698 | test('allows `build:` as a prefix', async () => { 699 | const context = buildContext() 700 | context.payload.pull_request.title = 'build: publish to npm' 701 | const expectedBody = { 702 | state: 'success', 703 | description: 'ready to be squashed', 704 | target_url: 'https://github.com/probot/semantic-pull-requests', 705 | context: 'Semantic Pull Request' 706 | } 707 | 708 | // since the title is semantic, no GET request for commits is needed 709 | const mock = nock('https://api.github.com') 710 | .get('/repos/sally/project-x/pulls/123/commits') 711 | .reply(200, unsemanticCommits()) 712 | .post('/repos/sally/project-x/statuses/abcdefg', expectedBody) 713 | .reply(200) 714 | .get('/repos/sally/project-x/contents/.github/semantic.yml') 715 | .reply(200, getConfigResponse()) 716 | 717 | await handlePullRequestChange(context) 718 | expect(mock.isDone()).toBe(true) 719 | }) 720 | }) 721 | }) 722 | 723 | function unsemanticCommits () { 724 | return [ 725 | { commit: { message: 'fix something' } }, 726 | { commit: { message: 'fix something else' } } 727 | ] 728 | } 729 | 730 | function semanticCommits () { 731 | return [ 732 | { commit: { message: 'build(scope1): something' } }, 733 | { commit: { message: 'build(scope2): something else' } } 734 | ] 735 | } 736 | 737 | function buildContext (overrides) { 738 | const defaults = { 739 | log: () => { /* no-op */ }, 740 | 741 | // an instantiated GitHub client like the one probot provides 742 | github: github, 743 | 744 | // context.repo() is a probot convenience function 745 | repo: (obj = {}) => { 746 | return Object.assign({ owner: 'sally', repo: 'project-x' }, obj) 747 | }, 748 | 749 | payload: { 750 | pull_request: { 751 | number: 123, 752 | title: 'do a thing', 753 | head: { 754 | sha: 'abcdefg' 755 | } 756 | } 757 | } 758 | } 759 | 760 | return Object.assign({}, defaults, overrides) 761 | } 762 | 763 | function getConfigResponse (content = '') { 764 | return { content: Buffer.from(content).toString('base64') } 765 | } 766 | -------------------------------------------------------------------------------- /__tests__/is-semantic-message.js: -------------------------------------------------------------------------------- 1 | const isSemanticMessage = require('../lib/is-semantic-message') 2 | 3 | describe('isSemanticMessage', () => { 4 | test('returns true when messages are semantic', () => { 5 | expect(isSemanticMessage('fix: something')).toBe(true) 6 | }) 7 | 8 | test('allows parenthetical scope following the type', () => { 9 | expect(isSemanticMessage('fix(subsystem): something')).toBe(true) 10 | }) 11 | 12 | test('returns false on bad input', () => { 13 | expect(isSemanticMessage('')).toBe(false) 14 | expect(isSemanticMessage(null)).toBe(false) 15 | expect(isSemanticMessage('unsemantic commit message')).toBe(false) 16 | }) 17 | 18 | test('should check message scope', () => { 19 | expect(isSemanticMessage('fix(validScope): something', ['validScope'])).toBe(true) 20 | expect(isSemanticMessage('fix(invalidScope): something', ['validScope'])).toBe(false) 21 | expect(isSemanticMessage('fix(validScope,anotherValidScope): something', ['validScope', 'anotherValidScope'])).toBe(true) 22 | expect(isSemanticMessage('fix(validScope, spaceAndAnotherValidScope): something', ['validScope', 'spaceAndAnotherValidScope'])).toBe(true) 23 | expect(isSemanticMessage('fix(validScope,inValidScope): something', ['validScope'])).toBe(false) 24 | expect(isSemanticMessage('fix(): something', ['validScope'])).toBe(false) 25 | }) 26 | 27 | test('allows merge commits', () => { 28 | expect(isSemanticMessage('Merge branch \'master\' into patch-1', null, null, true)).toBe(true) 29 | 30 | expect(isSemanticMessage('Merge refs/heads/master into angry-burritos/US-335', ['scope1'], null, true)).toBe(true) 31 | }) 32 | }) 33 | 34 | describe('isSemanticMessage - Handling validTypes', () => { 35 | test('return true for all default types when validTypes is provided', () => { 36 | expect(isSemanticMessage('feat: something')).toBe(true) 37 | expect(isSemanticMessage('fix: something')).toBe(true) 38 | expect(isSemanticMessage('docs: something')).toBe(true) 39 | expect(isSemanticMessage('style: something')).toBe(true) 40 | expect(isSemanticMessage('refactor: something')).toBe(true) 41 | expect(isSemanticMessage('perf: something')).toBe(true) 42 | expect(isSemanticMessage('test: something')).toBe(true) 43 | expect(isSemanticMessage('build: something')).toBe(true) 44 | expect(isSemanticMessage('ci: something')).toBe(true) 45 | expect(isSemanticMessage('chore: something')).toBe(true) 46 | expect(isSemanticMessage('revert: something')).toBe(true) 47 | }) 48 | 49 | test('return false for none default types when validTypes is provided', () => { 50 | expect(isSemanticMessage('alternative: something')).toBe(false) 51 | }) 52 | 53 | test('return true for types included in supplied validTypes', () => { 54 | const customConventionalCommitType = [ 55 | 'alternative', 56 | 'improvement' 57 | ] 58 | 59 | expect(isSemanticMessage('alternative: something', null, customConventionalCommitType)).toBe(true) 60 | expect(isSemanticMessage('improvement: something', null, customConventionalCommitType)).toBe(true) 61 | }) 62 | 63 | test('return false for types NOT included in supplied validTypes', () => { 64 | const customConventionalCommitType = [ 65 | 'alternative', 66 | 'improvement' 67 | ] 68 | 69 | expect(isSemanticMessage('feat: something', null, customConventionalCommitType)).toBe(false) 70 | expect(isSemanticMessage('fix: something', null, customConventionalCommitType)).toBe(false) 71 | expect(isSemanticMessage('docs: something', null, customConventionalCommitType)).toBe(false) 72 | expect(isSemanticMessage('style: something', null, customConventionalCommitType)).toBe(false) 73 | expect(isSemanticMessage('refactor: something', null, customConventionalCommitType)).toBe(false) 74 | expect(isSemanticMessage('perf: something', null, customConventionalCommitType)).toBe(false) 75 | expect(isSemanticMessage('test: something', null, customConventionalCommitType)).toBe(false) 76 | expect(isSemanticMessage('build: something', null, customConventionalCommitType)).toBe(false) 77 | expect(isSemanticMessage('ci: something', null, customConventionalCommitType)).toBe(false) 78 | expect(isSemanticMessage('chore: something', null, customConventionalCommitType)).toBe(false) 79 | expect(isSemanticMessage('revert: something', null, customConventionalCommitType)).toBe(false) 80 | }) 81 | }) 82 | -------------------------------------------------------------------------------- /assets/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zeke/semantic-pull-requests/f4916f442d077624ea7bc24751be187be3c3ac9f/assets/icon.png -------------------------------------------------------------------------------- /assets/icon.sketch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zeke/semantic-pull-requests/f4916f442d077624ea7bc24751be187be3c3ac9f/assets/icon.sketch -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = probotPlugin 2 | 3 | const handlePullRequestChange = require('./lib/handle-pull-request-change') 4 | 5 | function probotPlugin (robot) { 6 | robot.on([ 7 | 'pull_request.opened', 8 | 'pull_request.edited', 9 | 'pull_request.synchronize' 10 | ], handlePullRequestChange) 11 | } 12 | -------------------------------------------------------------------------------- /lib/handle-pull-request-change.js: -------------------------------------------------------------------------------- 1 | module.exports = handlePullRequestChange 2 | 3 | const isSemanticMessage = require('./is-semantic-message') 4 | const getConfig = require('probot-config') 5 | 6 | const DEFAULT_OPTS = { 7 | enabled: true, 8 | titleOnly: false, 9 | commitsOnly: false, 10 | titleAndCommits: false, 11 | anyCommit: false, 12 | scopes: null, 13 | types: null, 14 | allowMergeCommits: false, 15 | allowRevertCommits: false 16 | } 17 | 18 | async function getCommits (context) { 19 | const commits = await context.github.pullRequests.getCommits(context.repo({ 20 | number: context.payload.pull_request.number 21 | })) 22 | return commits.data 23 | } 24 | 25 | async function commitsAreSemantic (commits, scopes, types, allCommits = false, allowMergeCommits, allowRevertCommits) { 26 | return commits 27 | .map(element => element.commit)[allCommits ? 'every' : 'some'](commit => isSemanticMessage(commit.message, scopes, types, allowMergeCommits, allowRevertCommits)) 28 | } 29 | 30 | async function handlePullRequestChange (context) { 31 | const { title, head } = context.payload.pull_request 32 | const userConfig = await getConfig(context, 'semantic.yml', {}) 33 | const isVanillaConfig = Object.keys(userConfig).length === 0 34 | const { 35 | enabled, 36 | titleOnly, 37 | commitsOnly, 38 | titleAndCommits, 39 | anyCommit, 40 | scopes, 41 | types, 42 | allowMergeCommits, 43 | allowRevertCommits 44 | } = Object.assign({}, DEFAULT_OPTS, userConfig) 45 | 46 | const hasSemanticTitle = isSemanticMessage(title, scopes, types) 47 | const commits = await getCommits(context) 48 | const hasSemanticCommits = await commitsAreSemantic(commits, scopes, types, (commitsOnly || titleAndCommits) && !anyCommit, allowMergeCommits, allowRevertCommits) 49 | const nonMergeCommits = commits.filter(element => !element.commit.message.startsWith('Merge')) 50 | 51 | let isSemantic 52 | 53 | if (!enabled) { 54 | isSemantic = true 55 | } else if (titleOnly) { 56 | isSemantic = hasSemanticTitle 57 | } else if (commitsOnly) { 58 | isSemantic = hasSemanticCommits 59 | } else if (titleAndCommits) { 60 | isSemantic = hasSemanticTitle && hasSemanticCommits 61 | } else if (isVanillaConfig && nonMergeCommits.length === 1) { 62 | // Watch out for cases where there's only commit and it's not semantic. 63 | // GitHub won't squash PRs that have only one commit. 64 | isSemantic = hasSemanticCommits 65 | } else { 66 | isSemantic = hasSemanticTitle || hasSemanticCommits 67 | } 68 | 69 | const state = isSemantic ? 'success' : 'failure' 70 | 71 | function getDescription () { 72 | if (!enabled) return 'skipped; check disabled in semantic.yml config' 73 | if (!isSemantic && isVanillaConfig && nonMergeCommits.length === 1) return 'PR has only one non-merge commit and it\'s not semantic; add another commit before squashing' 74 | if (isSemantic && titleAndCommits) return 'ready to be merged, squashed or rebased' 75 | if (!isSemantic && titleAndCommits) return 'add a semantic commit AND PR title' 76 | if (hasSemanticTitle && !commitsOnly) return 'ready to be squashed' 77 | if (hasSemanticCommits && !titleOnly) return 'ready to be merged or rebased' 78 | if (titleOnly) return 'add a semantic PR title' 79 | if (commitsOnly && anyCommit) return 'add a semantic commit' 80 | if (commitsOnly) return 'make sure every commit is semantic' 81 | return 'add a semantic commit or PR title' 82 | } 83 | 84 | const status = { 85 | sha: head.sha, 86 | state, 87 | target_url: 'https://github.com/probot/semantic-pull-requests', 88 | description: getDescription(), 89 | context: 'Semantic Pull Request' 90 | } 91 | const result = await context.github.repos.createStatus(context.repo(status)) 92 | return result 93 | } 94 | -------------------------------------------------------------------------------- /lib/is-semantic-message.js: -------------------------------------------------------------------------------- 1 | const commitTypes = Object.keys(require('conventional-commit-types').types) 2 | const { validate } = require('parse-commit-message') 3 | 4 | module.exports = function isSemanticMessage (message, validScopes, validTypes, allowMergeCommits, allowRevertCommits) { 5 | const isMergeCommit = message && message.startsWith('Merge') 6 | if (allowMergeCommits && isMergeCommit) return true 7 | 8 | const isRevertCommit = message && message.startsWith('Revert') 9 | if (allowRevertCommits && isRevertCommit) return true 10 | 11 | const { error, value: commits } = validate(message, true) 12 | 13 | if (error) { 14 | if (process.env.NODE_ENV !== 'test') console.error(error) 15 | return false 16 | } 17 | 18 | const [result] = commits 19 | const { scope: scopes, type } = result.header 20 | const isScopeValid = !validScopes || !scopes || scopes.split(',').map(scope => scope.trim()).every(scope => validScopes.includes(scope)) 21 | return (validTypes || commitTypes).includes(type) && isScopeValid 22 | } 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "semantic-pull-requests", 3 | "version": "0.0.0-development", 4 | "description": "GitHub status check that ensures your pull requests follow the Conventional Commits spec", 5 | "main": "index.js", 6 | "files": [ 7 | "index.js", 8 | "lib" 9 | ], 10 | "scripts": { 11 | "start": "probot run ./index.js", 12 | "test": "jest test/* && npm run lint", 13 | "test:watch": "DEBUG=octokitz:rest* jest test/* --watch --notify --notifyMode=change --coverage", 14 | "lint": "standard --fix", 15 | "semantic-release": "semantic-release" 16 | }, 17 | "standard": { 18 | "env": [ 19 | "jest" 20 | ] 21 | }, 22 | "repository": "https://github.com/probot/semantic-pull-requests", 23 | "keywords": [ 24 | "probot", 25 | "probot-plugin" 26 | ], 27 | "author": "Zeke Sikelianos (https://github.com/zeke)", 28 | "license": "Apache-2.0", 29 | "dependencies": { 30 | "conventional-commit-types": "^3.0.0", 31 | "parse-commit-message": "^4.0.0-canary.20", 32 | "probot": "^7.5.3", 33 | "probot-config": "^1.0.0" 34 | }, 35 | "devDependencies": { 36 | "@octokit/rest": "^15.9.4", 37 | "jest": "^23.4.0", 38 | "nock": "^9.4.2", 39 | "semantic-release": "^12.2.2", 40 | "standard": "^12.0.1" 41 | }, 42 | "engines": { 43 | "node": "8" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /scripts/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # https://github.com/zeit/now-cli/issues/817 4 | now="npx now --debug --token=$NOW_TOKEN" 5 | 6 | echo "$ now rm --safe --yes wip-bot" 7 | $now rm --safe --yes wip-bot 8 | 9 | echo "$ now --public" 10 | $now --public 11 | 12 | echo "$ now alias" 13 | $now alias 14 | --------------------------------------------------------------------------------