/ script / vsts / get-release-version.js
get-release-version.js
 1  const path = require('path');
 2  const request = require('request-promise-native');
 3  
 4  const repositoryRootPath = path.resolve(__dirname, '..', '..');
 5  const appMetadata = require(path.join(repositoryRootPath, 'package.json'));
 6  
 7  const yargs = require('yargs');
 8  const argv = yargs
 9    .usage('Usage: $0 [options]')
10    .help('help')
11    .describe('nightly', 'Indicates that a nightly version should be produced')
12    .wrap(yargs.terminalWidth()).argv;
13  
14  function getAppName(version) {
15    const match = version.match(/\d+\.\d+\.\d+(-([a-z]+)(\d+|-\w{4,})?)?$/);
16    if (!match) {
17      throw new Error(`Found incorrectly formatted Atom version ${version}`);
18    } else if (match[2]) {
19      return `atom-${match[2]}`;
20    }
21  
22    return 'atom';
23  }
24  
25  async function getReleaseVersion() {
26    let releaseVersion = process.env.ATOM_RELEASE_VERSION || appMetadata.version;
27    if (argv.nightly) {
28      const releases = await request({
29        url: 'https://api.github.com/repos/atom/atom-nightly-releases/releases',
30        headers: {
31          Accept: 'application/vnd.github.v3+json',
32          'User-Agent': 'Atom Release Build'
33        },
34        json: true
35      });
36  
37      let releaseNumber = 0;
38      const baseVersion = appMetadata.version.split('-')[0];
39      if (releases && releases.length > 0) {
40        const latestRelease = releases.find(r => !r.draft);
41        const versionMatch = latestRelease.tag_name.match(
42          /^v?(\d+\.\d+\.\d+)-nightly(\d+)$/
43        );
44  
45        if (versionMatch && versionMatch[1] === baseVersion) {
46          releaseNumber = parseInt(versionMatch[2]) + 1;
47        }
48      }
49  
50      releaseVersion = `${baseVersion}-nightly${releaseNumber}`;
51    }
52  
53    // Set our ReleaseVersion build variable and update VSTS' build number to
54    // include the version.  Writing these strings to stdout causes VSTS to set
55    // the associated variables.
56    console.log(
57      `##vso[task.setvariable variable=ReleaseVersion;isOutput=true]${releaseVersion}`
58    );
59    if (!process.env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER) {
60      // Only set the build number on non-PR builds as it causes build errors when
61      // non-admins send PRs to the repo
62      console.log(
63        `##vso[build.updatebuildnumber]${releaseVersion}+${
64          process.env.BUILD_BUILDID
65        }`
66      );
67    }
68  
69    // Write out some variables that indicate whether artifacts should be uploaded
70    const buildBranch = process.env.BUILD_SOURCEBRANCHNAME;
71    const isReleaseBranch =
72      process.env.IS_RELEASE_BRANCH ||
73      argv.nightly ||
74      buildBranch.match(/\d\.\d+-releases/) !== null;
75    const isSignedZipBranch =
76      !isReleaseBranch &&
77      (process.env.IS_SIGNED_ZIP_BRANCH ||
78        buildBranch.startsWith('electron-') ||
79        (buildBranch === 'master' &&
80          !process.env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER));
81  
82    console.log(
83      `##vso[task.setvariable variable=AppName;isOutput=true]${getAppName(
84        releaseVersion
85      )}`
86    );
87    console.log(
88      `##vso[task.setvariable variable=IsReleaseBranch;isOutput=true]${isReleaseBranch}`
89    );
90    console.log(
91      `##vso[task.setvariable variable=IsSignedZipBranch;isOutput=true]${isSignedZipBranch}`
92    );
93  }
94  
95  getReleaseVersion();