11'use strict' ;
22
3- var Github = require ( 'github-api' ) ;
4- var options = getOptions ( process . argv ) ;
5- var token = options . token ;
6- var username = options . username ;
7- var repositoryName = options . repo ;
8- var releasePrefix = options . prefix || '' ;
9- var github = new Github ( {
10- token : token ,
11- auth : "oauth"
12- } ) ;
13- var repo = github . getRepo ( username , repositoryName ) ;
14-
15- /**
16- * Create a literal object of the node module options
17- *
18- * @param {Array } args The array of arguments (the module arguments start from index 2)
19- *
20- * @return {Object } The object containg the key/value options
21- */
22- function getOptions ( args ) {
23- var settings = { } ;
24-
25- for ( var i = 2 ; i < args . length ; i ++ ) {
26- settings [ args [ i ] . split ( '=' ) [ 0 ] . replace ( '--' , '' ) ] = args [ i ] . split ( '=' ) [ 1 ] ;
27- }
28-
29- return settings ;
30- }
31-
32- /**
33- * Create a release from a given tag (in the options)
34- *
35- * @param {Object } options The options to build the release:
36- * {
37- * "tag_name": "v1.0.0",
38- * "target_commitish": "master",
39- * "name": "v1.0.0",
40- * "body": "Description of the release",
41- * "draft": false,
42- * "prerelease": false
43- * }
44- */
45- function makeRelease ( releaseOptions ) {
46- repo . makeRelease ( releaseOptions , function ( err , release ) {
47- if ( err ) {
48- console . error (
49- ( JSON . parse ( err . request . responseText ) ) . message + '\n'
50- + ( JSON . parse ( err . request . responseText ) ) . errors [ 0 ] . code
51- ) ;
52- } else {
53- console . info ( release . tag_name + ' successfully created!' ) ;
54- }
55- } ) ;
56- }
57-
58- /**
59- * Return a string with a - to be a bullet list (used for a mapping)
60- *
61- * @param {string } message
62- *
63- * @return {string }
64- */
65- function createBody ( message ) {
66- return '- ' + message ;
67- }
68-
69- /**
70- * Transforms the commits to commit messages
71- *
72- * @param {[Object] } commits The array of object containing the commits
73- *
74- * @return {[string] }
75- */
76- function commitMessages ( commits ) {
77- return commits . map ( function ( commit ) {
78- return commit . commit . message ;
79- } ) ;
80- }
81-
82- /**
83- * Creates the options to make the release
84- *
85- * @param {[string] } commitMessages The commit messages to create the release body
86- */
87- function prepareRelease ( tags , commitMessages ) {
88- commitMessages . pop ( ) ;
89-
90- var body = commitMessages . filter ( function ( message ) {
91- return ! message . match ( 'Merge' ) ;
92- } ) . map ( createBody ) ;
93-
94- var releaseOptions = {
95- tag_name : tags [ 0 ] . name ,
96- name : releasePrefix + tags [ 0 ] . name ,
97- body : body . join ( '\n' ) ,
98- draft : options . draft || false ,
99- prerelease : options . prerelease || false
100- } ;
101-
102-
103- makeRelease ( releaseOptions ) ;
104- }
105-
106- /**
107- * Gets all the commits between two dates
108- *
109- * @param {string } since The since date in ISO
110- * @param {string } until The until date in ISO
111- *
112- * @return {Promise } The promise which resolves the [Array] commit messages
113- */
114- function getCommitsBetweenTwo ( since , until ) {
115- var options = {
116- since : since ,
117- until : until
118- } ;
119-
120- return new Promise ( function ( resolve , reject ) {
121-
122- repo . getCommits ( options , function ( err , commits ) {
123- if ( err ) {
124- reject ( err ) ;
125- } else {
126- resolve ( commitMessages ( commits ) ) ;
127- }
128- } ) ;
129- } ) ;
130- }
131-
132- /**
133- * Get the dates of the last two tags
134- *
135- * @param {[Object] } tags List of all the tags in the repo
136- * @return {[Promise] } The promises which returns the dates
137- */
138- function getTagDates ( lastTag , lastRelease ) {
139- return [ lastTag , lastRelease ] . map ( function ( tag ) {
140- return new Promise ( function ( resolve , reject ) {
141- repo . getCommit ( 'master' , tag . commit . sha , function ( err , commit ) {
142- if ( err ) {
143- reject ( err ) ;
144- } else {
145- resolve ( commit . committer . date ) ;
146- }
147- } ) ;
148- } ) ;
149- } )
150- }
151-
152- /**
153- * Get all the tags of the repo
154- *
155- * @return {Promise }
156- */
157- function getLastTag ( releaseTagName ) {
158- return new Promise ( function ( resolve , reject ) {
159- repo . listTags ( function ( err , tags ) {
160- if ( err ) {
161- reject ( err ) ;
162- } else {
163- resolve (
164- tags . filter ( function ( tag , index ) {
165- return ( index === 0 || tag . name === releaseTagName ) ;
166- } )
167- ) ;
168- }
169- } ) ;
170- } ) ;
171- }
172-
173- /**
174- * Get the latest release
175- *
176- * @return {Promise } The promise which resolves the tag name of the release
177- */
178- function getLatestRelease ( ) {
179- return new Promise ( function ( resolve , reject ) {
180- repo . getLatestRelease ( function ( err , release ) {
181- if ( err ) {
182- reject ( err ) ;
183- } else {
184- resolve ( release . tag_name ) ;
185- }
186- } ) ;
187- } ) ;
188- }
189-
190- /**
191- * Get All the tags, get the dates, get the commits between those dates and prepeare the release
192- */
193- function init ( ) {
194- getLatestRelease ( ) . then ( function ( releaseTagName ) {
195- getLastTag ( releaseTagName ) . then ( function ( tags ) {
196- if ( tags . length === 1 ) {
197- console . error ( 'The latest tag is the latest release!' ) ;
198- return ;
199- }
200-
201- Promise . all ( getTagDates ( tags [ 0 ] , tags [ 1 ] ) )
202- . then ( function ( data ) {
203- getCommitsBetweenTwo ( data [ 1 ] , data [ 0 ] ) . then ( prepareRelease . bind ( null , tags ) ) ;
204- } ) ;
205- } ) ;
206- } ) ;
207- }
208-
209- init ( ) ;
3+ var githubReleaseNotes = require ( './lib/github-release-notes' ) ;
4+ githubReleaseNotes . init ( ) ;
0 commit comments