Skip to content

Commit 84024c6

Browse files
committed
Support timezones
1 parent aafd23b commit 84024c6

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

index.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { DateTime } from 'luxon';
2+
13
export default class GitHubDriver {
24

35
#octokit;
@@ -7,26 +9,29 @@ export default class GitHubDriver {
79
}
810

911
async hasReminder(repository, reminder) {
10-
const { owner, name } = repository;
12+
const { owner, name: repo } = repository;
13+
const labels = this.#getKnuffLabels(reminder);
1114
const issues = await this.#octokit.issues.listForRepo({
1215
owner,
13-
repo: name,
16+
repo,
1417
state: 'all',
15-
labels: this.#getKnuffLabels(reminder),
18+
labels,
1619
});
1720

1821
return issues.data.length > 0;
1922
}
2023

2124
async createReminder(repository, reminder) {
2225
const { owner, name: repo } = repository;
23-
const { title, body, labels = [] } = reminder;
26+
const { title, body, labels: customLabels = [] } = reminder;
27+
const labels = this.#getLabels(customLabels, reminder);
28+
2429
return this.#octokit.issues.create({
2530
owner,
2631
repo,
2732
title,
2833
body,
29-
labels: this.#getLabels(labels, reminder),
34+
labels,
3035
});
3136
}
3237

@@ -37,15 +42,15 @@ export default class GitHubDriver {
3742
}
3843

3944
#getKnuffLabels(reminder) {
40-
return [this.#getKnuffIdLabel(reminder.id), this.#getKnuffDateLabel(reminder.date)];
45+
return [this.#getKnuffIdLabel(reminder), this.#getKnuffDateLabel(reminder)];
4146
}
4247

43-
#getKnuffIdLabel(id) {
44-
return `knuff:${id}`;
48+
#getKnuffIdLabel(reminder) {
49+
return `knuff:${reminder.id}`;
4550
}
4651

47-
#getKnuffDateLabel(date) {
48-
return `knuff:${date.toISOString().split('T')[0]}`;
52+
#getKnuffDateLabel(reminder) {
53+
return `knuff:${DateTime.fromJSDate(reminder.date).setZone(reminder.timezone).toFormat('yyyy-MM-dd')}`;
4954
}
5055

5156
#validateLabels(labels) {

package-lock.json

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@
4242
"bugs": {
4343
"url": "https://github.com/acuminous/knuff-github-driver/issues"
4444
},
45-
"homepage": "https://github.com/acuminous/knuff-github-driver#readme"
45+
"homepage": "https://github.com/acuminous/knuff-github-driver#readme",
46+
"dependencies": {
47+
"luxon": "^3.5.0"
48+
}
4649
}

test/index.test.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ok, strictEqual as eq, rejects } from 'node:assert';
2+
import crypto from 'node:crypto';
23
import zunit from 'zunit';
34
import { Octokit } from '@octokit/rest';
45
import GitHubDriver from '../index.js';
@@ -21,44 +22,73 @@ describe('driver', () => {
2122
it('should create issues with labels', async (t) => {
2223
const repository = { owner: 'acuminous', name: 'knuff-github-driver' };
2324
const reminder = {
24-
id: 'test',
25+
id: `test-${crypto.randomBytes(10).toString('hex')}`,
2526
title: t.name,
2627
body: 'the body',
2728
labels: ['l1', 'l2'],
28-
date: new Date('2024-12-25'),
29+
date: new Date('2024-12-25T13:00:00Z'),
30+
timezone: 'Europe/London',
2931
};
3032
const { data: issue } = await driver.createReminder(repository, reminder);
3133

3234
ok(issue.number);
3335
eq(issue.title, 'should create issues with labels');
3436
eq(issue.labels.length, 4);
35-
eq(issue.labels[0].name, 'knuff:2024-12-25');
36-
eq(issue.labels[1].name, 'knuff:test');
37-
eq(issue.labels[2].name, 'l1');
38-
eq(issue.labels[3].name, 'l2');
37+
38+
const labels = issue.labels.map((l) => l.name).sort();
39+
eq(labels[0], 'knuff:2024-12-25');
40+
eq(labels[1], `knuff:${reminder.id}`);
41+
eq(labels[2], 'l1');
42+
eq(labels[3], 'l2');
3943
});
4044

4145
it('should create issues without labels', async (t) => {
4246
const repository = { owner: 'acuminous', name: 'knuff-github-driver' };
4347
const reminder = {
44-
id: 'test',
48+
id: `test-${crypto.randomBytes(10).toString('hex')}`,
4549
title: t.name,
4650
body: 'the body',
4751
date: new Date('2024-12-25'),
52+
timezone: 'Europe/London',
4853
};
4954
const { data: issue } = await driver.createReminder(repository, reminder);
5055

5156
ok(issue.number);
5257
eq(issue.title, 'should create issues without labels');
5358
eq(issue.labels.length, 2);
54-
eq(issue.labels[0].name, 'knuff:2024-12-25');
55-
eq(issue.labels[1].name, 'knuff:test');
59+
60+
const labels = issue.labels.map((l) => l.name).sort();
61+
eq(labels[0], 'knuff:2024-12-25');
62+
eq(labels[1], `knuff:${reminder.id}`);
63+
});
64+
65+
it('should honour timezones', async (t) => {
66+
const repository = { owner: 'acuminous', name: 'knuff-github-driver' };
67+
const reminder = {
68+
id: `test-${crypto.randomBytes(10).toString('hex')}`,
69+
title: t.name,
70+
body: 'the body',
71+
labels: ['l1', 'l2'],
72+
date: new Date('2024-12-25T13:00:00Z'),
73+
timezone: 'Australia/Sydney',
74+
};
75+
const { data: issue } = await driver.createReminder(repository, reminder);
76+
77+
ok(issue.number);
78+
eq(issue.title, 'should honour timezones');
79+
eq(issue.labels.length, 4);
80+
81+
const labels = issue.labels.map((l) => l.name).sort();
82+
eq(labels[0], 'knuff:2024-12-26');
83+
eq(labels[1], `knuff:${reminder.id}`);
84+
eq(labels[2], 'l1');
85+
eq(labels[3], 'l2');
5686
});
5787

5888
it('should reject reminders with labels greater than 51 characters', async (t) => {
5989
const repository = { owner: 'acuminous', name: 'knuff-github-driver' };
6090
const reminder = {
61-
id: 'test',
91+
id: `test-${crypto.randomBytes(10).toString('hex')}`,
6292
title: t.name,
6393
body: 'the body',
6494
labels: ['123456789 123456789 123456789 123456789 123456789 12'],
@@ -89,7 +119,7 @@ describe('driver', () => {
89119
it('should find open issues with the same id and date', async (t) => {
90120
const repository = { owner: 'acuminous', name: 'knuff-github-driver' };
91121
const reminder = {
92-
id: 'test',
122+
id: `test-${crypto.randomBytes(10).toString('hex')}`,
93123
title: t.name,
94124
body: 'the body',
95125
labels: ['l1', 'l2'],
@@ -99,14 +129,13 @@ describe('driver', () => {
99129
await driver.createReminder(repository, reminder);
100130

101131
const found = await driver.hasReminder(repository, reminder);
102-
103132
eq(found, true);
104133
});
105134

106135
it('should find closed issues with the same id and date', async (t) => {
107136
const repository = { owner: 'acuminous', name: 'knuff-github-driver' };
108137
const reminder = {
109-
id: 'test',
138+
id: `test-${crypto.randomBytes(10).toString('hex')}`,
110139
title: t.name,
111140
body: 'the body',
112141
labels: ['l1', 'l2'],
@@ -123,11 +152,12 @@ describe('driver', () => {
123152
it('should ignore issues with the same id but different day', async (t) => {
124153
const repository = { owner: 'acuminous', name: 'knuff-github-driver' };
125154
const reminder = {
126-
id: 'test',
155+
id: `test-${crypto.randomBytes(10).toString('hex')}`,
127156
title: t.name,
128157
body: 'the body',
129158
labels: ['l1', 'l2'],
130159
date: new Date('2024-12-25'),
160+
timezone: 'Europe/London',
131161
};
132162

133163
await driver.createReminder(repository, reminder);

0 commit comments

Comments
 (0)