Skip to content

Commit

Permalink
impl (#548)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarisia committed Aug 11, 2024
1 parent 79f4a25 commit 31f9b74
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Inputs {
description: string
title: string
image: string
color: number
color?: number
url: string
username: string
avatar_url: string
Expand Down Expand Up @@ -55,14 +55,16 @@ 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(),
content: core.getInput('content').trim(),
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(),
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function getPayload(inputs: Readonly<Inputs>): 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) {
Expand Down
14 changes: 13 additions & 1 deletion test/input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
})
Expand Down Expand Up @@ -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
Expand Down
87 changes: 86 additions & 1 deletion test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('getPayload(Inputs)', () => {
content: '',
title: '',
image: '',
color: NaN,
color: undefined,
url: '',
username: '',
avatar_url: ''
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 31f9b74

Please sign in to comment.