Skip to content

Commit

Permalink
feat(pr-comments): Adding renamed file in check redirect section (#142)
Browse files Browse the repository at this point in the history
* Extract group file by state in method
* Updated tests
* Adding format command in all doc-sites workspace

Closes #141
  • Loading branch information
tbouffard authored Oct 16, 2024
1 parent ac68404 commit d603dcb
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/
*.iml
node_modules/
lib/
reports/
15 changes: 15 additions & 0 deletions packages/doc-contribs/common/__tests__/github-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ describe("github-utils", () => {
{ filename: "file1.ts", status: "modified" },
{ filename: "file2.ts", status: "added" },
{ filename: "file3.ts", status: "removed" },
{
filename: "file4.ts",
status: "renamed",
previous_filename: "old_file4.ts",
},
],
};

Expand All @@ -56,6 +61,11 @@ describe("github-utils", () => {
{ filename: "file1.ts", status: "modified" },
{ filename: "file2.ts", status: "added" },
{ filename: "file3.ts", status: "removed" },
{
filename: "file4.ts",
status: "renamed",
previous_filename: "old_file4.ts",
},
]);
expect(octokit.rest.pulls.listFiles).toHaveBeenCalled();
});
Expand All @@ -66,6 +76,11 @@ describe("github-utils", () => {
{ filename: "file1.ts", status: githubUtils.FILE_STATE.MODIFIED },
{ filename: "file2.ts", status: githubUtils.FILE_STATE.ADDED },
{ filename: "file3.ts", status: githubUtils.FILE_STATE.REMOVED },
{
filename: "file4.ts",
status: githubUtils.FILE_STATE.RENAMED,
previous_filename: "old_file4.ts",
},
],
};

Expand Down
7 changes: 6 additions & 1 deletion packages/doc-contribs/common/src/github-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function getFileContent(

export type FileInfo = {
filename: string;
previous_filename?: string;
status: FILE_STATE;
};
export async function getFilesFromPR(
Expand All @@ -96,7 +97,11 @@ export async function getFilesFromPR(

const prFiles = data
.filter((file: any) => states.includes(file.status))
.map((file: any) => ({ filename: file.filename, status: file.status }));
.map((file: any) => ({
filename: file.filename,
status: file.status,
previous_filename: file.previous_filename,
}));

core.debug(
`Analyze ${prFiles.length} files in PR #${prNumber}: \n ${prFiles.join(
Expand Down
3 changes: 2 additions & 1 deletion packages/doc-contribs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
},
"scripts": {
"build": "npm run build --workspaces --if-present",
"lint": "npm run format --workspaces --if-present",
"package": "npm run package --workspaces --if-present",
"package:all": "npm run package:all --workspaces --if-present",
"test": "npm run test --workspaces --if-present"
}
}
}
104 changes: 100 additions & 4 deletions packages/doc-contribs/pr-comments-with-links/__tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { run } from "../src/main";
import { groupFilesByChangeType, run } from "../src/main";
import * as core from "@actions/core";

import { FILE_STATE, getFilesFromPR, publishComment } from "actions-common";
import {
FILE_STATE,
FileInfo,
getFilesFromPR,
publishComment,
} from "actions-common";

jest.mock("@actions/core");
jest.mock("@actions/github", () => ({
Expand Down Expand Up @@ -49,7 +54,7 @@ describe("comments-pr-with-links", () => {
});
});

it("publishes comment with updated and deleted links", async () => {
it("publishes comment with updated and requiringRedirects links", async () => {
mockGetFilesFromPR.mockResolvedValue([
{
status: FILE_STATE.MODIFIED,
Expand All @@ -66,7 +71,11 @@ describe("comments-pr-with-links", () => {

it("publishes comment with renamed links", async () => {
mockGetFilesFromPR.mockResolvedValue([
{ status: FILE_STATE.RENAMED, filename: "modules/ROOT/pages/faq.adoc" },
{
status: FILE_STATE.RENAMED,
filename: "modules/ROOT/pages/faq.adoc",
previous_filename: "modules/ROOT/pages/faq_old.adoc",
},
]);

await run();
Expand All @@ -93,3 +102,90 @@ describe("comments-pr-with-links", () => {
expect(core.setFailed).toHaveBeenCalledWith("Failed to get files");
});
});

describe("groupFilesByChangeType", () => {
it("returns filesWithUpdatedContent and filesRequiringRedirects correctly", () => {
const files: FileInfo[] = [
{ status: FILE_STATE.MODIFIED, filename: "file1.adoc" },
{ status: FILE_STATE.ADDED, filename: "file2.adoc" },
{
status: FILE_STATE.RENAMED,
filename: "file3.adoc",
previous_filename: "file3_old.adoc",
},
{ status: FILE_STATE.REMOVED, filename: "file4.adoc" },
];

const result = groupFilesByChangeType(files);

expect(result.filesWithUpdatedContent).toEqual([
"file1.adoc",
"file2.adoc",
"file3.adoc",
]);
expect(result.filesRequiringRedirects).toEqual([
"file3_old.adoc",
"file4.adoc",
]);
});

it("returns empty arrays when no files match the criteria", () => {
const files: FileInfo[] = [
{ status: FILE_STATE.REMOVED, filename: "file1.adoc" },
{ status: FILE_STATE.REMOVED, filename: "file2.adoc" },
];

const result = groupFilesByChangeType(files);

expect(result.filesWithUpdatedContent).toEqual([]);
expect(result.filesRequiringRedirects).toEqual([
"file1.adoc",
"file2.adoc",
]);
});

it("handles an empty files array", () => {
const files: FileInfo[] = [];

const result = groupFilesByChangeType(files);

expect(result.filesWithUpdatedContent).toEqual([]);
expect(result.filesRequiringRedirects).toEqual([]);
});

it("handles files with only add/modify/rename states", () => {
const files: FileInfo[] = [
{ status: FILE_STATE.MODIFIED, filename: "file1.adoc" },
{ status: FILE_STATE.ADDED, filename: "file2.adoc" },
{
status: FILE_STATE.RENAMED,
filename: "file3.adoc",
previous_filename: "file3_old.adoc",
},
];

const result = groupFilesByChangeType(files);

expect(result.filesWithUpdatedContent).toEqual([
"file1.adoc",
"file2.adoc",
"file3.adoc",
]);
expect(result.filesRequiringRedirects).toEqual(["file3_old.adoc"]);
});

it("handles files with only removed state", () => {
const files: FileInfo[] = [
{ status: FILE_STATE.REMOVED, filename: "file1.adoc" },
{ status: FILE_STATE.REMOVED, filename: "file2.adoc" },
];

const result = groupFilesByChangeType(files);

expect(result.filesWithUpdatedContent).toEqual([]);
expect(result.filesRequiringRedirects).toEqual([
"file1.adoc",
"file2.adoc",
]);
});
});
74 changes: 44 additions & 30 deletions packages/doc-contribs/pr-comments-with-links/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29245,18 +29245,18 @@ class CommentsWithLinks {
return preparedLinks;
}
/**
* Builds a message containing links to the updated and deleted pages.
* Builds a message containing links to the updated and requiringRedirects pages.
*
* @param links - An object containing the updated and deleted links.
* @param links - An object containing the updated and requiringRedirects links.
* @param links.updated - An array of strings containing the links to the updated pages.
* @param links.deleted - An array of strings containing the links to the deleted pages.
* @param links.requiringRedirects - An array of strings containing the links to the requiringRedirects pages.
* @returns A string representing the complete message to be published.
*
* @example
* ```typescript
* const links = {
* updated: ['- [ ] [page1](http://example.com/component/1.0/page1)'],
* deleted: ['- [ ] [page2](http://example.com/component/1.0/page2)']
* requiringRedirects: ['- [ ] [page2](http://example.com/component/1.0/page2)']
* };
* ```
*/
Expand All @@ -29271,12 +29271,12 @@ class CommentsWithLinks {
${links === null || links === void 0 ? void 0 : links.updated.map((item) => `> ${item}`).join("\n")}`;
}
let deletedSection = "";
if ((links === null || links === void 0 ? void 0 : links.deleted.length) > 0) {
if ((links === null || links === void 0 ? void 0 : links.requiringRedirects.length) > 0) {
deletedSection = `
### :mag: Check redirects
> [!warning]
> At least one page has been renamed, moved or deleted in the Pull Request. Make sure you add [**aliases**](https://github.com/bonitasoft/bonita-documentation-site/blob/master/docs/content/CONTRIBUTING.adoc#use-alias-to-create-redirects) and **check that the following links redirect to the right place**:
${links === null || links === void 0 ? void 0 : links.deleted.map((item) => `> ${item}`).join("\n")}`;
${links === null || links === void 0 ? void 0 : links.requiringRedirects.map((item) => `> ${item}`).join("\n")}`;
}
return this.template
.concat(header)
Expand Down Expand Up @@ -29328,52 +29328,62 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.groupFilesByChangeType = groupFilesByChangeType;
exports.run = run;
const core = __importStar(__nccwpck_require__(5316));
const github = __importStar(__nccwpck_require__(2189));
const actions_common_1 = __nccwpck_require__(6458);
const CommentsWithLinks_1 = __nccwpck_require__(3264);
const template = "<!-- previewLinksCheck-->\n";
function groupFilesByChangeType(files) {
const filesWithUpdatedContent = files
.filter((file) => [actions_common_1.FILE_STATE.MODIFIED, actions_common_1.FILE_STATE.ADDED, actions_common_1.FILE_STATE.RENAMED].includes(file.status))
.map((file) => file.filename);
core.debug(`Files with updated content: ${filesWithUpdatedContent.join(", ")}`);
const filesRequiringRedirects = files
.filter((file) => [actions_common_1.FILE_STATE.REMOVED, actions_common_1.FILE_STATE.RENAMED].includes(file.status))
.map((file) => file.status == actions_common_1.FILE_STATE.REMOVED
? file.filename
: file.previous_filename);
core.debug(`Files requiring redirects: ${filesRequiringRedirects.join(", ")}`);
return { filesWithUpdatedContent, filesRequiringRedirects };
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
core.debug("Running pr-comments-with-links action");
try {
const token = core.getInput("github-token");
const componentName = core.getInput("component-name");
const siteUrl = core.getInput("site-url");
const octokit = github.getOctokit(token);
const modifiedFiles = yield (0, actions_common_1.getFilesFromPR)(octokit, [
const prFiles = yield (0, actions_common_1.getFilesFromPR)(octokit, [
actions_common_1.FILE_STATE.REMOVED,
actions_common_1.FILE_STATE.MODIFIED,
actions_common_1.FILE_STATE.ADDED,
actions_common_1.FILE_STATE.RENAMED,
]);
core.debug(`PR files: ${prFiles.map((file) => file.filename).join(", ")}`);
const { filesWithUpdatedContent, filesRequiringRedirects } = groupFilesByChangeType(prFiles);
const commentsWithLinks = new CommentsWithLinks_1.CommentsWithLinks(template);
const addModifyFiles = modifiedFiles
.filter((file) => [actions_common_1.FILE_STATE.MODIFIED, actions_common_1.FILE_STATE.ADDED, actions_common_1.FILE_STATE.RENAMED].includes(file.status))
.map((file) => file.filename);
core.debug(`Add/modify/renamed files: ${addModifyFiles.join(", ")}`);
const deletedFiles = modifiedFiles
.filter((file) => file.status === actions_common_1.FILE_STATE.REMOVED)
.map((file) => file.filename);
core.debug(`Deleted files: ${deletedFiles.join(", ")}`);
const links = {};
// We only have a single version for preview (latest)
// TODO: Handle "pre-release" (next)
let version = "latest";
links.updated = commentsWithLinks.prepareLinks({
files: addModifyFiles,
siteUrl: siteUrl,
component: componentName,
version: version,
});
links.deleted = commentsWithLinks.prepareLinks({
files: deletedFiles,
siteUrl: siteUrl,
component: componentName,
version: version,
});
if (links.deleted.length === 0 && links.updated.length === 0) {
const links = {
updated: commentsWithLinks.prepareLinks({
files: filesWithUpdatedContent,
siteUrl: siteUrl,
component: componentName,
version: version,
}),
requiringRedirects: commentsWithLinks.prepareLinks({
files: filesRequiringRedirects,
siteUrl: siteUrl,
component: componentName,
version: version,
}),
};
if (links.requiringRedirects.length === 0 && links.updated.length === 0) {
core.info(`⚠️ No updated or deleted pages were detected`);
}
else {
Expand Down Expand Up @@ -29525,7 +29535,11 @@ function getFilesFromPR(octokit_1) {
core.debug(`Keep only files with status: ${states.join(" - ")}`);
const prFiles = data
.filter((file) => states.includes(file.status))
.map((file) => ({ filename: file.filename, status: file.status }));
.map((file) => ({
filename: file.filename,
status: file.status,
previous_filename: file.previous_filename,
}));
core.debug(`Analyze ${prFiles.length} files in PR #${prNumber}: \n ${prFiles.join("\n")}`);
return prFiles;
});
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface Links {
updated: string[];
deleted: string[];
requiringRedirects: string[];
}

/**
Expand Down Expand Up @@ -55,18 +55,18 @@ export class CommentsWithLinks {
}

/**
* Builds a message containing links to the updated and deleted pages.
* Builds a message containing links to the updated and requiringRedirects pages.
*
* @param links - An object containing the updated and deleted links.
* @param links - An object containing the updated and requiringRedirects links.
* @param links.updated - An array of strings containing the links to the updated pages.
* @param links.deleted - An array of strings containing the links to the deleted pages.
* @param links.requiringRedirects - An array of strings containing the links to the requiringRedirects pages.
* @returns A string representing the complete message to be published.
*
* @example
* ```typescript
* const links = {
* updated: ['- [ ] [page1](http://example.com/component/1.0/page1)'],
* deleted: ['- [ ] [page2](http://example.com/component/1.0/page2)']
* requiringRedirects: ['- [ ] [page2](http://example.com/component/1.0/page2)']
* };
* ```
*/
Expand All @@ -84,12 +84,12 @@ ${links?.updated.map((item) => `> ${item}`).join("\n")}`;
}

let deletedSection = "";
if (links?.deleted.length > 0) {
if (links?.requiringRedirects.length > 0) {
deletedSection = `
### :mag: Check redirects
> [!warning]
> At least one page has been renamed, moved or deleted in the Pull Request. Make sure you add [**aliases**](https://github.com/bonitasoft/bonita-documentation-site/blob/master/docs/content/CONTRIBUTING.adoc#use-alias-to-create-redirects) and **check that the following links redirect to the right place**:
${links?.deleted.map((item) => `> ${item}`).join("\n")}`;
${links?.requiringRedirects.map((item) => `> ${item}`).join("\n")}`;
}

return this.template
Expand Down
Loading

0 comments on commit d603dcb

Please sign in to comment.