Skip to content

Commit

Permalink
refactor(operations): create a method to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
C0ZEN committed May 1, 2021
1 parent 4cf12ba commit 96bee75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
31 changes: 13 additions & 18 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ class IssuesProcessor {
return __awaiter(this, void 0, void 0, function* () {
const issueLogger = new issue_logger_1.IssueLogger(issue);
issueLogger.info(`Checking for label on $$type`);
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_a = this._statistics) === null || _a === void 0 ? void 0 : _a.incrementFetchedItemsEventsCount();
const options = this.client.issues.listEvents.endpoint.merge({
owner: github_1.context.repo.owner,
Expand Down Expand Up @@ -606,8 +605,7 @@ class IssuesProcessor {
}
if (!skipMessage) {
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_a = this._statistics) === null || _a === void 0 ? void 0 : _a.incrementAddedItemsComment(issue);
yield this.client.issues.createComment({
owner: github_1.context.repo.owner,
Expand All @@ -621,8 +619,7 @@ class IssuesProcessor {
}
}
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_b = this._statistics) === null || _b === void 0 ? void 0 : _b.incrementAddedItemsLabel(issue);
(_c = this._statistics) === null || _c === void 0 ? void 0 : _c.incrementStaleItemsCount(issue);
yield this.client.issues.addLabels({
Expand All @@ -649,8 +646,7 @@ class IssuesProcessor {
}
if (closeMessage) {
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_a = this._statistics) === null || _a === void 0 ? void 0 : _a.incrementAddedItemsComment(issue);
yield this.client.issues.createComment({
owner: github_1.context.repo.owner,
Expand All @@ -665,8 +661,7 @@ class IssuesProcessor {
}
if (closeLabel) {
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_b = this._statistics) === null || _b === void 0 ? void 0 : _b.incrementAddedItemsLabel(issue);
yield this.client.issues.addLabels({
owner: github_1.context.repo.owner,
Expand All @@ -680,8 +675,7 @@ class IssuesProcessor {
}
}
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_c = this._statistics) === null || _c === void 0 ? void 0 : _c.incrementClosedItemsCount(issue);
yield this.client.issues.update({
owner: github_1.context.repo.owner,
Expand All @@ -703,8 +697,7 @@ class IssuesProcessor {
return;
}
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_a = this._statistics) === null || _a === void 0 ? void 0 : _a.incrementFetchedPullRequestsCount();
const pullRequest = yield this.client.pulls.get({
owner: github_1.context.repo.owner,
Expand Down Expand Up @@ -735,8 +728,7 @@ class IssuesProcessor {
const branch = pullRequest.head.ref;
issueLogger.info(`Deleting the branch "${chalk_1.default.cyan(branch)}" from closed $$type`);
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_a = this._statistics) === null || _a === void 0 ? void 0 : _a.incrementDeletedBranchesCount();
yield this.client.git.deleteRef({
owner: github_1.context.repo.owner,
Expand All @@ -760,8 +752,7 @@ class IssuesProcessor {
return;
}
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
(_a = this._statistics) === null || _a === void 0 ? void 0 : _a.incrementDeletedItemsLabelsCount(issue);
yield this.client.issues.removeLabel({
owner: github_1.context.repo.owner,
Expand Down Expand Up @@ -859,6 +850,10 @@ class IssuesProcessor {
}
});
}
_consumeIssueOperation(issue) {
this._operations.consumeOperation();
issue.operations.consumeOperation();
}
}
exports.IssuesProcessor = IssuesProcessor;

Expand Down
36 changes: 16 additions & 20 deletions src/classes/issues-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ export class IssuesProcessor {

// Grab comments for an issue since a given date
async listIssueComments(
issueNumber: number,
sinceDate: string
issueNumber: Readonly<number>,
sinceDate: Readonly<string>
): Promise<IComment[]> {
// Find any comments since date on the given issue
try {
Expand Down Expand Up @@ -447,8 +447,7 @@ export class IssuesProcessor {

issueLogger.info(`Checking for label on $$type`);

this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementFetchedItemsEventsCount();
const options = this.client.issues.listEvents.endpoint.merge({
owner: context.repo.owner,
Expand Down Expand Up @@ -592,8 +591,7 @@ export class IssuesProcessor {

if (!skipMessage) {
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementAddedItemsComment(issue);
await this.client.issues.createComment({
owner: context.repo.owner,
Expand All @@ -607,8 +605,7 @@ export class IssuesProcessor {
}

try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementAddedItemsLabel(issue);
this._statistics?.incrementStaleItemsCount(issue);
await this.client.issues.addLabels({
Expand Down Expand Up @@ -639,8 +636,7 @@ export class IssuesProcessor {

if (closeMessage) {
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementAddedItemsComment(issue);
await this.client.issues.createComment({
owner: context.repo.owner,
Expand All @@ -655,8 +651,7 @@ export class IssuesProcessor {

if (closeLabel) {
try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementAddedItemsLabel(issue);
await this.client.issues.addLabels({
owner: context.repo.owner,
Expand All @@ -670,8 +665,7 @@ export class IssuesProcessor {
}

try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementClosedItemsCount(issue);
await this.client.issues.update({
owner: context.repo.owner,
Expand All @@ -694,8 +688,7 @@ export class IssuesProcessor {
}

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

try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementDeletedBranchesCount();
await this.client.git.deleteRef({
owner: context.repo.owner,
Expand Down Expand Up @@ -765,8 +757,7 @@ export class IssuesProcessor {
}

try {
this._operations.consumeOperation();
issue.operations.consumeOperation();
this._consumeIssueOperation(issue);
this._statistics?.incrementDeletedItemsLabelsCount(issue);
await this.client.issues.removeLabel({
owner: context.repo.owner,
Expand Down Expand Up @@ -891,4 +882,9 @@ export class IssuesProcessor {
this._statistics?.incrementDeletedCloseItemsLabelsCount(issue);
}
}

private _consumeIssueOperation(issue: Readonly<Issue>): void {
this._operations.consumeOperation();
issue.operations.consumeOperation();
}
}

0 comments on commit 96bee75

Please sign in to comment.