Skip to content

Commit 026bb85

Browse files
authored
[update-labels] throw if empty label name (#36554)
1 parent a58af20 commit 026bb85

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

.github/workflows/src/update-labels.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export async function updateLabelsImpl({
8585

8686
if (key.startsWith("label-")) {
8787
const name = key.substring("label-".length);
88+
89+
if (!name) {
90+
throw new Error(`Invalid value for label name: '${name}'`);
91+
}
92+
8893
if (value === "true") {
8994
labelsToAdd.push(name);
9095
} else if (value === "false") {

.github/workflows/test/update-labels.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,41 @@ describe("updateLabelsImpl", () => {
210210
expect(github.rest.issues.removeLabel).toBeCalledTimes(0);
211211
});
212212

213+
it("throws for invalid label name", async () => {
214+
const github = createMockGithub();
215+
github.rest.actions.listWorkflowRunArtifacts.mockResolvedValue({
216+
data: {
217+
artifacts: [
218+
{ name: "label-foo=true" },
219+
{ name: "label-bar=false" },
220+
{ name: "label-=true" },
221+
],
222+
},
223+
});
224+
225+
await expect(
226+
updateLabelsImpl({
227+
owner: "owner",
228+
repo: "repo",
229+
issue_number: 123,
230+
run_id: 456,
231+
github: github,
232+
core: createMockCore(),
233+
}),
234+
).rejects.toThrowErrorMatchingInlineSnapshot(`[Error: Invalid value for label name: '']`);
235+
236+
expect(github.rest.actions.listWorkflowRunArtifacts).toBeCalledWith({
237+
owner: "owner",
238+
repo: "repo",
239+
run_id: 456,
240+
per_page: PER_PAGE_MAX,
241+
});
242+
243+
// Ensure no labels are added or removed if any are invalid
244+
expect(github.rest.issues.addLabels).toBeCalledTimes(0);
245+
expect(github.rest.issues.removeLabel).toBeCalledTimes(0);
246+
});
247+
213248
it.each([404, 500, 501])("handles error removing label (%s)", async (status) => {
214249
const github = createMockGithub();
215250
github.rest.actions.listWorkflowRunArtifacts.mockResolvedValue({

0 commit comments

Comments
 (0)