Skip to content

Commit

Permalink
feat: outdated comment action option to minimize previous reg-action …
Browse files Browse the repository at this point in the history
…comments (#138)
  • Loading branch information
krrrr38 committed May 16, 2024
1 parent 9b3f8b6 commit 62a33a9
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ The option to disable push to a branch. When set to false, the `branch` option i

The option how to render changed file in comment. This action will change PR and workflow summary report format. Available options are `raw` and `summarized`. `raw` will render report comment with expanded results. `summarized` will render report comment using `<details>` tag to summarize by changed files.

#### `outdated-comment-action` (Optional)

- Type: String
- Default: `"none"`

The option to handle outdated comments in the PR. Available options are `none` and `minimize`. `none` do nothing. `minimize` will minimize outdated action comments.

## Limitation

- If the `artifact` is deleted, the report will also be deleted, see [`Artifact and log retention policy`](https://docs.github.com/ja/actions/learn-github-actions/usage-limits-billing-and-administration#artifact-and-log-retention-policy) for the retention period of the `artifact`.
Expand Down
3 changes: 3 additions & 0 deletions dist/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ inputs:
comment-report-format:
description: "The option how to render changed file in comment. `raw` by default."
required: false
outdated-comment-action:
description: "The option to handle outdated comments. `none` by default."
required: false
runs:
using: "node20"
main: "lib/index.js"
Expand Down
39 changes: 39 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,45 @@ export const createClient = (repository: Repository, octokit: Octokit) => {
{ numOfAttempts: 5 },
);
},
listComments: async (issueNumber: number): Promise<{ node_id: string; body?: string | undefined }[]> => {
return backOff(
() =>
octokit.paginate(
octokit.rest.issues.listComments,
{
...repository,
issue_number: issueNumber,
per_page: 100,
},
r => r.data,
),
{ numOfAttempts: 5 },
);
},
minimizeOutdatedComment: async (nodeId: string) => {
const _ = await backOff(
() =>
octokit.graphql(
`
mutation($input: MinimizeCommentInput!) {
minimizeComment(input: $input) {
minimizedComment {
isMinimized
}
}
}
`,
{
input: {
subjectId: nodeId,
classifier: 'OUTDATED',
},
},
),
{ numOfAttempts: 5 },
);
return;
},
postComment: async (issueNumber: number, comment: string) => {
const _ = await backOff(
() => octokit.rest.issues.createComment({ ...repository, issue_number: issueNumber, body: comment }),
Expand Down
6 changes: 6 additions & 0 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,9 @@ ${report}

return body;
};

export const isRegActionComment = ({ artifactName, body }: { artifactName: string; body: string }): boolean => {
return (
body.includes(`## ArtifactName: \`${artifactName}\``) || body.includes(`## ArtifactName: [\`${artifactName}\`]`)
);
};
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Config {
customReportPage: string | null;
reportFilePath: string | null;
commentReportFormat: 'raw' | 'summarized';
outdatedCommentAction: 'none' | 'minimize';
}

const validateGitHubToken = (githubToken: string | undefined) => {
Expand Down Expand Up @@ -101,6 +102,12 @@ function validateCommentReportFormat(format: string): asserts format is 'raw' |
}
}

function validateOutdatedCommentAction(action: string): asserts action is 'none' | 'minimize' {
if (action !== 'none' && action !== 'minimize') {
throw new Error(`'outdated-comment-action' input must be 'none' or 'minimized' but got '${action}'`);
}
}

export const getConfig = (): Config => {
const githubToken = core.getInput('github-token');
const imageDirectoryPath = core.getInput('image-directory-path');
Expand All @@ -121,6 +128,8 @@ export const getConfig = (): Config => {
validateReportFilePath(reportFilePath);
const commentReportFormat = core.getInput('comment-report-format') || 'raw';
validateCommentReportFormat(commentReportFormat);
const outdatedCommentAction = core.getInput('outdated-comment-action') || 'none';
validateOutdatedCommentAction(outdatedCommentAction);

return {
githubToken,
Expand All @@ -136,5 +145,6 @@ export const getConfig = (): Config => {
customReportPage,
reportFilePath,
commentReportFormat,
outdatedCommentAction,
};
};
19 changes: 18 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Config } from './config';
import { Event } from './event';
import { findRunAndArtifact, RunClient } from './run';
import { compare, CompareOutput } from './compare';
import { createCommentWithTarget, createCommentWithoutTarget } from './comment';
import { createCommentWithTarget, createCommentWithoutTarget, isRegActionComment } from './comment';
import * as constants from './constants';
import { workspace } from './path';
import { pushImages } from './push';
Expand Down Expand Up @@ -115,6 +115,17 @@ const init = async (config: Config) => {

type CommentClient = {
postComment: (issueNumber: number, comment: string) => Promise<void>;
listComments: (issueNumber: number) => Promise<{ node_id: string; body?: string | undefined }[]>;
minimizeOutdatedComment: (nodeId: string) => Promise<void>;
};

const minimizePreviousComments = async (client: CommentClient, issueNumber: number, artifactName: string) => {
const comments = await client.listComments(issueNumber);
for (const comment of comments) {
if (comment.body && isRegActionComment({ artifactName, body: comment.body })) {
await client.minimizeOutdatedComment(comment.node_id);
}
}
};

type SummaryClient = {
Expand Down Expand Up @@ -172,6 +183,9 @@ export const run = async ({
artifactName: config.artifactName,
customReportPage: config.customReportPage,
});
if (config.outdatedCommentAction === 'minimize') {
await minimizePreviousComments(client, event.number, config.artifactName);
}
await client.postComment(event.number, comment);
}
return;
Expand Down Expand Up @@ -218,6 +232,9 @@ export const run = async ({
});

try {
if (config.outdatedCommentAction === 'minimize') {
await minimizePreviousComments(client, event.number, config.artifactName);
}
await client.postComment(event.number, comment);

log.info('post summary comment');
Expand Down

0 comments on commit 62a33a9

Please sign in to comment.