Skip to content

Commit

Permalink
Add auto assign class
Browse files Browse the repository at this point in the history
  • Loading branch information
kentaro-m committed Mar 23, 2019
1 parent 72e5668 commit 1e73045
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 79 deletions.
45 changes: 13 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 74 additions & 0 deletions src/auto_assign.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Context } from 'probot'
import { chooseUsers, chooseUsersFromGroups } from './util'

interface AppConfig {
addReviewers: boolean,
addAssignees: boolean,
reviewers: string[],
assignees?: string[],
numberOfAssignees?: number,
numberOfReviewers: number,
skipKeywords?: string[],
useReviewGroups: boolean,
useAssigneeGroups: boolean,
reviewGroups: string[][],
assigneeGroups: string[][]
}

export default class AutoAssign {
private config: AppConfig
private context: Context
private reviewers: string[]

constructor (context: Context, config: AppConfig) {
this.config = config
this.context = context
this.reviewers = []
}

public async addReviewers () {
const owner = this.context.payload.repository.owner.login
const useGroups = this.config.hasOwnProperty('useReviewGroups') && this.config.useReviewGroups && Object.keys(this.config.reviewGroups).length > 0

if (useGroups) {
this.reviewers = chooseUsersFromGroups(owner, this.config.reviewGroups, this.config.numberOfReviewers)
} else {
this.reviewers = chooseUsers(owner, this.config.reviewers, this.config.numberOfReviewers)
}

if (this.config.addReviewers && this.reviewers) {
try {
const params = this.context.issue({ reviewers: this.reviewers })
const result: any = await this.context.github.pullRequests.createReviewRequest(params)
this.context.log(result)
} catch (error) {
this.context.log(error)
}
}
}

public async addAssignees () {
const owner = this.context.payload.repository.owner.login
const useGroups: boolean = this.config.hasOwnProperty('useAssigneeGroups') && this.config.useAssigneeGroups && Object.keys(this.config.assigneeGroups).length > 0

let assignees

if (useGroups) {
assignees = chooseUsersFromGroups(owner, this.config.assigneeGroups, this.config.numberOfAssignees || this.config.numberOfReviewers)
} else {
assignees = this.config.assignees ?
chooseUsers(owner, this.config.assignees, this.config.numberOfAssignees || this.config.numberOfReviewers)
: this.config.reviewers
}

if (assignees) {
try {
const params = this.context.issue({ assignees })
const result: any = await this.context.github.issues.addAssignees(params)
this.context.log(result)
} catch (error) {
this.context.log(error)
}
}
}
}
55 changes: 8 additions & 47 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import AutoAssign from './auto_assign'
import { includesSkipKeywords } from './util'
import { Context } from 'probot'
import { chooseUsers, chooseUsersFromGroups, includesSkipKeywords } from './util'

interface AppConfig {
addReviewers: boolean,
Expand Down Expand Up @@ -37,54 +38,14 @@ export async function handlePullRequest (context: Context): Promise<void> {
return
}

const reviewers: string[] = await chooseReviewers(context, config, [], context.payload.repository.owner.login)
await chooseAssignees(context, config, reviewers, context.payload.repository.owner.login)
}

export async function chooseReviewers (context: Context, config: AppConfig, reviewers: string[], owner: string) {
if (!config.reviewers && !config.reviewGroups) return []

const useGroups: boolean = config.useReviewGroups && Object.keys(config.reviewGroups).length > 0

if (useGroups) {
reviewers = chooseUsersFromGroups(owner, config.reviewGroups, config.numberOfReviewers)
} else {
reviewers = chooseUsers(owner, config.reviewers, config.numberOfReviewers)
}

if (config.addReviewers && reviewers) {
try {
const params = context.issue({ reviewers })
const result: any = await context.github.pullRequests.createReviewRequest(params)
context.log(result)
} catch (error) {
context.log(error)
}
}
return reviewers
}

export async function chooseAssignees (context: Context, config: AppConfig, reviewers: string[], owner: string) {
if (!config.addAssignees) return

let assignees: string[] = []
const useGroups: boolean = config.useAssigneeGroups && Object.keys(config.assigneeGroups).length > 0
// @ts-ignore
const autoAssign = new AutoAssign(context, config)

if (useGroups) {
assignees = chooseUsersFromGroups(owner, config.assigneeGroups, config.numberOfAssignees || config.numberOfReviewers)
} else if (reviewers.length > 0) {
assignees = config.assignees ?
chooseUsers(owner, config.assignees, config.numberOfAssignees || config.numberOfReviewers)
: reviewers
if (config.addReviewers) {
await autoAssign.addReviewers()
}

if (assignees) {
try {
const params = context.issue({ assignees })
const result: any = await context.github.issues.addAssignees(params)
context.log(result)
} catch (error) {
context.log(error)
}
if (config.addAssignees) {
await autoAssign.addAssignees()
}
}

0 comments on commit 1e73045

Please sign in to comment.