Skip to content

Commit de6d812

Browse files
committed
feat: support GitHub Enterprise in CLI
Add a `--url` option to the CLI to allow users of `conventional-github-releaser` to provide a custom URL for GitHub, which can be used to specify the API endpoint for an on-premise GitHub Enterprise instance. ```bash conventional-github-releaser --url https://api.github.com ``` Replace the `github` package with `gh-got` to simplify the way we interact with GitHub's API. Posting new releases is the only part of the GitHub API that we need to interact with, and `github` is a litte heavy for our purposes. Default to `https://api.github.com` as the endpoint so as to maintain the behavior supported by `github` when issuing requests to the GitHub API. The default URL is technically set by `gh-got`, so no further work is required on our part. BREAKING CHANGE: The `github` package has been replaced by `gh-got`. Options supported by `github` (https://www.npmjs.com/package/github) can no longer be provided through the `auth` parameter to `conventional-github-releaser`. To set a custom URL for GitHub please do one of the following: * If calling the `conventional-github-releaser` API, pass a URL as the `url` property of the `auth` parameter to `conventional-github-releaser`. The URL should be the fully qualified domain and path of the GitHub API, such as `https://api.github.com` or `https://api.github.example.com`. * If calling the `conventional-github-releaser` CLI, either pass a URL using the `--url` option, or set the `CONVENTIONAL_GITHUB_URL` environment variable. * If calling the CLI or API, set the `GITHUB_ENDPOINT` environment variable supported by the underlying `gh-got` library - https://www.npmjs.com/package/gh-got#endpoint
1 parent 4d8260b commit de6d812

File tree

11 files changed

+1789
-930
lines changed

11 files changed

+1789
-930
lines changed

.eslintrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "xo-space",
3+
"rules": {
4+
"camelcase": ["error", {"properties": "never"}],
5+
"comma-dangle": ["error", "always-multiline"]
6+
}
7+
}

.jscsrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

.jshintrc

Lines changed: 0 additions & 15 deletions
This file was deleted.

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ node_js:
3232
before_install:
3333
- npm install --global npm@${NPM_VERSION}
3434
after_success:
35-
- cat ./coverage/lcov.info | node $(npm bin)/coveralls
35+
- $(npm bin)/codecov

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# conventional-github-releaser
22

33
[![Build Status](https://travis-ci.org/conventional-changelog/conventional-github-releaser.svg?branch=master)](https://travis-ci.org/conventional-changelog/conventional-github-releaser)
4-
[![Coverage Status](https://coveralls.io/repos/github/conventional-changelog/conventional-github-releaser/badge.svg?branch=master)](https://coveralls.io/github/conventional-changelog/conventional-github-releaser?branch=master)
5-
[![dependencies Status](https://david-dm.org/conventional-changelog/conventional-github-releaser/status.svg)](https://david-dm.org/conventional-changelog/conventional-github-releaser)
64
[![Join the chat at https://gitter.im/conventional-changelog/conventional-github-releaser](https://badges.gitter.im/conventional-changelog/conventional-github-releaser.svg)](https://gitter.im/conventional-changelog/conventional-github-releaser?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
75

86
> Make a new GitHub release from git metadata.
@@ -57,10 +55,10 @@ Please use this [gist](https://gist.github.com/stevemao/280ef22ee861323993a0) to
5755

5856
## Why
5957

60-
- Based on [conventional-changelog](https://github.com/ajoslin/conventional-changelog) but GitHub releases are more elegant.
58+
- Based on [conventional-changelog](https://www.npmjs.com/package/conventional-changelog) but GitHub releases are more elegant.
6159
- Easy fully automate changelog generation. You could still add more points on top of it.
62-
- Detecting prerelease based on semver, ignoring reverted commits, templating with [handlebars.js](https://github.com/wycats/handlebars.js) and links to references, etc. Open an [issue](../../issues/new) if you want more reasonable features.
63-
- Intelligently setup defaults but yet fully configurable with presets of [popular projects](https://github.com/ajoslin/conventional-changelog#preset).
60+
- Detecting prerelease based on semver, ignoring reverted commits, templating with [handlebars.js](https://www.npmjs.com/package/handlebars) and links to references, etc. Open an [issue](../../issues/new) if you want more reasonable features.
61+
- Intelligently setup defaults but yet fully configurable with presets of [popular projects](https://www.npmjs.com/package/conventional-changelog#preset).
6462
- Everything internally or externally is pluggable.
6563
- A lot of tests and actively maintained.
6664

@@ -89,7 +87,21 @@ conventionalGithubReleaser(AUTH, {
8987

9088
#### auth
9189

92-
An auth object passed to [node-github](https://github.com/mikedeboer/node-github#authentication).
90+
An object that may contain the following properties:
91+
92+
* token - A [GitHub Personal Token](https://github.com/blog/1509-personal-api-tokens) with `repo` [scope permissions](https://developer.github.com/enterprise/2.11/apps/building-integrations/setting-up-and-registering-oauth-apps/about-scopes-for-oauth-apps/).
93+
* url - The fully qualified domain name for the GitHub instance (such as `https://api.github.com`).
94+
95+
For example:
96+
97+
```json
98+
{
99+
token: '0126af95c0e2d9b0a7c78738c4c00a860b04acc8',
100+
url: 'https://api.github.com'
101+
}
102+
```
103+
104+
> Please read [`gh-got`](https://www.npmjs.com/package/gh-got#token) for default behavior when `token` and/or `url` aren't provided.
93105
94106
#### callback
95107

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@
3333
"url": "https://github.com/conventional-changelog/conventional-github-releaser.git"
3434
},
3535
"scripts": {
36-
"test": "jshint src/ && jscs src/ && nyc --all --cache --include=src/ --exclude=src/**/*.spec.js --reporter=lcov --reporter=text mocha --check-leaks --full-trace --globals __core-js_shared__,__coverage__,YamlEscaper --inline-diffs --no-exit --recursive --reporter=progress --retries 1 src/**/*.spec.js"
36+
"dev": "docker run --rm --user node -v \"$(pwd)\":/app -w /app -it node:4 sh -c \"yarn install; yarn test; bash\"",
37+
"test": "eslint src/ && nyc --all --cache --include=src/ --exclude=src/cli.js --exclude=src/**/*.spec.js --exclude=src/**/*.mocks.js --reporter=lcov --reporter=text mocha --check-leaks --full-trace --globals __core-js_shared__,__coverage__,YamlEscaper --inline-diffs --no-exit --recursive --reporter=progress --retries 1 src/**/*.spec.js"
3738
},
3839
"version": "1.1.12",
3940
"dependencies": {
4041
"conventional-changelog": "^1.1.0",
41-
"dateformat": "^1.0.11",
42+
"dateformat": "^3.0.0",
43+
"gh-got": "^6.0.0",
4244
"git-semver-tags": "^1.0.0",
43-
"github": "^0.2.4",
4445
"lodash.merge": "^4.0.2",
4546
"meow": "^3.3.0",
4647
"object-assign": "^4.0.1",
@@ -50,12 +51,12 @@
5051
"through2": "^2.0.0"
5152
},
5253
"devDependencies": {
53-
"chai": "^3.0.0",
54-
"concat-stream": "^1.5.1",
55-
"coveralls": "^2.11.2",
56-
"jscs": "^3.0.7",
57-
"jshint": "^2.9.1",
58-
"mocha": "^3.2.0",
59-
"nyc": "^10.3.2"
54+
"chai": "^4.1.2",
55+
"codecov": "^3.0.0",
56+
"eslint": "^4.10.0",
57+
"eslint-config-xo-space": "^0.17.0",
58+
"mocha": "^4.0.1",
59+
"npm-publish-git-tag": "^1.1.14",
60+
"nyc": "^11.2.1"
6061
}
6162
}

renovate.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"automerge": true,
3+
"major": {
4+
"automerge": false
5+
},
6+
"labels": ["refactor"],
7+
"pinVersions": false,
8+
"prCreation": "not-pending",
9+
"rebaseStalePrs": true,
10+
"semanticCommits": true
11+
}

src/cli.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#!/usr/bin/env node
2+
23
'use strict';
3-
var meow = require('meow');
4-
var conventionalGithubReleaser = require('./index');
5-
var resolve = require('path').resolve;
64

7-
var cli = meow({
5+
const meow = require('meow');
6+
const conventionalGithubReleaser = require('./index');
7+
const resolve = require('path').resolve;
8+
9+
const cli = meow({
810
help: [
911
'Usage',
1012
' conventional-github-releaser',
@@ -13,6 +15,7 @@ var cli = meow({
1315
' conventional-github-releaser -p angular',
1416
'',
1517
'Options',
18+
' -u, --url URL of your GitHub provider. Defaults to `https://api.github.com`',
1619
' -t, --token Your GitHub auth token',
1720
'',
1821
' -p, --preset Name of the preset you want to use. Must be one of the following:',
@@ -34,28 +37,29 @@ var cli = meow({
3437
' -c, --context A filepath of a javascript that is used to define template variables',
3538
'',
3639
' -d, --draft Publishes a draft instead of a real release',
37-
' Default: false'
38-
]
40+
' Default: false',
41+
],
3942
}, {
4043
alias: {
44+
u: 'url',
4145
t: 'token',
4246
p: 'preset',
4347
k: 'pkg',
4448
r: 'releaseCount',
4549
v: 'verbose',
4650
n: 'config',
4751
c: 'context',
48-
d: 'draft'
49-
}
52+
d: 'draft',
53+
},
5054
});
5155

52-
var config;
53-
var flags = cli.flags;
56+
let config = {};
57+
const flags = cli.flags;
5458

55-
var templateContext;
56-
var gitRawCommitsOpts;
57-
var parserOpts;
58-
var writerOpts;
59+
let templateContext;
60+
let gitRawCommitsOpts;
61+
let parserOpts;
62+
let writerOpts;
5963

6064
try {
6165
if (flags.context) {
@@ -64,8 +68,6 @@ try {
6468

6569
if (flags.config) {
6670
config = require(resolve(process.cwd(), flags.config));
67-
} else {
68-
config = {};
6971
}
7072

7173
if (config.gitRawCommitsOpts) {
@@ -84,13 +86,13 @@ try {
8486
process.exit(1);
8587
}
8688

87-
var changelogOpts = {
89+
const changelogOpts = {
8890
preset: flags.preset,
8991
pkg: {
90-
path: flags.pkg
92+
path: flags.pkg,
9193
},
9294
releaseCount: flags.releaseCount,
93-
draft: flags.draft
95+
draft: flags.draft,
9496
};
9597

9698
if (flags.verbose) {
@@ -99,9 +101,9 @@ if (flags.verbose) {
99101
}
100102

101103
conventionalGithubReleaser({
102-
type: 'oauth',
103-
token: flags.token || process.env.CONVENTIONAL_GITHUB_RELEASER_TOKEN
104-
}, changelogOpts, templateContext, gitRawCommitsOpts, parserOpts, writerOpts, function(err, data) {
104+
url: flags.url || process.env.CONVENTIONAL_GITHUB_URL,
105+
token: flags.token || process.env.CONVENTIONAL_GITHUB_RELEASER_TOKEN,
106+
}, changelogOpts, templateContext, gitRawCommitsOpts, parserOpts, writerOpts, function (err, data) {
105107
if (err) {
106108
console.error(err.toString());
107109
process.exit(1);

src/index.js

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
'use strict';
2-
var assign = require('object-assign');
3-
var conventionalChangelog = require('conventional-changelog');
4-
var Github = require('github');
5-
var gitSemverTags = require('git-semver-tags');
6-
var merge = require('lodash.merge');
7-
var Q = require('q');
8-
var semver = require('semver');
9-
var through = require('through2');
10-
var transform = require('./transform');
11-
12-
var github = new Github({
13-
version: '3.0.0'
14-
});
152

3+
const assign = require('object-assign');
4+
const conventionalChangelog = require('conventional-changelog');
5+
const gitSemverTags = require('git-semver-tags');
6+
const ghGot = require('gh-got');
7+
const merge = require('lodash.merge');
8+
const Q = require('q');
9+
const semver = require('semver');
10+
const through = require('through2');
11+
const transform = require('./transform');
12+
13+
/* eslint max-params: ["error", 7] */
1614
function conventionalGithubReleaser(auth, changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts, userCb) {
1715
if (!auth) {
1816
throw new Error('Expected an auth object');
1917
}
2018

21-
var promises = [];
19+
const promises = [];
2220

23-
var changelogArgs = [changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts].map(function(arg) {
21+
const changelogArgs = [changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts].map(function (arg) {
2422
if (typeof arg === 'function') {
2523
userCb = arg;
2624
return {};
27-
} else {
28-
return arg || {};
2925
}
26+
return arg || {};
3027
});
3128

3229
if (!userCb) {
@@ -41,75 +38,79 @@ function conventionalGithubReleaser(auth, changelogOpts, context, gitRawCommitsO
4138

4239
changelogOpts = merge({
4340
transform: transform,
44-
releaseCount: 1
41+
releaseCount: 1,
4542
}, changelogOpts);
4643

4744
writerOpts.includeDetails = true;
4845

4946
// ignore the default header partial
5047
writerOpts.headerPartial = writerOpts.headerPartial || '';
5148

52-
github.authenticate(auth);
53-
5449
Q.nfcall(gitSemverTags)
55-
.then(function(tags) {
50+
.then(function (tags) {
5651
if (!tags || !tags.length) {
5752
setImmediate(userCb, new Error('No semver tags found'));
5853
return;
5954
}
6055

61-
var releaseCount = changelogOpts.releaseCount;
56+
const releaseCount = changelogOpts.releaseCount;
6257
if (releaseCount !== 0) {
6358
gitRawCommitsOpts = assign({
64-
from: tags[releaseCount]
59+
from: tags[releaseCount],
6560
}, gitRawCommitsOpts);
6661
}
6762

6863
gitRawCommitsOpts.to = gitRawCommitsOpts.to || tags[0];
6964

7065
conventionalChangelog(changelogOpts, context, gitRawCommitsOpts, parserOpts, writerOpts)
71-
.on('error', function(err) {
66+
.on('error', function (err) {
7267
userCb(err);
7368
})
74-
.pipe(through.obj(function(chunk, enc, cb) {
69+
.pipe(through.obj(function (chunk, enc, cb) {
7570
if (!chunk.keyCommit || !chunk.keyCommit.version) {
7671
cb();
7772
return;
7873
}
7974

80-
var version = chunk.keyCommit.version;
81-
82-
var prerelease = semver.parse(version).prerelease.length > 0;
75+
const version = chunk.keyCommit.version;
76+
const prerelease = semver.parse(version).prerelease.length > 0;
77+
const draft = changelogOpts.draft || false;
78+
79+
const options = {
80+
body: {
81+
body: chunk.log,
82+
draft: draft,
83+
name: changelogOpts.name || version,
84+
prerelease: prerelease,
85+
tag_name: version,
86+
target_commitish: changelogOpts.targetCommitish,
87+
},
88+
};
89+
90+
if (auth.token) {
91+
options.token = auth.token;
92+
}
8393

84-
var draft = changelogOpts.draft || false;
94+
if (auth.url) {
95+
options.endpoint = auth.url;
96+
}
8597

86-
var promise = Q.nfcall(github.releases.createRelease, {
87-
// jscs:disable
88-
owner: context.owner,
89-
repo: context.repository,
90-
tag_name: version,
91-
body: chunk.log,
92-
prerelease: prerelease,
93-
draft: draft,
94-
target_commitish: changelogOpts.targetCommitish,
95-
name: changelogOpts.name || version
96-
// jscs:enable
97-
});
98+
const promise = ghGot('repos/' + context.owner + '/' + context.repository + '/releases', options);
9899

99100
promises.push(promise);
100101

101102
cb();
102-
}, function() {
103+
}, function () {
103104
Q.all(promises)
104-
.then(function(responses) {
105+
.then(function (responses) {
105106
userCb(null, responses);
106107
})
107-
.catch(function(err) {
108+
.catch(function (err) {
108109
userCb(err);
109110
});
110111
}));
111112
})
112-
.catch(function(err) {
113+
.catch(function (err) {
113114
userCb(err);
114115
});
115116
}

0 commit comments

Comments
 (0)