@@ -8,6 +8,7 @@ function instanceEditPrimaryActions(
8
8
QueryAssist ,
9
9
$rootScope ,
10
10
$state ,
11
+ errs ,
11
12
$stateParams
12
13
) {
13
14
return {
@@ -17,7 +18,8 @@ function instanceEditPrimaryActions(
17
18
user : '=' ,
18
19
instance : '=' ,
19
20
loading : '=' ,
20
- openItems : '='
21
+ openItems : '=' ,
22
+ unsavedAcvs : '='
21
23
} ,
22
24
link : function ( $scope , elem , attrs ) {
23
25
// prevent multiple clicks
@@ -48,31 +50,34 @@ function instanceEditPrimaryActions(
48
50
}
49
51
} , cb ) ;
50
52
} ,
53
+ updateAppCodeVersions ,
51
54
function ( ) {
52
55
var build = $scope . newBuild ;
53
56
// Catch the update file error
54
57
$scope . newBuild . build (
55
58
buildObj ,
56
59
function ( err ) {
57
- if ( err ) { throw err ; }
60
+ if ( err ) {
61
+ return handleError ( err ) ;
62
+ }
58
63
var opts = {
59
64
build : build . id ( )
60
65
} ;
61
66
if ( $scope . instance . state && $scope . instance . state . env ) {
62
67
opts . env = $scope . instance . state . env ;
63
68
}
64
69
$scope . instance . update ( opts , function ( err ) {
65
- if ( err ) { throw err ; }
70
+ if ( err ) {
71
+ return handleError ( err ) ;
72
+ }
66
73
// will trigger display of completed message if build completes
67
74
// before reaching next state
68
75
// $scope.dataInstanceLayout.data.showBuildCompleted = true;
69
76
$state . go ( 'instance.instance' , $stateParams ) ;
70
77
} ) ;
71
78
} ) ;
72
79
}
73
- ] , function ( err ) {
74
- if ( err ) { throw err ; }
75
- } ) ;
80
+ ] , handleError ) ;
76
81
} ) ;
77
82
} ;
78
83
@@ -97,12 +102,63 @@ function instanceEditPrimaryActions(
97
102
$scope . newBuild = build ;
98
103
cb ( ) ;
99
104
} )
100
- . resolve ( function ( err ) {
101
- if ( err ) { throw err ; }
102
- } )
105
+ . resolve ( handleError )
103
106
. go ( ) ;
104
107
}
105
108
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
+ }
106
162
}
107
163
} ;
108
164
}
0 commit comments