Skip to content

Commit

Permalink
Merge branch 'master' into releases/v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrurodr committed Mar 4, 2022
2 parents f48769c + 970964a commit 8c6f8d6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ Discord Notification Made Easy.
color: 0x0000ff
username: GitHub Actions
avatar_url: ${{ secrets.AVATAR_URL }}
proxyHost: egress-proxy
proxyPort: 3129
```
![image](https://user-images.githubusercontent.com/33576079/102154036-ecf1ad00-3ebb-11eb-9af3-ff58982d9ecb.png)
Expand Down
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: 'Actions Status Discord'
description: 'Post Github Actions CI status to Discord'
author: 'Sarisia'
author: 'Despegar'

inputs:
webhook:
Expand Down Expand Up @@ -48,6 +48,14 @@ inputs:
description: "Suppress detailed embed fields"
required: false
default: 'false'
proxyHost:
description: "for using a proxy"
required: false
default: ''
proxyPort:
description: "for using a proxy"
required: false
default: ''

runs:
using: 'node12'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"release": "rm -rf node_modules && npm ci && npm run build && npm prune --production",
"version": "npm run release && git add ."
},
"author": "Sarisia",
"author": "Despegar",
"license": "MIT",
"devDependencies": {
"@types/jest": "^26.0.23",
Expand Down
26 changes: 17 additions & 9 deletions src/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import { logWarning, stob } from './utils'
import { asNumber, logWarning, stob } from './utils'

export interface Inputs {
webhooks: string[]
Expand All @@ -12,6 +12,8 @@ export interface Inputs {
avatar_url: string
nocontext: boolean
noprefix: boolean
proxyHost?: string
proxyPort?: number
}

interface StatusOption {
Expand All @@ -36,7 +38,7 @@ export const statusOpts: Record<string, StatusOption> = {

export function getInputs(): Inputs {
// webhook
const webhook: string = core.getInput('webhook').trim() || process.env.DISCORD_WEBHOOK || ''
const webhook: string = core.getInput('webhook', { required: true, trimWhitespace: true}) || process.env.DISCORD_WEBHOOK || ''
const webhooks: string[] = webhook.split('\n').filter(x => x || false)
// prevent webhooks from leak
webhooks.forEach((w, i) => {
Expand All @@ -52,17 +54,23 @@ export function getInputs(): Inputs {
const nocontext = nodetail || stob(core.getInput('nocontext'))
const noprefix = nodetail || stob(core.getInput('noprefix'))

// retrieve proxy config
const proxyHost = core.getInput('proxyHost', { required: false, trimWhitespace: true })
const proxyPort = asNumber(core.getInput('proxyPort', { required: false, trimWhitespace: true }))

const inputs: Inputs = {
webhooks: webhooks,
status: core.getInput('status').trim().toLowerCase(),
description: core.getInput('description').trim(),
title: (core.getInput('title') || core.getInput('job')).trim(),
image: core.getInput('image').trim(),
status: core.getInput('status', { trimWhitespace: true }).toLowerCase(),
description: core.getInput('description', { trimWhitespace: true }),
title: (core.getInput('title') || core.getInput('job', { trimWhitespace: true })),
image: core.getInput('image', { trimWhitespace: true }),
color: parseInt(core.getInput('color')),
username: core.getInput('username').trim(),
avatar_url: core.getInput('avatar_url').trim(),
username: core.getInput('username', { trimWhitespace: true }),
avatar_url: core.getInput('avatar_url', { trimWhitespace: true }),
nocontext: nocontext,
noprefix: noprefix
noprefix: noprefix,
proxyHost,
proxyPort
}

// validate
Expand Down
11 changes: 6 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ async function run() {
endGroup()

logInfo(`Triggering ${inputs.webhooks.length} webhook${inputs.webhooks.length>1 ? 's' : ''}...`)
await Promise.all(inputs.webhooks.map(w => wrapWebhook(w.trim(), payload)))
} catch(e) {
await Promise.all(inputs.webhooks.map(w => wrapWebhook(w.trim(), payload, inputs.proxyHost, inputs.proxyPort)))
} catch(e: any) {
logError(`Unexpected failure: ${e} (${e.message})`)
}
}

function wrapWebhook(webhook: string, payload: Object): Promise<void> {
function wrapWebhook(webhook: string, payload: Object, proxyHost: string | undefined, proxyPort: number | undefined ): Promise<void> {
return async function() {
try {
await axios.post(webhook, payload)
} catch(e) {
const config = proxyHost && proxyPort ? {proxy: { host: proxyHost, port: proxyPort }} : {}
await axios.post(webhook, payload, config)
} catch(e: any) {
if (e.response) {
logError(`Webhook response: ${e.response.status}: ${JSON.stringify(e.response.data)}`)
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ export function logWarning(msg: string) {
export function stob(s: string): boolean {
return s.trim().toLowerCase() === 'true'
}

export function asNumber(s?: string): number | undefined {
return s ? parseInt(s) : undefined
}

0 comments on commit 8c6f8d6

Please sign in to comment.