Skip to content

Commit f1193c2

Browse files
authored
Add code to handle both rename and reassign of default branch (#632)
* Add code to handle both rename and reassign of default branch * reduced log data * Make the code more readable
1 parent f382e35 commit f1193c2

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

lib/plugins/repository.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,7 @@ module.exports = class Repository extends ErrorStash {
9191
const promises = []
9292
if (topicChanges.hasChanges) {
9393
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+
}
10095
if (changes.hasChanges) {
10196
this.log.debug('There are repo changes')
10297
let updateDefaultBranchPromise = Promise.resolve()
@@ -166,40 +161,50 @@ module.exports = class Repository extends ErrorStash {
166161
owner: this.settings.owner,
167162
repo: this.settings.repo,
168163
branch: newname
169-
}).then(() => {
164+
}).then((res) => {
170165
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)
178170
} 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`)
185171
const parms = {
186172
owner: this.settings.owner,
187173
repo: this.settings.repo,
188-
branch: oldname,
189-
new_name: newname
174+
default_branch: newname
190175
}
191-
this.log.info(`Rename default branch repo with settings ${JSON.stringify(parms)}`)
192176
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'))
194178
} 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)
196181
}
182+
}
183+
}).catch(e => {
184+
if (e.status === 404) {
185+
return this.renameBranch(oldname, newname, resArray)
197186
} else {
198187
this.logError(`Error ${JSON.stringify(e)}`)
199188
}
200189
})
201190
}
202191

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+
203208
updaterepo (resArray) {
204209
this.log.debug(`Updating repo with settings ${JSON.stringify(this.topics)} ${JSON.stringify(this.settings)}`)
205210
if (this.nop) {

0 commit comments

Comments
 (0)