diff --git a/src/input.ts b/src/input.ts index d5062461..1841e1f3 100644 --- a/src/input.ts +++ b/src/input.ts @@ -8,7 +8,7 @@ export interface Inputs { description: string title: string image: string - color: number + color?: number url: string username: string avatar_url: string @@ -55,6 +55,8 @@ export function getInputs(): Inputs { const nocontext = nodetail || stob(core.getInput('nocontext')) const noprefix = nodetail || stob(core.getInput('noprefix')) + const colorParsed = parseInt(core.getInput("color")) + const inputs: Inputs = { webhooks: webhooks, status: core.getInput('status').trim().toLowerCase(), @@ -62,7 +64,7 @@ export function getInputs(): Inputs { description: core.getInput('description').trim(), title: (core.getInput('title') || core.getInput('job')).trim(), image: core.getInput('image').trim(), - color: parseInt(core.getInput('color')), + color: isNaN(colorParsed) ? undefined : colorParsed, url: core.getInput('url').trim(), username: core.getInput('username').trim(), avatar_url: core.getInput('avatar_url').trim(), diff --git a/src/main.ts b/src/main.ts index 1fbe4fbf..22dff85d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -51,7 +51,7 @@ export function getPayload(inputs: Readonly): Object { const eventDetail = formatEvent(eventName, payload) let embed: { [key: string]: any } = { - color: inputs.color || statusOpts[inputs.status].color + color: inputs.color === undefined ? statusOpts[inputs.status].color : inputs.color } if (!inputs.notimestamp) { diff --git a/test/input.test.ts b/test/input.test.ts index 0c4dbb7e..15b9120a 100644 --- a/test/input.test.ts +++ b/test/input.test.ts @@ -31,7 +31,7 @@ describe("getInputs()", () => { expect(got.description).toBe('') expect(got.title).toBe('github actions') expect(got.image).toBe('') - expect(got.color).toBeFalsy() + expect(got.color).toBe(undefined) expect(got.username).toBe('') expect(got.avatar_url).toBe('') }) @@ -75,6 +75,18 @@ describe("getInputs()", () => { expect(got.noprefix).toBe(true) }) + test("color 0 is accepted", () => { + process.env['INPUT_COLOR'] = '0' + const got = getInputs() + expect(got.color).toBe(0) + }) + + test("invalid color is defaulted to undefined", () => { + process.env['INPUT_COLOR'] = 'qwertyuiop' + const got = getInputs() + expect(got.color).toBe(undefined) + }) + test("all (job)", () => { // this pattern is rare because we have default value // for INPUT_TITLE, defined in action.yml, so this pattern diff --git a/test/main.test.ts b/test/main.test.ts index 8c4ab5a6..b9508ba0 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -41,7 +41,7 @@ describe('getPayload(Inputs)', () => { content: '', title: '', image: '', - color: NaN, + color: undefined, url: '', username: '', avatar_url: '' @@ -452,6 +452,91 @@ describe('getPayload(Inputs)', () => { expect(getPayload(inputs)).toStrictEqual(want) }) + test("no color defaults to job status color", () => { + const inputs: Inputs = { + ...baseInputs, + status: "failure" + } + const want = { + embeds: [{ + color: 0xCB2431, + timestamp: expect.any(String), + title: 'Failure', + fields: [ + { + name: 'Repository', + value: '[Codertocat/Hello-World](https://githubactions.serverurl.example.com/Codertocat/Hello-World)', + inline: true + }, + { + name: 'Ref', + value: 'refs/tags/simple-tag', + inline: true + }, + { + name: 'Event - push', + value: 'mocked format event', + inline: false + }, + { + name: 'Triggered by', + value: 'Codertocat', + inline: true + }, + { + name: 'Workflow', + value: "[push-ci](https://githubactions.serverurl.example.com/Codertocat/Hello-World/actions/runs/123123)", + inline: true + } + ] + }] + } + expect(getPayload(inputs)).toStrictEqual(want) + }) + + test("color 0 is accepted", () => { + const inputs: Inputs = { + ...baseInputs, + color: 0, + } + const want = { + embeds: [{ + color: 0, + timestamp: expect.any(String), + title: 'Success', + fields: [ + { + name: 'Repository', + value: '[Codertocat/Hello-World](https://githubactions.serverurl.example.com/Codertocat/Hello-World)', + inline: true + }, + { + name: 'Ref', + value: 'refs/tags/simple-tag', + inline: true + }, + { + name: 'Event - push', + value: 'mocked format event', + inline: false + }, + { + name: 'Triggered by', + value: 'Codertocat', + inline: true + }, + { + name: 'Workflow', + value: "[push-ci](https://githubactions.serverurl.example.com/Codertocat/Hello-World/actions/runs/123123)", + inline: true + } + ] + }] + } + expect(getPayload(inputs)).toStrictEqual(want) + }) + + test("username", () => { const inputs: Inputs = { ...baseInputs,