|
| 1 | +const { Toolkit } = require('actions-toolkit') |
| 2 | +const getConfig = require('./utils/config') |
| 3 | + |
| 4 | +const CONFIG_FILENAME = 'pr-lint.yml' |
| 5 | + |
| 6 | +const defaults = { |
| 7 | + projects: ['PROJ'], |
| 8 | + check_title: true, |
| 9 | + check_branch: false, |
| 10 | + ignore_case: false |
| 11 | +} |
| 12 | + |
| 13 | +Toolkit.run( |
| 14 | + async tools => { |
| 15 | + const repoInfo = { |
| 16 | + owner: tools.context.payload.repository.owner.login, |
| 17 | + repo: tools.context.payload.repository.name |
| 18 | + } |
| 19 | + const config = { |
| 20 | + ...defaults, |
| 21 | + ...(await getConfig(tools.github, CONFIG_FILENAME, repoInfo)) |
| 22 | + } |
| 23 | + |
| 24 | + const title = config.ignore_case ? |
| 25 | + tools.context.payload.pull_request.title.toLowerCase() : |
| 26 | + tools.context.payload.pull_request.title |
| 27 | + |
| 28 | + const head_branch = config.ignore_case ? |
| 29 | + tools.context.payload.pull_request.head_ref_name.toLowerCase() : |
| 30 | + tools.context.payload.pull_request.head_ref_name |
| 31 | + |
| 32 | + const projects = config.projects.map(project => config.ignore_case ? project.toLowerCase() : project) |
| 33 | + const title_passed = (() => { |
| 34 | + if (config.check_title) { |
| 35 | + // check the title matches [PROJECT-1234] somewhere |
| 36 | + if (!projects.some(project => title.match(new RegExp('\\[' + project + '-\\d*\\]')))) { |
| 37 | + tools.log('PR title does not contain approved project') |
| 38 | + return false |
| 39 | + } |
| 40 | + } |
| 41 | + return true |
| 42 | + })() |
| 43 | + |
| 44 | + const branch_passed = (() => { |
| 45 | + // check the branch matches PROJECT-1234 or PROJECT_1234 somewhere |
| 46 | + if (config.check_branch) { |
| 47 | + if (!projects.some(project => head_branch.match(new RegExp(project + '[-_]\\d*')))) { |
| 48 | + tools.log('PR branch does not contain an approved project') |
| 49 | + return false |
| 50 | + } |
| 51 | + } |
| 52 | + return true |
| 53 | + })() |
| 54 | + |
| 55 | + const statuses = [title_passed, branch_passed] |
| 56 | + |
| 57 | + if (statuses.some(status => status === false )){ |
| 58 | + tools.exit.failure("PR Linting Failed") |
| 59 | + } else { |
| 60 | + tools.exit.success() |
| 61 | + } |
| 62 | + }, |
| 63 | + { event: ['pull_request.opened', 'pull_request.edited', 'pull_request.synchronize'], secrets: ['GITHUB_TOKEN'] } |
| 64 | +) |
0 commit comments