@@ -16,6 +16,7 @@ import { ImportGithubProjectService, LoadingState } from './import-github-projec
1616import { ProjectSource } from '../../project-source.enum' ;
1717import { IGithubRepository } from './github-repository-interface' ;
1818import { AddImportProjectService } from '../add-import-project.service' ;
19+ import { CheNotification } from '../../../../../../../components/notification/che-notification.factory' ;
1920
2021/**
2122 * This class is handling the controller for the GitHub part
@@ -25,9 +26,23 @@ import { AddImportProjectService } from '../add-import-project.service';
2526 */
2627export class ImportGithubProjectController {
2728
28- static $inject = [ '$q' , '$mdDialog' , '$location' , '$browser' , '$scope' , 'githubPopup' , 'cheBranding' , 'githubOrganizationNameResolver' ,
29- 'importGithubProjectService' , 'cheListHelperFactory' , 'addImportProjectService' , 'keycloakAuth' ] ;
29+ static $inject = [
30+ '$browser' ,
31+ '$location' ,
32+ '$mdDialog' ,
33+ '$q' ,
34+ '$scope' ,
35+ 'addImportProjectService' ,
36+ 'cheBranding' ,
37+ 'cheListHelperFactory' ,
38+ 'cheNotification' ,
39+ 'githubOrganizationNameResolver' ,
40+ 'githubPopup' ,
41+ 'importGithubProjectService' ,
42+ 'keycloakAuth' ,
43+ ] ;
3044
45+ private cheNotification : CheNotification
3146 /**
3247 * Promises service.
3348 */
@@ -121,21 +136,33 @@ export class ImportGithubProjectController {
121136 /**
122137 * Default constructor that is using resource
123138 */
124- constructor ( $q : ng . IQService , $mdDialog : ng . material . IDialogService , $location : ng . ILocationService ,
125- $browser : any , $scope : ng . IScope , githubPopup : any , cheBranding : CheBranding ,
126- githubOrganizationNameResolver : any , importGithubProjectService : ImportGithubProjectService ,
127- cheListHelperFactory : che . widget . ICheListHelperFactory , addImportProjectService : AddImportProjectService , keycloakAuth : any ) {
128- this . $q = $q ;
129- this . $mdDialog = $mdDialog ;
130- this . $location = $location ;
139+ constructor (
140+ $browser : any ,
141+ $location : ng . ILocationService ,
142+ $mdDialog : ng . material . IDialogService ,
143+ $q : ng . IQService ,
144+ $scope : ng . IScope ,
145+ addImportProjectService : AddImportProjectService ,
146+ cheBranding : CheBranding ,
147+ cheListHelperFactory : che . widget . ICheListHelperFactory ,
148+ cheNotification : CheNotification ,
149+ githubOrganizationNameResolver : any ,
150+ githubPopup : any ,
151+ importGithubProjectService : ImportGithubProjectService ,
152+ keycloakAuth : any ,
153+ ) {
131154 this . $browser = $browser ;
132- this . githubPopup = githubPopup ;
155+ this . $location = $location ;
156+ this . $mdDialog = $mdDialog ;
157+ this . $q = $q ;
158+ this . addImportProjectService = addImportProjectService ;
133159 this . cheBranding = cheBranding ;
160+ this . cheNotification = cheNotification ;
134161 this . githubOrganizationNameResolver = githubOrganizationNameResolver ;
135- this . resolveOrganizationName = this . githubOrganizationNameResolver . resolve ;
136- this . addImportProjectService = addImportProjectService ;
137- this . keycloakAuth = keycloakAuth ;
162+ this . githubPopup = githubPopup ;
138163 this . importGithubProjectService = importGithubProjectService ;
164+ this . keycloakAuth = keycloakAuth ;
165+ this . resolveOrganizationName = this . githubOrganizationNameResolver . resolve ;
139166
140167 this . productName = cheBranding . getName ( ) ;
141168 this . loadingState = LoadingState ;
@@ -232,25 +259,31 @@ export class ImportGithubProjectController {
232259 return ;
233260 }
234261
235- if ( this . keycloakAuth . isPresent ) {
236- this . keycloakAuth . keycloak . updateToken ( 5 ) . success ( ( ) => {
237- let token = '&token=' + this . keycloakAuth . keycloak . token ;
238- this . openGithubPopup ( token ) ;
239- } ) . error ( ( ) => {
240- window . sessionStorage . setItem ( 'oidcDashboardRedirectUrl' , location . href ) ;
241- this . keycloakAuth . keycloak . login ( ) ;
242- } ) ;
243- } else {
244- this . openGithubPopup ( '' ) ;
245- }
262+ this . importGithubProjectService . getOrFetchUserId ( ) . then ( ( userId : string ) => {
263+ if ( this . keycloakAuth . isPresent ) {
264+ this . keycloakAuth . keycloak . updateToken ( 5 ) . success ( ( ) => {
265+ let token = '&token=' + this . keycloakAuth . keycloak . token ;
266+ this . openGithubPopup ( userId , token ) ;
267+ } ) . error ( ( ) => {
268+ window . sessionStorage . setItem ( 'oidcDashboardRedirectUrl' , location . href ) ;
269+ this . keycloakAuth . keycloak . login ( ) ;
270+ } ) ;
271+ } else {
272+ this . openGithubPopup ( userId , '' ) ;
273+ }
274+ } ) . catch ( ( error : any ) => {
275+ this . cheNotification . showError ( 'Authentication to GitHub failed.' ) ;
276+ console . error ( 'Authentication to GitHub failed:' , error ) ;
277+ } ) ;
246278 }
247279
248280 /**
249281 * Opens Github popup.
250282 *
283+ * @param {string } userId
251284 * @param {string } token
252285 */
253- openGithubPopup ( token : string ) : void {
286+ openGithubPopup ( userId : string , token : string ) : void {
254287 // given URL http://example.com - returns port => 80 (or 443 with https), which causes wrong redirect URL value:
255288 let port = ( this . $location . port ( ) === 80 || this . $location . port ( ) === 443 ) ? '' : ':' + this . $location . port ( ) ;
256289 const redirectUrl = this . $location . protocol ( ) + '://'
@@ -261,7 +294,7 @@ export class ImportGithubProjectController {
261294 let link = '/api/oauth/authenticate'
262295 + '?oauth_provider=github'
263296 + '&scope=' + [ 'user' , 'repo' , 'write:public_key' ] . join ( ',' )
264- + '&userId=' + this . importGithubProjectService . getCurrentUserId ( )
297+ + '&userId=' + userId
265298 + token
266299 + '&redirect_after_login='
267300 + redirectUrl ;
0 commit comments