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

Commit

Permalink
feat: remove old comments
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed Jul 8, 2018
1 parent c44093c commit 964642d
Show file tree
Hide file tree
Showing 11 changed files with 8,946 additions and 7,420 deletions.
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);
};
12 changes: 7 additions & 5 deletions lib/comments.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/**
* Checks for a previous bot comment, if found returns the commen
* 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]')
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
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`).join('')
message += c.warnings.map(w => ` - ⚠ ${w.message}\n`).join('')
})
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;
123 changes: 66 additions & 57 deletions lib/lint.js
Original file line number Diff line number Diff line change
@@ -1,78 +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 checkComments = require('./comments')
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)
const comment = await checkComments(issues,pull)
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 })
}
}
})
// 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

0 comments on commit 964642d

Please sign in to comment.