Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logs): add a new log when an issue consumed at least one operation #386

Merged
merged 41 commits into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c21e2f2
Merge pull request #1 from actions/main
C0ZEN Nov 22, 2020
c913fa4
Merge pull request #2 from actions/main
C0ZEN Jan 16, 2021
9cf9bf2
Merge pull request #3 from actions/main
C0ZEN Jan 16, 2021
f11f623
Merge pull request #4 from actions/main
C0ZEN Jan 16, 2021
0f1457a
Merge pull request #5 from actions/main
C0ZEN Jan 17, 2021
fef6333
Merge pull request #6 from actions/main
C0ZEN Jan 18, 2021
e1ba62c
Merge pull request #7 from actions/main
C0ZEN Jan 23, 2021
abab4a6
Merge pull request #8 from actions/main
C0ZEN Feb 9, 2021
920683c
Merge pull request #9 from actions/main
C0ZEN Feb 13, 2021
a98474b
Merge pull request #10 from actions/main
C0ZEN Feb 18, 2021
915ed27
Merge pull request #11 from actions/main
C0ZEN Mar 1, 2021
4b097b3
Merge pull request #12 from actions/main
C0ZEN Mar 1, 2021
5575ade
docs(only-labels): enhance the docs and fix duplicate (#341)
C0ZEN Mar 4, 2021
b806328
build(deps-dev): bump @typescript-eslint/eslint-plugin (#342)
dependabot[bot] Mar 4, 2021
c6b2c30
build(deps): bump @octokit/rest from 18.3.0 to 18.3.2 (#350)
dependabot[bot] Mar 4, 2021
5a8ec1e
Merge pull request #14 from actions/main
C0ZEN Mar 4, 2021
6c29691
test: add more coverage for the stale label behaviour (#352) (#15)
C0ZEN Mar 5, 2021
d485d43
test: add more coverage for the stale label behaviour (#352) (#17)
C0ZEN Mar 9, 2021
25621f6
test: add more coverage for the stale label behaviour (#352) (#18)
C0ZEN Mar 9, 2021
6257990
docs(operations-per-run): improve the doc for this option
C0ZEN Mar 10, 2021
c38af01
feat(logs): add a new log when an issue consumed at least one operation
C0ZEN Mar 10, 2021
173c540
Merge branch 'main' into feature/log-operations-per-issue
C0ZEN Mar 10, 2021
d0d072d
chore(readme): improve the operations per run
C0ZEN Mar 17, 2021
6d7053e
chore(readme): improve the operations per run
C0ZEN Mar 17, 2021
4236350
chore(readme): improve the operations per run
C0ZEN Mar 17, 2021
e7b8a38
chore(readme): improve the operations per run
C0ZEN Mar 17, 2021
304e352
chore(readme): improve the operations per run
C0ZEN Mar 17, 2021
c78dbd5
chore(readme): improve the operations per run
C0ZEN Mar 19, 2021
9a29082
Merge branch 'main' into feature/log-operations-per-issue
C0ZEN Apr 27, 2021
ee8f729
Typo in how to perform check for specific labels (#357)
romainr Apr 28, 2021
94499a2
feat(any-of-labels): add 2 new options to customize for issues/PRs (#…
C0ZEN Apr 28, 2021
8d43c50
feat(logs): enhance the logs for assignees and milestones (#382)
C0ZEN Apr 28, 2021
8025ac6
Merge branch 'main' into feature/log-operations-per-issue
C0ZEN Apr 28, 2021
72abf2b
Merge branch 'main' into feature/log-operations-per-issue
C0ZEN Apr 28, 2021
2570e86
style(typo): fix typo plural issue
C0ZEN Apr 28, 2021
ee986e5
Merge branch 'main' into feature/log-operations-per-issue
C0ZEN Apr 30, 2021
385b1d6
style(naming): rename two methods
C0ZEN Apr 30, 2021
783c103
chore(error): remove a potential useless throw of error
C0ZEN Apr 30, 2021
7fae5fa
style(naming): rename one method
C0ZEN Apr 30, 2021
4cf12ba
refactor(issue): change the way to count the operations
C0ZEN Apr 30, 2021
96bee75
refactor(operations): create a method to reduce code duplication
C0ZEN May 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
105 changes: 59 additions & 46 deletions README.md

Large diffs are not rendered by default.

1,925 changes: 1,907 additions & 18 deletions dist/index.js

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions src/classes/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {IIssuesProcessorOptions} from '../interfaces/issues-processor-options';
import {ILabel} from '../interfaces/label';
import {IMilestone} from '../interfaces/milestone';
import {IsoDateString} from '../types/iso-date-string';
import {Operations} from './operations';

export class Issue implements IIssue {
export class Issue extends Operations implements IIssue {
private readonly _options: IIssuesProcessorOptions;
readonly title: string;
readonly number: number;
Expand All @@ -21,10 +22,23 @@ export class Issue implements IIssue {
readonly assignees: IAssignee[];
isStale: boolean;

get isPullRequest(): boolean {
return isPullRequest(this);
}

get staleLabel(): string {
return this._getStaleLabel();
}

get hasAssignees(): boolean {
return this.assignees.length > 0;
}

constructor(
options: Readonly<IIssuesProcessorOptions>,
issue: Readonly<IIssue>
) {
super();
this._options = options;
this.title = issue.title;
this.number = issue.number;
Expand All @@ -40,18 +54,6 @@ export class Issue implements IIssue {
this.isStale = isLabeled(this, this.staleLabel);
}

get isPullRequest(): boolean {
return isPullRequest(this);
}

get staleLabel(): string {
return this._getStaleLabel();
}

get hasAssignees(): boolean {
return this.assignees.length > 0;
}

private _getStaleLabel(): string {
return this.isPullRequest
? this._options.stalePrLabel
Expand Down
55 changes: 45 additions & 10 deletions src/classes/issues-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {Issue} from './issue';
import {IssueLogger} from './loggers/issue-logger';
import {Logger} from './loggers/logger';
import {Milestones} from './milestones';
import {Operations} from './operations';
import {StaleOperations} from './stale-operations';
import {Statistics} from './statistics';

/***
Expand All @@ -35,8 +35,23 @@ export class IssuesProcessor {
return millisSinceLastUpdated <= daysInMillis;
}

private static _endIssueProcessing(issue: Issue): void {
const consumedOperationsCount: number = issue.getConsumedOperationsCount();

if (consumedOperationsCount > 0) {
const issueLogger: IssueLogger = new IssueLogger(issue);

issueLogger.info(
chalk.cyan(consumedOperationsCount),
`operation${
consumedOperationsCount > 1 ? '' : ''
C0ZEN marked this conversation as resolved.
Show resolved Hide resolved
} consumed for this $$type`
);
}
}

private readonly _logger: Logger = new Logger();
private readonly _operations: Operations;
private readonly _operations: StaleOperations;
private readonly _statistics: Statistics | undefined;
readonly client: InstanceType<typeof GitHub>;
readonly options: IIssuesProcessorOptions;
Expand All @@ -48,7 +63,7 @@ export class IssuesProcessor {
constructor(options: IIssuesProcessorOptions) {
this.options = options;
this.client = getOctokit(this.options.repoToken);
this._operations = new Operations(this.options);
this._operations = new StaleOperations(this.options);

this._logger.info(chalk.yellow('Starting the stale action process...'));

Expand Down Expand Up @@ -76,7 +91,7 @@ export class IssuesProcessor {
chalk.green('No more issues found to process. Exiting...')
);
this._statistics
?.setOperationsLeft(this._operations.getUnconsumedOperationsCount())
?.setOperationsLeft(this._operations.getOperationsLeftCount())
.logStats();

return this._operations.getOperationsLeftCount();
Expand Down Expand Up @@ -134,6 +149,8 @@ export class IssuesProcessor {
issueLogger.info(
`Skipping this $$type because it doesn't have all the required labels`
);

IssuesProcessor._endIssueProcessing(issue);
continue; // Don't process issues without all of the required labels
} else {
issueLogger.info(
Expand All @@ -152,16 +169,19 @@ export class IssuesProcessor {

if (!staleMessage && shouldMarkAsStale) {
issueLogger.info(`Skipping $$type due to empty stale message`);
IssuesProcessor._endIssueProcessing(issue);
continue;
}

if (issue.state === 'closed') {
issueLogger.info(`Skipping $$type because it is closed`);
IssuesProcessor._endIssueProcessing(issue);
continue; // don't process closed issues
}

if (issue.locked) {
issueLogger.info(`Skipping $$type because it is locked`);
IssuesProcessor._endIssueProcessing(issue);
continue; // don't process locked issues
}

Expand All @@ -181,11 +201,10 @@ export class IssuesProcessor {
// Expecting that GitHub will always set a creation date on the issues and PRs
// But you never know!
if (!isValidDate(createdAt)) {
core.setFailed(
new Error(
`Invalid issue field: "created_at". Expected a valid date`
)
);
IssuesProcessor._endIssueProcessing(issue);
const errorMessage = `Invalid issue field: "created_at". Expected a valid date`;
core.setFailed(new Error(errorMessage));
throw new Error(errorMessage);
}

issueLogger.info(
Expand All @@ -199,6 +218,7 @@ export class IssuesProcessor {
`Skipping $$type because it was created before the specified start date`
);

IssuesProcessor._endIssueProcessing(issue);
continue; // don't process issues which were created before the start date
}
}
Expand Down Expand Up @@ -226,6 +246,7 @@ export class IssuesProcessor {
}

issueLogger.info(`Skipping $$type because it has an exempt label`);
IssuesProcessor._endIssueProcessing(issue);
continue; // don't process exempt issues
}

Expand All @@ -240,18 +261,21 @@ export class IssuesProcessor {
issueLogger.info(
`Skipping $$type because it does not have any of the required labels`
);
IssuesProcessor._endIssueProcessing(issue);
continue; // don't process issues without any of the required labels
}

const milestones: Milestones = new Milestones(this.options, issue);

if (milestones.shouldExemptMilestones()) {
IssuesProcessor._endIssueProcessing(issue);
continue; // don't process exempt milestones
}

const assignees: Assignees = new Assignees(this.options, issue);

if (assignees.shouldExemptAssignees()) {
IssuesProcessor._endIssueProcessing(issue);
continue; // don't process exempt assignees
}

Expand Down Expand Up @@ -285,9 +309,11 @@ export class IssuesProcessor {
closeLabel
);
}

IssuesProcessor._endIssueProcessing(issue);
}

if (this._operations.hasOperationsLeft()) {
if (!this._operations.hasOperationsLeft()) {
C0ZEN marked this conversation as resolved.
Show resolved Hide resolved
this._logger.warning(
chalk.yellowBright('No more operations left! Exiting...')
);
Expand Down Expand Up @@ -388,6 +414,7 @@ export class IssuesProcessor {
issueLogger.info(`Checking for label on $$type`);

this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementFetchedIssuesEventsCount();
const options = this.client.issues.listEvents.endpoint.merge({
owner: context.repo.owner,
Expand Down Expand Up @@ -526,6 +553,7 @@ export class IssuesProcessor {
if (!skipMessage) {
try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementAddedComment();
await this.client.issues.createComment({
owner: context.repo.owner,
Expand All @@ -540,6 +568,7 @@ export class IssuesProcessor {

try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementAddedLabel();
this._statistics?.incrementStaleIssuesCount();
await this.client.issues.addLabels({
Expand Down Expand Up @@ -571,6 +600,7 @@ export class IssuesProcessor {
if (closeMessage) {
try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementAddedComment();
await this.client.issues.createComment({
owner: context.repo.owner,
Expand All @@ -586,6 +616,7 @@ export class IssuesProcessor {
if (closeLabel) {
try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementAddedLabel();
await this.client.issues.addLabels({
owner: context.repo.owner,
Expand All @@ -600,6 +631,7 @@ export class IssuesProcessor {

try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementClosedIssuesCount();
await this.client.issues.update({
owner: context.repo.owner,
Expand All @@ -623,6 +655,7 @@ export class IssuesProcessor {

try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementFetchedPullRequestsCount();
const pullRequest = await this.client.pulls.get({
owner: context.repo.owner,
Expand Down Expand Up @@ -660,6 +693,7 @@ export class IssuesProcessor {

try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementDeletedBranchesCount();
await this.client.git.deleteRef({
owner: context.repo.owner,
Expand All @@ -686,6 +720,7 @@ export class IssuesProcessor {

try {
this._operations.consumeOperation();
issue.consumeOperation();
this._statistics?.incrementDeletedLabelsCount();
await this.client.issues.removeLabel({
owner: context.repo.owner,
Expand Down
49 changes: 49 additions & 0 deletions src/classes/operations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {Operations} from './operations';

describe('Operations', (): void => {
let operations: Operations;

describe('consumeOperation()', (): void => {
beforeEach((): void => {
operations = new Operations();
});

it('should increase the count of operation consume by 1', (): void => {
expect.assertions(1);
operations.consumeOperation();

const result = operations.getConsumedOperationsCount();

expect(result).toStrictEqual(1);
});
});

describe('consumeOperations()', (): void => {
beforeEach((): void => {
operations = new Operations();
});

it('should increase the count of operation consume by the provided quantity', (): void => {
expect.assertions(1);
operations.consumeOperations(8);

const result = operations.getConsumedOperationsCount();

expect(result).toStrictEqual(8);
});
});

describe('getConsumedOperationsCount()', (): void => {
beforeEach((): void => {
operations = new Operations();
});

it('should return 0 by default', (): void => {
expect.assertions(1);

const result = operations.getConsumedOperationsCount();

expect(result).toStrictEqual(0);
});
});
});
24 changes: 4 additions & 20 deletions src/classes/operations.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import {IIssuesProcessorOptions} from '../interfaces/issues-processor-options';

export class Operations {
private readonly _options: IIssuesProcessorOptions;
private _operationsLeft;

constructor(options: Readonly<IIssuesProcessorOptions>) {
this._options = options;
this._operationsLeft = this._options.operationsPerRun;
}
protected _operationsConsumed = 0;

consumeOperation(): Operations {
return this.consumeOperations(1);
}

consumeOperations(quantity: Readonly<number>): Operations {
this._operationsLeft -= quantity;
this._operationsConsumed += quantity;

return this;
}

getUnconsumedOperationsCount(): number {
return this._options.operationsPerRun - this._operationsLeft;
}

hasOperationsLeft(): boolean {
return this._operationsLeft <= 0;
}

getOperationsLeftCount(): number {
return this._operationsLeft;
getConsumedOperationsCount(): number {
return this._operationsConsumed;
}
}