@airbnb/nimbus
Version:
Centralized CLI for JavaScript and TypeScript dev tools.
59 lines (58 loc) • 2.75 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const core_1 = require("@beemo/core");
const conventional_changelog_beemo_1 = require("conventional-changelog-beemo");
const createGitHubClient_1 = __importDefault(require("../helpers/createGitHubClient"));
const { TRAVIS_PULL_REQUEST, TRAVIS_PULL_REQUEST_SLUG } = process.env;
// Primarily used within CI jobs
class PullRequestChecksScript extends core_1.Script {
blueprint() {
return {};
}
bootstrap() {
if (TRAVIS_PULL_REQUEST === 'false') {
return;
}
const [owner, repo] = TRAVIS_PULL_REQUEST_SLUG.split('/');
this.owner = owner;
this.repo = repo;
this.client = createGitHubClient_1.default();
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(TRAVIS_PULL_REQUEST),
});
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(TRAVIS_PULL_REQUEST),
});
// this.tool.log('PR title: %s', pr.title);
if (!conventional_changelog_beemo_1.checkCommitFormat(pr.title)) {
throw new Error('Pull request title requires a conventional changelog prefix. More information: https://github.com/beemojs/conventional-changelog-beemo#commit-message-format');
}
}
}
exports.default = PullRequestChecksScript;