Skip to content

Commit 27d35c0

Browse files
committed
don't update issue if content is identical
1 parent 6df9aee commit 27d35c0

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

dist/index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -166,25 +166,22 @@ exports.IssueContentParser = void 0;
166166
const utils_1 = __nccwpck_require__(918);
167167
class IssueContentParser {
168168
extractIssueTasklist(issue) {
169-
var _a, _b;
170-
const contentLines = (_b = (_a = issue.body) === null || _a === void 0 ? void 0 : _a.split("\n")) !== null && _b !== void 0 ? _b : [];
169+
const contentLines = issue.body?.split("\n") ?? [];
171170
return contentLines
172171
.filter(x => this.isTaskListLine(x))
173172
.map(x => (0, utils_1.parseIssueUrl)(x))
174173
.filter((x) => x !== null);
175174
}
176175
extractIssueDependencies(issue) {
177-
var _a, _b;
178-
const contentLines = (_b = (_a = issue.body) === null || _a === void 0 ? void 0 : _a.split("\n")) !== null && _b !== void 0 ? _b : [];
176+
const contentLines = issue.body?.split("\n") ?? [];
179177
return contentLines
180178
.filter(x => this.isDependencyLine(x))
181179
.map(x => (0, utils_1.parseIssuesUrls)(x))
182180
.flat()
183181
.filter((x) => x !== null);
184182
}
185183
replaceIssueContent(issue, sectionTitle, newSectionContent) {
186-
var _a, _b;
187-
const contentLines = (_b = (_a = issue.body) === null || _a === void 0 ? void 0 : _a.split("\n")) !== null && _b !== void 0 ? _b : [];
184+
const contentLines = issue.body?.split("\n") ?? [];
188185
const sectionStartIndex = contentLines.findIndex(x => this.isMarkdownHeaderLine(x, sectionTitle));
189186
if (sectionStartIndex === -1) {
190187
throw new Error(`Markdown header '${sectionTitle}' is not found in issue body.`);
@@ -197,6 +194,16 @@ class IssueContentParser {
197194
...contentLines.slice(sectionEndIndex !== -1 ? sectionEndIndex : contentLines.length),
198195
].join("\n");
199196
}
197+
isIssueContentIdentical(issue, newIssueContent) {
198+
// GitHub automatically replace "\n" to "\r\n" line endings when issue body is modified through GitHub UI.
199+
// Replace "\r\n" to "\n" before comparing content to avoid unnecessary issue updates.
200+
const rawIssueBody = issue.body ?? "";
201+
const formattedIssueBody = rawIssueBody.replaceAll("\r\n", "\n");
202+
const formattedNewIssueContent = newIssueContent.replaceAll("\r\n", "\n");
203+
console.log(JSON.stringify(formattedIssueBody));
204+
console.log(JSON.stringify(formattedNewIssueContent));
205+
return formattedIssueBody === formattedNewIssueContent;
206+
}
200207
isMarkdownHeaderLine(str, sectionTitle) {
201208
if (!str.startsWith("#")) {
202209
return false;
@@ -290,6 +297,10 @@ const run = async () => {
290297
core.startGroup("Updated root issue content");
291298
core.info(updatedIssueContent);
292299
core.endGroup();
300+
if (issueContentParser.isIssueContentIdentical(rootIssue, updatedIssueContent)) {
301+
core.info("Skipping update of root issue content because new content is identical to current content. No changes in mermaid in diagram.");
302+
return;
303+
}
293304
if (inputs.dryRun) {
294305
console.log("Action is run in dry-run mode. Root issue won't be updated");
295306
return;

src/issue-content-parser.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ export class IssueContentParser {
4141
].join("\n");
4242
}
4343

44+
public isIssueContentIdentical(issue: GitHubIssue, newIssueContent: string) {
45+
// GitHub automatically replace "\n" to "\r\n" line endings when issue body is modified through GitHub UI.
46+
// Replace "\r\n" to "\n" before comparing content to avoid unnecessary issue updates.
47+
const rawIssueBody = issue.body ?? "";
48+
const formattedIssueBody = rawIssueBody.replaceAll("\r\n", "\n");
49+
const formattedNewIssueContent = newIssueContent.replaceAll("\r\n", "\n");
50+
51+
console.log(JSON.stringify(formattedIssueBody));
52+
console.log(JSON.stringify(formattedNewIssueContent));
53+
54+
return formattedIssueBody === formattedNewIssueContent;
55+
}
56+
4457
public isMarkdownHeaderLine(str: string, sectionTitle?: string): boolean {
4558
if (!str.startsWith("#")) {
4659
return false;

src/main.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ const run = async (): Promise<void> => {
4848
core.info(updatedIssueContent);
4949
core.endGroup();
5050

51+
if (issueContentParser.isIssueContentIdentical(rootIssue, updatedIssueContent)) {
52+
core.info(
53+
"Skipping update of root issue content because new content is identical to current content. No changes in mermaid in diagram."
54+
);
55+
return;
56+
}
57+
5158
if (inputs.dryRun) {
5259
console.log("Action is run in dry-run mode. Root issue won't be updated");
5360
return;

tests/issue-content-parser.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,35 @@ new diagram content
366366
});
367367
});
368368

369+
describe("isIssueContentIdentical", () => {
370+
it("content is identical", () => {
371+
const issue = {
372+
body: "# Header\r\nHello world\r\n\r\n## Header 2\r\n\r\n```mermaid\ntest content\n```\n\r\n## Header 3\r\nFooter",
373+
} as GitHubIssue;
374+
const newIssueContent =
375+
"# Header\r\nHello world\r\n\r\n## Header 2\r\n\r\n```mermaid\ntest content\n```\n\r\n## Header 3\r\nFooter";
376+
expect(issueContentParser.isIssueContentIdentical(issue, newIssueContent)).toBeTruthy();
377+
});
378+
379+
it("content is identical but different line endings", () => {
380+
const issue = {
381+
body: "# Header\r\nHello world\r\n\r\n## Header 2\r\n\r\n```mermaid\r\ntest content\r\n```\r\n\r\n## Header 3\r\nFooter",
382+
} as GitHubIssue;
383+
const newIssueContent =
384+
"# Header\r\nHello world\r\n\r\n## Header 2\r\n\r\n```mermaid\ntest content\n```\n\r\n## Header 3\r\nFooter";
385+
expect(issueContentParser.isIssueContentIdentical(issue, newIssueContent)).toBeTruthy();
386+
});
387+
388+
it("content is different", () => {
389+
const issue = {
390+
body: "# Header\r\nHello world\r\n\r\n## Header 2\r\n\r\n```mermaid\r\ntest content\r\n```\r\n\r\n## Header 3\r\nFooter",
391+
} as GitHubIssue;
392+
const newIssueContent =
393+
"# Header\r\nHello world\r\n\r\n## Header 2\r\n\r\n```mermaid\ntest content 2\n```\n\r\n## Header 3\r\nFooter";
394+
expect(issueContentParser.isIssueContentIdentical(issue, newIssueContent)).toBeFalsy();
395+
});
396+
});
397+
369398
describe("isMarkdownHeaderLine", () => {
370399
it("non-header line", () => {
371400
expect(issueContentParser.isMarkdownHeaderLine("Epic 1")).toBeFalsy();

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2019",
3+
"target": "ES2021",
44
"module": "commonjs",
55
"outDir": "./lib",
66
"rootDir": "./src",

0 commit comments

Comments
 (0)