prepareReleaseMaterials.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. // Prepare release materials like mail, release note
  20. const commander = require('commander');
  21. const fse = require('fs-extra');
  22. const pathTool = require('path');
  23. const https = require('https');
  24. commander
  25. .usage('[options]')
  26. .description([
  27. 'Generate source release'
  28. ].join('\n'))
  29. .option(
  30. '--rcversion <version>',
  31. 'Release version'
  32. )
  33. .option(
  34. '--commit <commit>',
  35. 'Hash of commit'
  36. )
  37. .option(
  38. '--repo <repo>',
  39. 'Repo'
  40. )
  41. .option(
  42. '--out <out>',
  43. 'Out directory. Default to be tmp/release-mail'
  44. )
  45. .parse(process.argv);
  46. const outDir = pathTool.resolve(process.cwd(), commander.out || 'tmp/release-materials');
  47. const releaseCommit = commander.commit;
  48. if (!releaseCommit) {
  49. throw new Error('Release commit is required');
  50. }
  51. const repo = commander.repo || 'apache/echarts';
  52. let rcVersion = commander.rcversion + '';
  53. if (rcVersion.startsWith('v')) { // tag may have v prefix, v5.1.0
  54. rcVersion = rcVersion.substr(1);
  55. }
  56. if (rcVersion.indexOf('-rc.') < 0) {
  57. throw new Error('Only rc version is accepeted.');
  58. }
  59. const parts = /(\d+)\.(\d+)\.(\d+)\-rc\.(\d+)/.exec(rcVersion);
  60. if (!parts) {
  61. throw new Error(`Invalid version number ${rcVersion}`);
  62. }
  63. const major = +parts[1];
  64. const minor = +parts[2];
  65. const patch = +parts[3];
  66. const rc = +parts[4];
  67. const stableVersion = `${major}.${minor}.${patch}`;
  68. const releaseFullName = `Apache ECharts ${stableVersion} (release candidate ${rc})`;
  69. console.log('[Release Repo] ' + repo);
  70. console.log('[Release Verion] ' + rcVersion);
  71. console.log('[Release Commit] ' + releaseCommit);
  72. console.log('[Release Name] ' + releaseFullName);
  73. const voteTpl = fse.readFileSync(pathTool.join(__dirname, './template/vote-release.tpl'), 'utf-8');
  74. const announceTpl = fse.readFileSync(pathTool.join(__dirname, './template/announce-release.tpl'), 'utf-8');
  75. const voteUntil = new Date(+new Date() + (72 + 12) * 3600 * 1000); // 3.5 day.
  76. fse.ensureDirSync(outDir);
  77. fse.writeFileSync(
  78. pathTool.resolve(outDir, 'vote.txt'),
  79. voteTpl.replace(/{{ECHARTS_RELEASE_VERSION}}/g, rcVersion)
  80. .replace(/{{ECHARTS_RELEASE_VERSION_FULL_NAME}}/g, releaseFullName)
  81. .replace(/{{ECHARTS_RELEASE_COMMIT}}/g, releaseCommit)
  82. .replace(/{{VOTE_UNTIL}}/g, voteUntil.toISOString()),
  83. 'utf-8'
  84. );
  85. fse.writeFileSync(
  86. pathTool.resolve(outDir, 'announce.txt'),
  87. announceTpl.replace(/{{ECHARTS_RELEASE_VERSION}}/g, stableVersion)
  88. .replace(/{{ECHARTS_RELEASE_COMMIT}}/g, releaseCommit),
  89. 'utf-8'
  90. );
  91. // Fetch RELEASE_NOTE
  92. https.get({
  93. hostname: 'api.github.com',
  94. path: `/repos/${repo}/releases`,
  95. headers: {
  96. 'User-Agent': 'NodeJS'
  97. }
  98. }, function (res) {
  99. console.log(`https://api.github.com/repos/${repo}/releases`);
  100. if (res.statusCode !== 200) {
  101. console.error(`Failed to fetch releases ${res.statusCode}`);
  102. res.resume();
  103. return;
  104. }
  105. res.setEncoding('utf8');
  106. let rawData = '';
  107. res.on('data', (chunk) => {
  108. rawData += chunk;
  109. });
  110. res.on('end', () => {
  111. let releaseNote = '';
  112. const releases = JSON.parse(rawData);
  113. const found = releases.find(release => release.name === rcVersion);
  114. if (!found) {
  115. throw 'Can\'t found release';
  116. }
  117. else {
  118. releaseNote = found.body.trim();
  119. if (!releaseNote) {
  120. throw 'Release description is empty';
  121. }
  122. }
  123. const firstLine = releaseNote.split('\n')[0];
  124. if (firstLine.indexOf(stableVersion) < 0) {
  125. // Add version if release note is not start with version.
  126. }
  127. releaseNote = `## ${stableVersion}\n\n${releaseNote}`;
  128. fse.writeFileSync(
  129. pathTool.resolve(outDir, 'RELEASE_NOTE.txt'),
  130. releaseNote,
  131. 'utf-8'
  132. );
  133. });
  134. }).on('error', (e) => {
  135. throw e;
  136. });