Skip to content

Commit

Permalink
Merge a5a8a05 into baaa9d5
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-nemiro-clearscale committed Dec 5, 2020
2 parents baaa9d5 + a5a8a05 commit f6c4633
Show file tree
Hide file tree
Showing 40 changed files with 1,683 additions and 302 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,3 @@ dist/

# Visual Studio Code
.vscode/

# Documents - in work
docs/
34 changes: 27 additions & 7 deletions src/Actions/BaseAction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DefaultConditionOptions,
WhenCommentTextConditionOptions,
WhenContainsRequestsToReviewConditionOptions,
WhenFileContentConditionOptions,
WhenFilePathConditionOptions,
WhenInternalConditionOptions,
Expand All @@ -16,7 +17,8 @@ import {
WhenCommitMessageCondition,
WhenCondition,
WhenConditionPredicate,
WhenContainsConflicts,
WhenContainsConflictsCondition,
WhenContainsRequestsToReviewCondition,
WhenDescriptionCondition,
WhenFileContentCondition,
WhenFileCountCondition,
Expand All @@ -27,7 +29,7 @@ import {
WhenMilestoneNameCondition,
WhenReviewStateCondition,
WhenSourceBranchNameCondition,
WhenState,
WhenStateCondition,
WhenTargetBranchNameCondition,
WhenTitleCondition,
} from '../Conditions';
Expand Down Expand Up @@ -198,7 +200,7 @@ export abstract class BaseAction implements IAction {

public whenHasConflicts(): DefaultConditionOptions {
const options = new DefaultConditionOptions(this);
const condition = new WhenContainsConflicts(true);
const condition = new WhenContainsConflictsCondition(true, options);

this.addCondition(condition);

Expand All @@ -207,7 +209,25 @@ export abstract class BaseAction implements IAction {

public whenHasNoConflicts(): DefaultConditionOptions {
const options = new DefaultConditionOptions(this);
const condition = new WhenContainsConflicts(false);
const condition = new WhenContainsConflictsCondition(false, options);

this.addCondition(condition);

return options;
}

public whenHasRequestsToReview(): WhenContainsRequestsToReviewConditionOptions {
const options = new WhenContainsRequestsToReviewConditionOptions(this);
const condition = new WhenContainsRequestsToReviewCondition(true, options);

this.addCondition(condition);

return options;
}

public whenHasNoRequestsToReview(): WhenContainsRequestsToReviewConditionOptions {
const options = new WhenContainsRequestsToReviewConditionOptions(this);
const condition = new WhenContainsRequestsToReviewCondition(false, options);

this.addCondition(condition);

Expand Down Expand Up @@ -252,7 +272,7 @@ export abstract class BaseAction implements IAction {

public whenOpen(): DefaultConditionOptions {
const options = new DefaultConditionOptions(this);
const condition = new WhenState('open');
const condition = new WhenStateCondition('open', options);

this.addCondition(condition);

Expand All @@ -261,7 +281,7 @@ export abstract class BaseAction implements IAction {

public whenClosed(): DefaultConditionOptions {
const options = new DefaultConditionOptions(this);
const condition = new WhenState('closed');
const condition = new WhenStateCondition('closed', options);

this.addCondition(condition);

Expand All @@ -270,7 +290,7 @@ export abstract class BaseAction implements IAction {

public whenWasMerged(): DefaultConditionOptions {
const options = new DefaultConditionOptions(this);
const condition = new WhenState('merged');
const condition = new WhenStateCondition('merged', options);

this.addCondition(condition);

Expand Down
1 change: 1 addition & 0 deletions src/ApiProviders/GitHubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class GitHubClient implements IApiProviderClient {
code: item.milestone.number,
name: item.milestone.title,
},
requestedReviewers: item.requested_reviewers?.map(this.convertDataToUser) || [],
createdDate: item.created_at && new Date(item.created_at),
updatedDate: item.updated_at && new Date(item.updated_at),
mergedDate: item.merged_at && new Date(item.merged_at),
Expand Down
2 changes: 2 additions & 0 deletions src/ApiProviders/Types/PullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ export type PullRequest = {

statusInfo: CacheableAction<PullRequestStatus>;

requestedReviewers?: Array<User>;

};
5 changes: 5 additions & 0 deletions src/ConditionOptions/Values/NumberConditionOptionsValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ export type NumberConditionOptionsValues = ConditionOptionsValues & {

greaterThanOrEqualTo?: number;

between?: {
from: number;
to: number;
}

};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StringTestValue } from '../../Types';
import { ConditionOptionsValues } from './ConditionOptionsValues';

export type WhenFileContentConditionOptionsValues = ConditionOptionsValues & {
Expand All @@ -6,4 +7,8 @@ export type WhenFileContentConditionOptionsValues = ConditionOptionsValues & {

onlyModifiedFiles?: boolean;

excludePaths?: StringTestValue;

includeOnlyPaths?: StringTestValue;

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { BaseConditionOptions } from './BaseConditionOptions';
import { ArrayConditionOptionsValues } from './Values';

export class WhenContainsRequestsToReviewConditionOptions extends BaseConditionOptions<ArrayConditionOptionsValues<string>> {

public oneOf(logins: Array<string>): WhenContainsRequestsToReviewConditionOptions {
this.values.oneOf = true;
this.values.all = false;
this.values.value = logins;

return this;
}

public all(logins: Array<string>): WhenContainsRequestsToReviewConditionOptions {
this.values.oneOf = false;
this.values.all = true;
this.values.value = logins;

return this;
}

}
21 changes: 21 additions & 0 deletions src/ConditionOptions/WhenFileContentConditionOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { StringTestValue } from '../Types';
import { BaseConditionOptions } from './BaseConditionOptions';
import { WhenFileContentConditionOptionsValues } from './Values';

Expand All @@ -17,4 +18,24 @@ export class WhenFileContentConditionOptions extends BaseConditionOptions<WhenFi
return this;
}

/**
* Ignores paths to matching files.
* This option takes precedence over `includeOnlyPaths`.
*/
public excludePaths(test: StringTestValue): WhenFileContentConditionOptions {
this.values.excludePaths = test;

return this;
}

/**
* Checks only matching files.
* If the path is excluded (`excludePaths`), then this option will not work.
*/
public includeOnlyPaths(test: StringTestValue): WhenFileContentConditionOptions {
this.values.includeOnlyPaths = test;

return this;
}

}
9 changes: 9 additions & 0 deletions src/ConditionOptions/WhenNumberConditionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ export class WhenNumberConditionOptions extends BaseConditionOptions<NumberCondi
return this;
}

public between(from: number, to: number): WhenNumberConditionOptions {
this.values.between = {
from,
to,
};

return this;
}

}
1 change: 1 addition & 0 deletions src/ConditionOptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export {
export { BaseConditionOptions } from './BaseConditionOptions';
export { DefaultConditionOptions } from './DefaultConditionOptions';
export { WhenCommentTextConditionOptions } from './WhenCommentTextConditionOptions';
export { WhenContainsRequestsToReviewConditionOptions } from './WhenContainsRequestsToReviewConditionOptions';
export { WhenFileContentConditionOptions } from './WhenFileContentConditionOptions';
export { WhenFilePathConditionOptions } from './WhenFilePathConditionOptions';
export { WhenInternalConditionOptions } from './WhenInternalConditionOptions';
Expand Down
42 changes: 10 additions & 32 deletions src/Conditions/WhenCommentCountCondition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WhenNumberConditionOptions } from '../ConditionOptions';
import { testNumberConditions } from '../Helpers';
import { LabelerContext } from '../LabelerContext';
import { DefaultPredicateType } from '../Types';
import { BaseCondition } from './BaseCondition';
Expand All @@ -10,38 +11,15 @@ export class WhenCommentCountCondition extends BaseCondition<DefaultPredicateTyp
}

public async test(context: LabelerContext): Promise<boolean> {
const {
equal,
greaterThan,
greaterThanOrEqualTo,
lessThan,
lessThanOrEqualTo,
} = super.getOptions(context);

const { comments: count } = await context.pullRequest.statusInfo.get();

// TODO: helper
if (equal && count === equal) {
return this.testResult(true, context);
}

if (greaterThan && count > greaterThan) {
return this.testResult(true, context);
}

if (greaterThanOrEqualTo && count >= greaterThanOrEqualTo) {
return this.testResult(true, context);
}

if (lessThan && count < lessThan) {
return this.testResult(true, context);
}

if (lessThanOrEqualTo && count <= lessThanOrEqualTo) {
return this.testResult(true, context);
}

return this.testResult(false, context);
const { comments } = await context.pullRequest.statusInfo.get();

return this.testResult(
testNumberConditions(
comments,
super.getOptions(context)
),
context
);
}

}
42 changes: 10 additions & 32 deletions src/Conditions/WhenCommitCountCondition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WhenNumberConditionOptions } from '../ConditionOptions';
import { testNumberConditions } from '../Helpers';
import { LabelerContext } from '../LabelerContext';
import { DefaultPredicateType } from '../Types';
import { BaseCondition } from './BaseCondition';
Expand All @@ -10,38 +11,15 @@ export class WhenCommitCountCondition extends BaseCondition<DefaultPredicateType
}

public async test(context: LabelerContext): Promise<boolean> {
const {
equal,
greaterThan,
greaterThanOrEqualTo,
lessThan,
lessThanOrEqualTo,
} = super.getOptions(context);

const { commits: count } = await context.pullRequest.statusInfo.get();

// TODO: helper
if (equal && count === equal) {
return this.testResult(true, context);
}

if (greaterThan && count > greaterThan) {
return this.testResult(true, context);
}

if (greaterThanOrEqualTo && count >= greaterThanOrEqualTo) {
return this.testResult(true, context);
}

if (lessThan && count < lessThan) {
return this.testResult(true, context);
}

if (lessThanOrEqualTo && count <= lessThanOrEqualTo) {
return this.testResult(true, context);
}

return this.testResult(false, context);
const { commits } = await context.pullRequest.statusInfo.get();

return this.testResult(
testNumberConditions(
commits,
super.getOptions(context)
),
context
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DefaultConditionOptions } from '../ConditionOptions';
import { LabelerContext } from '../LabelerContext';
import { BaseCondition } from './BaseCondition';

export class WhenContainsConflicts extends BaseCondition<boolean, DefaultConditionOptions> {
export class WhenContainsConflictsCondition extends BaseCondition<boolean, DefaultConditionOptions> {

constructor(hasConflicts: boolean, options?: DefaultConditionOptions) {
super(hasConflicts, options);
Expand Down
65 changes: 65 additions & 0 deletions src/Conditions/WhenContainsRequestsToReviewCondition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { User } from '../ApiProviders';
import { WhenContainsRequestsToReviewConditionOptions } from '../ConditionOptions';
import { LabelerContext } from '../LabelerContext';
import { BaseCondition } from './BaseCondition';

export class WhenContainsRequestsToReviewCondition extends BaseCondition<boolean, WhenContainsRequestsToReviewConditionOptions> {

constructor(hasConflicts: boolean, options?: WhenContainsRequestsToReviewConditionOptions) {
super(hasConflicts, options);
}

public test(context: LabelerContext): boolean {
const {
all,
oneOf,
value,
} = this.getOptions(context);

const users = [...value || []];
const hasUsers = !!users.length;

const {
requestedReviewers,
} = context.pullRequest;

if (hasUsers) {
if (oneOf) {
if (
requestedReviewers.filter(
(user: User): boolean => (
users.includes(user.login)
)
).length === 0
) {
return this.testResult(false, context);
}
}

if (all) {
if (
!requestedReviewers.every(
(user: User): boolean => (
users.includes(user.login)
)
)
) {
return this.testResult(false, context);
}
}
}

if (this.predicate) {
return this.testResult(
requestedReviewers.length > 0,
context
);
} else {
return this.testResult(
requestedReviewers.length === 0,
context
);
}
}

}

0 comments on commit f6c4633

Please sign in to comment.