Skip to content

Commit

Permalink
fix(issue-57): truncate the bp branch name (#58)
Browse files Browse the repository at this point in the history
fix: #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
  • Loading branch information
lampajr authored Jul 11, 2023
1 parent 9178250 commit ead1322
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 88 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

---

## :bangbang: Starting from v4 git-backporting has been moved under @kiegroup organization and renamed :bangbang:
## :bangbang: Starting from v4 git-backporting has been moved under @kiegroup organization :bangbang:

---

Expand Down Expand Up @@ -109,7 +109,7 @@ This tool comes with some inputs that allow users to override the default behavi
| Reviewers | --reviewers | N | Backporting pull request comma-separated reviewers list | [] |
| Assignees | --assignes | N | Backporting pull request comma-separated assignees list | [] |
| No Reviewers Inheritance | --no-inherit-reviewers | N | Considered only if reviewers is empty, if true keep reviewers as empty list, otherwise inherit from original pull request | false |
| Backport Branch Name | --bp-branch-name | N | Name of the backporting pull request branch | bp-{target-branch}-{sha} |
| Backport Branch Name | --bp-branch-name | N | Name of the backporting pull request branch, if it exceeds 250 chars it will be truncated | bp-{target-branch}-{sha1}...{shaN} |
| Labels | --labels | N | Provide custom labels to be added to the backporting pull request | [] |
| Inherit labels | --inherit-labels | N | If enabled inherit lables from the original pull request | false |
| No squash | --no-squash | N | If provided the backporting will try to backport all pull request commits without squashing | false |
Expand Down
11 changes: 10 additions & 1 deletion dist/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
11 changes: 10 additions & 1 deletion dist/gha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,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 ||
Expand Down
13 changes: 12 additions & 1 deletion src/service/runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,18 @@ export default class Runner {

// 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: string = 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
Expand Down
Loading

0 comments on commit ead1322

Please sign in to comment.