Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/semantic-release-1ec…
Browse files Browse the repository at this point in the history
…fe53883
  • Loading branch information
davelosert authored Aug 31, 2024
2 parents 4a66d21 + 9f92563 commit 14cf231
Show file tree
Hide file tree
Showing 21 changed files with 619 additions and 206 deletions.
62 changes: 49 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,70 @@ on:
pull_request:

jobs:
build-and-test:
test:
permissions:
pull-requests: write

strategy:
matrix:
branch:
- ${{ github.head_ref }}
- "main"

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ matrix.branch }}
## Set repository to correctly checkout from forks
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: "Install Node"
uses: actions/setup-node@v4
with:
node-version: "20.x"
cache: "npm"
- name: "Install Deps"
run: npm install
- name: "Build"
run: npm run build
run: npm ci
- name: "Test"
run: npm run test:coverage
# Remove node_modules to see if this action runs entirely compiled
- name: "Upload Coverage"
uses: actions/upload-artifact@v4
with:
name: coverage-${{ matrix.branch }}
path: coverage

build-and-report:
runs-on: ubuntu-latest
needs: test
steps:
- uses: actions/checkout@v4
- name: "Install Node"
uses: actions/setup-node@v4
with:
node-version: "20.x"
cache: "npm"
- name: "Install Deps"
run: npm ci
- name: "Build"
run: npm run build
# Remove node_modules to see if this action runs entirely compiled
- name: "Remove Node Modules"
run: rm -rf node_modules
- name: "Test Working Directory Option"
uses: ./

- name: "Download Coverage Artifacts for ${{ github.head_ref}}"
uses: actions/download-artifact@v4
with:
working-directory: "./test/mockReports"
name: "Mock Reports"
- name: "Test Default Action"
# run step also on failure of the previous step
if: always()
name: coverage-${{ github.head_ref }}
path: coverage

- name: "Download Coverage Artifacts for main"
uses: actions/download-artifact@v4
with:
name: coverage-main
path: coverage-main

- name: "Test Action by genearting coverage"
uses: ./
with:
file-coverage-mode: "all"
name: "Root"
json-summary-compare-path: coverage-main/coverage-summary.json
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ jobs:
- uses: actions/checkout@v4
with:
ref: ${{ matrix.branch }}
## Set repository to correctly checkout from forks
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: "Install Node"
uses: actions/setup-node@v4
with:
Expand Down
74 changes: 44 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,78 @@ import {
parseVitestJsonFinal,
parseVitestJsonSummary,
} from "./inputs/parseJsonReports.js";
import { createOctokit } from "./octokit.js";
import { generateCommitSHAUrl } from "./report/generateCommitSHAUrl.js";
import { generateFileCoverageHtml } from "./report/generateFileCoverageHtml.js";
import { generateHeadline } from "./report/generateHeadline.js";
import { generateSummaryTableHtml } from "./report/generateSummaryTableHtml.js";
import type { JsonSummary } from "./types/JsonSummary.js";
import { writeSummaryToPR } from "./writeSummaryToPR.js";

const run = async () => {
const {
fileCoverageMode,
jsonFinalPath,
jsonSummaryPath,
jsonSummaryComparePath,
name,
thresholds,
workingDirectory,
processedPrNumber,
} = await readOptions();
const octokit = createOctokit();

const jsonSummary = await parseVitestJsonSummary(jsonSummaryPath);
const options = await readOptions(octokit);
core.info(`Using options: ${JSON.stringify(options, null, 2)}`);

const jsonSummary = await parseVitestJsonSummary(options.jsonSummaryPath);

let jsonSummaryCompare: JsonSummary | undefined;
if (jsonSummaryComparePath) {
jsonSummaryCompare = await parseVitestJsonSummary(jsonSummaryComparePath);
if (options.jsonSummaryComparePath) {
jsonSummaryCompare = await parseVitestJsonSummary(
options.jsonSummaryComparePath,
);
}

const tableData = generateSummaryTableHtml(
jsonSummary.total,
thresholds,
jsonSummaryCompare?.total,
);
const summary = core.summary
.addHeading(generateHeadline({ workingDirectory, name }), 2)
.addRaw(tableData);
.addHeading(
generateHeadline({
workingDirectory: options.workingDirectory,
name: options.name,
}),
2,
)
.addRaw(
generateSummaryTableHtml(
jsonSummary.total,
options.thresholds,
jsonSummaryCompare?.total,
),
);

if (fileCoverageMode !== FileCoverageMode.None) {
if (options.fileCoverageMode !== FileCoverageMode.None) {
const pullChanges = await getPullChanges({
fileCoverageMode,
prNumber: processedPrNumber,
fileCoverageMode: options.fileCoverageMode,
prNumber: options.prNumber,
octokit,
});
const jsonFinal = await parseVitestJsonFinal(jsonFinalPath);

const jsonFinal = await parseVitestJsonFinal(options.jsonFinalPath);
const fileTable = generateFileCoverageHtml({
jsonSummary,
jsonFinal,
fileCoverageMode,
fileCoverageMode: options.fileCoverageMode,
pullChanges,
commitSHA: options.commitSHA,
});
summary.addDetails("File Coverage", fileTable);
}

const commitSHAUrl = generateCommitSHAUrl(options.commitSHA);

summary.addRaw(
`<em>Generated in workflow <a href=${getWorkflowSummaryURL()}>#${github.context.runNumber}</a></em>`,
`<em>Generated in workflow <a href=${getWorkflowSummaryURL()}>#${github.context.runNumber}</a> for commit <a href="${commitSHAUrl}">${options.commitSHA.substring(0, 7)}</a> by the <a href="https://github.com/davelosert/vitest-coverage-report-action">Vitest Coverage Report Action</a></em>`,
);

try {
await writeSummaryToPR({
octokit,
summary,
markerPostfix: getMarkerPostfix({ name, workingDirectory }),
userDefinedPrNumber: processedPrNumber,
markerPostfix: getMarkerPostfix({
name: options.name,
workingDirectory: options.workingDirectory,
}),
prNumber: options.prNumber,
});
} catch (error) {
if (
Expand All @@ -74,8 +88,8 @@ const run = async () => {
) {
core.warning(
`Couldn't write a comment to the pull-request. Please make sure your job has the permission 'pull-request: write'.
Original Error was: [${error.name}] - ${error.message}
`,
Original Error was: [${error.name}] - ${error.message}
`,
);
} else {
// Rethrow to handle it in the catch block of the run()-call.
Expand Down
81 changes: 81 additions & 0 deletions src/inputs/getCommitSHA.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { getCommitSHA } from "./getCommitSHA";

const mockContext = vi.hoisted(() => ({
repo: {
owner: "owner",
repo: "repo",
},
payload: {},
eventName: "",
sha: "defaultsha",
}));
vi.mock("@actions/github", () => ({
context: mockContext,
}));

describe("getCommitSHA()", () => {
beforeEach(() => {
vi.clearAllMocks();
mockContext.payload = {};
mockContext.eventName = "";
mockContext.sha = "defaultsha";
});

afterEach(() => {
mockContext.payload = {};
mockContext.eventName = "";
mockContext.sha = "defaultsha";
vi.unstubAllEnvs();
});

it("if in pull-request context, returns the head sha", () => {
mockContext.eventName = "pull_request";
mockContext.payload = {
pull_request: {
head: {
sha: "prsha",
},
},
};

const result = getCommitSHA();
expect(result).toBe("prsha");
});

it("if in pull_request_target context, returns the head sha", () => {
mockContext.eventName = "pull_request_target";
mockContext.payload = {
pull_request: {
head: {
sha: "prsha",
},
},
};

const result = getCommitSHA();
expect(result).toBe("prsha");
});

it("if in workflow_run context, returns the SHA from workflow_run context if found", () => {
mockContext.eventName = "workflow_run";
mockContext.payload = {
workflow_run: {
head_commit: {
id: "workflowsha",
},
},
};

const result = getCommitSHA();
expect(result).toBe("workflowsha");
});

it("returns the default SHA for other events", () => {
mockContext.eventName = "push";
mockContext.sha = "pushsha";

const result = getCommitSHA();
expect(result).toBe("pushsha");
});
});
33 changes: 33 additions & 0 deletions src/inputs/getCommitSHA.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as github from "@actions/github";

type Context = typeof github.context;
type Payload = Context["payload"];
type PRPayload = NonNullable<Payload["pull_request"]>;

type PRContext = Context & {
payload: Payload & {
pull_request: PRPayload;
};
};

function isPRContext(context: typeof github.context): context is PRContext {
return (
context.eventName === "pull_request" ||
context.eventName === "pull_request_target"
);
}

function getCommitSHA(): string {
if (isPRContext(github.context)) {
return github.context.payload.pull_request.head.sha;
}

if (github.context.eventName === "workflow_run") {
return github.context.payload.workflow_run.head_commit.id;
}

// For all other events, just return the current SHA
return github.context.sha;
}

export { getCommitSHA };
Loading

0 comments on commit 14cf231

Please sign in to comment.