From 99c52e646f959563a98529976f6c63bb04fedcad Mon Sep 17 00:00:00 2001 From: Yadhav Jayaraman <57544838+decyjphr@users.noreply.github.com> Date: Thu, 16 May 2024 09:40:37 -0400 Subject: [PATCH 1/3] Add code to handle both rename and reassign of default branch --- lib/plugins/repository.js | 46 +++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/plugins/repository.js b/lib/plugins/repository.js index ca35806f..cbcf00ce 100644 --- a/lib/plugins/repository.js +++ b/lib/plugins/repository.js @@ -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() @@ -166,8 +161,13 @@ module.exports = class Repository extends ErrorStash { owner: this.settings.owner, repo: this.settings.repo, branch: newname - }).then(() => { - this.log.debug(`Branch ${newname} already exists. Making it the default branch`) + }).then((res) => { + this.log.debug(`Branch ${newname} already exists ${JSON.stringify(res.data)}. Making it the default branch`) + // 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) + } const parms = { owner: this.settings.owner, repo: this.settings.repo, @@ -181,25 +181,29 @@ module.exports = class Repository extends ErrorStash { } }).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 - } - 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) - } + 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) { From fab2b4ad543955a1911ac80101070699e19fba69 Mon Sep 17 00:00:00 2001 From: Yadhav Jayaraman <57544838+decyjphr@users.noreply.github.com> Date: Thu, 16 May 2024 09:52:58 -0400 Subject: [PATCH 2/3] reduced log data --- lib/plugins/repository.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugins/repository.js b/lib/plugins/repository.js index cbcf00ce..2bf7ba54 100644 --- a/lib/plugins/repository.js +++ b/lib/plugins/repository.js @@ -162,7 +162,7 @@ module.exports = class Repository extends ErrorStash { repo: this.settings.repo, branch: newname }).then((res) => { - this.log.debug(`Branch ${newname} already exists ${JSON.stringify(res.data)}. Making it the default branch`) + this.log.debug(`Branch ${newname} already exists. Making it the default branch`) // 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) { From b922276904de5562b070a1f91e4b73d2512c3266 Mon Sep 17 00:00:00 2001 From: Yadhav Jayaraman <57544838+decyjphr@users.noreply.github.com> Date: Thu, 16 May 2024 09:54:33 -0400 Subject: [PATCH 3/3] Make the code more readable --- lib/plugins/repository.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/plugins/repository.js b/lib/plugins/repository.js index 2bf7ba54..42bbd28e 100644 --- a/lib/plugins/repository.js +++ b/lib/plugins/repository.js @@ -167,17 +167,18 @@ module.exports = class Repository extends ErrorStash { // So we'd have to rename it back to the oldname if (res.data.name !== newname) { return this.renameBranch(oldname, newname, resArray) - } - 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')) } else { - this.log.debug(`Updating repo with settings ${JSON.stringify(parms)}`) - return this.github.repos.update(parms) + 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')) + } else { + this.log.debug(`Updating repo with settings ${JSON.stringify(parms)}`) + return this.github.repos.update(parms) + } } }).catch(e => { if (e.status === 404) {