Files
stale/src/classes/loggers/issue-logger.ts
T

84 lines
2.1 KiB
TypeScript
Raw Normal View History

import {Issue} from '../issue';
import {Logger} from './logger';
2021-06-02 23:04:34 +02:00
import {LoggerService} from '../../services/logger.service';
/**
* @description
* Each log will prefix the message with the issue number
*
* @example
* warning('No stale') => "[#123] No stale"
*
* Each log method can have special tokens:
* - $$type => will replace this by either "pull request" or "issue" depending of the type of issue
*
* @example
* warning('The $$type will stale') => "The pull request will stale"
*/
2021-03-08 11:56:52 +01:00
export class IssueLogger extends Logger {
private readonly _issue: Issue;
constructor(issue: Issue) {
2021-03-08 11:56:52 +01:00
super();
this._issue = issue;
}
2021-03-08 11:56:52 +01:00
warning(...message: string[]): void {
super.warning(this._format(...message));
}
2021-03-08 11:56:52 +01:00
info(...message: string[]): void {
super.info(this._format(...message));
}
2021-03-08 11:56:52 +01:00
error(...message: string[]): void {
super.error(this._format(...message));
}
2021-06-07 23:22:55 +02:00
async grouping(message: string, fn: () => Promise<void>): Promise<void> {
return super.grouping(this._format(message), fn);
}
private _replaceTokens(message: Readonly<string>): string {
return this._replaceTypeToken(message);
}
private _replaceTypeToken(message: Readonly<string>): string {
return message
.replace(
/^\$\$type/,
this._issue.isPullRequest ? 'Pull request' : 'Issue'
)
.replace(
/\$\$type/g,
this._issue.isPullRequest ? 'pull request' : 'issue'
);
}
private _prefixWithIssueNumber(message: Readonly<string>): string {
2021-03-08 11:56:52 +01:00
return `${this._getPrefix()} ${message}`;
}
private _getIssueNumber(): number {
return this._issue.number;
}
2021-03-08 11:56:52 +01:00
private _format(...message: string[]): string {
return this._prefixWithIssueNumber(this._replaceTokens(message.join(' ')));
}
private _getPrefix(): string {
return this._issue.isPullRequest
? this._getPullRequestPrefix()
: this._getIssuePrefix();
}
private _getIssuePrefix(): string {
2021-06-02 23:04:34 +02:00
return LoggerService.red(`[#${this._getIssueNumber()}]`);
2021-03-08 11:56:52 +01:00
}
private _getPullRequestPrefix(): string {
2021-06-02 23:04:34 +02:00
return LoggerService.blue(`[#${this._getIssueNumber()}]`);
}
}