Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support @ and / when falling back to tags #673

Merged
merged 4 commits into from
Jan 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,18 +455,9 @@ export class GitHub {
// the branch we're configured for.
async latestTag(
prefix?: string,
preRelease = false,
// Allow a branch prefix to differ from a tag prefix. This was required
// for google-cloud-go, which uses the tag prefix library/vx.y.z.
// this is a requirement in the go community.
branchPrefix?: string
preRelease = false
): Promise<GitHubTag | undefined> {
const pull = await this.findMergedReleasePR(
[],
100,
branchPrefix ?? prefix,
preRelease
);
const pull = await this.findMergedReleasePR([], 100, prefix, preRelease);
if (!pull) return await this.latestTagFallback(prefix, preRelease);
const tag = {
name: `v${pull.version}`,
Expand Down Expand Up @@ -514,6 +505,16 @@ export class GitHub {
private async allTags(
prefix?: string
): Promise<{[version: string]: GitHubTag}> {
// If we've fallen back to using allTags, support "-", "@", and "/" as a
// suffix separating the library name from the version #. This allows
// a repository to be seamlessly be migrated from a tool like lerna:
const prefixes: string[] = [];
if (prefix) {
prefix = prefix.substring(0, prefix.length - 1);
for (const suffix of ['-', '@', '/']) {
prefixes.push(`${prefix}${suffix}`);
}
}
const tags: {[version: string]: GitHubTag} = {};
for await (const response of this.octokit.paginate.iterator(
this.decoratePaginateOpts({
Expand All @@ -526,8 +527,17 @@ export class GitHub {
response.data.forEach((data: ReposListTagsResponseItems) => {
// For monorepos, a prefix can be provided, indicating that only tags
// matching the prefix should be returned:
if (prefix && !data.name.startsWith(prefix)) return;
let version = data.name.replace(prefix, '');
let version = data.name;
if (prefix) {
let match = false;
for (prefix of prefixes) {
if (data.name.startsWith(prefix)) {
version = data.name.replace(prefix, '');
match = true;
}
}
if (!match) return;
}
if ((version = semver.valid(version))) {
tags[version] = {sha: data.commit.sha, name: data.name, version};
}
Expand Down
5 changes: 2 additions & 3 deletions src/releasers/go-yoshi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ export class GoYoshi extends ReleasePR {
static releaserName = 'go-yoshi';
protected async _run(): Promise<number | undefined> {
const latestTag = await this.gh.latestTag(
this.monorepoTags ? `${this.packageName}/` : undefined,
false,
this.monorepoTags ? `${this.packageName}` : undefined
this.monorepoTags ? `${this.packageName}-` : undefined,
false
);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_owner, repo] = parseGithubRepoUrl(this.repoUrl);
Expand Down
5 changes: 2 additions & 3 deletions src/releasers/ruby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ export class Ruby extends ReleasePR {
}
protected async _run(): Promise<number | undefined> {
const latestTag: GitHubTag | undefined = await this.gh.latestTag(
this.monorepoTags ? `${this.packageName}/` : undefined,
false,
this.monorepoTags ? `${this.packageName}-` : undefined
this.monorepoTags ? `${this.packageName}-` : undefined,
false
);
const commits: Commit[] = await this.commits({
sha: latestTag ? latestTag.sha : undefined,
Expand Down
116 changes: 116 additions & 0 deletions test/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,122 @@ describe('GitHub', () => {
expect(latestTag!.version).to.equal('3.0.0-rc1');
req.done();
});
it('falls back to using tags, for simple case', async () => {
req
.get(
'/repos/fake/fake/pulls?state=closed&per_page=100&sort=merged_at&direction=desc'
)
.reply(200, [])
.get('/repos/fake/fake/tags?per_page=100')
.reply(200, [
{
name: 'v1.0.0',
commit: {sha: 'abc123'},
},
{
name: 'v1.1.0',
commit: {sha: 'deadbeef'},
},
]);
const latestTag = await github.latestTag();
expect(latestTag!.version).to.equal('1.1.0');
req.done();
});
it('falls back to using tags, when prefix is provided', async () => {
req
.get(
'/repos/fake/fake/pulls?state=closed&per_page=100&sort=merged_at&direction=desc'
)
.reply(200, [])
.get('/repos/fake/fake/tags?per_page=100')
.reply(200, [
{
name: 'v1.0.0',
commit: {sha: 'abc123'},
},
{
name: 'v1.1.0',
commit: {sha: 'deadbeef'},
},
{
name: 'foo-v1.9.0',
commit: {sha: 'deadbeef'},
},
{
name: 'v1.2.0',
commit: {sha: 'deadbeef'},
},
]);
const latestTag = await github.latestTag('foo-');
expect(latestTag!.version).to.equal('1.9.0');
req.done();
});
it('allows for "@" rather than "-" when fallback used', async () => {
req
.get(
'/repos/fake/fake/pulls?state=closed&per_page=100&sort=merged_at&direction=desc'
)
.reply(200, [])
.get('/repos/fake/fake/tags?per_page=100')
.reply(200, [
{
name: 'v1.0.0',
commit: {sha: 'abc123'},
},
{
name: 'v1.1.0',
commit: {sha: 'deadbeef'},
},
{
name: 'foo@v1.9.0',
commit: {sha: 'dead'},
},
{
name: 'v1.2.0',
commit: {sha: 'beef'},
},
{
name: 'foo@v2.1.0',
commit: {sha: '123abc'},
},
]);
const latestTag = await github.latestTag('foo-');
expect(latestTag!.version).to.equal('2.1.0');
req.done();
});
it('allows for "/" rather than "-" when fallback used', async () => {
req
.get(
'/repos/fake/fake/pulls?state=closed&per_page=100&sort=merged_at&direction=desc'
)
.reply(200, [])
.get('/repos/fake/fake/tags?per_page=100')
.reply(200, [
{
name: 'v1.0.0',
commit: {sha: 'abc123'},
},
{
name: 'v1.1.0',
commit: {sha: 'deadbeef'},
},
{
name: 'foo/v2.3.0',
commit: {sha: 'dead'},
},
{
name: 'v1.2.0',
commit: {sha: 'beef'},
},
{
name: 'foo/v2.1.0',
commit: {sha: '123abc'},
},
]);
const latestTag = await github.latestTag('foo-');
expect(latestTag!.version).to.equal('2.3.0');
req.done();
});
});

describe('getFileContents', () => {
Expand Down