2021-01-19 11:54:16 +01:00
|
|
|
import {Issue} from '../issue';
|
2021-01-16 15:49:50 +01:00
|
|
|
import {Logger} from './logger';
|
2021-06-02 23:04:34 +02:00
|
|
|
import {LoggerService} from '../../services/logger.service';
|
2021-01-16 15:49:50 +01:00
|
|
|
|
2021-02-28 12:15:08 +01:00
|
|
|
/**
|
|
|
|
|
* @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 {
|
2021-01-16 15:49:50 +01:00
|
|
|
private readonly _issue: Issue;
|
|
|
|
|
|
2021-01-19 11:54:16 +01:00
|
|
|
constructor(issue: Issue) {
|
2021-03-08 11:56:52 +01:00
|
|
|
super();
|
2021-01-16 15:49:50 +01:00
|
|
|
this._issue = issue;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:56:52 +01:00
|
|
|
warning(...message: string[]): void {
|
|
|
|
|
super.warning(this._format(...message));
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:56:52 +01:00
|
|
|
info(...message: string[]): void {
|
|
|
|
|
super.info(this._format(...message));
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2021-03-08 11:56:52 +01:00
|
|
|
error(...message: string[]): void {
|
|
|
|
|
super.error(this._format(...message));
|
2021-02-28 12:15:08 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-07 23:22:55 +02:00
|
|
|
async grouping(message: string, fn: () => Promise<void>): Promise<void> {
|
|
|
|
|
return super.grouping(this._format(message), fn);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-28 12:15:08 +01:00
|
|
|
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'
|
|
|
|
|
);
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _prefixWithIssueNumber(message: Readonly<string>): string {
|
2021-03-08 11:56:52 +01:00
|
|
|
return `${this._getPrefix()} ${message}`;
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _getIssueNumber(): number {
|
|
|
|
|
return this._issue.number;
|
|
|
|
|
}
|
2021-02-28 12:15:08 +01:00
|
|
|
|
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()}]`);
|
2021-02-28 12:15:08 +01:00
|
|
|
}
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|