Skip to content
This repository was archived by the owner on Apr 1, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 964642d

Browse files
committedJul 8, 2018
feat: remove old comments
1 parent c44093c commit 964642d

File tree

11 files changed

+8946
-7420
lines changed

11 files changed

+8946
-7420
lines changed
 

‎.vs/slnx.sqlite

1.45 MB
Binary file not shown.

‎.vs/slnx.sqlite-journal

16.5 KB
Binary file not shown.

‎README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# commitlint [bot]
22

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

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

1615
## Usage
1716

18-
1. Browse to [GitHub Apps - commitlint][apps]
19-
2. Accept the permissions
20-
3. Allow access to repositories
17+
1. Browse to [GitHub Apps - commitlint][apps]
18+
2. Accept the permissions
19+
3. Allow access to repositories
2120

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

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

3938
## What is missing?
4039

41-
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)
40+
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)
4241

4342
## Development
4443

45-
1. Setup the repo:
44+
1. Setup the repo:
4645

4746
```shell
48-
git clone https://github.com/ahmed-taj/commitlint-bot.git
47+
git clone https://github.com/z0al/commitlint-bot.git
4948
cd commitlint-bot
5049
npm install
5150
```
5251

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

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

6059
## Deployment
6160

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

64-
6563
## Credits
6664

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

7573
## License
7674

77-
MIT © [Ahmed T. Ali](https://github.com/ahmed-taj)
75+
MIT © [Ahmed T. Ali](https://github.com/z0al)

‎app.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"name": "commitlint-bot",
3-
"description": "A GitHub App that runs commitlint for you",
4-
"keywords": ["validate", "commit", "pr", "conventional", "git", "probot-app"],
5-
"repository": "https://github.com/ahmed-taj/commitlint-bot",
6-
"env": {
7-
"PRIVATE_KEY": {
8-
"description": "the private key of your GitHub App."
9-
},
10-
"APP_ID": {
11-
"description": "the ID of your GitHub App"
12-
},
13-
"WEBHOOK_SECRET": {
14-
"description": "the secret configured for your GitHub App"
15-
}
16-
}
2+
"name": "commitlint-bot",
3+
"description": "A GitHub App that runs commitlint for you",
4+
"keywords": ["validate", "commit", "pr", "conventional", "git", "probot-app"],
5+
"repository": "https://github.com/z0al/commitlint-bot",
6+
"env": {
7+
"PRIVATE_KEY": {
8+
"description": "the private key of your GitHub App."
9+
},
10+
"APP_ID": {
11+
"description": "the ID of your GitHub App"
12+
},
13+
"WEBHOOK_SECRET": {
14+
"description": "the secret configured for your GitHub App"
15+
}
16+
}
1717
}

‎index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Ours
2-
const commitlint = require('./lib/lint')
2+
const commitlint = require("./lib/lint");
33

44
module.exports = robot => {
5-
// For more information on building apps:
6-
// https://probot.github.io/docs/
7-
robot.on('pull_request.opened', commitlint)
8-
robot.on('pull_request.synchronize', commitlint)
9-
}
5+
// For more information on building apps:
6+
// https://probot.github.io/docs/
7+
robot.on("pull_request.opened", commitlint);
8+
robot.on("pull_request.synchronize", commitlint);
9+
};

‎lib/comments.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/**
2-
* Checks for a previous bot comment, if found returns the commen
2+
* Checks for a previous bot comment, if found returns the comment
33
*/
4-
async function checkComments(issues,pull){
5-
const comments = await issues.getComments(pull)
6-
return comment = comments.data.find(comment => comment.user.login === process.env.APP_NAME + '[bot]')
4+
async function checkComments(issues, pull) {
5+
const comments = await issues.getComments(pull);
6+
return (comment = comments.data.find(
7+
comment => comment.user.login === process.env.APP_NAME + "[bot]"
8+
));
79
}
810

9-
module.exports = checkComments
11+
module.exports = checkComments;

‎lib/format.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ issues [here][issues].
1414
Happy coding!
1515
1616
[ref]: https://help.github.com/articles/changing-a-commit-message/
17-
[repo]: https://github.com/ahmed-taj/commitlint-bot
18-
[issues]: https://github.com/ahmed-taj/commitlint-bot/issues
19-
`
17+
[repo]: https://github.com/z0al/commitlint-bot
18+
[issues]: https://github.com/z0al/commitlint-bot/issues
19+
`;
2020

2121
/**
2222
* Formats array of commits warnings/errors as GitHub comment
2323
*
2424
* @param {Array} report
2525
*/
2626
function format(commits) {
27-
let message = ''
27+
let message = "";
2828

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

35-
return template.replace('<PLACEHOLDER>', message)
35+
return template.replace("<PLACEHOLDER>", message);
3636
}
3737

38-
module.exports = format
38+
module.exports = format;

‎lib/lint.js

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,87 @@
11
// Packages
2-
const { lint, load } = require('@commitlint/core')
2+
const { lint, load } = require("@commitlint/core");
33

44
// Ours
5-
const config = require('./config')
6-
const format = require('./format')
7-
const checkComments = require('./comments')
5+
const config = require("./config");
6+
const format = require("./format");
7+
const checkComments = require("./comments");
88

99
/**
1010
* Runs commitlint against all commits of the pull request and sets an appropriate
1111
* status check
1212
*/
1313
async function commitlint(context) {
14-
// 1. Extract necessary info
15-
const pull = context.issue()
16-
const { sha } = context.payload.pull_request.head
17-
const repo = context.repo()
14+
// 1. Extract necessary info
15+
const pull = context.issue();
16+
const { sha } = context.payload.pull_request.head;
17+
const repo = context.repo();
1818

19-
// GH API
20-
const { paginate, issues, repos, pullRequests } = context.github
19+
// GH API
20+
const { paginate, issues, repos, pullRequests } = context.github;
2121

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

25-
// Pending
26-
await repos.createStatus({
27-
...statusInfo,
28-
state: 'pending',
29-
description: 'Waiting for the status to be reported'
30-
})
25+
// Pending
26+
await repos.createStatus({
27+
...statusInfo,
28+
state: "pending",
29+
description: "Waiting for the status to be reported"
30+
});
3131

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

38-
// Keep counters
39-
let errorsCount = 0
40-
let warnsCount = 0
38+
// Keep counters
39+
let errorsCount = 0;
40+
let warnsCount = 0;
4141

42-
// Iterates over all commits
43-
for (const d of data) {
44-
const { valid, errors, warnings } = await lint(d.commit.message, rules)
45-
if (!valid) {
46-
report.valid = false
47-
}
42+
// Iterates over all commits
43+
for (const d of data) {
44+
const { valid, errors, warnings } = await lint(d.commit.message, rules);
45+
if (!valid) {
46+
report.valid = false;
47+
}
4848

49-
if (errors.length > 0 || warnings.length > 0) {
50-
// Update counts
51-
errorsCount += errors.length
52-
warnsCount += warnings.length
49+
if (errors.length > 0 || warnings.length > 0) {
50+
// Update counts
51+
errorsCount += errors.length;
52+
warnsCount += warnings.length;
5353

54-
report.commits.push({ sha: d.sha, errors, warnings })
55-
}
56-
}
54+
report.commits.push({ sha: d.sha, errors, warnings });
55+
}
56+
}
5757

58-
// Final status
59-
await repos.createStatus({
60-
...statusInfo,
61-
state: report.valid ? 'success' : 'failure',
62-
description: `found ${errorsCount} problems, ${warnsCount} warnings`
63-
})
58+
// Final status
59+
await repos.createStatus({
60+
...statusInfo,
61+
state: report.valid ? "success" : "failure",
62+
description: `found ${errorsCount} problems, ${warnsCount} warnings`
63+
});
6464

65-
// Write a comment with the details (if any)
66-
if (errorsCount > 0 || warnsCount > 0) {
67-
const message = format(report.commits)
68-
const comment = await checkComments(issues,pull)
69-
if(comment){ // edits previous bot comment if found
70-
await issues.editComment({...pull,id:comment.id,body:message})
71-
}else{ // if no previous comment create a new one
72-
await issues.createComment({ ...pull, body: message })
73-
}
74-
}
75-
})
65+
// Get commit
66+
const comment = await checkComments(issues, pull);
67+
68+
// Write a comment with the details (if any)
69+
if (errorsCount > 0 || warnsCount > 0) {
70+
const message = format(report.commits);
71+
if (comment) {
72+
// edits previous bot comment if found
73+
await issues.editComment({ ...pull, id: comment.id, body: message });
74+
} else {
75+
// if no previous comment create a new one
76+
await issues.createComment({ ...pull, body: message });
77+
}
78+
} else {
79+
if (comment) {
80+
// edits previous bot comment if found
81+
await issues.deleteComment({ ...pull, comment_id: comment.id });
82+
}
83+
}
84+
});
7685
}
7786

78-
module.exports = commitlint
87+
module.exports = commitlint;

‎package-lock.json

Lines changed: 8721 additions & 7220 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
{
2-
"name": "commitlint-bot",
3-
"version": "0.0.0-development",
4-
"description": "A GitHub App that runs commitlint for you",
5-
"author": "Ahmed T. Ali <ah.tajelsir@gmail.com> (https://ahmed.sd)",
6-
"license": "MIT",
7-
"repository": "https://github.com/ahmed-taj/commitlint-bot.git",
8-
"keywords": [
9-
"validate",
10-
"commit",
11-
"pr",
12-
"conventional",
13-
"git",
14-
"probot-app"
15-
],
16-
"main": "index.js",
17-
"scripts": {
18-
"start": "probot run ./index.js",
19-
"test": "jest",
20-
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
21-
},
22-
"files": [
23-
"lib",
24-
"index.js",
25-
"README.md"
26-
],
27-
"dependencies": {
28-
"@commitlint/core": "^4.3.0",
29-
"probot": "^5.0.0"
30-
},
31-
"devDependencies": {
32-
"jest": "^22.1.4",
33-
"semantic-release": "^8.2.0",
34-
"smee-client": "^1.0.1"
35-
},
36-
"engines": {
37-
"node": "8.x",
38-
"npm": "5.5.x"
39-
}
2+
"name": "commitlint-bot",
3+
"version": "0.0.0-development",
4+
"description": "A GitHub App that runs commitlint for you",
5+
"author": "Ahmed T. Ali <ah.tajelsir@gmail.com> (https://ahmed.sd)",
6+
"license": "MIT",
7+
"repository": "https://github.com/z0al/commitlint-bot.git",
8+
"keywords": [
9+
"validate",
10+
"commit",
11+
"pr",
12+
"conventional",
13+
"git",
14+
"probot-app"
15+
],
16+
"main": "index.js",
17+
"scripts": {
18+
"start": "probot run ./index.js",
19+
"test": "jest",
20+
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
21+
},
22+
"files": [
23+
"lib",
24+
"index.js",
25+
"README.md"
26+
],
27+
"dependencies": {
28+
"@commitlint/core": "^4.3.0",
29+
"probot": "^6.2.1"
30+
},
31+
"devDependencies": {
32+
"jest": "^22.1.4",
33+
"semantic-release": "^8.2.0",
34+
"smee-client": "^1.0.1"
35+
},
36+
"engines": {
37+
"node": "8.x",
38+
"npm": "5.5.x"
39+
}
4040
}

‎test/lint.test.js

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,76 @@
11
// Packages
2-
const { createRobot } = require('probot')
2+
const { createRobot } = require("probot");
3+
4+
// Update necessary env vars
5+
process.env.APP_NAME = "commitlint-dev";
36

47
// Ours
5-
const app = require('../index')
6-
const events = require('./events')
8+
const app = require("../index");
9+
const events = require("./events");
710

8-
const repo = { owner: 'user', repo: 'repo' }
11+
const repo = { owner: "user", repo: "repo" };
912

10-
let robot, github
13+
let robot, github;
1114

1215
beforeEach(() => {
13-
// Here we create a robot instance
14-
robot = createRobot()
15-
// Here we initialize the app on the robot instance
16-
app(robot)
17-
// Mock GitHub client
18-
github = {
19-
issues: { createComment: jest.fn(), editComment: jest.fn(),
20-
getComments: jest.fn().mockReturnValue({ data:[ { id: 1,user: { login: 'bot', id: 2, type: 'Bot', },} ] }) },
21-
repos: { createStatus: jest.fn() },
22-
pullRequests: {
23-
getCommits: jest
24-
.fn()
25-
.mockReturnValueOnce({
26-
data: [{ sha: 'abcd', commit: { message: 'good: message' } }]
27-
})
28-
.mockReturnValue({
29-
data: [{ sha: 'abcd', commit: { message: 'bad message' } }]
30-
})
31-
},
32-
paginate: (fn, cb) => cb(fn)
33-
}
34-
// Passes the mocked out GitHub API into out robot instance
35-
robot.auth = () => Promise.resolve(github)
36-
})
37-
38-
test('status update to pending', async () => {
39-
await robot.receive(events.opened)
40-
expect(github.repos.createStatus).toHaveBeenCalledWith(
41-
expect.objectContaining({ state: 'pending' })
42-
)
43-
})
44-
45-
test('fetching the list of commits', async () => {
46-
await robot.receive(events.opened)
47-
expect(github.pullRequests.getCommits).toHaveBeenCalledWith(
48-
expect.objectContaining({ ...repo, number: 1 })
49-
)
50-
})
51-
52-
test('comment with errors/warnings', async () => {
53-
// Good message
54-
await robot.receive(events.opened)
55-
expect(github.issues.createComment).not.toHaveBeenCalled()
56-
57-
// Bad message
58-
await robot.receive(events.opened)
59-
expect(github.issues.createComment).toHaveBeenCalled()
60-
})
16+
// Here we create a robot instance
17+
robot = createRobot();
18+
// Here we initialize the app on the robot instance
19+
app(robot);
20+
// Mock GitHub client
21+
github = {
22+
issues: {
23+
createComment: jest.fn(),
24+
editComment: jest.fn(),
25+
deleteComment: jest.fn(),
26+
getComments: jest.fn().mockReturnValue({
27+
data: [
28+
{
29+
id: 1,
30+
user: { login: `${process.env.APP_NAME}[bot]`, id: 2, type: "Bot" }
31+
}
32+
]
33+
})
34+
},
35+
repos: { createStatus: jest.fn() },
36+
pullRequests: {
37+
getCommits: jest
38+
.fn()
39+
.mockReturnValueOnce({
40+
data: [{ sha: "abcd", commit: { message: "good: message" } }]
41+
})
42+
.mockReturnValue({
43+
data: [{ sha: "abcd", commit: { message: "bad message" } }]
44+
})
45+
},
46+
paginate: (fn, cb) => cb(fn)
47+
};
48+
// Passes the mocked out GitHub API into out robot instance
49+
robot.auth = () => Promise.resolve(github);
50+
});
51+
52+
test("status update to pending", async () => {
53+
await robot.receive(events.opened);
54+
expect(github.repos.createStatus).toHaveBeenCalledWith(
55+
expect.objectContaining({ state: "pending" })
56+
);
57+
});
58+
59+
test("fetching the list of commits", async () => {
60+
await robot.receive(events.opened);
61+
expect(github.pullRequests.getCommits).toHaveBeenCalledWith(
62+
expect.objectContaining({ ...repo, number: 1 })
63+
);
64+
});
65+
66+
test("remove comment when no errors/warnings", async () => {
67+
await robot.receive(events.opened);
68+
expect(github.issues.createComment).not.toHaveBeenCalled();
69+
expect(github.issues.editComment).not.toHaveBeenCalled();
70+
expect(github.issues.getComments).toHaveBeenCalled();
71+
expect(github.issues.deleteComment).toHaveBeenCalled();
72+
73+
await robot.receive(events.opened);
74+
expect(github.issues.editComment).toHaveBeenCalled();
75+
expect(github.issues.getComments).toHaveBeenCalled();
76+
});

0 commit comments

Comments
 (0)
This repository has been archived.