UNPKG

danger-plugin-pull-request

Version:

A Danger plugin to verify the completion of a pull request

104 lines (103 loc) 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.gitlab = exports.github = exports.checkDeleteSourceBranchChecked = exports.checkMergeSquashChecked = exports.checkPRSize = exports.checkTitle = exports.checkDescription = exports.checkAssignees = void 0; const messages = require("./messages"); /** * Checks if the pull request request has at least one assignee. * @param reporter The reporter function if the check fails. */ exports.checkAssignees = (reporter = fail) => { const hasAssignees = danger.gitlab ? !!danger.gitlab.mr.assignee : !!danger.github.pr.assignee; if (!hasAssignees) { reporter(messages.noAssignee()); } }; /** * Checks if the description of the PR is long enough. * @param minimumLength The minimum length of the description. * @param reporter The reporter function if the check fails. */ exports.checkDescription = (minimumLength, reporter = fail) => { const description = danger.gitlab ? danger.gitlab.mr.description : danger.github.pr.body; const descriptionIsLongEnough = description.length >= minimumLength; if (!description) { reporter(messages.noDescription()); } else if (!descriptionIsLongEnough) { reporter(messages.descriptionNotLongEnough(minimumLength)); } }; /** * Checks if the pull request title matches a given pattern. * @param pattern The pattern to match. * @param patternMessage readable pattern message for reporter message. * @param reporter The reporter function if the check fails. */ exports.checkTitle = (pattern, patternMessage, reporter = fail) => { const title = danger.gitlab ? danger.gitlab.mr.title : danger.github.pr.title; const titleIsValid = pattern.test(title); const isWIP = /(^WIP|wip|\[WIP\]|\[wip\])|(^%s$)/.test(title); if (isWIP) { message(messages.wip()); } if (!titleIsValid) { reporter(messages.titleDoeNotMatch(patternMessage || pattern)); } }; /** * Checks if a pull request is too big. * @param maxSize The maximum changed files count to be valid * @param reporter The reporter function if the check fails. */ exports.checkPRSize = (maxSize, reporter = fail) => { const changesCount = Number(danger.gitlab ? danger.gitlab.mr.changes_count : danger.github.pr.changed_files); if (changesCount > maxSize) { reporter(messages.bigPR()); } }; /** * Checks if merge squash checked. (GitLab only) * @param reporter The reporter function if the check fails. */ exports.checkMergeSquashChecked = (reporter = fail) => { const squashChecked = danger.gitlab.mr.squash; if (!squashChecked) { reporter(messages.squashNotChecked()); } }; /** * Checks if delete source branch checked. (GitLab only) * @param reporter The reporter function if the check fails. */ exports.checkDeleteSourceBranchChecked = (reporter = fail) => { const deleteSourceBranchChecked = danger.gitlab.mr.force_remove_source_branch; if (!deleteSourceBranchChecked) { reporter(messages.deleteSourceBranchhNotChecked()); } }; /** * Github available plugins */ exports.github = { checkAssignees: exports.checkAssignees, checkDescription: exports.checkDescription, checkTitle: exports.checkTitle, checkPRSize: exports.checkPRSize, }; /** * GitLab available plugins */ exports.gitlab = { checkAssignees: exports.checkAssignees, checkDescription: exports.checkDescription, checkTitle: exports.checkTitle, checkPRSize: exports.checkPRSize, checkMergeSquashChecked: exports.checkMergeSquashChecked, checkDeleteSourceBranchChecked: exports.checkDeleteSourceBranchChecked, };