Skip to content

Commit c4bc080

Browse files
committed
chore: formatting
1 parent 19ba5f1 commit c4bc080

File tree

1 file changed

+86
-75
lines changed

1 file changed

+86
-75
lines changed

src/main.js

Lines changed: 86 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
const {DefaultArtifactClient} = require('@actions/artifact');
2-
const core = require('@actions/core');
3-
const exec = require('@actions/exec');
4-
const github = require('@actions/github');
5-
const glob = require('@actions/glob');
1+
const { DefaultArtifactClient } = require("@actions/artifact");
2+
const core = require("@actions/core");
3+
const exec = require("@actions/exec");
4+
const github = require("@actions/github");
5+
const glob = require("@actions/glob");
66
const lcovTotal = require("lcov-total");
7-
const os = require('os');
8-
const path = require('path');
7+
const os = require("os");
8+
const path = require("path");
99

10-
const events = ['pull_request', 'pull_request_target'];
10+
const events = ["pull_request", "pull_request_target"];
1111

1212
async function run() {
1313
try {
1414
const tmpPath = path.resolve(os.tmpdir(), github.context.action);
15-
const coverageFilesPattern = core.getInput('coverage-files');
15+
const coverageFilesPattern = core.getInput("coverage-files");
1616
const globber = await glob.create(coverageFilesPattern);
1717
const coverageFiles = await globber.glob();
18-
const titlePrefix = core.getInput('title-prefix');
19-
const additionalMessage = core.getInput('additional-message');
20-
const updateComment = core.getInput('update-comment') === 'true';
21-
const workingDirectory = core.getInput('working-directory').trim() || './';
18+
const titlePrefix = core.getInput("title-prefix");
19+
const additionalMessage = core.getInput("additional-message");
20+
const updateComment = core.getInput("update-comment") === "true";
21+
const workingDirectory = core.getInput("working-directory").trim() || "./";
2222

2323
await genhtml(coverageFiles, tmpPath, workingDirectory);
2424

2525
const coverageFile = await mergeCoverages(coverageFiles, tmpPath);
2626
const totalCoverage = lcovTotal(coverageFile);
27-
const minimumCoverage = core.getInput('minimum-coverage');
28-
const gitHubToken = core.getInput('github-token').trim();
27+
const minimumCoverage = core.getInput("minimum-coverage");
28+
const gitHubToken = core.getInput("github-token").trim();
2929
const errorMessage = `The code coverage is too low: ${totalCoverage}. Expected at least ${minimumCoverage}.`;
3030
const isMinimumCoverageReached = totalCoverage >= minimumCoverage;
3131

32-
const hasGithubToken = gitHubToken !== '';
32+
const hasGithubToken = gitHubToken !== "";
3333
const isPR = events.includes(github.context.eventName);
3434

3535
if (hasGithubToken && isPR) {
@@ -38,17 +38,33 @@ async function run() {
3838
const details = await detail(coverageFile, workingDirectory, octokit);
3939
const sha = github.context.payload.pull_request.head.sha;
4040
const shaShort = sha.substr(0, 7);
41-
const commentHeaderPrefix = `### ${titlePrefix ? `${titlePrefix} ` : ''}[LCOV](https://github.com/marketplace/actions/report-lcov) of commit`;
42-
let body = `${commentHeaderPrefix} [<code>${shaShort}</code>](${github.context.payload.pull_request.number}/commits/${sha}) during [${github.context.workflow} #${github.context.runNumber}](../actions/runs/${github.context.runId})\n<pre>${summary}\n\nFiles changed coverage rate:${details}</pre>${additionalMessage ? `\n${additionalMessage}` : ''}`;
41+
const commentHeaderPrefix = `### ${
42+
titlePrefix ? `${titlePrefix} ` : ""
43+
}[LCOV](https://github.com/marketplace/actions/report-lcov) of commit`;
44+
let body = `${commentHeaderPrefix} [<code>${shaShort}</code>](${
45+
github.context.payload.pull_request.number
46+
}/commits/${sha}) during [${github.context.workflow} #${
47+
github.context.runNumber
48+
}](../actions/runs/${
49+
github.context.runId
50+
})\n<pre>${summary}\n\nFiles changed coverage rate:${details}</pre>${
51+
additionalMessage ? `\n${additionalMessage}` : ""
52+
}`;
4353

4454
if (!isMinimumCoverageReached) {
4555
body += `\n:no_entry: ${errorMessage}`;
4656
}
4757

48-
updateComment ? await upsertComment(body, commentHeaderPrefix, octokit) : await createComment(body, octokit);
58+
updateComment
59+
? await upsertComment(body, commentHeaderPrefix, octokit)
60+
: await createComment(body, octokit);
4961
} else if (!hasGithubToken) {
50-
core.info("github-token received is empty. Skipping writing a comment in the PR.");
51-
core.info("Note: This could happen even if github-token was provided in workflow file. It could be because your github token does not have permissions for commenting in target repo.")
62+
core.info(
63+
"github-token received is empty. Skipping writing a comment in the PR."
64+
);
65+
core.info(
66+
"Note: This could happen even if github-token was provided in workflow file. It could be because your github token does not have permissions for commenting in target repo."
67+
);
5268
} else if (!isPR) {
5369
core.info("The event is not a pull request. Skipping writing a comment.");
5470
core.info("The event type is: " + github.context.eventName);
@@ -65,7 +81,7 @@ async function run() {
6581
}
6682

6783
async function createComment(body, octokit) {
68-
core.debug("Creating a comment in the PR.")
84+
core.debug("Creating a comment in the PR.");
6985

7086
await octokit.rest.issues.createComment({
7187
repo: github.context.repo.repo,
@@ -82,8 +98,8 @@ async function upsertComment(body, commentHeaderPrefix, octokit) {
8298
issue_number: github.context.payload.pull_request.number,
8399
});
84100

85-
const existingComment = issueComments.data.find(comment =>
86-
comment.body.includes(commentHeaderPrefix),
101+
const existingComment = issueComments.data.find((comment) =>
102+
comment.body.includes(commentHeaderPrefix)
87103
);
88104

89105
if (existingComment) {
@@ -103,28 +119,23 @@ async function upsertComment(body, commentHeaderPrefix, octokit) {
103119
}
104120

105121
async function genhtml(coverageFiles, tmpPath, workingDirectory) {
106-
const artifactName = core.getInput('artifact-name').trim();
107-
const artifactPath = path.resolve(tmpPath, 'html').trim();
108-
const args = [...coverageFiles, '--rc', 'lcov_branch_coverage=1'];
122+
const artifactName = core.getInput("artifact-name").trim();
123+
const artifactPath = path.resolve(tmpPath, "html").trim();
124+
const args = [...coverageFiles, "--rc", "lcov_branch_coverage=1"];
109125

110-
args.push('--output-directory');
126+
args.push("--output-directory");
111127
args.push(artifactPath);
112128

113-
await exec.exec('genhtml', args, { cwd: workingDirectory });
129+
await exec.exec("genhtml", args, { cwd: workingDirectory });
114130

115-
if (artifactName !== '') {
131+
if (artifactName !== "") {
116132
const artifact = new DefaultArtifactClient();
117133
const globber = await glob.create(`${artifactPath}/**/**.*`);
118134
const htmlFiles = await globber.glob();
119135

120136
core.info(`Uploading artifacts.`);
121137

122-
await artifact
123-
.uploadArtifact(
124-
artifactName,
125-
htmlFiles,
126-
artifactPath,
127-
);
138+
await artifact.uploadArtifact(artifactName, htmlFiles, artifactPath);
128139
} else {
129140
core.info("Skip uploading artifacts");
130141
}
@@ -133,24 +144,24 @@ async function genhtml(coverageFiles, tmpPath, workingDirectory) {
133144
async function mergeCoverages(coverageFiles, tmpPath) {
134145
// This is broken for some reason:
135146
//const mergedCoverageFile = path.resolve(tmpPath, 'lcov.info');
136-
const mergedCoverageFile = tmpPath + '/lcov.info';
147+
const mergedCoverageFile = tmpPath + "/lcov.info";
137148
const args = [];
138149

139150
for (const coverageFile of coverageFiles) {
140-
args.push('--add-tracefile');
151+
args.push("--add-tracefile");
141152
args.push(coverageFile);
142153
}
143154

144-
args.push('--output-file');
155+
args.push("--output-file");
145156
args.push(mergedCoverageFile);
146157

147-
await exec.exec('lcov', [...args, '--rc', 'lcov_branch_coverage=1']);
158+
await exec.exec("lcov", [...args, "--rc", "lcov_branch_coverage=1"]);
148159

149160
return mergedCoverageFile;
150161
}
151162

152163
async function summarize(coverageFile) {
153-
let output = '';
164+
let output = "";
154165

155166
const options = {};
156167
options.listeners = {
@@ -159,27 +170,24 @@ async function summarize(coverageFile) {
159170
},
160171
stderr: (data) => {
161172
output += data.toString();
162-
}
173+
},
163174
};
164175

165-
await exec.exec('lcov', [
166-
'--summary',
167-
coverageFile,
168-
'--rc',
169-
'lcov_branch_coverage=1'
170-
], options);
176+
await exec.exec(
177+
"lcov",
178+
["--summary", coverageFile, "--rc", "lcov_branch_coverage=1"],
179+
options
180+
);
171181

172-
const lines = output
173-
.trim()
174-
.split(/\r?\n/)
182+
const lines = output.trim().split(/\r?\n/);
175183

176184
lines.shift(); // Removes "Reading tracefile..."
177185

178-
return lines.join('\n');
186+
return lines.join("\n");
179187
}
180188

181189
async function detail(coverageFile, workingDirectory, octokit) {
182-
let output = '';
190+
let output = "";
183191

184192
const options = {};
185193
options.listeners = {
@@ -188,51 +196,54 @@ async function detail(coverageFile, workingDirectory, octokit) {
188196
},
189197
stderr: (data) => {
190198
output += data.toString();
191-
}
199+
},
192200
};
193201

194-
await exec.exec('lcov', [
195-
'--list',
196-
coverageFile,
197-
'--list-full-path',
198-
'--rc',
199-
'lcov_branch_coverage=1',
200-
], options);
202+
await exec.exec(
203+
"lcov",
204+
[
205+
"--list",
206+
coverageFile,
207+
"--list-full-path",
208+
"--rc",
209+
"lcov_branch_coverage=1",
210+
],
211+
options
212+
);
201213

202-
let lines = output
203-
.trim()
204-
.split(/\r?\n/)
214+
let lines = output.trim().split(/\r?\n/);
205215

206216
lines.shift(); // Removes "Reading tracefile..."
207217
lines.pop(); // Removes "Total..."
208218
lines.pop(); // Removes "========"
209219

210-
const listFilesOptions = octokit
211-
.rest.pulls.listFiles.endpoint.merge({
212-
owner: github.context.repo.owner,
213-
repo: github.context.repo.repo,
214-
pull_number: github.context.payload.pull_request.number,
215-
});
220+
const listFilesOptions = octokit.rest.pulls.listFiles.endpoint.merge({
221+
owner: github.context.repo.owner,
222+
repo: github.context.repo.repo,
223+
pull_number: github.context.payload.pull_request.number,
224+
});
216225
const listFilesResponse = await octokit.paginate(listFilesOptions);
217-
const changedFiles = listFilesResponse.map(file => file.filename);
226+
const changedFiles = listFilesResponse.map((file) => file.filename);
218227

219228
lines = lines.filter((line, index) => {
220229
if (index <= 2) return true; // Include header
221230

222231
for (const changedFile of changedFiles) {
223232
console.log(`${line} === ${changedFile}`);
224233

225-
if (path.join(workingDirectory, line).startsWith(changedFile)) return true;
234+
if (path.join(workingDirectory, line).startsWith(changedFile))
235+
return true;
226236
}
227237

228238
return false;
229239
});
230240

231-
if (lines.length === 3) { // Only the header remains
232-
return ' n/a';
241+
if (lines.length === 3) {
242+
// Only the header remains
243+
return " n/a";
233244
}
234245

235-
return '\n ' + lines.join('\n ');
246+
return "\n " + lines.join("\n ");
236247
}
237248

238249
run();

0 commit comments

Comments
 (0)