Skip to content

Commit

Permalink
chore(comments): add hidden header in the comments
Browse files Browse the repository at this point in the history
They will be used for future features that need to identify the comments that were sent by the action and also which kind of comment they are.
  • Loading branch information
C0ZEN committed Sep 5, 2022
1 parent bf18153 commit 437f7cc
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 54 deletions.
8 changes: 4 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ To help us have a clear vision over the workflow and also for you if you are jus
- Else, check if issue last update is older than X days (coming from `issue-days-before-close`)
- If it is old, close the issue (using the close reason coming from `issue-close-reason`)
- Check if the action should also add a comment (coming from the `issue-close-comment` input)
- When the input value is not empty, add a comment
- When the input value is not empty, add a comment (with a special header)
- Check if the action should also add extra labels (coming from the `issue-add-labels-after-close` input)
- When the input value is filled list, add the listed labels
- Check if the action should also remove extra labels (coming from the `issue-remove-labels-after-close` input)
- When the input value is filled list, remove the listed labels
- Check if the issue last update is older than X days (coming from the `issue-days-before-stale`)
- If the issue last update is older than X days (coming from the `issue-days-before-stale`)
- Check if the action should also add a comment (coming from the `issue-stale-comment` input)
- When the input value is not empty, add a comment
- When the input value is not empty, add a comment (with a special header)
- Check if the action should also add extra labels (coming from the `issue-add-labels-after-stale` input)
- When the input value is filled list, add the listed labels
- Check if the action should also remove extra labels (coming from the `issue-remove-labels-after-stale` input)
Expand Down Expand Up @@ -212,7 +212,7 @@ To help us have a clear vision over the workflow and also for you if you are jus
- Else, check if pull request last update is older than X days (coming from `pull-request-days-before-close`)
- If it is old, close the pull request
- Check if the action should also add a comment (coming from the `pull-request-close-comment` input)
- When the input value is not empty, add a comment
- When the input value is not empty, add a comment (with a special header)
- Check if the action should also add extra labels (coming from the `pull-request-add-labels-after-close` input)
- When the input value is filled list, add the listed labels
- Check if the action should also remove extra labels (coming from the `pull-request-remove-labels-after-close` input)
Expand All @@ -226,7 +226,7 @@ To help us have a clear vision over the workflow and also for you if you are jus
- Convert to draft and stop the processing
- If the draft mode is disabled
- Check if the action should also add a comment (coming from the `pull-request-stale-comment` input)
- When the input value is not empty, add a comment
- When the input value is not empty, add a comment (with a special header)
- Check if the action should also add extra labels (coming from the `pull-request-add-labels-after-stale` input)
- When the input value is filled list, add the listed labels
- Check if the action should also remove extra labels (coming from the `pull-request-remove-labels-after-stale` input)
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions src/core/processing/abstract-comments-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { CommonInputsService } from '@core/inputs/common-inputs.service';
import { ICommonInputs } from '@core/inputs/interfaces/common-inputs.interface';
import { IssueProcessor } from '@core/processing/issues/issue-processor';
import { PullRequestProcessor } from '@core/processing/pull-requests/pull-request-processor';
import { ECommentType } from '@utils/enums/comment-type.enum';
import { ICommentHeaderOptions } from '@utils/interfaces/comment-header-options.interface';
import { LoggerService } from '@utils/loggers/logger.service';
import { AbstractProcessor } from '@utils/processors/abstract-processor';
import { IComment } from '@utils/types/comment';
Expand Down Expand Up @@ -31,7 +33,9 @@ export abstract class AbstractCommentsProcessor<
if (!commonInputs.dryRun) {
this.processor.logger.info(`Adding the stale comment...`);

await this._addComment(this._getItemId(), staleComment);
await this._addComment(this._getItemId(), staleComment, {
commentType: ECommentType.STALE,
});
}

this._increaseAddedCommentsCountStatistic();
Expand All @@ -55,7 +59,9 @@ export abstract class AbstractCommentsProcessor<
if (!commonInputs.dryRun) {
this.processor.logger.info(`Adding the close comment...`);

await this._addComment(this._getItemId(), closeComment);
await this._addComment(this._getItemId(), closeComment, {
commentType: ECommentType.CLOSE,
});
}

this._increaseAddedCommentsCountStatistic();
Expand All @@ -64,7 +70,11 @@ export abstract class AbstractCommentsProcessor<

protected abstract _getItemId(): IUuid;

protected abstract _addComment(itemId: Readonly<IUuid>, comment: Readonly<IComment>): Promise<void>;
protected abstract _addComment(
itemId: Readonly<IUuid>,
comment: Readonly<IComment>,
commentHeaderOptions: Readonly<ICommentHeaderOptions>
): Promise<void>;

protected abstract _getCloseComment(): IComment | '';

Expand Down
14 changes: 12 additions & 2 deletions src/core/processing/issues/issue-comments-processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { IssueCommentsProcessor } from '@core/processing/issues/issue-comments-p
import { IssueProcessor } from '@core/processing/issues/issue-processor';
import { IssuesStatisticsService } from '@core/statistics/issues-statistics.service';
import { GithubApiIssueCommentsService } from '@github/api/comments/github-api-issue-comments.service';
import { ECommentType } from '@utils/enums/comment-type.enum';
import { ICommentHeaderOptions } from '@utils/interfaces/comment-header-options.interface';
import { IUuid } from '@utils/types/uuid';
import faker from 'faker';
import { createHydratedMock } from 'ts-auto-mock';
Expand Down Expand Up @@ -180,7 +182,7 @@ describe(`IssueCommentsProcessor`, (): void => {
});

it(`should add the stale comment on the issue`, async (): Promise<void> => {
expect.assertions(4);
expect.assertions(6);

await issueCommentsProcessor.processStaleComment();

Expand All @@ -192,6 +194,10 @@ describe(`IssueCommentsProcessor`, (): void => {
);
expect(issueProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(3, `Adding the stale comment...`);
expect(issueProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(4, `Stale comment added`);
expect(githubApiIssueCommentsServiceAddCommentSpy).toHaveBeenCalledTimes(1);
expect(githubApiIssueCommentsServiceAddCommentSpy).toHaveBeenCalledWith(issueId, `dummy-comment`, {
commentType: ECommentType.STALE,
} as ICommentHeaderOptions);
});

it(`should increase the added issues comments count by 1`, async (): Promise<void> => {
Expand Down Expand Up @@ -350,7 +356,7 @@ describe(`IssueCommentsProcessor`, (): void => {
});

it(`should add the close comment on the issue`, async (): Promise<void> => {
expect.assertions(4);
expect.assertions(6);

await issueCommentsProcessor.processCloseComment();

Expand All @@ -362,6 +368,10 @@ describe(`IssueCommentsProcessor`, (): void => {
);
expect(issueProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(3, `Adding the close comment...`);
expect(issueProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(4, `Close comment added`);
expect(githubApiIssueCommentsServiceAddCommentSpy).toHaveBeenCalledTimes(1);
expect(githubApiIssueCommentsServiceAddCommentSpy).toHaveBeenCalledWith(issueId, `dummy-comment`, {
commentType: ECommentType.CLOSE,
} as ICommentHeaderOptions);
});

it(`should increase the added issues comments count by 1`, async (): Promise<void> => {
Expand Down
9 changes: 7 additions & 2 deletions src/core/processing/issues/issue-comments-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AbstractCommentsProcessor } from '@core/processing/abstract-comments-pr
import { IssueProcessor } from '@core/processing/issues/issue-processor';
import { IssuesStatisticsService } from '@core/statistics/issues-statistics.service';
import { GithubApiIssueCommentsService } from '@github/api/comments/github-api-issue-comments.service';
import { ICommentHeaderOptions } from '@utils/interfaces/comment-header-options.interface';
import { IComment } from '@utils/types/comment';
import { IUuid } from '@utils/types/uuid';

Expand Down Expand Up @@ -35,7 +36,11 @@ export class IssueCommentsProcessor extends AbstractCommentsProcessor<IssueProce
IssuesStatisticsService.getInstance().increaseAddedIssuesCommentsCount();
}

protected _addComment(itemId: Readonly<IUuid>, comment: Readonly<IComment>): Promise<void> {
return this.githubApiIssueCommentsService$$.addComment(itemId, comment);
protected _addComment(
itemId: Readonly<IUuid>,
comment: Readonly<IComment>,
commentHeaderOptions: Readonly<ICommentHeaderOptions>
): Promise<void> {
return this.githubApiIssueCommentsService$$.addComment(itemId, comment, commentHeaderOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { PullRequestCommentsProcessor } from '@core/processing/pull-requests/pul
import { PullRequestProcessor } from '@core/processing/pull-requests/pull-request-processor';
import { PullRequestsStatisticsService } from '@core/statistics/pull-requests-statistics.service';
import { GithubApiPullRequestCommentsService } from '@github/api/comments/github-api-pull-request-comments.service';
import { ECommentType } from '@utils/enums/comment-type.enum';
import { ICommentHeaderOptions } from '@utils/interfaces/comment-header-options.interface';
import { IUuid } from '@utils/types/uuid';
import faker from 'faker';
import { createHydratedMock } from 'ts-auto-mock';
Expand Down Expand Up @@ -187,7 +189,7 @@ describe(`PullRequestCommentsProcessor`, (): void => {
});

it(`should add the stale comment on the pull request`, async (): Promise<void> => {
expect.assertions(4);
expect.assertions(6);

await pullRequestCommentsProcessor.processStaleComment();

Expand All @@ -199,6 +201,14 @@ describe(`PullRequestCommentsProcessor`, (): void => {
);
expect(pullRequestProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(3, `Adding the stale comment...`);
expect(pullRequestProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(4, `Stale comment added`);
expect(githubApiPullRequestCommentsServiceAddCommentSpy).toHaveBeenCalledTimes(1);
expect(githubApiPullRequestCommentsServiceAddCommentSpy).toHaveBeenCalledWith(
pullRequestId,
`dummy-comment`,
{
commentType: ECommentType.STALE,
} as ICommentHeaderOptions
);
});

it(`should increase the added pull requests comments count by 1`, async (): Promise<void> => {
Expand Down Expand Up @@ -364,7 +374,7 @@ describe(`PullRequestCommentsProcessor`, (): void => {
});

it(`should add the close comment on the pull request`, async (): Promise<void> => {
expect.assertions(4);
expect.assertions(6);

await pullRequestCommentsProcessor.processCloseComment();

Expand All @@ -376,6 +386,14 @@ describe(`PullRequestCommentsProcessor`, (): void => {
);
expect(pullRequestProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(3, `Adding the close comment...`);
expect(pullRequestProcessorLoggerInfoSpy).toHaveBeenNthCalledWith(4, `Close comment added`);
expect(githubApiPullRequestCommentsServiceAddCommentSpy).toHaveBeenCalledTimes(1);
expect(githubApiPullRequestCommentsServiceAddCommentSpy).toHaveBeenCalledWith(
pullRequestId,
`dummy-comment`,
{
commentType: ECommentType.CLOSE,
} as ICommentHeaderOptions
);
});

it(`should increase the added pull requests comments count by 1`, async (): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AbstractCommentsProcessor } from '@core/processing/abstract-comments-pr
import { PullRequestProcessor } from '@core/processing/pull-requests/pull-request-processor';
import { PullRequestsStatisticsService } from '@core/statistics/pull-requests-statistics.service';
import { GithubApiPullRequestCommentsService } from '@github/api/comments/github-api-pull-request-comments.service';
import { ICommentHeaderOptions } from '@utils/interfaces/comment-header-options.interface';
import { IComment } from '@utils/types/comment';
import { IUuid } from '@utils/types/uuid';

Expand Down Expand Up @@ -35,7 +36,11 @@ export class PullRequestCommentsProcessor extends AbstractCommentsProcessor<Pull
PullRequestsStatisticsService.getInstance().increaseAddedPullRequestsCommentsCount();
}

protected _addComment(itemId: Readonly<IUuid>, comment: Readonly<IComment>): Promise<void> {
return this.githubApiPullRequestCommentsService$$.addComment(itemId, comment);
protected _addComment(
itemId: Readonly<IUuid>,
comment: Readonly<IComment>,
commentHeaderOptions: Readonly<ICommentHeaderOptions>
): Promise<void> {
return this.githubApiPullRequestCommentsService$$.addComment(itemId, comment, commentHeaderOptions);
}
}
34 changes: 32 additions & 2 deletions src/github/api/comments/abstract-github-api-comments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@ import { GITHUB_API_ADD_COMMENT_MUTATION } from '@github/api/comments/constants/
import { OctokitService } from '@github/octokit/octokit.service';
import { AnnotationsService } from '@utils/annotations/annotations.service';
import { EAnnotationError } from '@utils/annotations/enums/annotation-error.enum';
import { ECommentType } from '@utils/enums/comment-type.enum';
import { ICommentHeaderOptions } from '@utils/interfaces/comment-header-options.interface';
import { LoggerFormatService } from '@utils/loggers/logger-format.service';
import { LoggerService } from '@utils/loggers/logger.service';
import { IComment } from '@utils/types/comment';
import { IUuid } from '@utils/types/uuid';

const COMMENT_EOL = `\r\n`;
const COMMENT_HEADER_START = `<!-- SONIA-STALE-COMMENT-HEADER:START -->${COMMENT_EOL}`;
const COMMENT_HEADER_END = `<!-- SONIA-STALE-COMMENT-HEADER:END -->${COMMENT_EOL}${COMMENT_EOL}`;
const COMMENT_TYPE_MAP: Record<ECommentType, IComment> = {
[ECommentType.CLOSE]: `<!-- Close comment -->${COMMENT_EOL}`,
[ECommentType.STALE]: `<!-- Stale comment -->${COMMENT_EOL}`,
};

export abstract class AbstractGithubApiCommentsService<
TProcessor extends IssueProcessor | PullRequestProcessor
> extends AbstractGithubApiService<TProcessor> {
protected constructor(processor: TProcessor) {
super(processor);
}

public addComment(targetId: Readonly<IUuid>, comment: Readonly<IComment>): Promise<void> | never {
public addComment(
targetId: Readonly<IUuid>,
comment: Readonly<IComment>,
commentHeaderOptions: Readonly<ICommentHeaderOptions>
): Promise<void> | never {
this.processor.logger.info(
`Adding the comment`,
LoggerService.value(comment),
Expand All @@ -27,7 +41,7 @@ export abstract class AbstractGithubApiCommentsService<

return OctokitService.getOctokit()
.graphql<unknown>(GITHUB_API_ADD_COMMENT_MUTATION, {
comment,
comment: this._addCommentHeader(comment, commentHeaderOptions),
id: targetId,
})
.then((): void => {
Expand Down Expand Up @@ -56,5 +70,21 @@ export abstract class AbstractGithubApiCommentsService<
});
}

private _addCommentHeader(comment: Readonly<IComment>, options: Readonly<ICommentHeaderOptions>): IComment {
return `${this._createCommentHeader(options)}${comment}`;
}

private _createCommentHeader(options: Readonly<ICommentHeaderOptions>): IComment {
return `${COMMENT_HEADER_START}${this._getHeaderOptions(options)}${COMMENT_HEADER_END}`;
}

private _getHeaderOptions(options: Readonly<ICommentHeaderOptions>): IComment {
let headerOptions: IComment = ``;

headerOptions += COMMENT_TYPE_MAP[options.commentType];

return headerOptions;
}

protected abstract _increaseCalledApiMutationsCount(): void;
}

0 comments on commit 437f7cc

Please sign in to comment.