-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(cli): setup commitizen + pr robot (#3)
- Loading branch information
Showing
5 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |