Skip to content

Commit 68bea7a

Browse files
committed
Render Newsletter action fixes
- Also consider post title when matching the new post against existing Single Sends - Added `after_date` input to render-newsletter action. Overrides what is considered "today" for the purposes of filtering posts. - Workflow passes the latest commit date in as `after_date`
1 parent 83c3b78 commit 68bea7a

File tree

5 files changed

+61
-14
lines changed

5 files changed

+61
-14
lines changed

.github/actions/render-newsletter/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ inputs:
88
description: 'Path to directory containing posts'
99
required: false
1010

11+
after_date:
12+
description: 'Only render posts that have a send date >= this ISO 8601-formatted date'
13+
required: false
14+
1115
out_path:
1216
description: 'Path where the output should be written'
1317
required: false

.github/actions/render-newsletter/src/index.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@ async function render(opts: Options) {
156156
};
157157
}
158158

159-
const dateStr = (d: Date) => ((d.toISOString()).split('T', 1)[0]);
160-
161-
const floorDate = (d: Date) => (new Date(d.getFullYear(),
162-
d.getMonth(),
163-
d.getDate()));
159+
const dateStr =
160+
(d: Date | string) => ((typeof d === 'string' ? d : d.toISOString()).split('T', 1)[0]);
164161

165162
function getSendDate(c: TemplateContext) {
166163
let date = c.post.date;
@@ -175,15 +172,33 @@ function getSendDate(c: TemplateContext) {
175172
return date;
176173
}
177174

175+
function singleSendId(context: TemplateContext, index?: SG.SingleSendIndex) {
176+
if (!index)
177+
return undefined;
178+
179+
const date = dateStr(context.post.date);
180+
181+
for (const ss of Object.values(index.byId)) {
182+
if (dateStr(ss.send_at) === date)
183+
return ss.id;
184+
185+
if (ss.name.includes(context.post.title))
186+
return ss.id;
187+
}
188+
}
189+
178190
async function run(options: Options) {
179191
const { text, context } = await render(options);
180-
// console.log(context);
181192

182193
if (options.output) {
183194
await writeFile(options.output, text);
184195
} else if (options.apiKey) {
185196
const sendAt = getSendDate(context);
186-
const id = options.index?.byDate[dateStr(context.post.date)][0];
197+
const id = singleSendId(context, options.index);
198+
199+
if (id)
200+
console.log(`Updating existing Single Send ${id}`);
201+
187202
const response = await SG.singleSend({
188203
html: text,
189204
listId: options.listId,
@@ -219,7 +234,7 @@ type RunOptions = Omit<Options, 'filePath'> & {
219234
function dateFilter(after: number) {
220235
return (path: string) => {
221236
const ctx = contextFromPath(path);
222-
return ctx.date.getTime() > after;
237+
return ctx.date.getTime() >= after;
223238
};
224239
}
225240

@@ -247,7 +262,6 @@ async function runAll(options: RunOptions) {
247262
const index = await SG.indexSingleSends({ token: options.apiKey });
248263

249264
for (const post of posts) {
250-
251265
const result = await run({
252266
...options,
253267
filePath: post,
@@ -276,7 +290,7 @@ async function runAction() {
276290
INPUT_SUBJECT_FORMAT: subject = '%s',
277291
INPUT_POSTS_DIR: postsDir,
278292
INPUT_SLACK_URL: slackUrl,
279-
TODAY_OVERRIDE: today,
293+
INPUT_AFTER_DATE: today,
280294
} = process.env;
281295

282296
if (!(path || postsDir)) {
@@ -302,7 +316,6 @@ async function runAction() {
302316
}
303317

304318
async function testRun() {
305-
// const apiKey = 'REAS-yuff0naum!krar';
306319
process.env['INPUT_SENDGRID_LIST_ID'] = "559adb5e-7164-4ac8-bbb5-1398d4ff0df9";
307320
// process.env['INPUT_SENDGRID_API_KEY'] = apiKey;
308321
// process.env['INPUT_TEXT_PATH'] = __dirname + '/../../../../_posts/2021-11-16-communications-lead.md';
@@ -311,8 +324,6 @@ async function testRun() {
311324
process.env['INPUT_CONTEXT'] = `{}`;
312325
process.env['INPUT_SUPPRESSION_GROUP_ID'] = '17889';
313326
process.env['INPUT_SITE_YAML'] = __dirname + '/../../../../_config.yml';
314-
process.env['INPUT_SLACK_URL'] = 'https://hooks.slack.com/services/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';
315-
process.env['TODAY_OVERRIDE'] = '2022-01-10';
316327

317328
await runAction();
318329
}

.github/actions/render-newsletter/src/sendgrid.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,19 @@ export async function singleSend(params: SingleSendParams) {
4747
});
4848
}
4949

50+
export async function deleteSingleSend(params: any) {
51+
const { id, token } = params;
52+
return await fetch(`${API_BASE}/marketing/singlesends/${id}`, {
53+
method: 'DELETE',
54+
headers: {
55+
'Authorization': `Bearer ${token}`
56+
}
57+
});
58+
}
59+
5060
type GetSingleSendsParams = {
5161
token: string,
62+
categories?: string[]
5263
};
5364

5465
export async function *getSingleSends(params: GetSingleSendsParams) {
@@ -62,7 +73,7 @@ export async function *getSingleSends(params: GetSingleSendsParams) {
6273
},
6374
body: JSON.stringify({
6475
status: ['scheduled', 'draft'],
65-
// categories: ['newsletter']
76+
categories: params.categories
6677
})
6778
});
6879

@@ -94,3 +105,15 @@ export async function indexSingleSends(params: GetSingleSendsParams) {
94105

95106
return idx;
96107
}
108+
109+
export async function cleanup(params: GetSingleSendsParams) {
110+
const deleteBefore = new Date(2022, 2, 2).getTime();
111+
for await (const ss of getSingleSends(params)) {
112+
if (new Date(ss.send_at).getTime() < deleteBefore) {
113+
await deleteSingleSend({
114+
id: ss.id,
115+
token: params.token,
116+
});
117+
}
118+
}
119+
}

.github/workflows/commit-date.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
commit_date=$(git log --pretty=format:%ai -1)
4+
5+
echo "::set-output name=commit_date::$commit_date"

.github/workflows/send-newsletter.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ jobs:
1212
- run: ./.github/workflows/get_domain.sh
1313
shell: bash
1414
id: site-domain
15+
- run: ./.github/workflows/commit-date.sh
16+
shell: bash
17+
id: commit-date
1518
- name: Render newsletter
1619
id: newsletter
1720
uses: ./.github/actions/render-newsletter
@@ -24,6 +27,7 @@ jobs:
2427
site_yaml: "./_config.yml"
2528
subject_format: "[Code for Boston] %s"
2629
slack_url: ${{ secrets.SLACK_URL }}
30+
after_date: ${{ steps.commit-date.outputs.commit-date }}
2731
context: >-
2832
{
2933
"domain": "${{ steps.site-domain.outputs.domain }}"

0 commit comments

Comments
 (0)