Skip to content

Commit

Permalink
feat: add commit sha to comment (#5)
Browse files Browse the repository at this point in the history
closes #2
  • Loading branch information
corymhall authored Oct 31, 2023
1 parent be6055d commit 2a557f2
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions .projen/tasks.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const project = new GitHubActionTypeScriptProject({
},
},
deps: [
'@octokit/webhooks-definitions',
'@aws-cdk/cloudformation-diff',
'@aws-cdk/cloud-assembly-schema',
'@actions/exec@^1.1.1',
Expand Down
18 changes: 12 additions & 6 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export async function run() {
};
const octokit = github.getOctokit(inputs.githubToken);
const context = github.context;

try {
const assembly = AssemblyManifestReader.fromPath('cdk.out');
let stages = assembly.stages;
Expand Down
12 changes: 10 additions & 2 deletions src/comment.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Context } from '@actions/github/lib/context';
import { GitHub } from '@actions/github/lib/utils';
import { PullRequestEvent } from '@octokit/webhooks-definitions/schema';

export class Comments {
private readonly issueNumber: number;
private readonly commitSha: string;
constructor(
private readonly octokit: InstanceType<typeof GitHub>,
private readonly context: Context,
) {
if (!context.payload.pull_request?.number) {
const payload = context.payload as PullRequestEvent;
this.commitSha = payload.pull_request.head.sha;
if (!payload.pull_request.number) {
throw new Error('Cannot find PR number, is this from a pull request?');
}
this.issueNumber = this.context.payload.pull_request?.number!;
Expand All @@ -18,7 +22,7 @@ export class Comments {
...this.context.repo,
issue_number: this.issueNumber,
});
return comments.data.find(comment => comment.body_text?.includes(hash))?.id;
return comments.data.find(comment => comment.body?.includes(hash))?.id;
}

public async updateComment(commentId: number, hash: string, content: string[]) {
Expand All @@ -27,6 +31,8 @@ export class Comments {
body: [
`<!-- cdk diff action with hash ${hash} -->`,
...content,
'',
`_Generated for commit ${this.commitSha}_`,
].join('\n'),
comment_id: commentId,
});
Expand All @@ -38,6 +44,8 @@ export class Comments {
body: [
`<!-- cdk diff action with hash ${hash} -->`,
...content,
'',
`_Generated for commit ${this.commitSha}_`,
].join('\n'),
issue_number: this.issueNumber,
});
Expand Down
15 changes: 10 additions & 5 deletions test/comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ const context: Context = {
full_name: 'some-repo',
},
pull_request: {
head: { sha: '123' },
number: 1,
},
},
} as Context;
} as unknown as Context;
// const pullRequestData = {
// data: {
// items: [
Expand All @@ -34,11 +35,11 @@ const commentDataWithTag = {
data: [
{
id: 1,
body_text: 'some comment',
body: 'some comment',
},
{
id: 2,
body_text: `<!-- cdk diff action with hash ${hash} -->\nprevious-message`,
body: `<!-- cdk diff action with hash ${hash} -->\nprevious-message`,
},
],
};
Expand All @@ -47,11 +48,11 @@ const commentDataWithUnMatchedTag = {
data: [
{
id: 1,
body_text: 'some comment',
body: 'some comment',
},
{
id: 2,
body_text: '<!-- cdk diff action with hash SOME-DIFFERENT-HASH -->\nprevious-message',
body: '<!-- cdk diff action with hash SOME-DIFFERENT-HASH -->\nprevious-message',
},
],
};
Expand Down Expand Up @@ -89,6 +90,8 @@ describe('comments', () => {
body: [
`<!-- cdk diff action with hash ${hash} -->`,
'message',
'',
`_Generated for commit ${context.payload.pull_request?.head.sha}_`,
].join('\n'),
comment_id: 1,
});
Expand All @@ -103,6 +106,8 @@ describe('comments', () => {
body: [
`<!-- cdk diff action with hash ${hash} -->`,
'message',
'',
`_Generated for commit ${context.payload.pull_request?.head.sha}_`,
].join('\n'),
issue_number: context.payload.pull_request?.number,
});
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a557f2

Please sign in to comment.