@@ -91,12 +91,7 @@ module.exports = class Repository extends ErrorStash {
91
91
const promises = [ ]
92
92
if ( topicChanges . hasChanges ) {
93
93
promises . push ( this . updatetopics ( resp . data , resArray ) )
94
- } else {
95
- this . log . debug ( `There are no changes for repo ${ JSON . stringify ( this . repo ) } .` )
96
- if ( this . nop ) {
97
- resArray . push ( new NopCommand ( 'Repository' , this . repo , null , `There are no changes for repo ${ JSON . stringify ( this . repo ) } .` ) )
98
- }
99
- }
94
+ }
100
95
if ( changes . hasChanges ) {
101
96
this . log . debug ( 'There are repo changes' )
102
97
let updateDefaultBranchPromise = Promise . resolve ( )
@@ -166,40 +161,50 @@ module.exports = class Repository extends ErrorStash {
166
161
owner : this . settings . owner ,
167
162
repo : this . settings . repo ,
168
163
branch : newname
169
- } ) . then ( ( ) => {
164
+ } ) . then ( ( res ) => {
170
165
this . log . debug ( `Branch ${ newname } already exists. Making it the default branch` )
171
- const parms = {
172
- owner : this . settings . owner ,
173
- repo : this . settings . repo ,
174
- default_branch : newname
175
- }
176
- if ( this . nop ) {
177
- resArray . push ( new NopCommand ( this . constructor . name , this . repo , this . github . repos . update . endpoint ( parms ) , 'Update Repo' ) )
166
+ // If the old branch was renamed github will find the branch with the oldname the branch but the ref doesn't exist
167
+ // So we'd have to rename it back to the oldname
168
+ if ( res . data . name !== newname ) {
169
+ return this . renameBranch ( oldname , newname , resArray )
178
170
} else {
179
- this . log . debug ( `Updating repo with settings ${ JSON . stringify ( parms ) } ` )
180
- return this . github . repos . update ( parms )
181
- }
182
- } ) . catch ( e => {
183
- if ( e . status === 404 ) {
184
- this . log . debug ( `${ newname } does not exist` )
185
171
const parms = {
186
172
owner : this . settings . owner ,
187
173
repo : this . settings . repo ,
188
- branch : oldname ,
189
- new_name : newname
174
+ default_branch : newname
190
175
}
191
- this . log . info ( `Rename default branch repo with settings ${ JSON . stringify ( parms ) } ` )
192
176
if ( this . nop ) {
193
- resArray . push ( new NopCommand ( this . constructor . name , this . repo , this . github . repos . renameBranch . endpoint ( oldname , this . settings . default_branch ) , `Repo rename default branch to ${ this . settings . default_branch } ` ) )
177
+ resArray . push ( new NopCommand ( this . constructor . name , this . repo , this . github . repos . update . endpoint ( parms ) , 'Update Repo' ) )
194
178
} else {
195
- return this . github . repos . renameBranch ( parms )
179
+ this . log . debug ( `Updating repo with settings ${ JSON . stringify ( parms ) } ` )
180
+ return this . github . repos . update ( parms )
196
181
}
182
+ }
183
+ } ) . catch ( e => {
184
+ if ( e . status === 404 ) {
185
+ return this . renameBranch ( oldname , newname , resArray )
197
186
} else {
198
187
this . logError ( `Error ${ JSON . stringify ( e ) } ` )
199
188
}
200
189
} )
201
190
}
202
191
192
+ renameBranch ( oldname , newname , resArray ) {
193
+ this . log . error ( `Branch ${ newname } does not exist. So renaming the current default branch ${ oldname } to ${ newname } ` )
194
+ const parms = {
195
+ owner : this . settings . owner ,
196
+ repo : this . settings . repo ,
197
+ branch : oldname ,
198
+ new_name : newname
199
+ }
200
+ this . log . info ( `Rename default branch repo with settings ${ JSON . stringify ( parms ) } ` )
201
+ if ( this . nop ) {
202
+ resArray . push ( new NopCommand ( this . constructor . name , this . repo , this . github . repos . renameBranch . endpoint ( oldname , this . settings . default_branch ) , `Repo rename default branch to ${ this . settings . default_branch } ` ) )
203
+ } else {
204
+ return this . github . repos . renameBranch ( parms )
205
+ }
206
+ }
207
+
203
208
updaterepo ( resArray ) {
204
209
this . log . debug ( `Updating repo with settings ${ JSON . stringify ( this . topics ) } ${ JSON . stringify ( this . settings ) } ` )
205
210
if ( this . nop ) {
0 commit comments