|
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(d.commit.message, rules); |
| 45 | + if (!valid) { |
| 46 | + report.valid = false; |
| 47 | + } |
48 | 48 |
|
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; |
53 | 53 |
|
54 |
| - report.commits.push({ sha: d.sha, errors, warnings }) |
55 |
| - } |
56 |
| - } |
| 54 | + report.commits.push({ sha: d.sha, errors, warnings }); |
| 55 | + } |
| 56 | + } |
57 | 57 |
|
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 | + }); |
64 | 64 |
|
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 | + }); |
76 | 85 | }
|
77 | 86 |
|
78 |
| -module.exports = commitlint |
| 87 | +module.exports = commitlint; |
0 commit comments