Skip to content

Commit

Permalink
feat: support for GitHub releases and Ruby (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Nov 1, 2019
1 parent fe2f5b2 commit 8c10eb7
Show file tree
Hide file tree
Showing 8 changed files with 874 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/release-please/README.md
Expand Up @@ -24,6 +24,7 @@ options:
| `primaryBranch` | The primary branch from which releases are started | `string` | `master` |
| `releaseLabels` | List of labels to add to the release PR. | `string[]` | `null` | `undefined` |
| `releaseType` | Release strategy | `string` | strategy detected from the repository's primary language |
| `handleGHRelease` | Release to GitHub | `boolean` | should release please create a GitHub release, when release pull-requests are merged? |

## Testing

Expand Down
803 changes: 790 additions & 13 deletions packages/release-please/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/release-please/package.json
Expand Up @@ -28,7 +28,7 @@
"dependencies": {
"gcf-utils": "^1.0.0",
"probot": "9.4.0",
"release-please": "^2.11.0"
"release-please": "^2.14.0"
},
"devDependencies": {
"@types/bunyan": "^1.8.6",
Expand Down
46 changes: 42 additions & 4 deletions packages/release-please/src/release-please.ts
Expand Up @@ -20,6 +20,10 @@ import { Application } from 'probot';
// See https://github.com/googleapis/release-please/issues/249
import { ReleaseType, BuildOptions } from 'release-please/build/src/release-pr';
import { ReleasePRFactory } from 'release-please/build/src/release-pr-factory';
import {
GitHubRelease,
GitHubReleaseOptions,
} from 'release-please/build/src/github-release';
import { Runner } from './runner';
import { GitHubAPI } from 'probot/lib/github';

Expand All @@ -28,6 +32,7 @@ interface ConfigurationOptions {
releaseLabels?: string[];
releaseType?: ReleaseType;
packageName?: string;
handleGHRelease?: boolean;
}

const DEFAULT_API_URL = 'https://api.github.com';
Expand All @@ -42,7 +47,7 @@ function releaseTypeFromRepoLanguage(language: string | null): ReleaseType {
}
switch (language.toLowerCase()) {
case 'ruby':
return ReleaseType.RubyYoshi;
return ReleaseType.Ruby;
case 'java':
return ReleaseType.JavaYoshi;
case 'typescript':
Expand All @@ -55,7 +60,8 @@ function releaseTypeFromRepoLanguage(language: string | null): ReleaseType {
}
}

function runReleasePlease(
// creates or updates the evergreen release-please release PR.
function createReleasePR(
releaseType: ReleaseType,
packageName: string,
repoUrl: string,
Expand All @@ -79,6 +85,27 @@ function runReleasePlease(
Runner.runner(ReleasePRFactory.build(releaseType, buildOptions));
}

// turn a merged release-please release PR into a GitHub release.
function createGitHubRelease(
packageName: string,
repoUrl: string,
github: GitHubAPI
) {
const releaseOptions: GitHubReleaseOptions = {
label: 'autorelease: pending',
repoUrl,
packageName,
apiUrl: DEFAULT_API_URL,
octokitAPIs: {
octokit: github,
graphql: github.graphql,
request: github.request,
},
};
const ghr = new GitHubRelease(releaseOptions);
Runner.releaser(ghr);
}

export = (app: Application) => {
app.on('push', async context => {
const repoUrl = context.payload.repository.full_name;
Expand Down Expand Up @@ -114,13 +141,24 @@ export = (app: Application) => {
app.log.info(`push (${repoUrl})`);

// TODO: this should be refactored into an interface.
runReleasePlease(
createReleasePR(
releaseType,
configuration.packageName || repoName,
repoUrl,
context.github,
configuration.releaseLabels
);

// release-please can handle creating a release on GitHub, we opt not to do
// this for our repos that have autorelease enabled.
if (configuration.handleGHRelease) {
app.log.info(`handling GitHub release for (${repoUrl})`);
createGitHubRelease(
configuration.packageName || repoName,
repoUrl,
context.github
);
}
});

app.on('release.published', async context => {
Expand Down Expand Up @@ -155,7 +193,7 @@ export = (app: Application) => {
: releaseTypeFromRepoLanguage(context.payload.repository.language);

// TODO: this should be refactored into an interface.
runReleasePlease(
createReleasePR(
releaseType,
configuration.packageName || repoName,
repoUrl,
Expand Down
4 changes: 4 additions & 0 deletions packages/release-please/src/runner.ts
Expand Up @@ -16,9 +16,13 @@

// TODO: fix these imports when release-please exports types from the root
import { ReleasePR } from 'release-please/build/src/release-pr';
import { GitHubRelease } from 'release-please/build/src/github-release';

export class Runner {
static runner = (pr: ReleasePR) => {
pr.run();
};
static releaser = (release: GitHubRelease) => {
release.createRelease();
};
}
@@ -1 +1 @@
releaseType: ruby-yoshi
releaseType: ruby
@@ -0,0 +1,5 @@
primaryBranch: master
releaseLabels:
- foo
- bar
handleGHRelease: true
33 changes: 30 additions & 3 deletions packages/release-please/test/release-please.ts
Expand Up @@ -24,9 +24,10 @@ import Webhooks from '@octokit/webhooks';
import nock from 'nock';
import * as fs from 'fs';
import assert, { fail } from 'assert';
import { GitHubRelease } from 'release-please/build/src/github-release';
import { ReleasePR } from 'release-please/build/src/release-pr';
import { JavaYoshi } from 'release-please/build/src/releasers/java-yoshi';
import { RubyYoshi } from 'release-please/build/src/releasers/ruby-yoshi';
import { Ruby } from 'release-please/build/src/releasers/ruby';

nock.disableNetConnect();

Expand Down Expand Up @@ -84,6 +85,32 @@ describe('ReleasePleaseBot', () => {
assert(executed, 'should have executed the runner');
});

it('should handle GitHub releases, if configured', async () => {
let runnerExecuted = false;
let releaserExecuted = false;
Runner.runner = (pr: ReleasePR) => {
assert(pr instanceof JavaYoshi);
runnerExecuted = true;
};
Runner.releaser = (pr: GitHubRelease) => {
assert(pr instanceof GitHubRelease);
releaserExecuted = true;
};
const config = fs.readFileSync(
resolve(fixturesPath, 'config', 'valid_handle_gh_release.yml')
);
const requests = nock('https://api.github.com')
.get(
'/repos/chingor13/google-auth-library-java/contents/.github/release-please.yml'
)
.reply(200, { content: config });

await probot.receive({ name: 'push', payload, id: 'abc123' });
requests.done();
assert(runnerExecuted, 'should have executed the runner');
assert(releaserExecuted, 'GitHub release should have run');
});

it('should ignore if the branch is the configured primary branch', async () => {
Runner.runner = (pr: ReleasePR) => {
fail('should not be running a release');
Expand All @@ -104,7 +131,7 @@ describe('ReleasePleaseBot', () => {
it('should allow overriding the release strategy from configuration', async () => {
let executed = false;
Runner.runner = (pr: ReleasePR) => {
assert(pr instanceof RubyYoshi);
assert(pr instanceof Ruby);
executed = true;
};
const config = fs.readFileSync(
Expand Down Expand Up @@ -255,7 +282,7 @@ describe('ReleasePleaseBot', () => {
it('should try to create a snapshot', async () => {
let executed = false;
Runner.runner = (pr: ReleasePR) => {
assert(pr instanceof RubyYoshi);
assert(pr instanceof Ruby);
executed = true;
};
const config = fs.readFileSync(
Expand Down

0 comments on commit 8c10eb7

Please sign in to comment.