UNPKG

2.49 kBJavaScriptView Raw
1const {parse} = require('url');
2const GitHubApi = require('github');
3const parseGithubUrl = require('parse-github-url');
4const deployOnce = require('travis-deploy-once');
5const SemanticReleaseError = require('@semantic-release/error');
6const resolveConfig = require('./lib/resolve-config');
7
8module.exports = async function(pluginConfig, {options: {branch, repositoryUrl}}) {
9 const {githubToken, githubUrl, githubApiPathPrefix, travisUrl} = resolveConfig(pluginConfig);
10 if (process.env.TRAVIS !== 'true') {
11 throw new SemanticReleaseError(
12 'semantic-release didn’t run on Travis CI and therefore a new version won’t be published.\nYou can customize this behavior using "verifyConditions" plugins: git.io/sr-plugins',
13 'ENOTRAVIS'
14 );
15 }
16
17 if (
18 Object.prototype.hasOwnProperty.call(process.env, 'TRAVIS_PULL_REQUEST') &&
19 process.env.TRAVIS_PULL_REQUEST !== 'false'
20 ) {
21 throw new SemanticReleaseError(
22 'This test run was triggered by a pull request and therefore a new version won’t be published.',
23 'EPULLREQUEST'
24 );
25 }
26
27 if (branch !== process.env.TRAVIS_BRANCH) {
28 throw new SemanticReleaseError(
29 `This test run was triggered on the branch ${
30 process.env.TRAVIS_BRANCH
31 }, while semantic-release is configured to only publish from ${branch}.\nYou can customize this behavior using the "branch" option: git.io/sr-options`,
32 'EBRANCHMISMATCH'
33 );
34 }
35
36 const {name: repo, owner} = parseGithubUrl(repositoryUrl);
37 if (!owner || !repo) {
38 throw new SemanticReleaseError(
39 `The git repository URL ${repositoryUrl} is not a valid Github URL.`,
40 'EINVALIDGITURL'
41 );
42 }
43
44 let {port, protocol, hostname: host} = githubUrl ? parse(githubUrl) : {};
45 protocol = (protocol || '').split(':')[0] || null;
46
47 const github = new GitHubApi({port, protocol, host, pathPrefix: githubApiPathPrefix});
48 github.authenticate({type: 'token', token: githubToken});
49
50 const {data: {private: pro}} = await github.repos.get({owner, repo});
51
52 const result = await deployOnce({travisOpts: {pro, enterprise: travisUrl}});
53
54 if (result === null) {
55 throw new SemanticReleaseError(
56 'This test run is not the build leader and therefore a new version won’t be published.',
57 'ENOBUILDLEADER'
58 );
59 }
60
61 if (result === false) {
62 throw new SemanticReleaseError(
63 'In this test run not all jobs passed and therefore a new version won’t be published.',
64 'EOTHERSFAILED'
65 );
66 }
67};