@@ -7,6 +7,7 @@ var GitHubAPI = require('github');
77
88var Promise = require ( '../ext/promise' ) ;
99var ui = require ( '../ui' ) ;
10+ var gitHubCredentials = require ( '../utils/github-credentials' ) ;
1011
1112function createGitSignature ( options ) {
1213 var millisecondsFromStandardEpoch = Date . now ( ) ;
@@ -47,11 +48,11 @@ function createInitialCommitOnLocalRepo(options, localRepo, oid) {
4748 return localRepo . createCommit ( "HEAD" , signature , signature , "Initial commit" , oid , [ ] ) ;
4849}
4950
50- function createRemoteRepo ( gitHubApi , repoName ) {
51+ function createRemoteRepo ( github , repoName ) {
5152 ui . write ( 'Creating remote repository...' ) ;
5253
5354 return new Promise ( function ( resolve , reject ) {
54- gitHubApi . repos . create ( {
55+ github . repos . create ( {
5556 name : repoName
5657 } , function ( err , result ) {
5758 if ( err ) {
@@ -64,7 +65,9 @@ function createRemoteRepo(gitHubApi, repoName) {
6465}
6566
6667function addRemoteOrigin ( localRepo , remoteRepo ) {
67- return git . Remote . create ( localRepo , 'origin' , remoteRepo . html_url ) ;
68+ var result = git . Remote . create ( localRepo , 'origin' , remoteRepo . html_url ) ;
69+ console . log ( 'result' , result ) ;
70+ return result ;
6871}
6972
7073function pushToRepo ( options , localRepo , remote ) {
@@ -84,33 +87,69 @@ function pushToRepo(options, localRepo, remote) {
8487 return remote . push ( refSpecs , pushOptions , signature , message ) ;
8588}
8689
87- function initializeGitHubAPI ( ) {
88- return new GitHubAPI ( {
90+ function initializeGitHub ( options ) {
91+ var github = new GitHubAPI ( {
8992 version : '3.0.0' ,
9093 } ) ;
94+
95+ return gitHubCredentials . getToken ( ) . then ( function ( token ) {
96+ console . log ( 'token exists:' , token ) ;
97+ authenticateWithToken ( github , token ) ;
98+ return github ;
99+ } ) . catch ( function ( ) {
100+ console . log ( 'token missing, creating' ) ;
101+ authenticateWithUserProvidedCredentials ( github , options ) ;
102+ return createToken ( github ) . then ( function ( ) {
103+ return github ;
104+ } ) ;
105+ } ) ;
91106}
92107
93- function authenticateGitHubAPI ( gitHubApi , options ) {
94- gitHubApi . authenticate ( {
108+ function authenticateWithToken ( github , token ) {
109+ github . authenticate ( {
110+ type : 'oauth' ,
111+ token : token ,
112+ } ) ;
113+ }
114+
115+ function authenticateWithUserProvidedCredentials ( github , options ) {
116+ github . authenticate ( {
95117 type : 'basic' ,
96118 username : options . username ,
97119 password : options . password
98120 } ) ;
99121}
100122
123+ function createToken ( github ) {
124+ return new Promise ( function ( resolve , reject ) {
125+ github . authorization . create ( {
126+ scopes : [ 'user' , 'public_repo' , 'repo' , 'repo:status' , 'gist' ] ,
127+ note : 'ember-micro-addon authorization' ,
128+ note_url : 'https://github.com/coderly/ember-micro-addon' ,
129+ headers : {
130+ 'X-GitHub-OTP' : 'two-factor-code'
131+ } ,
132+ } , function ( err , res ) {
133+ if ( err ) {
134+ reject ( err ) ;
135+ } else {
136+ resolve ( res . token ) ;
137+ }
138+ } ) ;
139+ } ) ;
140+ }
141+
101142module . exports = function ( options ) {
102143 options . workingDirectory = path . join ( process . cwd ( ) , options . addonName ) ;
103144
104- var gitHubApi = initializeGitHubAPI ( ) ;
105-
106- authenticateGitHubAPI ( gitHubApi , options ) ;
107-
108- ui . write ( 'Publishing ' + options . addonName + ' to GitHub...' ) ;
109- return createLocalRepo ( options ) . then ( function ( localRepo ) {
110- return createRemoteRepo ( gitHubApi , options . addonName ) . then ( function ( remoteRepo ) {
111- return addRemoteOrigin ( localRepo , remoteRepo ) ;
112- } ) . then ( function ( remoteResult ) {
113- return pushToRepo ( options , localRepo , remoteResult ) ;
145+ return initializeGitHub ( options ) . then ( function ( github ) {
146+ ui . write ( 'Publishing ' + options . addonName + ' to GitHub...' ) ;
147+ return createLocalRepo ( options ) . then ( function ( localRepo ) {
148+ return createRemoteRepo ( github , options . addonName ) . then ( function ( remoteRepo ) {
149+ return addRemoteOrigin ( localRepo , remoteRepo ) ;
150+ } ) . then ( function ( remoteResult ) {
151+ return pushToRepo ( options , localRepo , remoteResult ) ;
152+ } ) ;
114153 } ) ;
115154 } ) ;
116155
0 commit comments