Skip to content

Commit 308b2fd

Browse files
authored
Don't create a comment with hide: true (#1661)
1 parent 3bbec31 commit 308b2fd

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

__tests__/main.test.ts

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,24 @@ describe("run", () => {
119119
)
120120
})
121121

122+
test("fails when deleteOldComment and onlyCreateComment are both true", async () => {
123+
mockConfig.deleteOldComment = true
124+
mockConfig.onlyCreateComment = true
125+
const {core} = await runMain()
126+
expect(core.setFailed).toHaveBeenCalledWith(
127+
"delete and only_create cannot be both set to true",
128+
)
129+
})
130+
131+
test("fails when deleteOldComment and hideOldComment are both true", async () => {
132+
mockConfig.deleteOldComment = true
133+
mockConfig.hideOldComment = true
134+
const {core} = await runMain()
135+
expect(core.setFailed).toHaveBeenCalledWith(
136+
"delete and hide cannot be both set to true",
137+
)
138+
})
139+
122140
test("fails when onlyCreateComment and onlyUpdateComment are both true", async () => {
123141
mockConfig.onlyCreateComment = true
124142
mockConfig.onlyUpdateComment = true
@@ -137,7 +155,7 @@ describe("run", () => {
137155
)
138156
})
139157

140-
test("deletes previous comment when deleteOldComment is true and previous exists", async () => {
158+
test("deletes previous comment when deleteOldComment is true and previous comment exists", async () => {
141159
mockConfig.deleteOldComment = true
142160
const previous = {id: "existing-id", body: "old body"}
143161
const {comment, core} = await runMain(({comment}) => {
@@ -146,19 +164,41 @@ describe("run", () => {
146164
expect(core.setOutput).toHaveBeenCalledWith("previous_comment_id", "existing-id")
147165
expect(comment.deleteComment).toHaveBeenCalledWith(expect.anything(), "existing-id")
148166
expect(comment.createComment).not.toHaveBeenCalled()
167+
expect(comment.updateComment).not.toHaveBeenCalled()
149168
})
150169

151170
test("skips delete when deleteOldComment is true but no previous comment exists", async () => {
152171
mockConfig.deleteOldComment = true
153172
const {comment, core} = await runMain()
154173
expect(core.setOutput).toHaveBeenCalledWith("previous_comment_id", undefined)
155174
expect(comment.deleteComment).not.toHaveBeenCalled()
175+
expect(comment.createComment).not.toHaveBeenCalled()
176+
expect(comment.updateComment).not.toHaveBeenCalled()
156177
})
157178

158-
test("skips creating comment when onlyUpdateComment is true and no previous exists", async () => {
179+
test("Updates previous comment when onlyUpdateComment is true and previous comment exists", async () => {
180+
mockConfig.onlyUpdateComment = true
181+
const previous = {id: "existing-id", body: "old body"}
182+
const {comment, core} = await runMain(({comment}) => {
183+
vi.mocked(comment.findPreviousComment).mockResolvedValue(previous as any)
184+
vi.mocked(comment.getBodyOf).mockReturnValue("previous body content")
185+
})
186+
expect(comment.updateComment).toHaveBeenCalledWith(
187+
expect.anything(),
188+
"existing-id",
189+
"test body",
190+
"",
191+
"previous body content",
192+
)
193+
expect(comment.createComment).not.toHaveBeenCalled()
194+
expect(core.setOutput).toHaveBeenCalledWith("previous_comment_id", "existing-id")
195+
})
196+
197+
test("skips creating comment when onlyUpdateComment is true and no previous comment exists", async () => {
159198
mockConfig.onlyUpdateComment = true
160199
const {comment} = await runMain()
161200
expect(comment.createComment).not.toHaveBeenCalled()
201+
expect(comment.updateComment).not.toHaveBeenCalled()
162202
})
163203

164204
test("creates comment when no previous comment exists", async () => {
@@ -173,7 +213,7 @@ describe("run", () => {
173213
expect(core.setOutput).toHaveBeenCalledWith("created_comment_id", 456)
174214
})
175215

176-
test("skips update when onlyCreateComment is true and previous exists", async () => {
216+
test("skips update when onlyCreateComment is true and previous comment exists", async () => {
177217
mockConfig.onlyCreateComment = true
178218
const previous = {id: "existing-id", body: "old body"}
179219
const {comment} = await runMain(({comment}) => {
@@ -197,6 +237,14 @@ describe("run", () => {
197237
expect(comment.updateComment).not.toHaveBeenCalled()
198238
})
199239

240+
test("skips when hideOldComment is true and no previous comment exists", async () => {
241+
mockConfig.hideOldComment = true
242+
const {comment} = await runMain()
243+
expect(comment.minimizeComment).not.toHaveBeenCalled()
244+
expect(comment.createComment).not.toHaveBeenCalled()
245+
expect(comment.updateComment).not.toHaveBeenCalled()
246+
})
247+
200248
test("skips update when skipUnchanged is true and body is unchanged", async () => {
201249
mockConfig.skipUnchanged = true
202250
const previous = {id: "existing-id", body: "old body"}

src/main.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ async function run(): Promise<undefined> {
5050
throw new Error("delete and recreate cannot be both set to true")
5151
}
5252

53+
if (deleteOldComment && onlyCreateComment) {
54+
throw new Error("delete and only_create cannot be both set to true")
55+
}
56+
57+
if (deleteOldComment && hideOldComment) {
58+
throw new Error("delete and hide cannot be both set to true")
59+
}
60+
5361
if (onlyCreateComment && onlyUpdateComment) {
5462
throw new Error("only_create and only_update cannot be both set to true")
5563
}
@@ -63,15 +71,8 @@ async function run(): Promise<undefined> {
6371

6472
core.setOutput("previous_comment_id", previous?.id)
6573

66-
if (deleteOldComment) {
67-
if (previous) {
68-
await deleteComment(octokit, previous.id)
69-
}
70-
return
71-
}
72-
7374
if (!previous) {
74-
if (onlyUpdateComment) {
75+
if (onlyUpdateComment || hideOldComment || deleteOldComment) {
7576
return
7677
}
7778
const created = await createComment(octokit, repo, pullRequestNumber, body, header)
@@ -90,6 +91,11 @@ async function run(): Promise<undefined> {
9091
return
9192
}
9293

94+
if (deleteOldComment) {
95+
await deleteComment(octokit, previous.id)
96+
return
97+
}
98+
9399
if (skipUnchanged && commentsEqual(body, previous.body || "", header)) {
94100
// don't recreate or update if the message is unchanged
95101
return

0 commit comments

Comments
 (0)