Skip to content

Commit

Permalink
PR hygiene checking (#910)
Browse files Browse the repository at this point in the history
* pr lint check

Fixes #782

* undo

* ignore more

* remove env.GIT_DIFF

* only context

* run it

* remove env.GIT_DIFF

* unbreak cache

* updates

* updates

* all types

* set -e

* don't catch

* set -e needed?

* debugging

* better errors

* testing

* testing

* using returns

* using returns

* last cleanup

* remove debug

* touchups

* touchups

* remove emojis

* remove the need for the PR description to be filled in
  • Loading branch information
peterbe authored Feb 3, 2021
1 parent f99dbe9 commit bcb104c
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/pr-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Checks the PR itself for hygiene things.

name: PR Lint

on:
pull_request:
# Necessary to re-run if someone edits the PR without a new pushed commit.
types: [opened, edited, synchronize, reopened]

jobs:
check:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Setup Node.js environment
uses: actions/setup-node@v2.1.4
with:
node-version: "12"

- name: Install all yarn packages
run: |
cd pr-lint
yarn --frozen-lockfile
- name: Run checks
run: |
cd pr-lint
yarn run check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
/pr-lint/node_modules/
/build
.env
yarn-debug.log*
Expand Down
16 changes: 16 additions & 0 deletions pr-lint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "pr-lint",
"version": "1.0.0",
"main": "src/index.js",
"license": "SEE LICENSE IN LICENSE.md",
"engines": {
"node": ">=12.0.0"
},
"scripts": {
"check": "node src/index.js"
},
"dependencies": {
"@actions/github": "4.0.0",
"boxen": "5.0.0"
}
}
63 changes: 63 additions & 0 deletions pr-lint/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const boxen = require("boxen");
const github = require("@actions/github");

async function main() {
// TODO: Consider being super friendly and possibly...
// * Post a comment that is friendly and useful that explains the problems.
// * Just edit the PR title (or whatever it is) if it's easily fixable.
// See https://www.npmjs.com/package/@actions/github for documentation
// about how to use @actions/github to do authenticated things.
const context = github.context;

const contextPullRequest = context.payload.pull_request;
if (!contextPullRequest) {
throw new Error(
"This action can only be invoked in `pull_request` events. Otherwise the pull request can't be inferred."
);
}

const { title } = contextPullRequest;
if (title === "Update index.html") {
// This is the default title you get when you use the GitHub UI to
// edit a file and following web form steps to create a PR.
// These PRs are hard to triage and to organize because of the
// rather "useless" title so let's put the onus back on the author
// to come up with a better title.
// See https://github.com/mdn/content/issues/782
return (
'Pull request title can\'t just be "Update index.html"\n' +
"Please update the pull request to be more descriptive. " +
"For example 'fix typo on Web/JavaScript'."
);
}

// No errors or problems!
return null;
}

main()
.then((ret) => {
if (ret) {
const lines = ret.split("\n");
console.log(
boxen(lines[0], { padding: 1, margin: 1, borderStyle: "double" })
);
// Because boxen breaks, in GitHub Actions logging, when multiple lines.
if (lines.length > 1) {
console.log(`\n${lines.slice(1).join("\n")}\n`);
}
process.exitCode = 2;
} else {
console.log(
boxen("Happiness is when nothing's wrong!", {
padding: 1,
margin: 1,
borderStyle: "double",
})
);
}
})
.catch((error) => {
console.error(error);
process.exitCode = 1;
});
Loading

0 comments on commit bcb104c

Please sign in to comment.