Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for multiple URLs of same client #3

Merged
merged 6 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
126 changes: 85 additions & 41 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.

6 changes: 3 additions & 3 deletions src/helpers/send-payload.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ export const sendPayload = async (
});

if (response.ok) {
debug(`Successfully sent ${key} payload to`);
debug(`Successfully sent payload to ${key}`);
return { key, success: true };
} else {
error(
`Failed sending the ${key} payload to. API returned HTTP status ${response.status}`
`Failed sending the payload to ${key}. API returned HTTP status ${response.status}`
);
return { key, success: false };
}
} catch (err) {
if (err instanceof Error) {
error(`Failed sending the ${key} payload to. Error:`, err.message);
error(`Failed sending the payload to ${key}`);
}

return { key, success: false };
Expand Down
137 changes: 86 additions & 51 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,75 @@ import { normalizeSlackPayload } from './normalizers/slack.normalizer';
import { normalizeTeamsPayload } from './normalizers/teams.normalizer';
import { sendPayload } from './helpers/send-payload.helper';
import { Result } from './interfaces/result';
import { parseJSONInput } from './parsers/json-input-parser';

const run = async (): Promise<void> => {
try {
const dryRun = getInput('dry-run').toLowerCase() === 'true';
debug(`Dry-run: ${dryRun ? '✔' : '❌'}`);

const discordURL = getInput('discord-url');
const teamsURL = getInput('teams-url');
const slackURL = getInput('slack-url');
const rawDiscordURL = getInput('discord-url');
let discordURLs: string[] = [];
if (rawDiscordURL) {
const isJSONArray = rawDiscordURL.startsWith('[');

if (!discordURL && !teamsURL && !slackURL)
debug(`Found Slack input ${isJSONArray ? '(Multiple)' : ''}`);

discordURLs = isJSONArray
? parseJSONInput<string[]>('discord-url', rawDiscordURL)
: [rawDiscordURL];
}

const rawTeamsURL = getInput('teams-url');
let teamsURLs: string[] = [];
if (rawTeamsURL) {
const isJSONArray = rawTeamsURL.startsWith('[');

debug(`Found Slack input ${isJSONArray ? '(Multiple)' : ''}`);

teamsURLs = isJSONArray
? parseJSONInput<string[]>('teams-url', rawTeamsURL)
: [rawTeamsURL];
}

const rawSlackURL = getInput('slack-url');
let slackURLs: string[] = [];
if (rawSlackURL) {
const isJSONArray = rawSlackURL.startsWith('[');

debug(`Found Slack input ${isJSONArray ? '(Multiple)' : ''}`);

slackURLs = isJSONArray
? parseJSONInput<string[]>('slack-url', rawSlackURL)
: [rawSlackURL];
}

if (
discordURLs.length === 0 &&
teamsURLs.length === 0 &&
slackURLs.length === 0
)
throw new Error('No webhooks defined');

const color = normalizeColor(getInput('color', { required: true }));
const rawColor = getInput('color', { required: true });
const color = normalizeColor(rawColor);

const title = getInput('title', { required: true });
const text = getInput('text');

const rawFields = getInput('fields');
let fields: Field[] = [];
try {
if (rawFields) {
fields = JSON.parse(rawFields);
}
} catch (err) {
setFailed('Failed to parse fields');
}
let fields: Field[] = rawFields
? parseJSONInput<Field[]>('fields', rawFields)
: [];

const rawButtons = getInput('buttons');
let buttons: Button[] = [];
try {
if (rawButtons) {
buttons = JSON.parse(rawButtons);
}
} catch (err) {
setFailed('Failed to parse buttons');
}
let buttons: Button[] = rawFields
? parseJSONInput<Button[]>('buttons', rawButtons)
: [];

const failed: Result[] = [];

if (discordURL) {
if (discordURLs.length > 0) {
const discordPayload = normalizeDiscordPayload(
title,
text,
Expand All @@ -54,18 +83,20 @@ const run = async (): Promise<void> => {
buttons
);

const result = await sendPayload(
'Discord',
discordURL,
discordPayload,
dryRun
);
if (!result.success) {
failed.push(result);
}
discordURLs.forEach(async (url, idx) => {
const result = await sendPayload(
`Discord[${idx}]`,
url,
discordPayload,
dryRun
);
if (!result.success) {
failed.push(result);
}
});
}

if (slackURL) {
if (slackURLs.length > 0) {
const slackPayload = normalizeSlackPayload(
title,
text,
Expand All @@ -74,18 +105,20 @@ const run = async (): Promise<void> => {
buttons
);

const result = await sendPayload(
'Slack',
slackURL,
slackPayload,
dryRun
);
if (!result.success) {
failed.push(result);
}
slackURLs.forEach(async (url, idx) => {
const result = await sendPayload(
`Slack[${idx}]`,
url,
slackPayload,
dryRun
);
if (!result.success) {
failed.push(result);
}
});
}

if (teamsURL) {
if (teamsURLs.length > 0) {
const teamsPayload = normalizeTeamsPayload(
title,
text,
Expand All @@ -94,15 +127,17 @@ const run = async (): Promise<void> => {
buttons
);

const result = await sendPayload(
'Microsoft Teams',
teamsURL,
teamsPayload,
dryRun
);
if (!result.success) {
failed.push(result);
}
teamsURLs.forEach(async (url, idx) => {
const result = await sendPayload(
`Microsoft Teams[${idx}]`,
url,
teamsPayload,
dryRun
);
if (!result.success) {
failed.push(result);
}
});
}

if (failed.length > 0) {
Expand Down
10 changes: 10 additions & 0 deletions src/parsers/json-input-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { setFailed } from '@actions/core';

export const parseJSONInput = <T>(key: string, input: string): T => {
try {
return JSON.parse(input) as T;
} catch (err) {
setFailed(`Failed to parse ${key}`);
return {} as T;
}
};