Skip to content

Commit abe4416

Browse files
committed
minor refactoring
1 parent 006f94c commit abe4416

File tree

8 files changed

+95
-196
lines changed

8 files changed

+95
-196
lines changed

src/templates/argocd.ts

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { z } from 'zod';
22

33
import type { AdaptiveCard, ArgoCdTemplateData } from '../types';
4-
import { createBaseCard, createCardFrame, createSectionSeparator, formatRelativeTimeOrIso, toTextColor } from './shared';
5-
6-
const IsoTimestampSchema = z.union([
7-
z.string().datetime({ offset: true }),
8-
z.string().datetime(),
9-
]);
4+
import {
5+
IsoTimestampSchema,
6+
createBaseCard,
7+
createCardFrame,
8+
createFactSet,
9+
createSectionSeparator,
10+
formatRelativeTimeOrIso,
11+
toTextColor,
12+
} from './shared';
1013

1114
export const ArgoCdTemplateDataSchema = z.object({
1215
event: z.enum(['sync_succeeded', 'sync_failed', 'out_of_sync']),
@@ -112,25 +115,6 @@ function createStatusPills(sync: string | undefined, health: string | undefined)
112115
};
113116
}
114117

115-
function createArgoFactSet(entries: Array<{ title: string; value?: string }>): Record<string, unknown> | null {
116-
const facts = entries
117-
.filter((entry) => entry.value !== undefined && entry.value !== null && entry.value !== '')
118-
.map((entry) => ({
119-
title: `${entry.title}:`,
120-
value: String(entry.value),
121-
}));
122-
123-
if (facts.length === 0) {
124-
return null;
125-
}
126-
127-
return {
128-
type: 'FactSet',
129-
facts,
130-
spacing: 'Medium',
131-
};
132-
}
133-
134118
export function renderArgoCdTemplate(data: ArgoCdTemplateData): AdaptiveCard {
135119
const contentItems: Array<Record<string, unknown>> = [
136120
{
@@ -177,7 +161,7 @@ export function renderArgoCdTemplate(data: ArgoCdTemplateData): AdaptiveCard {
177161
});
178162
}
179163

180-
const factSet = createArgoFactSet([
164+
const factSet = createFactSet([
181165
{
182166
title: 'Revision',
183167
value: data.revision,
@@ -217,9 +201,7 @@ export function renderArgoCdTemplate(data: ArgoCdTemplateData): AdaptiveCard {
217201
});
218202
}
219203

220-
const body: Array<Record<string, unknown>> = [
221-
createCardFrame(eventStyles[data.event], contentItems),
222-
];
204+
const body: Array<Record<string, unknown>> = [createCardFrame(contentItems)];
223205

224206
return createBaseCard(body);
225207
}

src/templates/dbbackup.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ export function renderDbBackupTemplate(data: DbBackupTemplateData): AdaptiveCard
7878
contentItems.push(factSet);
7979
}
8080

81-
const body: Array<Record<string, unknown>> = [
82-
createCardFrame(statusStyles[data.status], contentItems),
83-
];
81+
const body: Array<Record<string, unknown>> = [createCardFrame(contentItems)];
8482

8583
return createBaseCard(body);
8684
}

src/templates/generic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function renderGenericTemplate(data: GenericTemplateData): AdaptiveCard {
8787
});
8888
}
8989

90-
const body: Array<Record<string, unknown>> = [createCardFrame(severityStyles[severity], contentItems)];
90+
const body: Array<Record<string, unknown>> = [createCardFrame(contentItems)];
9191

9292
return createBaseCard(body);
9393
}

src/templates/github.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ export const GitHubTemplateDataSchema = z.object({
1212
body: z.string().optional(),
1313
}).strict();
1414

15-
const eventStyles: Record<GitHubTemplateData['event'], 'accent' | 'good' | 'attention'> = {
16-
merged: 'accent',
17-
opened: 'good',
18-
closed: 'attention',
19-
};
20-
2115
const eventBadges: Record<GitHubTemplateData['event'], string> = {
2216
opened: 'Pull Request Opened',
2317
merged: 'Pull Request Merged',
@@ -86,9 +80,7 @@ export function renderGitHubTemplate(data: GitHubTemplateData): AdaptiveCard {
8680
],
8781
});
8882

89-
const body: Array<Record<string, unknown>> = [
90-
createCardFrame(eventStyles[data.event], contentItems),
91-
];
83+
const body: Array<Record<string, unknown>> = [createCardFrame(contentItems)];
9284

9385
return createBaseCard(body);
9486
}

src/templates/shared.ts

Lines changed: 8 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { z } from 'zod';
2+
13
import type { AdaptiveCard } from '../types';
24

35
export type AdaptiveContainerStyle = 'default' | 'accent' | 'good' | 'warning' | 'attention';
@@ -12,6 +14,11 @@ interface FactEntry {
1214

1315
const relativeTimeFormatter = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
1416

17+
export const IsoTimestampSchema = z.union([
18+
z.string().datetime({ offset: true }),
19+
z.string().datetime(),
20+
]);
21+
1522
function toCategoryTextColor(style: AdaptiveContainerStyle): AdaptiveTextColor {
1623
switch (style) {
1724
case 'good':
@@ -31,12 +38,7 @@ export function toTextColor(style: AdaptiveContainerStyle): AdaptiveTextColor {
3138
return toCategoryTextColor(style);
3239
}
3340

34-
export function createCardFrame(
35-
style: AdaptiveContainerStyle,
36-
items: Array<Record<string, unknown>>,
37-
): Record<string, unknown> {
38-
void style;
39-
41+
export function createCardFrame(items: Array<Record<string, unknown>>): Record<string, unknown> {
4042
return {
4143
type: 'ColumnSet',
4244
columns: [
@@ -50,122 +52,6 @@ export function createCardFrame(
5052
};
5153
}
5254

53-
export interface HeaderContainerOptions {
54-
topLine?: string;
55-
extraItems?: Array<Record<string, unknown>>;
56-
actions?: Array<Record<string, unknown>>;
57-
}
58-
59-
export function createHeaderContainer(
60-
title: string,
61-
style: AdaptiveContainerStyle,
62-
subtitle?: string,
63-
category?: string,
64-
badge?: string,
65-
options?: HeaderContainerOptions,
66-
): Record<string, unknown> {
67-
const contentItems: Array<Record<string, unknown>> = [];
68-
69-
if (options?.topLine) {
70-
contentItems.push({
71-
type: 'TextBlock',
72-
text: options.topLine,
73-
size: 'Small',
74-
isSubtle: true,
75-
spacing: 'None',
76-
});
77-
}
78-
79-
if (category || badge) {
80-
const columns: Array<Record<string, unknown>> = [];
81-
82-
if (category) {
83-
columns.push({
84-
type: 'Column',
85-
width: 'stretch',
86-
items: [
87-
{
88-
type: 'TextBlock',
89-
text: category,
90-
size: 'Small',
91-
weight: 'Bolder',
92-
color: toCategoryTextColor(style),
93-
wrap: true,
94-
spacing: 'None',
95-
},
96-
],
97-
});
98-
} else {
99-
columns.push({
100-
type: 'Column',
101-
width: 'stretch',
102-
items: [{ type: 'TextBlock', text: ' ', spacing: 'None' }],
103-
});
104-
}
105-
106-
if (badge) {
107-
columns.push({
108-
type: 'Column',
109-
width: 'auto',
110-
items: [
111-
{
112-
type: 'TextBlock',
113-
text: badge,
114-
size: 'Small',
115-
isSubtle: true,
116-
horizontalAlignment: 'Right',
117-
wrap: false,
118-
spacing: 'None',
119-
},
120-
],
121-
});
122-
}
123-
124-
contentItems.push({
125-
type: 'ColumnSet',
126-
spacing: 'None',
127-
columns,
128-
});
129-
}
130-
131-
contentItems.push({
132-
type: 'TextBlock',
133-
text: title,
134-
weight: 'Bolder',
135-
wrap: true,
136-
size: 'Large',
137-
spacing: category || badge ? 'Small' : 'None',
138-
});
139-
140-
if (subtitle) {
141-
contentItems.push({
142-
type: 'TextBlock',
143-
text: subtitle,
144-
size: 'Small',
145-
isSubtle: true,
146-
wrap: true,
147-
spacing: 'None',
148-
});
149-
}
150-
151-
if (options?.extraItems) {
152-
contentItems.push(...options.extraItems);
153-
}
154-
155-
if (options?.actions && options.actions.length > 0) {
156-
contentItems.push({
157-
type: 'ActionSet',
158-
spacing: 'Medium',
159-
actions: options.actions,
160-
});
161-
}
162-
163-
return {
164-
type: 'Container',
165-
items: contentItems,
166-
};
167-
}
168-
16955
export function createFactSet(entries: FactEntry[]): Record<string, unknown> | null {
17056
const facts = entries
17157
.filter((entry) => entry.value !== undefined && entry.value !== null && entry.value !== '')

src/templates/sysdig.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { z } from 'zod';
22

33
import type { AdaptiveCard, SysdigTemplateData } from '../types';
4-
import { createBaseCard, createCardFrame, createFactSet, createSectionSeparator, formatRelativeTimeOrIso } from './shared';
5-
6-
const IsoTimestampSchema = z.union([
7-
z.string().datetime({ offset: true }),
8-
z.string().datetime(),
9-
]);
4+
import {
5+
IsoTimestampSchema,
6+
createBaseCard,
7+
createCardFrame,
8+
createFactSet,
9+
createSectionSeparator,
10+
formatRelativeTimeOrIso,
11+
} from './shared';
1012

1113
export const SysdigTemplateDataSchema = z.object({
1214
severity: z.enum(['critical', 'high', 'medium', 'low', 'info']),
@@ -17,14 +19,6 @@ export const SysdigTemplateDataSchema = z.object({
1719
url: z.string().url().optional(),
1820
}).strict();
1921

20-
const severityStyles: Record<SysdigTemplateData['severity'], 'attention' | 'warning' | 'accent' | 'default'> = {
21-
critical: 'attention',
22-
high: 'warning',
23-
medium: 'warning',
24-
low: 'accent',
25-
info: 'default',
26-
};
27-
2822
const severityFactLabels: Record<SysdigTemplateData['severity'], string> = {
2923
critical: '🔴 Critical',
3024
high: '🟠 High',
@@ -124,9 +118,7 @@ export function renderSysdigTemplate(data: SysdigTemplateData): AdaptiveCard {
124118
});
125119
}
126120

127-
const body: Array<Record<string, unknown>> = [
128-
createCardFrame(severityStyles[data.severity], contentItems),
129-
];
121+
const body: Array<Record<string, unknown>> = [createCardFrame(contentItems)];
130122

131123
return createBaseCard(body);
132124
}

src/templates/uptime.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { z } from 'zod';
22

33
import type { AdaptiveCard, UptimeTemplateData } from '../types';
4-
import { createBaseCard, createCardFrame, createFactSet, createSectionSeparator, formatRelativeTimeOrIso } from './shared';
5-
6-
const IsoTimestampSchema = z.union([
7-
z.string().datetime({ offset: true }),
8-
z.string().datetime(),
9-
]);
4+
import {
5+
IsoTimestampSchema,
6+
createBaseCard,
7+
createCardFrame,
8+
createFactSet,
9+
createSectionSeparator,
10+
formatRelativeTimeOrIso,
11+
} from './shared';
1012

1113
export const UptimeTemplateDataSchema = z.object({
1214
status: z.enum(['up', 'degraded', 'down']),
@@ -16,12 +18,6 @@ export const UptimeTemplateDataSchema = z.object({
1618
url: z.string().url().optional(),
1719
}).strict();
1820

19-
const statusStyles: Record<UptimeTemplateData['status'], 'good' | 'warning' | 'attention'> = {
20-
up: 'good',
21-
degraded: 'warning',
22-
down: 'attention',
23-
};
24-
2521
const statusBadges: Record<UptimeTemplateData['status'], string> = {
2622
up: '🟢 UP',
2723
degraded: '🟡 DEGRADED',
@@ -116,9 +112,7 @@ export function renderUptimeTemplate(data: UptimeTemplateData): AdaptiveCard {
116112
});
117113
}
118114

119-
const body: Array<Record<string, unknown>> = [
120-
createCardFrame(statusStyles[data.status], contentItems),
121-
];
115+
const body: Array<Record<string, unknown>> = [createCardFrame(contentItems)];
122116

123117
return createBaseCard(body);
124118
}

0 commit comments

Comments
 (0)