Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
"project": "./tsconfig.eslint.json"
},
"rules": {
"i18n-text/no-en": "off",
Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,10 @@ jobs:
username: Bill
content: Discord will not confirm this was sent successfully before replying.
avatar-url: https://cdn.discordapp.com/avatars/742807869023322232/dd41912939ffccbea0276f70688fa0ec.webp?size=256
wait: false
wait: false
- name: Discord Webhook Action With Raw Content
uses: ./
with:
webhook-url: ${{ secrets.WEBHOOK_URL }}
username: Bill
raw-content: __tests__/data/raw-content.txt
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Want to know more about Discord Webhooks? Check out the [intro](https://support.
| avatar-url | `false` | URL for the avatar that should appear with the message |
| tts | `false` | Whether the message is text-to-speech |
| raw-data | `false` | Filename of raw JSON body to send. **If this is provided, all other inputs (except `webhook-url`) are ignored** |
| raw-content | `false` | Filename of raw message content to send |
| filename | `false` | Filename of file to upload. **This input is overridden by `raw-data`. If this is provided, `username` and `avatar-url` are still honored** |
| embed-title | `false` | Title for embed |
| embed-url | `false` | URL for embed |
Expand Down
3 changes: 3 additions & 0 deletions __tests__/data/raw-content.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ansi
This is some blue, underlined text.
```
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ inputs:
raw-data:
description: 'Name of a json file that will be sent as the data for the webhook'
required: false
raw-content:
description: 'Name of a text file containing the message to be sent via the webhook'
required: false
filename:
description: 'Name of a file that will be uploaded via the webhook'
required: false
Expand Down
30 changes: 28 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CONTENT = 'content'
const USERNAME = 'username'
const AVATAR_URL = 'avatar-url'
const RAW_DATA = 'raw-data'
const RAW_CONTENT = 'raw-content'
const TITLE = 'title'
const DESCRIPTION = 'description'
const TIMESTAMP = 'timestamp'
Expand Down Expand Up @@ -37,7 +38,22 @@ function createPayload(): Record<string, unknown> {
return JSON.parse(readFileSync(rawData, 'utf-8'))
}

const webhookPayloadMap = parseMapFromParameters(TOP_LEVEL_WEBHOOK_KEYS)
let webhookPayloadMap: Map<string, unknown>

// If raw-content is provided, use it in place of content
const rawContentPath = core.getInput(RAW_CONTENT)
if (rawContentPath.length > 0) {
if (core.getInput(CONTENT).length > 0) {
throw new Error("content and raw-content can't be used at the same time.")
}
webhookPayloadMap = parseMapFromParameters([
RAW_CONTENT,
USERNAME,
AVATAR_URL
])
} else {
webhookPayloadMap = parseMapFromParameters(TOP_LEVEL_WEBHOOK_KEYS)
}
const embedPayloadMap = createEmbedObject()
if (embedPayloadMap.size > 0) {
webhookPayloadMap.set('embeds', [Object.fromEntries(embedPayloadMap)])
Expand Down Expand Up @@ -92,14 +108,25 @@ function parseMapFromParameters(
const parameterMap = new Map<string, unknown>()
core.info(`inputObjectKey: ${inputObjectKey}`)

for (const parameter of parameters) {
for (let parameter of parameters) {
const inputKey =
inputObjectKey !== '' ? `${inputObjectKey}-${parameter}` : parameter
let value = core.getInput(inputKey)
if (value === '') {
continue
}

if (parameter === RAW_CONTENT) {
const rawContentPath = core.getInput(RAW_CONTENT)

if (rawContentPath.length > 0) {
parameter = 'content'
value = readFileSync(rawContentPath, 'utf-8')
} else {
throw new Error(`${rawContentPath} is empty.`)
}
}

if (parameter === TIMESTAMP) {
const parsedDate = new Date(value)
value = parsedDate.toISOString()
Expand Down
8 changes: 8 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"include": [
"jest.config.ts",
"src/*.ts",
"__tests__/*.ts"
]
}