mirror of
https://gitea.com/actions/stale.git
synced 2026-09-05 20:24:24 +00:00
eaf9131fae
- Changed import statements across multiple files to use the .js extension for ES module compatibility. - Refactored test files to utilize jest's unstable_mockModule for mocking core actions. - Removed unnecessary spy instances in tests, replacing them with direct mocked function calls. - Updated TypeScript configuration to target ES2022 and use NodeNext module resolution. - Removed the tsconfig.spec.json file and adjusted the main tsconfig.json to exclude test files.
84 lines
2.1 KiB
TypeScript
84 lines
2.1 KiB
TypeScript
import {Issue} from '../issue.js';
|
|
import {Logger} from './logger.js';
|
|
import {LoggerService} from '../../services/logger.service.js';
|
|
|
|
/**
|
|
* @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"
|
|
*/
|
|
export class IssueLogger extends Logger {
|
|
private readonly _issue: Issue;
|
|
|
|
constructor(issue: Issue) {
|
|
super();
|
|
this._issue = issue;
|
|
}
|
|
|
|
warning(...message: string[]): void {
|
|
super.warning(this._format(...message));
|
|
}
|
|
|
|
info(...message: string[]): void {
|
|
super.info(this._format(...message));
|
|
}
|
|
|
|
error(...message: string[]): void {
|
|
super.error(this._format(...message));
|
|
}
|
|
|
|
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 {
|
|
return `${this._getPrefix()} ${message}`;
|
|
}
|
|
|
|
private _getIssueNumber(): number {
|
|
return this._issue.number;
|
|
}
|
|
|
|
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 {
|
|
return LoggerService.red(`[#${this._getIssueNumber()}]`);
|
|
}
|
|
|
|
private _getPullRequestPrefix(): string {
|
|
return LoggerService.blue(`[#${this._getIssueNumber()}]`);
|
|
}
|
|
}
|