fix(issue-57): truncate the bp branch name (#58)

fix: https://github.com/kiegroup/git-backporting/issues/57

This pr will ensure that if the provided/generated backport branch name
exceede the maximum branch name length set for git, which is 250 chars,
it truncates that name to 250 chars exactly.

In order to include as much commits as possible the branch name will
contain by default the shortened version of all commits
This commit is contained in:
Andrea Lamparelli 2023-07-11 22:46:21 +02:00 committed by GitHub
parent 91782505ce
commit ead1322c0f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 169 additions and 88 deletions

11
dist/cli/index.js vendored
View file

@ -1192,7 +1192,16 @@ class Runner {
await git.clone(configs.originalPullRequest.targetRepo.cloneUrl, configs.folder, configs.targetBranch);
// 5. create new branch from target one and checkout
this.logger.debug("Creating local branch..");
const backportBranch = backportPR.branchName ?? `bp-${configs.targetBranch}-${originalPR.commits.join("-")}`;
let backportBranch = backportPR.branchName;
if (backportBranch === undefined || backportBranch.trim() === "") {
// for each commit takes the first 7 chars that are enough to uniquely identify them in most of the projects
const concatenatedCommits = originalPR.commits.map(c => c.slice(0, 7)).join("-");
backportBranch = `bp-${configs.targetBranch}-${concatenatedCommits}`;
}
if (backportBranch.length > 250) {
this.logger.warn(`Backport branch (length=${backportBranch.length}) exceeded the max length of 250 chars, branch name truncated!`);
backportBranch = backportBranch.slice(0, 250);
}
await git.createLocalBranch(configs.folder, backportBranch);
// 6. fetch pull request remote if source owner != target owner or pull request still open
if (configs.originalPullRequest.sourceRepo.owner !== configs.originalPullRequest.targetRepo.owner ||