Skip to content

Commit

Permalink
Merge pull request #19 from getappmap/feat/create-pr-summary-comment
Browse files Browse the repository at this point in the history
Create PR summary comment
  • Loading branch information
kgilpin authored Jul 19, 2023
2 parents 28760b7 + ddd2aec commit e6367f9
Show file tree
Hide file tree
Showing 10 changed files with 18,531 additions and 7,609 deletions.
25,376 changes: 17,771 additions & 7,605 deletions dist/preflight/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
"@types/glob": "^8.1.0",
"@types/jest": "^29.5.0",
"@types/node": "^18.15.11",
"@types/sinon": "^10.0.15",
"@vercel/ncc": "^0.36.1",
"glob": "^9.3.4",
"jest": "^29.5.0",
"openapi-diff": "^0.23.6",
"prettier": "^2.8.7",
"sinon": "^15.2.0",
"ts-jest": "^29.0.5",
"ts-node": "^10.9.1",
"typescript": "^5.0.2"
Expand Down
62 changes: 62 additions & 0 deletions src/Commenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as github from '@actions/github';
import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types';

import assert from 'assert';
import fs from 'fs';

export default class Commenter {
// TODO: don't use "any" type
private readonly octokit: any;
public static readonly COMMENT_TAG_PATTERN = '<!-- "appmap" -->';

constructor(private readonly filePath: string, private readonly githubToken: string) {
this.octokit = github.getOctokit(this.githubToken);
}

public async comment() {
const { context } = github;
const { octokit } = this;

const issue_number = context.payload.pull_request?.number || context.payload.issue?.number;
assert(issue_number);

const content = fs.readFileSync(this.filePath, 'utf8');
const body = `${content}\n${Commenter.COMMENT_TAG_PATTERN}`;
const comment = await this.getAppMapComment(issue_number);

if (comment) {
await octokit.rest.issues.updateComment({
...context.repo,
comment_id: comment.id,
body,
});
} else {
await octokit.rest.issues.createComment({
...context.repo,
issue_number,
body,
});
}
}

public async getAppMapComment(issue_number: number): Promise<any> {
const { context } = github;
const { octokit } = this;

type ListCommentsResponseDataType = GetResponseDataTypeFromEndpointMethod<
typeof octokit.rest.issues.listComments
>;

let comment: ListCommentsResponseDataType[0] | undefined;
for await (const { data: comments } of octokit.paginate.iterator(octokit.rest.issues.listComments, {
...context.repo,
issue_number,
})) {
// TODO: don't use "any" type
comment = comments.find((comment: any) => comment?.body?.includes(Commenter.COMMENT_TAG_PATTERN));
if (comment) break;
}

return comment;
}
}
4 changes: 4 additions & 0 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {cp} from 'fs/promises';
import {inspect} from 'util';
import ReportOptions from './ReportOptions';
import CompareOptions from './CompareOptions';
import Commenter from './Commenter';

async function runInGitHub(): Promise<void> {
verbose(core.getBooleanInput('verbose'));
Expand Down Expand Up @@ -75,6 +76,9 @@ async function runInGitHub(): Promise<void> {
const compareResult = await compare(new GitHubArtifactStore(), compareOptions);
const reportResult = await summarizeChanges(compareResult.reportDir, reportOptions);

const commenter = new Commenter(reportResult.reportFile, githubToken);
await commenter.comment();

core.setOutput('report-dir', compareResult.reportDir);
if (process.env.GITHUB_STEP_SUMMARY) {
await cp(reportResult.reportFile, process.env.GITHUB_STEP_SUMMARY);
Expand Down
Loading

0 comments on commit e6367f9

Please sign in to comment.