Skip to content

Commit

Permalink
Add code to handle both rename and reassign of default branch (#632)
Browse files Browse the repository at this point in the history
* Add code to handle both rename and reassign of default branch

* reduced log data

* Make the code more readable
  • Loading branch information
decyjphr authored May 22, 2024
1 parent f382e35 commit f1193c2
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions lib/plugins/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ module.exports = class Repository extends ErrorStash {
const promises = []
if (topicChanges.hasChanges) {
promises.push(this.updatetopics(resp.data, resArray))
} else {
this.log.debug(`There are no changes for repo ${JSON.stringify(this.repo)}.`)
if (this.nop) {
resArray.push(new NopCommand('Repository', this.repo, null, `There are no changes for repo ${JSON.stringify(this.repo)}.`))
}
}
}
if (changes.hasChanges) {
this.log.debug('There are repo changes')
let updateDefaultBranchPromise = Promise.resolve()
Expand Down Expand Up @@ -166,40 +161,50 @@ module.exports = class Repository extends ErrorStash {
owner: this.settings.owner,
repo: this.settings.repo,
branch: newname
}).then(() => {
}).then((res) => {
this.log.debug(`Branch ${newname} already exists. Making it the default branch`)
const parms = {
owner: this.settings.owner,
repo: this.settings.repo,
default_branch: newname
}
if (this.nop) {
resArray.push(new NopCommand(this.constructor.name, this.repo, this.github.repos.update.endpoint(parms), 'Update Repo'))
// If the old branch was renamed github will find the branch with the oldname the branch but the ref doesn't exist
// So we'd have to rename it back to the oldname
if (res.data.name !== newname) {
return this.renameBranch(oldname, newname, resArray)
} else {
this.log.debug(`Updating repo with settings ${JSON.stringify(parms)}`)
return this.github.repos.update(parms)
}
}).catch(e => {
if (e.status === 404) {
this.log.debug(`${newname} does not exist`)
const parms = {
owner: this.settings.owner,
repo: this.settings.repo,
branch: oldname,
new_name: newname
default_branch: newname
}
this.log.info(`Rename default branch repo with settings ${JSON.stringify(parms)}`)
if (this.nop) {
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}`))
resArray.push(new NopCommand(this.constructor.name, this.repo, this.github.repos.update.endpoint(parms), 'Update Repo'))
} else {
return this.github.repos.renameBranch(parms)
this.log.debug(`Updating repo with settings ${JSON.stringify(parms)}`)
return this.github.repos.update(parms)
}
}
}).catch(e => {
if (e.status === 404) {
return this.renameBranch(oldname, newname, resArray)
} else {
this.logError(`Error ${JSON.stringify(e)}`)
}
})
}

renameBranch(oldname, newname, resArray) {
this.log.error(`Branch ${newname} does not exist. So renaming the current default branch ${oldname} to ${newname}`)
const parms = {
owner: this.settings.owner,
repo: this.settings.repo,
branch: oldname,
new_name: newname
}
this.log.info(`Rename default branch repo with settings ${JSON.stringify(parms)}`)
if (this.nop) {
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}`))
} else {
return this.github.repos.renameBranch(parms)
}
}

updaterepo (resArray) {
this.log.debug(`Updating repo with settings ${JSON.stringify(this.topics)} ${JSON.stringify(this.settings)}`)
if (this.nop) {
Expand Down

0 comments on commit f1193c2

Please sign in to comment.