Skip to content

Commit

Permalink
Initialize repository
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsao committed Oct 10, 2017
0 parents commit d9c67f8
Show file tree
Hide file tree
Showing 11 changed files with 3,933 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"parserOptions": {
"ecmaVersion": 2017,
"ecmaFeatures": {
"experimentalObjectRestSpread": true
}
},
"plugins": [
"prettier"
],
"rules": {
"prettier/prettier": ["error", {
"singleQuote": true,
"trailingComma": "all",
"bracketSpacing": false,
"jsxBracketSameLine": true
}]
}
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
npm-debug.log
*.pem
.env
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
dist: trusty
sudo: false
language: node_js
node_js: 8.6.0
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --version 1.2.0
- export PATH=$HOME/.yarn/bin:$PATH
cache:
yarn: true
before_install: yarn global add greenkeeper-lockfile@1
before_script: greenkeeper-lockfile-update
after_script: greenkeeper-lockfile-upload
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# probot-app-pr-title

> a GitHub App built with [probot](https://github.com/probot/probot) that enforces the format of PR titles/commits
## Setup

```
# Install dependencies
npm install
# Run the bot
npm start
```

See [docs/deploy.md](docs/deploy.md) if you would like to run your own instance of this app.
44 changes: 44 additions & 0 deletions __tests__/validate-title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const t = require('assert');

const isValidTitle = require('../validate-title.js');

test('valid commits pass', () => {
const valid = [
'Update subsystem X for readability',
'Refactor subsystem X for readability',
'Update getting started documentation',
'Remove deprecated methods',
'Release version 1.0.0',
'Merge pull request #123 from user/branch',
'Add tests for X',
'Delete thing',
'Revert whatever',
];

valid.forEach(msg => {
t.equal(isValidTitle(msg), true);
});
});

test('invalid commits fail', () => {
const invalid = [
'Revert',
'fixed bug with Y',
'changing behavior of X',
'more fixes for broken stuff',
'sweet new API methods',
'UPDATE documentation',
'Add a cool thing!',
'I added tests for X',
'Adding tests for X',
'tea is good',
'bottle it up entirely',
'Add cool thing. Add another thing',
`potato doesn't work`,
'revert whatever',
];

invalid.forEach(msg => {
t.equal(isValidTitle(msg), false);
});
});
7 changes: 7 additions & 0 deletions docs/deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Deploying

If you would like to run your own instance of this app, see the [docs for deployment](https://probot.github.io/docs/deployment/).

This app requires these **Permissions & events** for the GitHub App:

> **TODO**: List permissions required for deployment here. See [probot/stale](https://github.com/probot/stale/blob/master/docs/deploy.md) for an example.
33 changes: 33 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const isValidTitle = require('./validate-title.js');

module.exports = robot => {
robot.on('pull_request.opened', check);
robot.on('pull_request.edited', check);

async function check(context) {
const passed = isValidTitle(pr.title);
setStatus(context, passed);
}
};

function setStatus(context, passing) {
const {github} = context;

const status = passing
? {
state: 'success',
description: 'PR title is valid',
}
: {
state: 'failure',
description: 'PR title is invalid',
};

github.repos.createStatus(
context.repo({
...status,
sha: context.payload.pull_request.head.sha,
context: 'pr_title',
}),
);
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "probot-app-pr-title",
"version": "1.0.0",
"description": "GitHub App that enforces the format of PR titles/commits",
"author": "Ryan Tsao <[email protected]>",
"contributors": [],
"repository": "https://github.com/uber-web/probot-app-pr-title.git",
"scripts": {
"start": "probot run ./index.js",
"test": "npm run lint && jest",
"lint": "eslint .",
"fix": "eslint . --fix"
},
"dependencies": {
"compromise": "^10.7.2",
"probot": "^0.11.0"
},
"devDependencies": {
"eslint": "^4.8.0",
"eslint-plugin-prettier": "^2.3.1",
"jest": "^21.2.1",
"localtunnel": "^1.8.3",
"prettier": "^1.7.4"
},
"engines": {
"node": ">= 8.6.0"
},
"license": "MIT"
}
60 changes: 60 additions & 0 deletions validate-title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const nlp = require('compromise');

module.exports = function isValidTitle(text) {
const parsed = nlp(text);
return isValidSentence(parsed) && firstTermIsValidVerb(parsed);
};

// Validate text is a single sentence with no end punctuation
function isValidSentence(parsed) {
const sentences = parsed.sentences();
// must be one and only one sentence
if (sentences.length !== 1) {
return false;
}
// must be at least two words long
if (parsed.terms().length < 2) {
return false;
}
// must not have any end punctuation
if (sentences.list[0].endPunctuation() !== null) {
return false;
}
return true;
}

// The following verbs starting with `re` are categorized as singular nouns
// See: https://github.com/nlp-compromise/compromise/issues/412
const nounWhitelist = new Set([
'refactor',
'release',
'revert',
'restore',
'recycle',
'repeat',
'rebuild',
'reconcile',
'record',
'redefine',
'recover',
'restructure',
'resolve',
]);

// Validate first word is a capitalized imperative mood verb
function firstTermIsValidVerb(parsed) {
const term = parsed.terms().data()[0];
return isValidVerb(term) && isCapitalized(term);
}

function isValidVerb(term) {
return nounWhitelist.has(term.normal) || isInfinitiveVerb(term);
}

function isInfinitiveVerb(term) {
return term.tags.includes('Verb') && term.tags.includes('Infinitive');
}

function isCapitalized(term) {
return term.tags.includes('TitleCase');
}
Loading

0 comments on commit d9c67f8

Please sign in to comment.