function detectIssueId (ref) {
    const reg = /^(feature|fix|hotfix|fixbug|bugfix|improve|solve|update)-(\d+)/i
    const matches = reg.exec(ref) || []
    return matches[2]
}

module.exports = robot => {
    robot.on('pull_request.opened', async context => {
        const repo = context.repo()
        const pr = context.payload.pull_request
        const branchName = pr.head.ref

        pr.body = pr.body || ''
        console.log('check pr: ', pr.number)
        // link with issue
        const issueId = detectIssueId(branchName)
        if (issueId) {
            const ctn = pr.body.replace(/resolve #(\d+)/g, '')
            const newIssue = context.issue({
                number: pr.number,
                body: ctn + `\n\n resolve #${issueId} `
            })

            console.log('link issue: ', issueId)
            // create comment
            context.github.issues.edit(newIssue)
        }

        // check milestone
        const isMilestoneSetted = !!pr.milestone
        if (!isMilestoneSetted) {
            context.github.issues.getMilestones(context.repo({
                state: 'open',
                sort: 'due_on',
                direction: 'desc',
                per_page: 3
            })).then(({ data: milestones })=> {
                console.log('milestone list:', milestones)
                if (milestones && milestones[0]) {
                    const curMilestone = milestones[0]

                    console.log('set milestone:', curMilestone)
                    // set milestone
                    context.github.issues.edit(context.issue({
                        number: pr.number,
                        milestone: curMilestone.number
                    }))
                }
            })
        }

        // set assignee
        const hasAssignee = !!pr.assignees.length
        if (!hasAssignee) {
            context.github.issues.edit(context.issue({
                number: pr.number,
                assignee: pr.user.login
            }))
        }

        // default set wip status
        const wipTitle = pr.title.replace(/\bwip\b/g, '') + ' wip'
        context.github.issues.edit(context.issue({
            number: pr.number,
            title: wipTitle
        }))
    })
}

@@ -2013,232 +2013,8 @@
    %7D
-%0A%0A        // default set wip status%0A        const wipTitle = pr.title.replace(/%5Cbwip%5Cb/g, '') + ' wip'%0A        context.github.issues.edit(context.issue(%7B%0A            number: pr.number,%0A            title: wipTitle%0A        %7D))
 %0A   

remove default wip on title'