Skip to content

Commit

Permalink
fix(github-release): Clean all releases, then create all releases fro…
Browse files Browse the repository at this point in the history
…m scratch (#11)
  • Loading branch information
dominique-mueller committed Apr 8, 2017
1 parent 094d4ea commit 12448ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function main() {
await gitCommitTagPush( newVersion );

// Publish release on GitHub
await releaseGithub();
await releaseGithub( repositoryUrl );

} catch( error ) {
hasFinishedSuccessfully = false;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"conventional-recommended-bump": "1.0.x",
"figures": "2.0.x",
"github-url-from-git": "1.5.x",
"github-remove-all-releases": "1.0.x",
"semver": "5.3.x",
"simple-git": "1.70.x"
},
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/generate-changelog-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = function( repositoryUrl ) {
// Wait for the changelog stream to be done, then prepend and append additional information
changelogFileStream.write( `# Changelog\n\nAlso see the **[release page]( ${ repositoryUrl }/releases )**.\n\n` );
changelogFileStream.write( data );
changelogFileStream.write( '<br>\n---\n\n<small>*Changelog generated automatically by [automatic-release](https://github.com/dominique-mueller/automatic-release).*</small>\n' );
changelogFileStream.write( '<br>\n\n---\n\n<sup>*Changelog generated automatically by [automatic-release](https://github.com/dominique-mueller/automatic-release).*</sup>\n' );
changelogFileStream.end();

} ) );
Expand Down
67 changes: 44 additions & 23 deletions src/tasks/release-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require( 'path' );

const chalk = require( 'chalk' );
const conventionalGithubReleaser = require( 'conventional-github-releaser' );
const githubRemoveAllReleases = require( 'github-remove-all-releases' );
const figures = require( 'figures' );

const customTransformFunction = require( './../templates/changelog-transform' );
Expand All @@ -13,44 +14,64 @@ const customTransformFunction = require( './../templates/changelog-transform' );
* Default export:
* Create a new release on GitHub, including release notes (aka changelog content).
*/
module.exports = async function() {
module.exports = async function( repositoryUrl ) {
return new Promise( ( resolve, reject ) => {

console.log( '' );
console.log( chalk.white( ' Publish release on GitHub' ) );

// Get the GitHub API token from the environment variables (remove all empty spaces, just to be sure)
const githubApiToken = process.env.GH_TOKEN.replace( /\s+/g, '' );
const repositoryOwner = repositoryUrl.split( '/' )[ 3 ];
const repositoryName = repositoryUrl.split( '/' )[ 4 ];

// Create release on GitHub
conventionalGithubReleaser( {
// Clean all releases on GitHub
githubRemoveAllReleases( {
type: 'oauth',
token: githubApiToken // GITHUB TOKEN ENVIRONMENT VARIABLE
}, {
preset: 'angular',
releaseCount: 0 // Regenerate the whole thing every time
}, {
linkCompare: false // We use a custom link
}, {}, {}, {
transform: customTransformFunction, // Custom transform (shows all commit types)
mainTemplate: fs.readFileSync( path.resolve( __dirname, './../templates/changelog-main.hbs' ), 'utf-8' ),
commitPartial: fs.readFileSync( path.resolve( __dirname, './../templates/changelog-commit.hbs' ), 'utf-8' ),
headerPartial: '', // Empty header for release notes
footerTemplate: fs.readFileSync( path.resolve( __dirname, './../templates/changelog-footer.hbs' ), 'utf-8' )
}, ( error, responses ) => {

// Catch library errors
if ( error ) {
reject( 'An error occured while creating a new release in GitHub.' );
}, repositoryOwner, repositoryName, ( error, response ) => {

// Catch library errors (except missing releases)
if ( error && error.toString() !== 'Error: No releases found' ) {
reject( 'An error occured while cleaning all releases on GitHub.' );
}

// Catch GitHub errors
if ( responses.length > 0 && responses[ 0 ].state === 'rejected' ) {
reject( `An error occured while creating a new release in GitHub. ${ responses[ 0 ].state }: ${ responses[ 0 ].reason }` );
if ( response.length > 0 && response[ 0 ].state === 'rejected' ) {
reject( `An error occured while cleaning all releases on GitHub. ${ response[ 0 ].state }: ${ response[ 0 ].reason }` );
}

console.log( chalk.green( ` ${ figures.tick } Created new release (w/ changelog in the notes) on GitHub.` ) );
resolve();
// Create a new release on GitHub
conventionalGithubReleaser( {
type: 'oauth',
token: githubApiToken // GITHUB TOKEN ENVIRONMENT VARIABLE
}, {
preset: 'angular',
releaseCount: 0 // Regenerate the whole thing every time
}, {
linkCompare: false // We use a custom link
}, {}, {}, {
transform: customTransformFunction, // Custom transform (shows all commit types)
mainTemplate: fs.readFileSync( path.resolve( __dirname, './../templates/changelog-main.hbs' ), 'utf-8' ),
commitPartial: fs.readFileSync( path.resolve( __dirname, './../templates/changelog-commit.hbs' ), 'utf-8' ),
headerPartial: '', // Empty header for release notes
footerTemplate: fs.readFileSync( path.resolve( __dirname, './../templates/changelog-footer.hbs' ), 'utf-8' )
}, ( error, response ) => {

// Catch library errors
if ( error ) {
reject( 'An error occured while creating a new release on GitHub.' );
}

// Catch GitHub errors
if ( response.length > 0 && response[ 0 ].state === 'rejected' ) {
reject( `An error occured while creating a new release on GitHub. ${ response[ 0 ].state }: ${ response[ 0 ].reason }` );
}

console.log( chalk.green( ` ${ figures.tick } Created new release (w/ changelog in the notes) on GitHub.` ) );
resolve();

} );

} );

Expand Down

0 comments on commit 12448ae

Please sign in to comment.