|
1 | 1 | // Packages
|
2 |
| -const { lint, load } = require("@commitlint/core"); |
| 2 | +const { lint, load } = require('@commitlint/core'); |
3 | 3 |
|
4 | 4 | // 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'); |
8 | 8 |
|
9 | 9 | /**
|
10 | 10 | * Runs commitlint against all commits of the pull request and sets an appropriate
|
11 | 11 | * status check
|
12 | 12 | */
|
13 | 13 | 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(); |
18 | 18 |
|
19 |
| - // GH API |
20 |
| - const { paginate, issues, repos, pullRequests } = context.github; |
| 19 | + // GH API |
| 20 | + const { paginate, issues, repos, pullRequests } = context.github; |
21 | 21 |
|
22 |
| - // Hold this PR info |
23 |
| - const statusInfo = { ...repo, sha, context: "commitlint" }; |
| 22 | + // Hold this PR info |
| 23 | + const statusInfo = { ...repo, sha, context: 'commitlint' }; |
24 | 24 |
|
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 | + }); |
31 | 31 |
|
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); |
37 | 37 |
|
38 |
| - // Keep counters |
39 |
| - let errorsCount = 0; |
40 |
| - let warnsCount = 0; |
| 38 | + // Keep counters |
| 39 | + let errorsCount = 0; |
| 40 | + let warnsCount = 0; |
41 | 41 |
|
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( |
| 45 | + d.commit.message, |
| 46 | + rules |
| 47 | + ); |
| 48 | + if (!valid) { |
| 49 | + report.valid = false; |
| 50 | + } |
48 | 51 |
|
49 |
| - if (errors.length > 0 || warnings.length > 0) { |
50 |
| - // Update counts |
51 |
| - errorsCount += errors.length; |
52 |
| - warnsCount += warnings.length; |
| 52 | + if (errors.length > 0 || warnings.length > 0) { |
| 53 | + // Update counts |
| 54 | + errorsCount += errors.length; |
| 55 | + warnsCount += warnings.length; |
53 | 56 |
|
54 |
| - report.commits.push({ sha: d.sha, errors, warnings }); |
55 |
| - } |
56 |
| - } |
| 57 | + report.commits.push({ sha: d.sha, errors, warnings }); |
| 58 | + } |
| 59 | + } |
57 | 60 |
|
58 |
| - // Final status |
59 |
| - await repos.createStatus({ |
60 |
| - ...statusInfo, |
61 |
| - state: report.valid ? "success" : "failure", |
62 |
| - description: `found ${errorsCount} problems, ${warnsCount} warnings` |
63 |
| - }); |
| 61 | + // Final status |
| 62 | + await repos.createStatus({ |
| 63 | + ...statusInfo, |
| 64 | + state: report.valid ? 'success' : 'failure', |
| 65 | + description: `found ${errorsCount} problems, ${warnsCount} warnings` |
| 66 | + }); |
64 | 67 |
|
65 |
| - // Get commit |
66 |
| - const comment = await checkComments(issues, pull); |
| 68 | + // Get commit |
| 69 | + const comment = await checkComments(issues, pull); |
67 | 70 |
|
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 |
| - }); |
| 71 | + // Write a comment with the details (if any) |
| 72 | + if (errorsCount > 0 || warnsCount > 0) { |
| 73 | + const message = format(report.commits); |
| 74 | + if (comment) { |
| 75 | + // edits previous bot comment if found |
| 76 | + await issues.editComment({ |
| 77 | + ...pull, |
| 78 | + id: comment.id, |
| 79 | + body: message |
| 80 | + }); |
| 81 | + } else { |
| 82 | + // if no previous comment create a new one |
| 83 | + await issues.createComment({ ...pull, body: message }); |
| 84 | + } |
| 85 | + } else { |
| 86 | + if (comment) { |
| 87 | + // edits previous bot comment if found |
| 88 | + await issues.deleteComment({ ...pull, comment_id: comment.id }); |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
85 | 92 | }
|
86 | 93 |
|
87 | 94 | module.exports = commitlint;
|
0 commit comments