Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#440 feat: added support for task lists in PR
- Loading branch information
Showing
9 changed files
with
1,150 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
5.7.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import Check, { getPayloadFn } from './Check' | ||
import { PULL_REQUEST } from '../model/GithubEvents' | ||
import { logger } from '../../common/debug' | ||
|
||
const CHECK_TYPE = 'pullrequesttasks' | ||
const CONTEXT = 'zappr/pr/tasks' | ||
const info = logger(CHECK_TYPE, 'info') | ||
const error = logger(CHECK_TYPE, 'error') | ||
const debug = logger(CHECK_TYPE) | ||
const createStatePayload = getPayloadFn(CONTEXT) | ||
|
||
export function countOpenTasks(prBody, repoName = "", prNo = "") { | ||
const regex = /(-|\*) \[(\s|x)\]/g; | ||
let count = 0; | ||
let match; | ||
|
||
while ((match = regex.exec(prBody)) !== null) { | ||
// This is necessary to avoid infinite loops with zero-width matches | ||
if (match.index === regex.lastIndex) { | ||
regex.lastIndex++; | ||
} | ||
|
||
let task = match[0]; | ||
let marker = match[2]; | ||
|
||
debug(`${repoName}#${prNo}: Processing task: ${task}`); | ||
|
||
if (marker === " ") { | ||
count++; | ||
} | ||
} | ||
|
||
info(`${repoName}#${prNo}: PR has ${count} open tasks.`); | ||
|
||
return count; | ||
} | ||
|
||
export default class PullRequestTasks extends Check { | ||
static TYPE = CHECK_TYPE; | ||
static CONTEXT = CONTEXT; | ||
static HOOK_EVENTS = [PULL_REQUEST]; | ||
static NAME = 'Pull request tasks check' | ||
|
||
constructor(github) { | ||
super() | ||
this.github = github; | ||
} | ||
|
||
async execute(config, hookPayload, token) { | ||
const {action, repository, number, pull_request} = hookPayload | ||
const repoOwner = repository.owner.login | ||
const repoName = repository.name | ||
const fullName = repository.full_name | ||
let status; | ||
|
||
try { | ||
if (pull_request.state === 'open' && ['opened', 'edited', 'reopened'].indexOf(action) !== -1) { | ||
let openTaskCount = countOpenTasks(pull_request.body, repoName, number); | ||
let msg; | ||
|
||
if (openTaskCount > 0) { | ||
info(`${fullName}#${number}: Failed the PR due to open tasks.`); | ||
status = createStatePayload(`PR has ${openTaskCount} open tasks.`, 'failure'); | ||
} else { | ||
msg = `PR has no open tasks.`; | ||
info(`${fullName}#${number}: ${msg}`); | ||
status = createStatePayload(msg); | ||
} | ||
|
||
await this.github.setCommitStatus(repoOwner, repoName, pull_request.head.sha, status, token); | ||
} | ||
} catch (e) { | ||
error(`${fullName}#${number}: Could not execute Pull Request Tasks check`, e) | ||
status = createStatePayload(`Error: ${e.message}`, 'error') | ||
await this.github.setCommitStatus(repoOwner, repoName, pull_request.head.sha, status, token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.