Skip to content

Commit 47549f1

Browse files
authored
feat: add timestamp to comment (#135)
Add ISO formatted timestamp to PR comments. fixes #132
1 parent 64c1750 commit 47549f1

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

dist/index.js

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

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comment.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ export class Comments {
4646
hash: string,
4747
content: string[],
4848
) {
49+
const timestamp = new Date().toISOString();
4950
await this.octokit.rest.issues.updateComment({
5051
...this.context.repo,
5152
body: [
5253
`<!-- cdk diff action with hash ${hash} -->`,
5354
...content,
5455
'',
55-
`_Generated for commit ${this.commitSha}_`,
56+
`_Generated for commit ${this.commitSha} at ${timestamp}_`,
5657
].join('\n'),
5758
comment_id: commentId,
5859
});
@@ -65,13 +66,14 @@ export class Comments {
6566
* @param content the content of the comment
6667
*/
6768
public async createComment(hash: string, content: string[]) {
69+
const timestamp = new Date().toISOString();
6870
await this.octokit.rest.issues.createComment({
6971
...this.context.repo,
7072
body: [
7173
`<!-- cdk diff action with hash ${hash} -->`,
7274
...content,
7375
'',
74-
`_Generated for commit ${this.commitSha}_`,
76+
`_Generated for commit ${this.commitSha} at ${timestamp}_`,
7577
].join('\n'),
7678
issue_number: this.issueNumber,
7779
});

test/comment.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as core from '@actions/core';
2-
import { Context } from '@actions/github/lib/context';
3-
import { GitHub } from '@actions/github/lib/utils';
2+
import type { Context } from '@actions/github/lib/context';
3+
import type { GitHub } from '@actions/github/lib/utils';
44
import { Comments } from '../src/comment';
55

66
jest.spyOn(core, 'debug').mockImplementation(() => {});
@@ -12,6 +12,8 @@ const issues = { createComment, updateComment, listComments };
1212

1313
const rest = { issues };
1414
const octokit = { rest } as unknown as InstanceType<typeof GitHub>;
15+
let timestamp = '';
16+
1517
const context: Context = {
1618
sha: 'some-sha',
1719
payload: {
@@ -63,6 +65,11 @@ const commentDataWithUnMatchedTag = {
6365
beforeEach(() => {
6466
createComment.mockClear();
6567
updateComment.mockClear();
68+
jest.useFakeTimers({
69+
now: new Date('2021-02-26T22:42:16.652Z'),
70+
advanceTimers: true,
71+
});
72+
timestamp = new Date().toISOString();
6673
});
6774

6875
describe('comments', () => {
@@ -94,14 +101,14 @@ describe('comments', () => {
94101
`<!-- cdk diff action with hash ${hash} -->`,
95102
'message',
96103
'',
97-
`_Generated for commit ${context.payload.pull_request?.head.sha}_`,
104+
`_Generated for commit ${context.payload.pull_request?.head.sha} at ${timestamp}_`,
98105
].join('\n'),
99106
comment_id: 1,
100107
});
101108
});
102109

103110
test('create comment', async () => {
104-
updateComment.mockResolvedValue({});
111+
createComment.mockResolvedValue({});
105112
const comments = new Comments(octokit, context);
106113
expect(comments.createComment(hash, ['message'])).resolves;
107114
expect(createComment).toHaveBeenCalledWith({
@@ -110,7 +117,7 @@ describe('comments', () => {
110117
`<!-- cdk diff action with hash ${hash} -->`,
111118
'message',
112119
'',
113-
`_Generated for commit ${context.payload.pull_request?.head.sha}_`,
120+
`_Generated for commit ${context.payload.pull_request?.head.sha} at ${timestamp}_`,
114121
].join('\n'),
115122
issue_number: context.payload.pull_request?.number,
116123
});

0 commit comments

Comments
 (0)