diff --git a/package.json b/package.json index 7b8cac56..ad16b507 100644 --- a/package.json +++ b/package.json @@ -18,11 +18,14 @@ "@semantic-release/condition-travis": "^1.0.0", "@semantic-release/error": "^1.0.0", "@semantic-release/release-notes-generator": "^1.0.0", + "git-head": "^1.2.1", + "github": "^0.2.4", "lodash": "^3.9.3", "nopt": "^3.0.3", "npm-registry-client": "^6.4.0", "npmconf": "^2.1.2", "npmlog": "^1.2.1", + "parse-github-repo-url": "^1.0.0", "require-relative": "^0.8.7", "run-auto": "^1.1.2", "semver": "^5.0.1" diff --git a/src/index.js b/src/index.js index 5dec9497..fb6e1175 100644 --- a/src/index.js +++ b/src/index.js @@ -111,6 +111,14 @@ npmconf.load({}, (err, conf) => { } else if (options.argv.cooked[0] === 'post') { log.verbose(PREFIX, 'Running post-script.') + require('./post')(pkg, options, plugins, (err, published, release) => { + if (err) { + log.error(PREFIX, 'Failed to publish release notes.', err) + process.exit(1) + } + + log.verbose(PREFIX, `${published ? 'Published' : 'Generated'} release notes.`, release) + }) } else { log.error(PREFIX, `Command "${options.argv.cooked[0]}" not recognized. User either "pre" or "post"`) } diff --git a/src/post.js b/src/post.js new file mode 100644 index 00000000..42107af0 --- /dev/null +++ b/src/post.js @@ -0,0 +1,50 @@ +const url = require('url') + +const gitHead = require('git-head') +const GitHubApi = require('github') +const parseSlug = require('parse-github-repo-url') + +module.exports = function (pkg, argv, plugins, cb) { + const config = argv['github-url'] ? url.parse(argv['github-url']) : {} + + const github = new GitHubApi({ + version: '3.0.0', + port: config.port, + protocol: (config.protocol || '').split(':')[0] || null, + host: config.hostname + }) + + plugins.generateNotes(pkg, (err, log) => { + if (err) return cb(err) + + gitHead((err, hash) => { + if (err) return cb(err) + + const ghRepo = parseSlug(pkg.repository.url) + const release = { + owner: ghRepo[0], + repo: ghRepo[1], + name: `v${pkg.version}`, + tag_name: `v${pkg.version}`, + target_commitish: hash, + draft: !!argv.debug, + body: log + } + + if (argv.debug && !argv['github-token']) { + return cb(null, false, release) + } + + github.authenticate({ + type: 'oauth', + token: argv['github-token'] + }) + + github.releases.createRelease(release, (err) => { + if (err) return cb(err) + + cb(null, true, release) + }) + }) + }) +}