Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code to handle both rename and reassign of default branch #632

Merged
merged 3 commits into from
May 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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