Skip to content

Commit

Permalink
move checkApproved in utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
chitacan committed Oct 4, 2019
1 parent bfd943b commit 4eea5cf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 25 deletions.
31 changes: 10 additions & 21 deletions lib/index.js
@@ -1,48 +1,37 @@
const {getInput} = require('@actions/core')
const {getInput, warning, setFailed} = require('@actions/core')
const {getProjectsAndBoards, getTaskDetails, moveTask} = require('./knz')
const {extractRef} = require('./utils')
const {extractRef, checkApproved} = require('./utils')
const moveTo = getInput('MOVE_TO', {required: true})

const checkApproved = () => {
if (process.env.GITHUB_EVENT_NAME !== 'pull_request_review') {
return false
}

const path = process.env.GITHUB_EVENT_PATH
const event = require(path)
return event.review.state === 'approved'
}

const main = async () => {
const branch = await extractRef()
const [type, prefixedTask] = branch.split('/')
if (!prefixedTask.startsWith('KB-')) {
console.log(`not kanbanize branch.(${branch}) ignore.`)
process.exit(0)
warning(`not kanbanize branch.(${branch}) ignore.`)
return
}

const taskid = prefixedTask.replace('KB-', '')
const {projects: [{id}]} = await getProjectsAndBoards()

const task = await getTaskDetails(id, taskid)
if (!task) {
console.error('cannot find task on kanbanize. error.')
process.exit(1)
setFailed(`cannot find task (${taskid}) on kanbanize.`)
return
}

const {boardid, columnname} = task
if (moveTo === columnname) {
console.log(`task already on ${columnname}. ignore.`)
process.exit(0)
warning(`task already on ${columnname}. ignore.`)
return
}

if (moveTo === 'approved' && !checkApproved()) {
console.log(`review type is not approved. ignore.`)
process.exit(0)
warning('approve condition should be satisfied. ignore')
return
}

await moveTask(boardid, taskid, moveTo)
process.exit(0)
}

main()
55 changes: 51 additions & 4 deletions lib/utils.js
@@ -1,15 +1,20 @@
const {GitHub} = require('@actions/github')
const {getInput} = require('@actions/core')
const {getInput, warning} = require('@actions/core')

const {GITHUB_REPOSITORY, GITHUB_EVENT_NAME, GITHUB_HEAD_REF, GITHUB_REF} = process.env
const {GITHUB_REPOSITORY, GITHUB_EVENT_NAME, GITHUB_HEAD_REF, GITHUB_REF, GITHUB_EVENT_PATH} = process.env
const token = getInput('GITHUB_TOKEN', {required: true})
const octokit = new GitHub(token)

exports.getSlug = getSlug = () => {
const [owner, repo] = GITHUB_REPOSITORY.split('/')
return {owner, repo}
}

exports.extractRef = async () => {
if (GITHUB_EVENT_NAME === 'pull_request_review') {
const [owner, repo] = GITHUB_REPOSITORY.split('/')
const slug = getSlug()
const [,, pull_number,] = GITHUB_REF.split('/')
const {data: {head: {ref}}} = await octokit.pulls.get({owner, repo, pull_number})
const {data: {head: {ref}}} = await octokit.pulls.get({...slug, pull_number})
return ref
} else if (GITHUB_HEAD_REF) {
return GITHUB_HEAD_REF
Expand All @@ -18,3 +23,45 @@ exports.extractRef = async () => {
return branch.join('/')
}
}

exports.checkApproved = async () => {
if (GITHUB_EVENT_NAME !== 'pull_request_review') {
warning(`[checkApproved] GITHUB_EVENT_NAME should be pull_request_review. (${GITHUB_EVENT_NAME})`)
return false
}

const {review: {state}, pull_request: {number, base: {ref}}} = require(GITHUB_EVENT_PATH)
const slug = getSlug()

if (state === 'approved') {
warning(`[checkApproved] pull_request_review.state should be approved. (${state})`)
return false
}

const approvedCount = await octokit.pulls.listReviews({
...slug,
pull_number: number
})
.then(({data}) => {
const approvedUsers = data.filter(d => d.state === 'APPROVED').map(d => d.user.login)
// uniq users
return Array.from(new Set(approvedUsers)).length
})

const requiredApproveCount = await octokit.repos.getBranchProtection({
...slug,
branch: ref,
headers: {
accept: 'application/vnd.github.luke-cage-preview+json'
}
})
.then(({data}) => data.required_pull_request_reviews.required_approving_review_count)
.catch(() => 1)

if (approvedCount < requiredApproveCount) {
warning(`[checkApproved] approvedCount should be greater or equal to requiredApproveCount. (approvedCount: ${approvedCount}, requiredApproveCount: ${requiredApproveCount})`)
return false
}

return true
}

0 comments on commit 4eea5cf

Please sign in to comment.