UNPKG

@rajzik/lumos

Version:

Centralized CLI for JavaScript and TypeScript dev tools.

67 lines (66 loc) 2.99 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const github_1 = require("@actions/github"); const core_1 = require("@beemo/core"); const conventional_changelog_beemo_1 = require("@rajzik/conventional-changelog-beemo"); const path_1 = __importDefault(require("path")); const createGitHubClient_1 = require("../helpers/createGitHubClient"); const { GITHUB_REF } = process.env; const parsePullRequestId = (githubRef) => { const result = /refs\/pull\/(\d+)\/merge/g.exec(githubRef); if (!result) throw new Error('Reference not found.'); const [, pullRequestId] = result; return pullRequestId; }; // Primarily used within CI jobs class PullRequestChecksScript extends core_1.Script { blueprint() { return {}; } bootstrap() { this.pullRequest = parsePullRequestId(GITHUB_REF); if (this.pullRequest === 'false') { return; } const { repo: { owner, repo }, } = github_1.context; this.owner = owner; this.repo = repo; this.client = createGitHubClient_1.createGitHubClient(); this.task('Checking for invalid lock file changes', this.checkForInvalidLocks); this.task('Checking pull request title', this.checkForConventionalTitle); } async checkForInvalidLocks() { const { data: files } = await this.client.pulls.listFiles({ owner: this.owner, repo: this.repo, pull_number: Number(this.pullRequest), }); const fileNames = new Set(files.map(file => path_1.default.basename(file.filename))); const hasPackageChanges = fileNames.has('package.json'); // this.tool.log('Changed files: %s', Array.from(fileNames).join(', ')); if (fileNames.has('package-lock.json') && !hasPackageChanges) { throw new Error('Your PR contains changes to package-lock.json, but not package.json.'); } else if (fileNames.has('npm-shrinkwrap.json') && !hasPackageChanges) { throw new Error('Your PR contains changes to npm-shrinkwrap.json, but not package.json.'); } else if (fileNames.has('yarn.lock') && !hasPackageChanges) { throw new Error('Your PR contains changes to yarn.lock, but not package.json.'); } } async checkForConventionalTitle() { const { data: pr } = await this.client.pulls.get({ owner: this.owner, repo: this.repo, pull_number: Number(this.pullRequest), }); if (!conventional_changelog_beemo_1.checkCommitFormat(pr.title)) { throw new Error('Pull request title requires a conventional changelog prefix. More information: https://github.com/rajzik/conventional-changelog-beemo#commit-message-format'); } } } exports.default = PullRequestChecksScript;