|
| 1 | +const core = require('@actions/core') |
| 2 | +const github = require('@actions/github') |
| 3 | + |
| 4 | +async function run() { |
| 5 | + try { |
| 6 | + |
| 7 | + // ----- |
| 8 | + // --------------- GET DATA |
| 9 | + // ---------- |
| 10 | + |
| 11 | + // Get octokit |
| 12 | + const githubToken = core.getInput('github-token') |
| 13 | + const octokit = github.getOctokit(githubToken) |
| 14 | + |
| 15 | + // Get data |
| 16 | + const { data: pullRequest } = await octokit.pulls.get({ |
| 17 | + owner: github.context.payload.repository.owner.login, |
| 18 | + repo: github.context.payload.repository.name, |
| 19 | + pull_number: github.context.payload.pull_request.number |
| 20 | + }) |
| 21 | + const { data: commits } = await octokit.pulls.listCommits({ |
| 22 | + owner: github.context.payload.repository.owner.login, |
| 23 | + repo: github.context.payload.repository.name, |
| 24 | + pull_number: github.context.payload.pull_request.number |
| 25 | + }) |
| 26 | + |
| 27 | + // ----- |
| 28 | + // --------------- GET INPUT |
| 29 | + // ---------- |
| 30 | + |
| 31 | + const commitMessageBodyMaxLength = parseInt(core.getInput('commit-message-body-max-length')) |
| 32 | + const commitMessageBodyMinLength = parseInt(core.getInput('commit-message-body-min-length')) |
| 33 | + const commitMessageSubjectMaxLength = parseInt(core.getInput('commit-message-subject-max-length')) |
| 34 | + const commitMessageSubjectMinLength = parseInt(core.getInput('commit-message-subject-min-length')) |
| 35 | + |
| 36 | + const prohibitUnknownCommitAuthors = (core.getInput('prohibit-unknown-commit-authors') == 'true') |
| 37 | + const prohibitUnknownCommitCommitters = (core.getInput('prohibit-unknown-commit-committers') == 'true') |
| 38 | + const prohibitUnsignedCommits = (core.getInput('prohibit-unsigned-commits') == 'true') |
| 39 | + |
| 40 | + const regexBranchName = RegExp(core.getInput('re-branch-name')) |
| 41 | + const regexCommitAuthorEmail = RegExp(core.getInput('re-commit-author-email')) |
| 42 | + const regexCommitAuthorName = RegExp(core.getInput('re-commit-author-name')) |
| 43 | + const regexCommitCommitterEmail = RegExp(core.getInput('re-commit-committer-email')) |
| 44 | + const regexCommitCommitterName = RegExp(core.getInput('re-commit-committer-name')) |
| 45 | + const regexCommitMessageBody = RegExp(core.getInput('re-commit-message-body'), 's') |
| 46 | + const regexCommitMessageSplit = RegExp(core.getInput('re-commit-message-split'), 's') |
| 47 | + const regexCommitMessageSubject = RegExp(core.getInput('re-commit-message-subject')) |
| 48 | + const regexPullRequestTitle = RegExp(core.getInput('re-pull-request-title')) |
| 49 | + |
| 50 | + // ----- |
| 51 | + // --------------- CHECK DATA |
| 52 | + // ---------- |
| 53 | + |
| 54 | + core.info(`Pull request title: ${pullRequest.title}`) |
| 55 | + core.info(`Branch name: ${pullRequest.head.ref}`) |
| 56 | + |
| 57 | + // Check pull request title |
| 58 | + if (!regexPullRequestTitle.test(pullRequest.title)) |
| 59 | + core.setFailed("Pull Request title does not match regex") |
| 60 | + |
| 61 | + // Check branch name |
| 62 | + if (!regexBranchName.test(pullRequest.head.ref)) |
| 63 | + core.setFailed("Branch name does not match regex") |
| 64 | + |
| 65 | + // Check all commits |
| 66 | + commits.forEach(commit => { |
| 67 | + // Split commit message |
| 68 | + let matches = regexCommitMessageSplit.exec(commit.commit.message) |
| 69 | + let commitMessageSubject = matches[1] |
| 70 | + let commitMessageBody = matches[2] |
| 71 | + |
| 72 | + console.log('-----') |
| 73 | + core.info(`Commit hash: ${commit.sha}`) |
| 74 | + core.info(`Commit author email: ${commit.commit.author.email}`) |
| 75 | + core.info(`Commit author name: ${commit.commit.author.name}`) |
| 76 | + core.info(`Commit author GitHub account: ${commit.author}`) |
| 77 | + core.info(`Commit committer email: ${commit.commit.committer.email}`) |
| 78 | + core.info(`Commit committer name: ${commit.commit.committer.name}`) |
| 79 | + core.info(`Commit committer GitHub account: ${commit.committer}`) |
| 80 | + core.info(`Commit has valid signature: ${commit.commit.verification.verified}`) |
| 81 | + core.info(`Commit message subject: ${commitMessageSubject}`) |
| 82 | + core.info(`Commit message body: ${commitMessageBody}`) |
| 83 | + |
| 84 | + // Check commit author |
| 85 | + if (prohibitUnknownCommitAuthors && commit.author == null) |
| 86 | + core.setFailed(`Commit author does not exist on GitHub (${commit.sha.substr(0, 7)})`) |
| 87 | + |
| 88 | + if (!regexCommitAuthorEmail.test(commit.commit.author.email)) |
| 89 | + core.setFailed(`Commit author email does not match regex (${commit.sha.substr(0, 7)})`) |
| 90 | + |
| 91 | + if (!regexCommitAuthorName.test(commit.commit.author.name)) |
| 92 | + core.setFailed(`Commit author name does not match regex (${commit.sha.substr(0, 7)})`) |
| 93 | + |
| 94 | + // Check commit committer |
| 95 | + if (prohibitUnknownCommitCommitters && commit.committer == null) |
| 96 | + core.setFailed(`Commit committer does not exist on GitHub (${commit.sha.substr(0, 7)})`) |
| 97 | + |
| 98 | + if (!regexCommitCommitterEmail.test(commit.commit.committer.email)) |
| 99 | + core.setFailed(`Commit committer email does not match regex (${commit.sha.substr(0, 7)})`) |
| 100 | + |
| 101 | + if (!regexCommitCommitterName.test(commit.commit.committer.name)) |
| 102 | + core.setFailed(`Commit committer name does not match regex (${commit.sha.substr(0, 7)})`) |
| 103 | + |
| 104 | + // Check for valid signature |
| 105 | + if (prohibitUnsignedCommits && !commit.commit.verification.verified) |
| 106 | + core.setFailed(`Commit has no valid signature (${commit.sha.substr(0, 7)})`) |
| 107 | + |
| 108 | + // Check commit message subject |
| 109 | + if (commitMessageSubjectMinLength != -1 && commitMessageSubject.length < commitMessageSubjectMinLength) |
| 110 | + core.setFailed(`Commit message subject is too short (${commit.sha.substr(0, 7)})`) |
| 111 | + |
| 112 | + if (commitMessageSubjectMaxLength != -1 && commitMessageSubject.length > commitMessageSubjectMaxLength) |
| 113 | + core.setFailed(`Commit message subject is too long (${commit.sha.substr(0, 7)})`) |
| 114 | + |
| 115 | + if (!regexCommitMessageSubject.test(commitMessageSubject)) |
| 116 | + core.setFailed(`Commit message subject does not match regex (${commit.sha.substr(0, 7)})`) |
| 117 | + |
| 118 | + // Check commit message body |
| 119 | + if (commitMessageBody != null) { |
| 120 | + commitMessageBody.split("\n").forEach(function (line, index) { |
| 121 | + if (commitMessageBodyMinLength != -1 && line.length < commitMessageBodyMinLength) |
| 122 | + core.setFailed(`Commit message body line ${(index+1).toString()} is too short (${commit.sha.substr(0, 7)})`) |
| 123 | + |
| 124 | + if (commitMessageBodyMaxLength != -1 && line.length > commitMessageBodyMaxLength) |
| 125 | + core.setFailed(`Commit message body line ${(index+1).toString()} is too long (${commit.sha.substr(0, 7)})`) |
| 126 | + }) |
| 127 | + |
| 128 | + if (!regexCommitMessageBody.test(commitMessageBody)) |
| 129 | + core.setFailed(`Commit message body does not match regex (${commit.sha.substr(0, 7)})`) |
| 130 | + } |
| 131 | + }) |
| 132 | + } catch (error) { |
| 133 | + core.setFailed(error.message); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +run() |
0 commit comments