@@ -8,6 +8,7 @@ function instanceEditPrimaryActions(
88 QueryAssist ,
99 $rootScope ,
1010 $state ,
11+ errs ,
1112 $stateParams
1213) {
1314 return {
@@ -17,7 +18,8 @@ function instanceEditPrimaryActions(
1718 user : '=' ,
1819 instance : '=' ,
1920 loading : '=' ,
20- openItems : '='
21+ openItems : '=' ,
22+ unsavedAcvs : '='
2123 } ,
2224 link : function ( $scope , elem , attrs ) {
2325 // prevent multiple clicks
@@ -48,31 +50,34 @@ function instanceEditPrimaryActions(
4850 }
4951 } , cb ) ;
5052 } ,
53+ updateAppCodeVersions ,
5154 function ( ) {
5255 var build = $scope . newBuild ;
5356 // Catch the update file error
5457 $scope . newBuild . build (
5558 buildObj ,
5659 function ( err ) {
57- if ( err ) { throw err ; }
60+ if ( err ) {
61+ return handleError ( err ) ;
62+ }
5863 var opts = {
5964 build : build . id ( )
6065 } ;
6166 if ( $scope . instance . state && $scope . instance . state . env ) {
6267 opts . env = $scope . instance . state . env ;
6368 }
6469 $scope . instance . update ( opts , function ( err ) {
65- if ( err ) { throw err ; }
70+ if ( err ) {
71+ return handleError ( err ) ;
72+ }
6673 // will trigger display of completed message if build completes
6774 // before reaching next state
6875 // $scope.dataInstanceLayout.data.showBuildCompleted = true;
6976 $state . go ( 'instance.instance' , $stateParams ) ;
7077 } ) ;
7178 } ) ;
7279 }
73- ] , function ( err ) {
74- if ( err ) { throw err ; }
75- } ) ;
80+ ] , handleError ) ;
7681 } ) ;
7782 } ;
7883
@@ -97,12 +102,63 @@ function instanceEditPrimaryActions(
97102 $scope . newBuild = build ;
98103 cb ( ) ;
99104 } )
100- . resolve ( function ( err ) {
101- if ( err ) { throw err ; }
102- } )
105+ . resolve ( handleError )
103106 . go ( ) ;
104107 }
105108
109+ function updateAppCodeVersions ( cb ) {
110+ var modifiedAcvs = $scope . unsavedAcvs . filter ( function ( obj ) {
111+ return obj . unsavedAcv . attrs . commit !== obj . acv . attrs . commit ;
112+ } ) ;
113+ if ( ! modifiedAcvs . length ) {
114+ return cb ( ) ;
115+ }
116+
117+ var context = $scope . newBuild . contexts . models [ 0 ] ;
118+ var contextVersion = $scope . newBuild . contextVersions . models [ 0 ] ;
119+ var infraCodeVersionId = contextVersion . attrs . infraCodeVersion ;
120+
121+ var appCodeVersionStates = $scope . unsavedAcvs . map ( function ( obj ) {
122+ var acv = obj . unsavedAcv ;
123+ return {
124+ repo : acv . attrs . repo ,
125+ branch : acv . attrs . branch ,
126+ commit : acv . attrs . commit
127+ } ;
128+ } ) ;
129+ async . waterfall ( [
130+ createContextVersion ,
131+ createBuild
132+ ] , cb ) ;
133+
134+ function createContextVersion ( cb ) {
135+ var body = {
136+ infraCodeVersion : infraCodeVersionId
137+ } ;
138+ var newContextVersion = context . createVersion ( body , function ( err ) {
139+ async . each ( appCodeVersionStates , function ( acvState , cb ) {
140+ newContextVersion . appCodeVersions . create ( acvState , cb ) ;
141+ } , function ( err ) {
142+ cb ( err , newContextVersion ) ;
143+ } ) ;
144+ } ) ;
145+ }
146+ function createBuild ( contextVersion , cb ) {
147+ var build = $scope . user . createBuild ( {
148+ contextVersions : [ contextVersion . id ( ) ] ,
149+ owner : $scope . instance . attrs . owner
150+ } , function ( err ) {
151+ $scope . newBuild = build ;
152+ cb ( err , build ) ;
153+ } ) ;
154+ }
155+ }
156+ function handleError ( err ) {
157+ if ( err ) {
158+ $scope . loading = false ;
159+ errs . handler ( err ) ;
160+ }
161+ }
106162 }
107163 } ;
108164}
0 commit comments