Skip to content

Commit

Permalink
Merge pull request #6 from jgillick/link-files
Browse files Browse the repository at this point in the history
Link files in summary
  • Loading branch information
jgillick authored Jan 11, 2023
2 parents 5fb7e83 + e9fd079 commit 38a41a4
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 9 deletions.
46 changes: 43 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26752,7 +26752,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const crypto_1 = __importDefault(__nccwpck_require__(6113));
const github = __importStar(__nccwpck_require__(5438));
/**
* A helper class that holds a list of all the files included in the PR
Expand All @@ -26764,6 +26768,7 @@ class PRFiles {
this.files = [];
this.pathPrefix = "";
this.inputs = inputs;
this.fileMap = new Map();
}
/**
* Is this coverage file included in the list of PR files
Expand All @@ -26774,6 +26779,29 @@ class PRFiles {
}
return this.files.includes(filepath);
}
/**
* Get the URL to the file in the PR commit
*/
fileUrl(filepath) {
var _a, _b, _c;
// Construct commit URL
const repoUrl = (_a = this.inputs.context.payload.repository) === null || _a === void 0 ? void 0 : _a.html_url;
const commitSha = ((_c = (_b = this.inputs.context.payload.pull_request) === null || _b === void 0 ? void 0 : _b.head) === null || _c === void 0 ? void 0 : _c.sha) ||
this.inputs.context.sha;
if (!repoUrl || !commitSha) {
return null;
}
const baseUrl = `${repoUrl}/commit/${commitSha}`;
// File path sha
if (filepath.startsWith(this.pathPrefix)) {
filepath = filepath.substring(this.pathPrefix.length);
}
const filenameSha = crypto_1.default
.createHash("sha256")
.update(filepath)
.digest("hex");
return `${baseUrl}#diff-${filenameSha}`;
}
/**
* Load the list of files included in the PR
*/
Expand All @@ -26789,6 +26817,9 @@ class PRFiles {
});
// Get the list of file names and sort them by length
this.files = results.map((file) => file.filename).sort(pathSort);
// Add files to map
this.fileMap = new Map();
results.forEach((file) => this.fileMap.set(file.filename, file));
}
/**
* Read the test coverage file and extract a list of files that are included in the PR
Expand Down Expand Up @@ -26866,6 +26897,7 @@ function generateDiffReport(coverage, baseCoverage, prFiles, inputs) {
// Generate delta
const section = {
isNewFile,
fileUrl: key !== "total" ? prFiles.fileUrl(key) : null,
lines: generateDiff("lines", target, base),
statements: generateDiff("statements", target, base),
functions: generateDiff("functions", target, base),
Expand Down Expand Up @@ -27188,6 +27220,7 @@ function getTemplateVars(report, failureMessage, inputs) {
const tmplFileSummary = {
name,
isNewFile: summary.isNewFile,
fileUrl: summary.fileUrl,
lines: { percent: "0", diff: "0" },
statements: { percent: "0", diff: "0" },
functions: { percent: "0", diff: "0" },
Expand Down Expand Up @@ -27322,10 +27355,10 @@ function renderFileSummaryFactory(inputs) {
else if (linePercent > 40) {
coverageStatus = ":yellow_circle:";
}
// Diff change status for the file
// Overall diff status for the file
// If any of the diffs are below zero, the file get's a negative icon
const minDiff = Object.values(summary).reduce((val, item) => {
if (typeof item === "object") {
if (typeof item === "object" && item !== null) {
const diff = Number(item.diff);
if (diff < val) {
return diff;
Expand All @@ -27340,6 +27373,13 @@ function renderFileSummaryFactory(inputs) {
}
return text;
};
const formatTitle = () => {
const value = formatText(summary.name);
if (summary.fileUrl) {
return `[${value}](${summary.fileUrl})`;
}
return value;
};
const itemOutput = (item) => {
let itemOut = `${item.percent}%`;
if (hasDiffs && item.diff !== "0") {
Expand All @@ -27348,7 +27388,7 @@ function renderFileSummaryFactory(inputs) {
return formatText(itemOut);
};
return (`| ${coverageStatus}${changeStatus} ` +
`| ${formatText(summary.name)} ` +
`| ${formatTitle()} ` +
`| ${itemOutput(summary.statements)} ` +
`| ${itemOutput(summary.branches)} ` +
`| ${itemOutput(summary.functions)} ` +
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

39 changes: 38 additions & 1 deletion src/PRFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ describe("PRFiles", () => {
repo: "",
owner: "",
},
} as Context,
payload: {
pull_request: {
number: 5,
head: {
sha: "123",
},
},
repository: {
html_url: "https://github.com/jgillick/test-coverage-reporter",
},
},
} as unknown as Context,
};
prFiles = new PRFiles(inputs);
});
Expand Down Expand Up @@ -84,4 +95,30 @@ describe("PRFiles", () => {
expect(prFiles.inPR("/x/y/z/b")).toBe(false);
});
});

describe("fileUrl", () => {
beforeEach(async () => {
jest.spyOn(github, "getOctokit").mockReturnValue({
paginate: jest.fn(() =>
Promise.resolve([
{ filename: "/z" },
{ filename: "/a/b/c" },
{ filename: "/abcdefg/hijklmn" },
{ filename: "/c/d/e/f/g" },
{ filename: "/x/y" },
])
) as unknown as OctoKit,
} as unknown as OctoKit);

await prFiles.fetchPRFiles();
});

test("get file URL", async () => {
prFiles.pathPrefix = "/x";
const url = prFiles.fileUrl("/x/a/b/c");
expect(url).toBe(
"https://github.com/jgillick/test-coverage-reporter/commit/123#diff-42146b29e39fde22717ef69b5b3d8205802517fa2f0c55ea1d6730861b908578"
);
});
});
});
40 changes: 40 additions & 0 deletions src/PRFiles.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import crypto from "crypto";
import * as github from "@actions/github";
import { Inputs, CoverageSummary } from "./types";

type Octokit = ReturnType<typeof github.getOctokit>;
type PrFile = Awaited<
ReturnType<Octokit["rest"]["pulls"]["listFiles"]>
>["data"][0];

/**
* A helper class that holds a list of all the files included in the PR
* and provides helper methods to query this list.
*/
export default class PRFiles {
inputs: Inputs;
files: string[];
fileMap: Map<string, PrFile>;
pathPrefix: string = "";

constructor(inputs: Inputs) {
this.files = [];
this.pathPrefix = "";
this.inputs = inputs;
this.fileMap = new Map<string, PrFile>();
}

/**
Expand All @@ -26,6 +34,34 @@ export default class PRFiles {
return this.files.includes(filepath);
}

/**
* Get the URL to the file in the PR commit
*/
fileUrl(filepath: string): string | null {
// Construct commit URL
const repoUrl = this.inputs.context.payload.repository?.html_url;
const commitSha =
this.inputs.context.payload.pull_request?.head?.sha ||
this.inputs.context.sha;

if (!repoUrl || !commitSha) {
return null;
}

const baseUrl = `${repoUrl}/commit/${commitSha}`;

// File path sha
if (filepath.startsWith(this.pathPrefix)) {
filepath = filepath.substring(this.pathPrefix.length);
}
const filenameSha = crypto
.createHash("sha256")
.update(filepath)
.digest("hex");

return `${baseUrl}#diff-${filenameSha}`;
}

/**
* Load the list of files included in the PR
*/
Expand All @@ -47,6 +83,10 @@ export default class PRFiles {

// Get the list of file names and sort them by length
this.files = results.map((file) => file.filename).sort(pathSort);

// Add files to map
this.fileMap = new Map<string, PrFile>();
results.forEach((file) => this.fileMap.set(file.filename, file));
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ describe("diff", () => {
issue: {
number: 123,
},
} as Context,
payload: {
pull_request: {
head: {
sha: "1234",
},
},
},
} as unknown as Context,
};

prFiles = new PRFiles(inputs);
Expand Down
1 change: 1 addition & 0 deletions src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export function generateDiffReport(
// Generate delta
const section = {
isNewFile,
fileUrl: key !== "total" ? prFiles.fileUrl(key) : null,
lines: generateDiff("lines", target, base),
statements: generateDiff("statements", target, base),
functions: generateDiff("functions", target, base),
Expand Down
15 changes: 12 additions & 3 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export function getTemplateVars(
const tmplFileSummary: TemplateDiffSummary = {
name,
isNewFile: summary.isNewFile,
fileUrl: summary.fileUrl,
lines: { percent: "0", diff: "0" },
statements: { percent: "0", diff: "0" },
functions: { percent: "0", diff: "0" },
Expand Down Expand Up @@ -258,10 +259,10 @@ function renderFileSummaryFactory(inputs: Inputs) {
coverageStatus = ":yellow_circle:";
}

// Diff change status for the file
// Overall diff status for the file
// If any of the diffs are below zero, the file get's a negative icon
const minDiff = Object.values(summary).reduce((val, item) => {
if (typeof item === "object") {
if (typeof item === "object" && item !== null) {
const diff = Number(item.diff);
if (diff < val) {
return diff;
Expand All @@ -278,6 +279,14 @@ function renderFileSummaryFactory(inputs: Inputs) {
return text;
};

const formatTitle = () => {
const value = formatText(summary.name);
if (summary.fileUrl) {
return `[${value}](${summary.fileUrl})`;
}
return value;
};

const itemOutput = (item: TemplateDiffSummaryValues) => {
let itemOut = `${item.percent}%`;
if (hasDiffs && item.diff !== "0") {
Expand All @@ -288,7 +297,7 @@ function renderFileSummaryFactory(inputs: Inputs) {

return (
`| ${coverageStatus}${changeStatus} ` +
`| ${formatText(summary.name)} ` +
`| ${formatTitle()} ` +
`| ${itemOutput(summary.statements)} ` +
`| ${itemOutput(summary.branches)} ` +
`| ${itemOutput(summary.functions)} ` +
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type DiffSummary = {
functions: CoverageDiff;
branches: CoverageDiff;
isNewFile?: boolean;
fileUrl?: string | null;
};

export type CoverageDiff = {
Expand Down Expand Up @@ -79,6 +80,7 @@ export type TemplateDiffSummary = {
functions: TemplateDiffSummaryValues;
branches: TemplateDiffSummaryValues;
isNewFile?: boolean;
fileUrl?: string | null;
};

export type TemplateDiffSummaryValues = {
Expand Down

0 comments on commit 38a41a4

Please sign in to comment.