Skip to content

Commit

Permalink
build(cli): setup commitizen + pr robot (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
olamothe authored Jan 27, 2021
1 parent ff9c8d1 commit 000468f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 1 deletion.
18 changes: 18 additions & 0 deletions .github/workflows/prbot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: PR Robot
on:
- pull_request

jobs:
run-script:
runs-on: ubuntu-latest
env:
GITHUB_CREDENTIALS: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-node@v1
with:
node-version: '14'
- run: npm i
- run: npm run pr:report
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
"bugs": "https://github.com/coveo/cli/issues",
"dependencies": {},
"devDependencies": {
"@actions/github": "^4.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@commitlint/config-lerna-scopes": "^11.0.0",
"@commitlint/lint": "^11.0.0",
"@types/node": "^10",
"@typescript-eslint/eslint-plugin": "^4.14.0",
"@typescript-eslint/parser": "^4.14.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^7.18.0",
"eslint-config-prettier": "^7.2.0",
"gts": "^3.1.0",
Expand All @@ -35,6 +38,14 @@
"@commitlint/config-conventional"
]
},
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog",
"defaultScope": [
"cli"
]
}
},
"lint-staged": {
"**/*.ts": [
"npm run lint:fix",
Expand All @@ -52,6 +63,8 @@
"lint:fix": "eslint --fix .",
"prepack": "rm -rf lib && tsc -b && oclif-dev manifest && oclif-dev readme",
"postpack": "rm -f oclif.manifest.json",
"version": "lerna run version"
"version": "lerna run version",
"commit": "git-cz",
"pr:report": "node ./scripts/pr-bot.js"
}
}
50 changes: 50 additions & 0 deletions scripts/github-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const github = require('@actions/github');
const octokit = github.getOctokit(process.env.GITHUB_CREDENTIALS);
const owner = 'coveo';
const repo = 'cli';

const getPullRequestTitle = async () => {
const pull_number = getPullRequestNumber();
return (await octokit.pulls.get({owner, repo, pull_number})).data.title;
};

const getPullRequestNumber = () => {
return (
(github.context.payload.pull_request &&
github.context.payload.pull_request.number) ||
0
);
};

const getHeadBranchName = async () => {
const pull_number = getPullRequestNumber();
return (await octokit.pulls.get({owner, repo, pull_number})).data.head.ref;
};

const getBaseBranchName = async () => {
const pull_number = getPullRequestNumber();
return (await octokit.pulls.get({owner, repo, pull_number})).data.base.ref;
};

const getPullRequestComments = () => {
const issue_number = getPullRequestNumber();
return octokit.issues.listComments({repo, owner, issue_number});
};

const createPullRequestComment = (body) => {
const issue_number = getPullRequestNumber();
return octokit.issues.createComment({repo, owner, issue_number, body});
};

const updatePullRequestComment = (comment_id, body) => {
return octokit.issues.updateComment({repo, owner, body, comment_id});
};

module.exports = {
getPullRequestTitle,
getPullRequestComments,
createPullRequestComment,
updatePullRequestComment,
getHeadBranchName,
getBaseBranchName,
};
39 changes: 39 additions & 0 deletions scripts/pr-bot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const {
getPullRequestComments,
updatePullRequestComment,
createPullRequestComment,
} = require('./github-client');
const {buildTitleReport} = require('./verify-title');

const reportTitle = 'Pull Request Report';

async function main() {
const report = await buildReport();
sendReport(report);
}

async function buildReport() {
const titleFormatReport = await buildTitleReport();

return `
**${reportTitle}**
${titleFormatReport}
`;
}

async function sendReport(report) {
console.log('sending report');
const comments = await getPullRequestComments();
const comment = findBundleSizeComment(comments.data);

comment
? updatePullRequestComment(comment.id, report)
: createPullRequestComment(report);
}

function findBundleSizeComment(comments) {
return comments.find((comment) => comment.body.indexOf(reportTitle) !== -1);
}

main();
54 changes: 54 additions & 0 deletions scripts/verify-title.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { getPullRequestTitle } = require('./github-client');

const load = require('@commitlint/load').default;
const lint = require('@commitlint/lint').default;

const specUrl = 'https://www.conventionalcommits.org/en/v1.0.0/#summary';

async function buildTitleReport() {
const prTitle = await getPullRequestTitle() || '';
const {valid} = await analyze(prTitle);
const isTitleValid = prTitle && valid;

return buildReport(isTitleValid);
}

async function analyze(title) {
const {rules, parserPreset} = await getLinterConfiguration();
return await lint(title, rules, parserPreset || {});
}

async function getLinterConfiguration() {
const conventionalConfig = { extends: ['@commitlint/config-conventional'] };
return await load(conventionalConfig)
}

function buildReport(isTitleValid) {
const message = isTitleValid ? buildSuccessMessage() : buildErrorMessage();

return `
**PR Title**
${message}
`
}

function buildSuccessMessage() {
return `
:white_check_mark: Title follows the [conventional commit](${specUrl}) spec.
`
}

function buildErrorMessage() {
return `
:x: Title should follow the [conventional commit](${specUrl}) spec:
<type>(optional scope): <description>
Example:
feat(cli): add new command
`
}

module.exports = { buildTitleReport };

0 comments on commit 000468f

Please sign in to comment.