Skip to content

Commit

Permalink
feat(logs): enhance the logs and add a prefix with the issue number
Browse files Browse the repository at this point in the history
  • Loading branch information
C0ZEN committed Nov 22, 2020
1 parent c21e2f2 commit 650f214
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 31 deletions.
84 changes: 53 additions & 31 deletions src/IssueProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core';
import {context, getOctokit} from '@actions/github';
import {GetResponseTypeFromEndpointMethod} from '@octokit/types';
import {IssueLogger} from './classes/issue-logger';
import {Logger} from './classes/logger';
import {isLabeled} from './functions/is-labeled';
import {labelsToList} from './functions/labels-to-list';

Expand Down Expand Up @@ -56,6 +57,8 @@ export interface IssueProcessorOptions {
skipStalePrMessage: boolean;
}

const LOGGER: Logger = new Logger();

/***
* Handle processing of issues for staleness/closure.
*/
Expand Down Expand Up @@ -97,7 +100,7 @@ export class IssueProcessor {
}

if (this.options.debugOnly) {
core.warning(
LOGGER.warning(
'Executing in debug mode. Debug output will be written but no issues will be processed.'
);
}
Expand All @@ -109,14 +112,16 @@ export class IssueProcessor {
this.operationsLeft -= 1;

if (issues.length <= 0) {
core.info('No more issues found to process. Exiting.');
LOGGER.info('---');
LOGGER.info('No more issues found to process. Exiting.');
return this.operationsLeft;
}

for (const issue of issues.values()) {
const ISSUE_LOGGER: IssueLogger = new IssueLogger(issue);
const isPr = !!issue.pull_request;

core.info(
ISSUE_LOGGER.info(
`Found issue: issue #${issue.number} last updated ${issue.updated_at} (is pr? ${isPr})`
);

Expand All @@ -143,17 +148,17 @@ export class IssueProcessor {
const shouldMarkWhenStale = this.options.daysBeforeStale > -1;

if (!staleMessage && shouldMarkWhenStale) {
core.info(`Skipping ${issueType} due to empty stale message`);
ISSUE_LOGGER.info(`Skipping ${issueType} due to empty stale message`);
continue;
}

if (issue.state === 'closed') {
core.info(`Skipping ${issueType} because it is closed`);
ISSUE_LOGGER.info(`Skipping ${issueType} because it is closed`);
continue; // don't process closed issues
}

if (issue.locked) {
core.info(`Skipping ${issueType} because it is locked`);
ISSUE_LOGGER.info(`Skipping ${issueType} because it is locked`);
continue; // don't process locked issues
}

Expand All @@ -162,7 +167,9 @@ export class IssueProcessor {
isLabeled(issue, exemptLabel)
)
) {
core.info(`Skipping ${issueType} because it has an exempt label`);
ISSUE_LOGGER.info(
`Skipping ${issueType} because it has an exempt label`
);
continue; // don't process exempt issues
}

Expand All @@ -177,7 +184,7 @@ export class IssueProcessor {

// determine if this issue needs to be marked stale first
if (!isStale && shouldBeStale && shouldMarkWhenStale) {
core.info(
ISSUE_LOGGER.info(
`Marking ${issueType} stale because it was last updated on ${issue.updated_at} and it does not have a stale label`
);
await this.markStale(issue, staleMessage, staleLabel, skipMessage);
Expand All @@ -186,7 +193,7 @@ export class IssueProcessor {

// process the issue if it was marked stale
if (isStale) {
core.info(`Found a stale ${issueType}`);
ISSUE_LOGGER.info(`Found a stale ${issueType}`);
await this.processStaleIssue(
issue,
issueType,
Expand All @@ -198,7 +205,7 @@ export class IssueProcessor {
}

if (this.operationsLeft <= 0) {
core.warning('Reached max number of operations to process. Exiting.');
LOGGER.warning('Reached max number of operations to process. Exiting.');
return 0;
}

Expand All @@ -214,27 +221,32 @@ export class IssueProcessor {
closeMessage?: string,
closeLabel?: string
) {
const ISSUE_LOGGER: IssueLogger = new IssueLogger(issue);
const markedStaleOn: string =
(await this.getLabelCreationDate(issue, staleLabel)) || issue.updated_at;
core.info(`Issue #${issue.number} marked stale on: ${markedStaleOn}`);
ISSUE_LOGGER.info(
`Issue #${issue.number} marked stale on: ${markedStaleOn}`
);

const issueHasComments: boolean = await this.hasCommentsSince(
issue,
markedStaleOn
);
core.info(
ISSUE_LOGGER.info(
`Issue #${issue.number} has been commented on: ${issueHasComments}`
);

const issueHasUpdate: boolean = IssueProcessor.updatedSince(
issue.updated_at,
this.options.daysBeforeClose
);
core.info(`Issue #${issue.number} has been updated: ${issueHasUpdate}`);
ISSUE_LOGGER.info(
`Issue #${issue.number} has been updated: ${issueHasUpdate}`
);

// should we un-stale this issue?
if (this.options.removeStaleWhenUpdated && issueHasComments) {
core.info(
ISSUE_LOGGER.info(
`Issue #${issue.number} is no longer stale. Removing stale label.`
);
await this.removeLabel(issue, staleLabel);
Expand All @@ -246,12 +258,12 @@ export class IssueProcessor {
}

if (!issueHasComments && !issueHasUpdate) {
core.info(
ISSUE_LOGGER.info(
`Closing ${issueType} because it was last updated on ${issue.updated_at}`
);
await this.closeIssue(issue, closeMessage, closeLabel);
} else {
core.info(
ISSUE_LOGGER.info(
`Stale ${issueType} is not old enough to close yet (hasComments? ${issueHasComments}, hasUpdate? ${issueHasUpdate})`
);
}
Expand All @@ -262,7 +274,9 @@ export class IssueProcessor {
issue: Issue,
sinceDate: string
): Promise<boolean> {
core.info(
const ISSUE_LOGGER: IssueLogger = new IssueLogger(issue);

ISSUE_LOGGER.info(
`Checking for comments on issue #${issue.number} since ${sinceDate}`
);

Expand All @@ -278,7 +292,7 @@ export class IssueProcessor {
comment.user.type === 'User' && comment.user.login !== context.actor
);

core.info(
ISSUE_LOGGER.info(
`Comments not made by actor or another bot: ${filteredComments.length}`
);

Expand All @@ -301,7 +315,7 @@ export class IssueProcessor {
});
return comments.data;
} catch (error) {
core.error(`List issue comments error: ${error.message}`);
LOGGER.error(`List issue comments error: ${error.message}`);
return Promise.resolve([]);
}
}
Expand All @@ -326,7 +340,7 @@ export class IssueProcessor {
);
return issueResult.data;
} catch (error) {
core.error(`Get issues for repo error: ${error.message}`);
LOGGER.error(`Get issues for repo error: ${error.message}`);
return Promise.resolve([]);
}
}
Expand All @@ -338,7 +352,9 @@ export class IssueProcessor {
staleLabel: string,
skipMessage: boolean
): Promise<void> {
core.info(`Marking issue #${issue.number} as stale`);
const ISSUE_LOGGER: IssueLogger = new IssueLogger(issue);

ISSUE_LOGGER.info(`Marking issue #${issue.number} as stale`);

this.staleIssues.push(issue);

Expand All @@ -362,7 +378,7 @@ export class IssueProcessor {
body: staleMessage
});
} catch (error) {
core.error(`Error creating a comment: ${error.message}`);
ISSUE_LOGGER.error(`Error creating a comment: ${error.message}`);
}
}

Expand All @@ -374,7 +390,7 @@ export class IssueProcessor {
labels: [staleLabel]
});
} catch (error) {
core.error(`Error adding a label: ${error.message}`);
ISSUE_LOGGER.error(`Error adding a label: ${error.message}`);
}
}

Expand All @@ -384,7 +400,9 @@ export class IssueProcessor {
closeMessage?: string,
closeLabel?: string
): Promise<void> {
core.info(`Closing issue #${issue.number} for being stale`);
const ISSUE_LOGGER: IssueLogger = new IssueLogger(issue);

ISSUE_LOGGER.info(`Closing issue #${issue.number} for being stale`);

this.closedIssues.push(issue);

Expand All @@ -403,7 +421,7 @@ export class IssueProcessor {
body: closeMessage
});
} catch (error) {
core.error(`Error creating a comment: ${error.message}`);
ISSUE_LOGGER.error(`Error creating a comment: ${error.message}`);
}
}

Expand All @@ -416,7 +434,7 @@ export class IssueProcessor {
labels: [closeLabel]
});
} catch (error) {
core.error(`Error adding a label: ${error.message}`);
ISSUE_LOGGER.error(`Error adding a label: ${error.message}`);
}
}

Expand All @@ -428,13 +446,15 @@ export class IssueProcessor {
state: 'closed'
});
} catch (error) {
core.error(`Error updating an issue: ${error.message}`);
ISSUE_LOGGER.error(`Error updating an issue: ${error.message}`);
}
}

// Remove a label from an issue
private async removeLabel(issue: Issue, label: string): Promise<void> {
core.info(`Removing label from issue #${issue.number}`);
const ISSUE_LOGGER: IssueLogger = new IssueLogger(issue);

ISSUE_LOGGER.info(`Removing label from issue #${issue.number}`);

this.removedLabelIssues.push(issue);

Expand All @@ -452,7 +472,7 @@ export class IssueProcessor {
name: encodeURIComponent(label) // A label can have a "?" in the name
});
} catch (error) {
core.error(`Error removing a label: ${error.message}`);
ISSUE_LOGGER.error(`Error removing a label: ${error.message}`);
}
}

Expand All @@ -462,7 +482,9 @@ export class IssueProcessor {
issue: Issue,
label: string
): Promise<string | undefined> {
core.info(`Checking for label on issue #${issue.number}`);
const ISSUE_LOGGER: IssueLogger = new IssueLogger(issue);

ISSUE_LOGGER.info(`Checking for label on issue #${issue.number}`);

this.operationsLeft -= 1;

Expand Down
78 changes: 78 additions & 0 deletions src/classes/issue-logger.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {Issue} from '../IssueProcessor';
import {IssueLogger} from './issue-logger';
import * as core from '@actions/core';

describe('IssueLogger', (): void => {
let issue: Issue;
let issueLogger: IssueLogger;

beforeEach((): void => {
issue = {
number: 8
} as Issue;
issueLogger = new IssueLogger(issue);
});

describe('warning()', (): void => {
let message: string;

let coreWarningSpy: jest.SpyInstance;

beforeEach((): void => {
message = 'dummy-message';

coreWarningSpy = jest.spyOn(core, 'warning').mockImplementation();
});

it('should log a warning with the given message and with the issue number as prefix', (): void => {
expect.assertions(2);

issueLogger.warning(message);

expect(coreWarningSpy).toHaveBeenCalledTimes(1);
expect(coreWarningSpy).toHaveBeenCalledWith('[#8] dummy-message');
});
});

describe('info()', (): void => {
let message: string;

let coreInfoSpy: jest.SpyInstance;

beforeEach((): void => {
message = 'dummy-message';

coreInfoSpy = jest.spyOn(core, 'info').mockImplementation();
});

it('should log an information with the given message and with the issue number as prefix', (): void => {
expect.assertions(2);

issueLogger.info(message);

expect(coreInfoSpy).toHaveBeenCalledTimes(1);
expect(coreInfoSpy).toHaveBeenCalledWith('[#8] dummy-message');
});
});

describe('error()', (): void => {
let message: string;

let coreErrorSpy: jest.SpyInstance;

beforeEach((): void => {
message = 'dummy-message';

coreErrorSpy = jest.spyOn(core, 'error').mockImplementation();
});

it('should log an error with the given message and with the issue number as prefix', (): void => {
expect.assertions(2);

issueLogger.error(message);

expect(coreErrorSpy).toHaveBeenCalledTimes(1);
expect(coreErrorSpy).toHaveBeenCalledWith('[#8] dummy-message');
});
});
});
Loading

0 comments on commit 650f214

Please sign in to comment.