|
| 1 | +const {Plugin} = require('release-it'); |
| 2 | +const {Octokit} = require('@octokit/rest'); |
| 3 | +const pkg = require('../package.json'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +class WandbPlugin extends Plugin { |
| 7 | + async init() { |
| 8 | + this.octokit = new Octokit({ |
| 9 | + baseUrl: 'https://api.github.com', |
| 10 | + auth: process.env['GITHUB_TOKEN'], |
| 11 | + userAgent: `local/${pkg.version}`, |
| 12 | + log: null, |
| 13 | + request: { |
| 14 | + timeout: 10000, |
| 15 | + }, |
| 16 | + }); |
| 17 | + this.registerPrompts({ |
| 18 | + release_notes: { |
| 19 | + type: 'editor', |
| 20 | + name: 'release_notes', |
| 21 | + default: this.defaultNotes.bind(this), |
| 22 | + message: () => "Let's edit the release notes.", |
| 23 | + }, |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + async defaultNotes() { |
| 28 | + if (this.options.legacy) { |
| 29 | + // TODO: version |
| 30 | + const res = await this.octokit.repos.getReleaseByTag({ |
| 31 | + owner: 'wandb', |
| 32 | + repo: 'core', |
| 33 | + tag: 'local/v0.9.4', |
| 34 | + }); |
| 35 | + this.setContext({date: new Date(res.data.published_at)}); |
| 36 | + return res.data.body; |
| 37 | + } else { |
| 38 | + this.setContext({date: new Date()}); |
| 39 | + // TODO: latestVersion |
| 40 | + let res = await this.octokit.repos.getReleaseByTag({ |
| 41 | + owner: 'wandb', |
| 42 | + repo: 'core', |
| 43 | + tag: 'local/v0.9.30', |
| 44 | + }); |
| 45 | + const lastReleaseSHA = res.data.target_commitish; |
| 46 | + const publishedAt = res.data.published_at; |
| 47 | + console.log('Grabbing all commits from since', publishedAt, lastReleaseSHA); |
| 48 | + res = await this.octokit.repos.listCommits({ |
| 49 | + owner: 'wandb', |
| 50 | + repo: 'core', |
| 51 | + per_page: 100, |
| 52 | + since: publishedAt, |
| 53 | + }); |
| 54 | + //new Date(commit.author.date) > new Date(publishedAt) |
| 55 | + if(res.data.length > 100) { |
| 56 | + console.warn("There have been more than 100 commits since the last release!") |
| 57 | + } |
| 58 | + const notes = res.data |
| 59 | + .map((commit) => `* ${commit.commit.message.split('\n')[0]}`) |
| 60 | + .join('\n'); |
| 61 | + return notes; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + bump(version) { |
| 66 | + this.setContext({version}); |
| 67 | + } |
| 68 | + |
| 69 | + getChangelog(latestVersion) { |
| 70 | + this.setContext({latestVersion}); |
| 71 | + } |
| 72 | + |
| 73 | + saveChangelogToFile(filePath, renderedTemplate) { |
| 74 | + const fileDescriptor = fs.openSync(filePath, 'a+'); |
| 75 | + |
| 76 | + const oldData = fs.readFileSync(filePath); |
| 77 | + const newData = new Buffer.from(renderedTemplate); |
| 78 | + |
| 79 | + fs.writeSync(fileDescriptor, newData, 0, newData.length, 0); |
| 80 | + fs.writeSync(fileDescriptor, oldData, 0, oldData.length, newData.length); |
| 81 | + |
| 82 | + fs.closeSync(fileDescriptor); |
| 83 | + } |
| 84 | + |
| 85 | + saveReleaseNotesToFile(filePath, notes) { |
| 86 | + const fileDescriptor = fs.openSync(filePath, 'a+'); |
| 87 | + const newData = new Buffer.from(notes); |
| 88 | + fs.writeSync(fileDescriptor, newData, 0, newData.length, 0); |
| 89 | + fs.closeSync(fileDescriptor); |
| 90 | + } |
| 91 | + |
| 92 | + getFormattedDate() { |
| 93 | + const date = this.getContext('date'); |
| 94 | + return date.toLocaleDateString('en-us', { |
| 95 | + month: 'long', |
| 96 | + day: 'numeric', |
| 97 | + year: 'numeric', |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + async beforeRelease() { |
| 102 | + await this.step({ |
| 103 | + enabled: true, |
| 104 | + task: (notes) => { |
| 105 | + this.setContext({notes}); |
| 106 | + console.log('CTX', this.getContext()); |
| 107 | + this.saveReleaseNotesToFile('staging/RELEASE.md', notes); |
| 108 | + this.saveChangelogToFile( |
| 109 | + 'CHANGELOG.md', |
| 110 | + `# wandb/local:${this.getContext( |
| 111 | + 'version' |
| 112 | + )} - ${this.getFormattedDate()}\n\n${notes}\n\n` |
| 113 | + ); |
| 114 | + }, |
| 115 | + label: 'Creating notes', |
| 116 | + prompt: 'release_notes', |
| 117 | + }); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +module.exports = WandbPlugin; |
0 commit comments