Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.

Prev. Comments & Formatting #12

Merged
merged 7 commits into from
Jul 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .vs/slnx.sqlite
Binary file not shown.
Binary file added .vs/slnx.sqlite-journal
Binary file not shown.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# commitlint [bot]


[![Travis](https://img.shields.io/travis/ahmed-taj/commitlint-bot.svg)](https://travis-ci.org/ahmed-taj/commitlint-bot)
[![Travis](https://img.shields.io/travis/z0al/commitlint-bot.svg)](https://travis-ci.org/z0al/commitlint-bot)
[![npm](https://img.shields.io/npm/v/commitlint-bot.svg)](https://www.npmjs.com/package/commitlint-bot)

<p align="center">
Expand All @@ -15,9 +14,9 @@ and sets an appropriate status check.

## Usage

1. Browse to [GitHub Apps - commitlint][apps]
2. Accept the permissions
3. Allow access to repositories
1. Browse to [GitHub Apps - commitlint][apps]
2. Accept the permissions
3. Allow access to repositories

On the next pull request, a status check from `commitlint` will appear:

Expand All @@ -38,30 +37,29 @@ For best results, enable branch protection (in the repository's settings) and re

## What is missing?

We don't currently support custom configuration (i.e. `.commitlint.yml` or `.commitlint.json`), but [we will](https://github.com/ahmed-taj/commitlint-bot/issues/1)
We don't currently support custom configuration (i.e. `.commitlint.yml` or `.commitlint.json`), but [we will](https://github.com/z0al/commitlint-bot/issues/1)

## Development

1. Setup the repo:
1. Setup the repo:

```shell
git clone https://github.com/ahmed-taj/commitlint-bot.git
git clone https://github.com/z0al/commitlint-bot.git
cd commitlint-bot
npm install
```

2. Create your own [GitHub app][]
3. Store the private key as `private-key.pem` somewhere safe, and point to its location in `.env`
4. Start the app with `APP_ID=1234 npm start` where `1234` is your GitHub app's ID
5. Update your GitHub app's Webhook URL to your localtunnel.me URL
2. Create your own [GitHub app][]
3. Store the private key as `private-key.pem` somewhere safe, and point to its location in `.env`
4. Start the app with `APP_ID=1234 npm start` where `1234` is your GitHub app's ID
5. Update your GitHub app's Webhook URL to your localtunnel.me URL

[GitHub app]: https://probot.github.io/docs/development/#configure-a-github-app
[github app]: https://probot.github.io/docs/development/#configure-a-github-app

## Deployment

See [docs/deploy.md](./docs/deploy.md) for more info.


## Credits

Robot designed by [Freepik](https://www.freepik.com/free-vector/fun-pack-of-robots-avatars_1258314.htm).
Expand All @@ -74,4 +72,4 @@ Give it a star(:star:) :point_up_2:

## License

MIT © [Ahmed T. Ali](https://github.com/ahmed-taj)
MIT © [Ahmed T. Ali](https://github.com/z0al)
30 changes: 15 additions & 15 deletions app.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "commitlint-bot",
"description": "A GitHub App that runs commitlint for you",
"keywords": ["validate", "commit", "pr", "conventional", "git", "probot-app"],
"repository": "https://github.com/ahmed-taj/commitlint-bot",
"env": {
"PRIVATE_KEY": {
"description": "the private key of your GitHub App."
},
"APP_ID": {
"description": "the ID of your GitHub App"
},
"WEBHOOK_SECRET": {
"description": "the secret configured for your GitHub App"
}
}
"name": "commitlint-bot",
"description": "A GitHub App that runs commitlint for you",
"keywords": ["validate", "commit", "pr", "conventional", "git", "probot-app"],
"repository": "https://github.com/z0al/commitlint-bot",
"env": {
"PRIVATE_KEY": {
"description": "the private key of your GitHub App."
},
"APP_ID": {
"description": "the ID of your GitHub App"
},
"WEBHOOK_SECRET": {
"description": "the secret configured for your GitHub App"
}
}
}
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Ours
const commitlint = require('./lib/lint')
const commitlint = require("./lib/lint");

module.exports = robot => {
// For more information on building apps:
// https://probot.github.io/docs/
robot.on('pull_request.opened', commitlint)
robot.on('pull_request.synchronize', commitlint)
}
// For more information on building apps:
// https://probot.github.io/docs/
robot.on("pull_request.opened", commitlint);
robot.on("pull_request.synchronize", commitlint);
};
11 changes: 11 additions & 0 deletions lib/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Checks for a previous bot comment, if found returns the comment
*/
async function checkComments(issues, pull) {
const comments = await issues.getComments(pull);
return (comment = comments.data.find(
comment => comment.user.login === process.env.APP_NAME + "[bot]"
));
}

module.exports = checkComments;
22 changes: 11 additions & 11 deletions lib/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ issues [here][issues].
Happy coding!

[ref]: https://help.github.com/articles/changing-a-commit-message/
[repo]: https://github.com/ahmed-taj/commitlint-bot
[issues]: https://github.com/ahmed-taj/commitlint-bot/issues
`
[repo]: https://github.com/z0al/commitlint-bot
[issues]: https://github.com/z0al/commitlint-bot/issues
`;

/**
* Formats array of commits warnings/errors as GitHub comment
*
* @param {Array} report
*/
function format(commits) {
let message = ''
let message = "";

commits.forEach(c => {
message += `* Commit: ${c.sha}\n`
message += c.errors.map(e => ` - ✖ ${e.message}\n`)
message += c.warnings.map(w => ` - ⚠ ${w.message}\n`)
})
commits.forEach(c => {
message += `* Commit: ${c.sha}\n`;
message += c.errors.map(e => ` - ✖ ${e.message}\n`).join("");
message += c.warnings.map(w => ` - ⚠ ${w.message}\n`).join("");
});

return template.replace('<PLACEHOLDER>', message)
return template.replace("<PLACEHOLDER>", message);
}

module.exports = format
module.exports = format;
117 changes: 66 additions & 51 deletions lib/lint.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,87 @@
// Packages
const { lint, load } = require('@commitlint/core')
const { lint, load } = require("@commitlint/core");

// Ours
const config = require('./config')
const format = require('./format')
const config = require("./config");
const format = require("./format");
const checkComments = require("./comments");

/**
* Runs commitlint against all commits of the pull request and sets an appropriate
* status check
*/
async function commitlint(context) {
// 1. Extract necessary info
const pull = context.issue()
const { sha } = context.payload.pull_request.head
const repo = context.repo()
// 1. Extract necessary info
const pull = context.issue();
const { sha } = context.payload.pull_request.head;
const repo = context.repo();

// GH API
const { paginate, issues, repos, pullRequests } = context.github
// GH API
const { paginate, issues, repos, pullRequests } = context.github;

// Hold this PR info
const statusInfo = { ...repo, sha, context: 'commitlint' }
// Hold this PR info
const statusInfo = { ...repo, sha, context: "commitlint" };

// Pending
await repos.createStatus({
...statusInfo,
state: 'pending',
description: 'Waiting for the status to be reported'
})
// Pending
await repos.createStatus({
...statusInfo,
state: "pending",
description: "Waiting for the status to be reported"
});

// Paginate all PR commits
return paginate(pullRequests.getCommits(pull), async ({ data }) => {
// empty summary
const report = { valid: true, commits: [] }
const { rules } = await load(config)
// Paginate all PR commits
return paginate(pullRequests.getCommits(pull), async ({ data }) => {
// empty summary
const report = { valid: true, commits: [] };
const { rules } = await load(config);

// Keep counters
let errorsCount = 0
let warnsCount = 0
// Keep counters
let errorsCount = 0;
let warnsCount = 0;

// Iterates over all commits
for (const d of data) {
const { valid, errors, warnings } = await lint(d.commit.message, rules)
if (!valid) {
report.valid = false
}
// Iterates over all commits
for (const d of data) {
const { valid, errors, warnings } = await lint(d.commit.message, rules);
if (!valid) {
report.valid = false;
}

if (errors.length > 0 || warnings.length > 0) {
// Update counts
errorsCount += errors.length
warnsCount += warnings.length
if (errors.length > 0 || warnings.length > 0) {
// Update counts
errorsCount += errors.length;
warnsCount += warnings.length;

report.commits.push({ sha: d.sha, errors, warnings })
}
}
report.commits.push({ sha: d.sha, errors, warnings });
}
}

// Final status
await repos.createStatus({
...statusInfo,
state: report.valid ? 'success' : 'failure',
description: `found ${errorsCount} problems, ${warnsCount} warnings`
})
// Final status
await repos.createStatus({
...statusInfo,
state: report.valid ? "success" : "failure",
description: `found ${errorsCount} problems, ${warnsCount} warnings`
});

// Write a comment with the details (if any)
if (errorsCount > 0 || warnsCount > 0) {
const message = format(report.commits)
await issues.createComment({ ...pull, body: message })
}
})
// Get commit
const comment = await checkComments(issues, pull);

// Write a comment with the details (if any)
if (errorsCount > 0 || warnsCount > 0) {
const message = format(report.commits);
if (comment) {
// edits previous bot comment if found
await issues.editComment({ ...pull, id: comment.id, body: message });
} else {
// if no previous comment create a new one
await issues.createComment({ ...pull, body: message });
}
} else {
if (comment) {
// edits previous bot comment if found
await issues.deleteComment({ ...pull, comment_id: comment.id });
}
}
});
}

module.exports = commitlint
module.exports = commitlint;
Loading