Skip to content

Commit

Permalink
Check which files were changed in pr
Browse files Browse the repository at this point in the history
  • Loading branch information
romeovs committed Mar 5, 2020
1 parent 05abfce commit 6614b74
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 32 deletions.
68 changes: 52 additions & 16 deletions dist/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22881,7 +22881,7 @@ function uncovered(file, options) {
.join(", ")
}

function comment (lcov, options) {
function comment(lcov, options) {
return fragment(
`Coverage after merging ${b(options.head)} into ${b(options.base)}`,
table(tbody(tr(th(percentage(lcov).toFixed(2), "%")))),
Expand All @@ -22899,24 +22899,52 @@ function diff(lcov, before, options) {
const pafter = percentage(lcov);
const pdiff = pafter - pbefore;
const plus = pdiff > 0 ? "+" : "";
const arrow =
pdiff === 0
? ""
: pdiff < 0
? "▾"
: "▴";
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴";

return fragment(
`Coverage after merging ${b(options.head)} into ${b(options.base)}`,
table(tbody(tr(
th(pafter.toFixed(2), "%"),
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
))),
table(
tbody(
tr(
th(pafter.toFixed(2), "%"),
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
),
),
),
"\n\n",
details(summary("Coverage Report"), tabulate(lcov, options)),
)
}

async function changedFiles(github, repo, commits) {
const files = [];

await Promise.all(
commits.map(async function(commit) {
if (!commit.distinct) {
return
}

const result = await github.repos.getCommit({
...repo,
ref: commit.id,
});

if (!result || !result.data) {
return
}

for (const file of result.data.files) {
if (file.status === "added" || file.status === "modified") {
files.push(file.filename);
}
}
}),
);

return files
}

async function main$1() {
const token = core$1.getInput("github-token");
const lcovFile = core$1.getInput("lcov-file") || "./coverage/lcov.info";
Expand All @@ -22928,11 +22956,20 @@ async function main$1() {
return
}

const baseRaw = baseFile && await fs.promises.readFile(baseFile, "utf-8").catch(err => null);
const baseRaw =
baseFile && (await fs.promises.readFile(baseFile, "utf-8").catch(err => null));
if (baseFile && !baseRaw) {
console.log(`No coverage report found at '${baseFile}', ignoring...`);
}

const github = new github_2(token);
const files = await changedFiles(
github,
github_1.repo,
github_1.payload.commits,
);
console.log(JSON.stringify(files, null, 2));

const options = {
repository: github_1.payload.repository.full_name,
commit: github_1.payload.pull_request.head.sha,
Expand All @@ -22942,12 +22979,11 @@ async function main$1() {
};

const lcov = await parse$2(raw);
const baselcov = baseRaw && await parse$2(baseRaw);
const baselcov = baseRaw && (await parse$2(baseRaw));
const body = diff(lcov, baselcov, options);

await new github_2(token).issues.createComment({
repo: github_1.repo.repo,
owner: github_1.repo.owner,
await github.issues.createComment({
...github_1.repo,
issue_number: github_1.payload.pull_request.number,
body: diff(lcov, baselcov, options),
});
Expand Down
21 changes: 10 additions & 11 deletions src/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { details, summary, b, fragment, table, tbody, tr, th } from "./html"
import { percentage } from "./lcov"
import { tabulate } from "./tabulate"

export function comment (lcov, options) {
export function comment(lcov, options) {
return fragment(
`Coverage after merging ${b(options.head)} into ${b(options.base)}`,
table(tbody(tr(th(percentage(lcov).toFixed(2), "%")))),
Expand All @@ -21,19 +21,18 @@ export function diff(lcov, before, options) {
const pafter = percentage(lcov)
const pdiff = pafter - pbefore
const plus = pdiff > 0 ? "+" : ""
const arrow =
pdiff === 0
? ""
: pdiff < 0
? "▾"
: "▴"
const arrow = pdiff === 0 ? "" : pdiff < 0 ? "▾" : "▴"

return fragment(
`Coverage after merging ${b(options.head)} into ${b(options.base)}`,
table(tbody(tr(
th(pafter.toFixed(2), "%"),
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
))),
table(
tbody(
tr(
th(pafter.toFixed(2), "%"),
th(arrow, " ", plus, pdiff.toFixed(2), "%"),
),
),
),
"\n\n",
details(summary("Coverage Report"), tabulate(lcov, options)),
)
Expand Down
28 changes: 28 additions & 0 deletions src/files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export async function changedFiles(github, repo, commits) {
const files = []

await Promise.all(
commits.map(async function(commit) {
if (!commit.distinct) {
return
}

const result = await github.repos.getCommit({
...repo,
ref: commit.id,
})

if (!result || !result.data) {
return
}

for (const file of result.data.files) {
if (file.status === "added" || file.status === "modified") {
files.push(file.filename)
}
}
}),
)

return files
}
19 changes: 14 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { GitHub, context } from "@actions/github"

import { parse } from "./lcov"
import { diff } from "./comment"
import { changedFiles } from "./files"

async function main() {
const token = core.getInput("github-token")
Expand All @@ -16,11 +17,20 @@ async function main() {
return
}

const baseRaw = baseFile && await fs.readFile(baseFile, "utf-8").catch(err => null)
const baseRaw =
baseFile && (await fs.readFile(baseFile, "utf-8").catch(err => null))
if (baseFile && !baseRaw) {
console.log(`No coverage report found at '${baseFile}', ignoring...`)
}

const github = new GitHub(token)
const files = await changedFiles(
github,
context.repo,
context.payload.commits,
)
console.log(JSON.stringify(files, null, 2))

const options = {
repository: context.payload.repository.full_name,
commit: context.payload.pull_request.head.sha,
Expand All @@ -30,12 +40,11 @@ async function main() {
}

const lcov = await parse(raw)
const baselcov = baseRaw && await parse(baseRaw)
const baselcov = baseRaw && (await parse(baseRaw))
const body = diff(lcov, baselcov, options)

await new GitHub(token).issues.createComment({
repo: context.repo.repo,
owner: context.repo.owner,
await github.issues.createComment({
...context.repo,
issue_number: context.payload.pull_request.number,
body: diff(lcov, baselcov, options),
})
Expand Down

0 comments on commit 6614b74

Please sign in to comment.