Skip to content

Commit

Permalink
feat: add env var injection for content
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Aug 10, 2024
1 parent 8804c2e commit 3cf77e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { container } from '#lib/utils/container';
import { insertEnvVars } from '#lib/utils/insertEnvVars';
import { obfuscateWebhookUrl, UnboundWebhookRegex } from '#lib/utils/obfuscateWebhookUrl';
import { preflightChecks } from '#lib/utils/preflightChecks';
import { setupLogger } from '#lib/utils/setup-logger';
Expand Down Expand Up @@ -87,7 +88,7 @@ try {
await rest.post(Routes.webhook(hookId, hookToken), {
auth: false,
body: {
content: checkedOptions.content,
content: insertEnvVars(checkedOptions.content),
allowed_mentions: {
users: checkedOptions['allowed-user-mentions'],
roles: checkedOptions['allowed-role-mentions']
Expand Down
29 changes: 29 additions & 0 deletions src/lib/utils/insertEnvVars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { env } from 'node:process';

interface ValidEnvValues {
readonly ATC_EXTERNAL_URL: string;
readonly BUILD_ID: string;
readonly BUILD_JOB_ID: string;
readonly BUILD_JOB_NAME: string;
readonly BUILD_NAME: string;
readonly BUILD_PIPELINE_ID: string;
readonly BUILD_PIPELINE_NAME: string;
readonly BUILD_TEAM_ID: string;
readonly BUILD_TEAM_NAME: string;
}

const validEnvValues: ValidEnvValues = {
BUILD_PIPELINE_NAME: env.BUILD_PIPELINE_NAME!,
BUILD_PIPELINE_ID: env.BUILD_PIPELINE_ID!,
BUILD_NAME: env.BUILD_NAME!,
BUILD_TEAM_NAME: env.BUILD_TEAM_NAME!,
BUILD_JOB_NAME: env.BUILD_JOB_NAME!,
BUILD_ID: env.BUILD_ID!,
BUILD_TEAM_ID: env.BUILD_TEAM_ID!,
BUILD_JOB_ID: env.BUILD_JOB_ID!,
ATC_EXTERNAL_URL: env.ATC_EXTERNAL_URL!
};

export function insertEnvVars(content: string): string {
return content.replaceAll(/\${?(?<varName>\w+)}?/g, (_, varName) => validEnvValues[varName as keyof typeof validEnvValues]);
}

0 comments on commit 3cf77e4

Please sign in to comment.