From 0dd1c516dba52f0a6312798d65b54db1c4844d59 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Thu, 2 Jan 2020 15:18:18 -0800 Subject: [PATCH] feat!: add autorelease: tagged label when creating GitHub release (#329) --- __snapshots__/github-release.js | 62 ++++++++++++++++++--------------- src/github-release.ts | 6 ++++ src/github.ts | 4 +-- src/release-pr.ts | 2 +- test/github-release.ts | 47 ++++++++++++++++++++++++- 5 files changed, 89 insertions(+), 32 deletions(-) diff --git a/__snapshots__/github-release.js b/__snapshots__/github-release.js index cdc15d2fa..855be95c3 100644 --- a/__snapshots__/github-release.js +++ b/__snapshots__/github-release.js @@ -1,31 +1,8 @@ -exports['GitHubRelease extractLatestReleaseNotes extracts appropriate release notes when prior release is patch 1'] = ` - - -### ⚠ BREAKING CHANGES - -* temp directory now defaults to setting for report directory - -### Features - -* default temp directory to report directory ([#102](https://www.github.com/bcoe/c8/issues/102)) ([8602f4a](https://www.github.com/bcoe/c8/commit/8602f4a)) -* load .nycrc/.nycrc.json to simplify migration ([#100](https://www.github.com/bcoe/c8/issues/100)) ([bd7484f](https://www.github.com/bcoe/c8/commit/bd7484f)) - -` - -exports['GitHubRelease extractLatestReleaseNotes handles CHANGELOG with new format entries 1'] = ` - - -### Bug Fixes - -* candidate issue should only be updated every 15 minutes. ([#70](https://www.github.com/googleapis/release-please/issues/70)) ([edcd1f7](https://www.github.com/googleapis/release-please/commit/edcd1f7)) - - -### Features - -* add GitHub action for generating candidate issue ([#69](https://www.github.com/googleapis/release-please/issues/69)) ([6373aed](https://www.github.com/googleapis/release-please/commit/6373aed)) -* checkbox based releases ([#77](https://www.github.com/googleapis/release-please/issues/77)) ([1e4193c](https://www.github.com/googleapis/release-please/commit/1e4193c)) - -` +exports['GitHubRelease createRelease creates and labels release on GitHub 1'] = { + "labels": [ + "autorelease: tagged" + ] +} exports['GitHubRelease extractLatestReleaseNotes handles CHANGELOG with old and new format entries 1'] = ` @@ -65,6 +42,35 @@ exports['GitHubRelease extractLatestReleaseNotes handles CHANGELOG with old form ` +exports['GitHubRelease extractLatestReleaseNotes handles CHANGELOG with new format entries 1'] = ` + + +### Bug Fixes + +* candidate issue should only be updated every 15 minutes. ([#70](https://www.github.com/googleapis/release-please/issues/70)) ([edcd1f7](https://www.github.com/googleapis/release-please/commit/edcd1f7)) + + +### Features + +* add GitHub action for generating candidate issue ([#69](https://www.github.com/googleapis/release-please/issues/69)) ([6373aed](https://www.github.com/googleapis/release-please/commit/6373aed)) +* checkbox based releases ([#77](https://www.github.com/googleapis/release-please/issues/77)) ([1e4193c](https://www.github.com/googleapis/release-please/commit/1e4193c)) + +` + +exports['GitHubRelease extractLatestReleaseNotes extracts appropriate release notes when prior release is patch 1'] = ` + + +### ⚠ BREAKING CHANGES + +* temp directory now defaults to setting for report directory + +### Features + +* default temp directory to report directory ([#102](https://www.github.com/bcoe/c8/issues/102)) ([8602f4a](https://www.github.com/bcoe/c8/commit/8602f4a)) +* load .nycrc/.nycrc.json to simplify migration ([#100](https://www.github.com/bcoe/c8/issues/100)) ([bd7484f](https://www.github.com/bcoe/c8/commit/bd7484f)) + +` + exports['GitHubRelease extractLatestReleaseNotes php-yoshi extracts appropriate release notes, when multiple packages updated 1'] = `
google/cloud-bigquerydatatransfer 0.11.1 diff --git a/src/github-release.ts b/src/github-release.ts index e6d6802ff..bff1a20e5 100644 --- a/src/github-release.ts +++ b/src/github-release.ts @@ -18,6 +18,7 @@ import { checkpoint, CheckpointType } from './util/checkpoint'; import { GitHub, GitHubReleasePR, OctokitAPIs } from './github'; const parseGithubRepoUrl = require('parse-github-repo-url'); +const GITHUB_RELEASE_LABEL = 'autorelease: tagged'; export interface GitHubReleaseOptions { label: string; @@ -82,6 +83,11 @@ export class GitHubRelease { gitHubReleasePR.sha, latestReleaseNotes ); + // Add a label indicating that a release has been created on GitHub, + // but a publication has not yet occurred. + await this.gh.addLabels([GITHUB_RELEASE_LABEL], gitHubReleasePR.number); + // Remove 'autorelease: pending' which indicates a GitHub release + // has not yet been created. await this.gh.removeLabels(this.labels, gitHubReleasePR.number); } else { checkpoint('no recent release PRs found', CheckpointType.Failure); diff --git a/src/github.ts b/src/github.ts index df5b48306..967a70cde 100644 --- a/src/github.ts +++ b/src/github.ts @@ -514,14 +514,14 @@ export class GitHub { return tags; } - async addLabels(pr: number, labels: string[]) { + async addLabels(labels: string[], pr: number) { checkpoint( `adding label ${chalk.green(labels.join(','))} to https://github.com/${ this.owner }/${this.repo}/pull/${pr}`, CheckpointType.Success ); - this.request( + return this.request( `POST /repos/:owner/:repo/issues/:issue_number/labels${ this.proxyKey ? `?key=${this.proxyKey}` : '' }`, diff --git a/src/release-pr.ts b/src/release-pr.ts index 83069936a..d097ebece 100644 --- a/src/release-pr.ts +++ b/src/release-pr.ts @@ -223,7 +223,7 @@ export class ReleasePR { }); // a return of -1 indicates that PR was not updated. if (pr > 0) { - await this.gh.addLabels(pr, this.labels); + await this.gh.addLabels(this.labels, pr); await this.closeStaleReleasePRs(pr, includePackageName); } } diff --git a/test/github-release.ts b/test/github-release.ts index 1407e9327..276328d89 100644 --- a/test/github-release.ts +++ b/test/github-release.ts @@ -13,14 +13,59 @@ // limitations under the License. import { readFileSync } from 'fs'; -import { basename, resolve } from 'path'; +import { resolve } from 'path'; import * as snapshot from 'snap-shot-it'; +import * as nock from 'nock'; +nock.disableNetConnect(); + import { GitHubRelease } from '../src/github-release'; const fixturesPath = './test/fixtures'; describe('GitHubRelease', () => { + describe('createRelease', () => { + it('creates and labels release on GitHub', async () => { + const release = new GitHubRelease({ + label: 'autorelease: pending', + repoUrl: 'googleapis/foo', + packageName: 'foo', + apiUrl: 'https://api.github.com', + }); + const requests = nock('https://api.github.com') + .get('/repos/googleapis/foo/pulls?state=closed&per_page=100') + .reply(200, [ + { + labels: [{ name: 'autorelease: pending' }], + head: { + label: 'head:release-v1.0.3', + }, + number: 1, + merged_at: new Date().toISOString(), + }, + ]) + .get('/repos/googleapis/foo/contents/CHANGELOG.md') + .reply(200, { + content: Buffer.from('#Changelog\n\n## v1.0.3\n\n* entry', 'utf8'), + }) + .post('/repos/googleapis/foo/releases') + .reply(200) + .post( + '/repos/googleapis/foo/issues/1/labels', + (body: { [key: string]: string }) => { + snapshot(body); + return true; + } + ) + .reply(200) + .delete('/repos/googleapis/foo/issues/1/labels/autorelease:%20pending') + .reply(200); + + await release.createRelease(); + requests.done(); + }); + }); + describe('extractLatestReleaseNotes', () => { it('handles CHANGELOG with old and new format entries', () => { const changelogContent = readFileSync(