Skip to content

Commit

Permalink
feat(go): add support for mono-repo google-cloud-go repo (#617)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Nov 9, 2020
1 parent 01bbffb commit 7121575
Show file tree
Hide file tree
Showing 11 changed files with 470 additions and 58 deletions.
24 changes: 24 additions & 0 deletions __snapshots__/github-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,27 @@ exports['GitHubRelease createRelease creates and labels release on GitHub 2'] =
'autorelease: tagged'
]
}

exports['GitHubRelease createRelease creates releases for submodules in monorepo 1'] = {
'tag_name': 'bigquery/v1.0.3',
'body': '\n* entry',
'name': 'foo bigquery/v1.0.3'
}

exports['GitHubRelease createRelease creates releases for submodules in monorepo 2'] = {
'labels': [
'autorelease: tagged'
]
}

exports['GitHubRelease createRelease creates release for root module in monorepo 1'] = {
'tag_name': 'v1.0.3',
'body': '\n* entry',
'name': 'foo v1.0.3'
}

exports['GitHubRelease createRelease creates release for root module in monorepo 2'] = {
'labels': [
'autorelease: tagged'
]
}
1 change: 0 additions & 1 deletion __snapshots__/yoshi-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ filename: CHANGES.md
* **all:** auto-regenerate gapics , refs [#1000](https://github.com/googleapis/google-cloud-go/issues/1000) [#1001](https://github.com/googleapis/google-cloud-go/issues/1001)
* **asset:** added a really cool feature ([d7d1c89](https://github.com/googleapis/google-cloud-go/commit/d7d1c890dc1526f4d62ceedad581f498195c8939))
* **pubsublite:** start generating v1 ([1d9662c](https://github.com/googleapis/google-cloud-go/commit/1d9662cf08ab1cf3b68d95dee4dc99b7c4aac371))
### Bug Fixes
Expand Down
5 changes: 5 additions & 0 deletions src/bin/release-please.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ const argv = yargs
.option('path', {
describe: 'release from path other than root directory',
type: 'string',
})
.option('monorepo-tags', {
describe: 'include library name in tags and release branches',
type: 'boolean',
default: false,
});
},
(argv: GitHubReleaseOptions) => {
Expand Down
77 changes: 61 additions & 16 deletions src/github-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.

import chalk = require('chalk');
import {join} from 'path';

import {checkpoint, CheckpointType} from './util/checkpoint';
import {ReleasePRFactory} from './release-pr-factory';
import {
Expand All @@ -33,6 +31,7 @@ export interface GitHubReleaseOptions {
repoUrl: string;
path?: string;
packageName?: string;
monorepoTags?: boolean;
token?: string;
apiUrl: string;
proxyKey?: string;
Expand All @@ -49,6 +48,7 @@ export class GitHubRelease {
repoUrl: string;
path?: string;
packageName?: string;
monorepoTags?: boolean;
token?: string;
proxyKey?: string;
releaseType?: string;
Expand All @@ -58,6 +58,7 @@ export class GitHubRelease {
this.proxyKey = options.proxyKey;
this.labels = options.label.split(',');
this.repoUrl = options.repoUrl;
this.monorepoTags = options.monorepoTags;
this.token = options.token;
this.path = options.path;
this.packageName = options.packageName;
Expand All @@ -69,11 +70,44 @@ export class GitHubRelease {
}

async createRelease(): Promise<ReleaseCreateResponse | undefined> {
const gitHubReleasePR:
| GitHubReleasePR
| undefined = await this.gh.findMergedReleasePR(this.labels);
if (gitHubReleasePR) {
let gitHubReleasePRs: GitHubReleasePR[] = [];
const rootPath = this.path;
const releases = [];
// In most configurations, createRelease() should be called close to when
// a release PR is merged, e.g., a GitHub action that kicks off this
// workflow on merge. For tis reason, we can pull a fairly small number of PRs:
const pageSize = 25;
if (this.monorepoTags) {
gitHubReleasePRs = await this.gh.findMergedReleasePRs(
this.labels,
pageSize
);
} else {
const releasePR = await this.gh.findMergedReleasePR(
this.labels,
pageSize
);
if (releasePR) {
gitHubReleasePRs = [releasePR];
}
}
if (gitHubReleasePRs.length === 0) {
checkpoint('no recent release PRs found', CheckpointType.Failure);
return undefined;
}
for (const gitHubReleasePR of gitHubReleasePRs) {
const version = `v${gitHubReleasePR.version}`;
// If we've enabled monorepoTags, and a prefix was found on release PR
// e.g., release-bigquery-v1.0.0, then assume we are releasing a
// module from within the "bigquery" folder:
if (this.monorepoTags && gitHubReleasePR.packageName) {
this.path = gitHubReleasePR.packageName;
} else {
// As in the case of google-cloud-go, a repo may contain both a
// top level module, and submodules. If no submodule is found in
// the branch name, we use the initial rootPath:
this.path = rootPath;
}

checkpoint(
`found release branch ${chalk.green(version)} at ${chalk.green(
Expand All @@ -87,8 +121,7 @@ export class GitHubRelease {
).parsedContent;
const latestReleaseNotes = GitHubRelease.extractLatestReleaseNotes(
changelogContents,
// For monorepo releases, the library name is prepended to the tag and branch:
version.split('-').pop() || version
version
);
checkpoint(
`found release notes: \n---\n${chalk.grey(latestReleaseNotes)}\n---\n`,
Expand All @@ -97,18 +130,27 @@ export class GitHubRelease {

// Attempt to lookup the package name from a well known location, such
// as package.json, if none is provided:
if (this.packageName === undefined && this.releaseType) {
if (!this.packageName && this.releaseType) {
this.packageName = await ReleasePRFactory.class(
this.releaseType
).lookupPackageName(this.gh);
}
// Go uses '/' for a tag separator, rather than '-':
let tagSeparator = '-';
if (this.releaseType) {
tagSeparator = ReleasePRFactory.class(this.releaseType).tagSeparator();
}
if (this.packageName === undefined) {
throw Error('could not determine package name for release');
throw Error(
`could not determine package name for release repo = ${this.repoUrl}`
);
}

const release = await this.gh.createRelease(
this.packageName,
version,
this.monorepoTags && this.path
? `${this.path}${tagSeparator}${version}`
: version,
gitHubReleasePR.sha,
latestReleaseNotes
);
Expand All @@ -118,18 +160,21 @@ export class GitHubRelease {
// Remove 'autorelease: pending' which indicates a GitHub release
// has not yet been created.
await this.gh.removeLabels(this.labels, gitHubReleasePR.number);
return release;
} else {
checkpoint('no recent release PRs found', CheckpointType.Failure);
return undefined;
releases.push(release);
}
// TODO(bcoe): it will be a breaking change, but we should come up with
// an approach to return a list of releases rather than a single
// release (we will need to make this work with the GitHub action).
return releases[0];
}

addPath(file: string) {
if (this.path === undefined) {
return file;
} else {
return join(this.path, `./${file}`);
const path = this.path.replace(/[/\\]$/, '');
file = file.replace(/^[/\\]/, '');
return `${path}/${file}`;
}
}

Expand Down
Loading

0 comments on commit 7121575

Please sign in to comment.