Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 71 additions & 29 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,65 @@
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getChangedFiles = void 0;
const github_1 = __nccwpck_require__(3228);
const emptyFilesStatus = () => ({
all: [],
added: [],
removed: [],
modified: [],
renamed: [],
copied: [],
changed: [],
unchanged: [],
});
const addFile = (allFiles, filename, status) => {
allFiles.all.push(filename);
const key = status;
if (key !== 'all' && key in allFiles) {
allFiles[key].push(filename);
}
};
const getChangedFiles = async (eventInfo) => {
const allFiles = {
all: [],
added: [],
removed: [],
modified: [],
renamed: [],
copied: [],
changed: [],
unchanged: [],
};
const allFiles = emptyFilesStatus();
const octokit = (0, github_1.getOctokit)(eventInfo.token);
const perPage = 50;
let currPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const { data: { files }, } = await octokit.rest.repos.compareCommitsWithBasehead({
owner: eventInfo.owner,
repo: eventInfo.repo,
basehead: `${eventInfo.baseRef}...${eventInfo.headRef}`,
per_page: perPage,
page: currPage,
});
if (files) {
if (eventInfo.prNumber) {
const perPage = 50;
let currPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const { data: files } = await octokit.rest.pulls.listFiles({
owner: eventInfo.owner,
repo: eventInfo.repo,
pull_number: eventInfo.prNumber,
per_page: perPage,
page: currPage,
});
for (const file of files) {
allFiles.all.push(file.filename);
const status = `${file.status}`;
if (status !== 'all' && status in allFiles) {
allFiles[status].push(file.filename);
addFile(allFiles, file.filename, `${file.status}`);
}
hasMorePages = files.length >= perPage;
currPage++;
}
}
else if (eventInfo.baseRef && eventInfo.headRef) {
const perPage = 50;
let currPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const { data: { files }, } = await octokit.rest.repos.compareCommitsWithBasehead({
owner: eventInfo.owner,
repo: eventInfo.repo,
basehead: `${eventInfo.baseRef}...${eventInfo.headRef}`,
per_page: perPage,
page: currPage,
});
if (files) {
for (const file of files) {
addFile(allFiles, file.filename, `${file.status}`);
}
}
hasMorePages = (files?.length ?? 0) >= perPage;
currPage++;
}
hasMorePages = (files?.length ?? 0) >= perPage;
currPage++;
}
return allFiles;
};
Expand Down Expand Up @@ -450,12 +475,14 @@ const getEventInfo = () => {
commitSha: '',
headRef: '',
baseRef: '',
prNumber: undefined,
pwd: process.env.GITHUB_WORKSPACE || '',
};
if (github_1.context.eventName === 'pull_request' && github_1.context.payload) {
eventInfo.commitSha = github_1.context.payload.pull_request?.head.sha;
eventInfo.headRef = github_1.context.payload.pull_request?.head.ref;
eventInfo.baseRef = github_1.context.payload.pull_request?.base.ref;
eventInfo.prNumber = github_1.context.payload.pull_request?.number;
}
else if (github_1.context.eventName === 'push') {
eventInfo.commitSha = github_1.context.payload.after;
Expand Down Expand Up @@ -586,7 +613,22 @@ const main = async () => {
: [],
junit: eventInfo.showJunit ? await (0, junit_1.parse)(eventInfo.junitPath) : undefined,
};
const changedFile = await (0, changedFiles_1.getChangedFiles)(eventInfo);
let changedFile = {
all: [],
added: [],
removed: [],
modified: [],
renamed: [],
copied: [],
changed: [],
unchanged: [],
};
try {
changedFile = await (0, changedFiles_1.getChangedFiles)(eventInfo);
}
catch (error) {
core.warning(`Failed to get changed files: ${error instanceof Error ? error.message : String(error)}`);
}
const diffInfo = await (0, diffCover_1.diffCover)(eventInfo, changedFile, coverageInfo);
await (0, commentCoverage_1.commentCoverage)(eventInfo, (0, commentCoverage_1.buildBody)(eventInfo, coverageInfo.junit, diffInfo));
}
Expand Down
86 changes: 56 additions & 30 deletions src/changedFiles.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,69 @@
import { getOctokit } from '@actions/github';
import { FilesStatus, EventInfo } from './types';

const emptyFilesStatus = (): FilesStatus => ({
all: [],
added: [],
removed: [],
modified: [],
renamed: [],
copied: [],
changed: [],
unchanged: [],
});

const addFile = (allFiles: FilesStatus, filename: string, status: string): void => {
allFiles.all.push(filename);
const key = status as keyof FilesStatus;
if (key !== 'all' && key in allFiles) {
allFiles[key].push(filename);
}
};

export const getChangedFiles = async (eventInfo: EventInfo): Promise<FilesStatus> => {
const allFiles: FilesStatus = {
all: [],
added: [],
removed: [],
modified: [],
renamed: [],
copied: [],
changed: [],
unchanged: [],
};
const allFiles = emptyFilesStatus();
const octokit = getOctokit(eventInfo.token);

const perPage = 50;
let currPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const {
data: { files },
} = await octokit.rest.repos.compareCommitsWithBasehead({
owner: eventInfo.owner,
repo: eventInfo.repo,
basehead: `${eventInfo.baseRef}...${eventInfo.headRef}`,
per_page: perPage,
page: currPage,
});
if (files) {
if (eventInfo.prNumber) {
const perPage = 50;
let currPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const { data: files } = await octokit.rest.pulls.listFiles({
owner: eventInfo.owner,
repo: eventInfo.repo,
pull_number: eventInfo.prNumber,
per_page: perPage,
page: currPage,
});
for (const file of files) {
allFiles.all.push(file.filename);
const status = `${file.status}` as keyof FilesStatus;
if (status !== 'all' && status in allFiles) {
allFiles[status].push(file.filename);
addFile(allFiles, file.filename, `${file.status}`);
}
hasMorePages = files.length >= perPage;
currPage++;
}
} else if (eventInfo.baseRef && eventInfo.headRef) {
const perPage = 50;
let currPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const {
data: { files },
} = await octokit.rest.repos.compareCommitsWithBasehead({
owner: eventInfo.owner,
repo: eventInfo.repo,
basehead: `${eventInfo.baseRef}...${eventInfo.headRef}`,
per_page: perPage,
page: currPage,
});
if (files) {
for (const file of files) {
addFile(allFiles, file.filename, `${file.status}`);
}
}
hasMorePages = (files?.length ?? 0) >= perPage;
currPage++;
}
hasMorePages = (files?.length ?? 0) >= perPage;
currPage++;
}
return allFiles;
};
2 changes: 2 additions & 0 deletions src/eventInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ export const getEventInfo = (): EventInfo => {
commitSha: '',
headRef: '',
baseRef: '',
prNumber: undefined,
pwd: process.env.GITHUB_WORKSPACE || '',
};
if (context.eventName === 'pull_request' && context.payload) {
eventInfo.commitSha = context.payload.pull_request?.head.sha;
eventInfo.headRef = context.payload.pull_request?.head.ref;
eventInfo.baseRef = context.payload.pull_request?.base.ref;
eventInfo.prNumber = context.payload.pull_request?.number;
} else if (context.eventName === 'push') {
eventInfo.commitSha = context.payload.after;
eventInfo.headRef = context.ref;
Expand Down
20 changes: 18 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getChangedFiles } from './changedFiles';
import { CoverageTypeInfo, DiffInfo, EventInfo } from './types';
import { CoverageTypeInfo, DiffInfo, EventInfo, FilesStatus } from './types';
import { getEventInfo } from './eventInfo';
import { diffCover } from './diffCover';
import { parseFile as parseLcovFile } from './parsers/lcov';
Expand Down Expand Up @@ -30,7 +30,23 @@ export const main = async (): Promise<void> => {
: [],
junit: eventInfo.showJunit ? await parseJunit(eventInfo.junitPath) : undefined,
};
const changedFile = await getChangedFiles(eventInfo);
let changedFile: FilesStatus = {
all: [],
added: [],
removed: [],
modified: [],
renamed: [],
copied: [],
changed: [],
unchanged: [],
};
try {
changedFile = await getChangedFiles(eventInfo);
} catch (error) {
core.warning(
`Failed to get changed files: ${error instanceof Error ? error.message : String(error)}`,
);
}

const diffInfo: DiffInfo[] = await diffCover(eventInfo, changedFile, coverageInfo);
await commentCoverage(eventInfo, buildBody(eventInfo, coverageInfo.junit, diffInfo));
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface EventInfo {
commitSha: string;
headRef: string;
baseRef: string;
prNumber: number | undefined;
pwd: string;
filesStatus?: FilesStatus;
}
Expand Down
5 changes: 5 additions & 0 deletions test/actions.spy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export function spyActions(data = defaultData, eventName = 'pull_request') {
data: data.compareCommitsWithBasehead,
})),
},
pulls: {
listFiles: jest.fn(async () => ({
data: data.compareCommitsWithBasehead.files || [],
})),
},
},
}) as any,
);
Expand Down
Loading
Loading