2021-01-16 15:49:50 +01:00
|
|
|
import * as core from '@actions/core';
|
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-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-01-16 15:49:50 +01:00
|
|
|
export class IssueLogger implements Logger {
|
|
|
|
|
private readonly _issue: Issue;
|
|
|
|
|
|
2021-01-19 11:54:16 +01:00
|
|
|
constructor(issue: Issue) {
|
2021-01-16 15:49:50 +01:00
|
|
|
this._issue = issue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
warning(message: Readonly<string>): void {
|
2021-02-28 12:15:08 +01:00
|
|
|
core.warning(this._format(message));
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info(message: Readonly<string>): void {
|
2021-02-28 12:15:08 +01:00
|
|
|
core.info(this._format(message));
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error(message: Readonly<string>): void {
|
2021-02-28 12:15:08 +01:00
|
|
|
core.error(this._format(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
return `[#${this._getIssueNumber()}] ${message}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _getIssueNumber(): number {
|
|
|
|
|
return this._issue.number;
|
|
|
|
|
}
|
2021-02-28 12:15:08 +01:00
|
|
|
|
|
|
|
|
private _format(message: Readonly<string>): string {
|
|
|
|
|
return this._prefixWithIssueNumber(this._replaceTokens(message));
|
|
|
|
|
}
|
2021-01-16 15:49:50 +01:00
|
|
|
}
|