Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c3ab8ff

Browse files
authored
Prevent branches ending with dash (#119)
* fix: prevent branches ending with dashes * fix: skip sonar check if repo is a fork * fix: skip sonar check if repo is a fork, for sonar-branch-analysis
1 parent 3f82c1a commit c3ab8ff

4 files changed

Lines changed: 40 additions & 4 deletions

File tree

.github/workflows/PR-verify.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ jobs:
2929
make verify
3030
3131
- name: SonarCloud Scan
32+
if: ${{ vars.IS_INDITEXTECH_REPO == 'true' }}
3233
uses: sonarsource/sonarcloud-github-action@master
3334
env:
3435
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -70,4 +71,4 @@ jobs:
7071
ref: ${{ github.event.pull_request.head.sha }}
7172

7273
- name: REUSE Compliance Check
73-
uses: fsfe/reuse-action@v3
74+
uses: fsfe/reuse-action@v3

.github/workflows/sonar-branch-analysis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ env:
1212

1313
jobs:
1414
verify:
15-
if: github.event.pull_request.merged == true
15+
if: ${{ github.event.pull_request.merged == true && vars.IS_INDITEXTECH_REPO == 'true' }}
1616
name: Verify
1717
runs-on: ubuntu-latest
1818
steps:
@@ -37,4 +37,4 @@ jobs:
3737
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
3838
with:
3939
args: >
40-
-Dsonar.branch.name=main
40+
-Dsonar.branch.name=main

internal/branches/branches.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ func (b BranchProvider) formatBranchName(repoNameWithOwner string, branchType st
120120
branchName = branchName[:min(maxBranchNameLength, branchNameLength)]
121121
}
122122

123-
return strings.TrimSuffix(branchName, "-")
123+
// Remove all trailing dashes to ensure branches never end with a dash
124+
trailingDashPattern := regexp.MustCompile(`-+$`)
125+
return trailingDashPattern.ReplaceAllString(branchName, "")
124126
}
125127

126128
// min returns the smallest of x or y.

internal/branches/branches_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,39 @@ func TestFormatBranchName(t *testing.T) {
283283
},
284284
want: "feature/GH-1-this-is-a-very-long-title-that-will-not-be-cropped",
285285
},
286+
{
287+
name: "Does not end with dash when truncating at single dash",
288+
args: args{
289+
repository: repositoryName,
290+
branchType: "feature",
291+
issueId: "JIRA-123",
292+
issueContext: "back-do-something",
293+
maxLength: 43, // "InditexTech/gh-sherpa" (21) + "feature/JIRA-123-back-" (22) = 43, so this truncates to "feature/JIRA-123-back-"
294+
},
295+
want: "feature/JIRA-123-back",
296+
},
297+
{
298+
name: "Does not end with dash when truncating at multiple dashes",
299+
args: args{
300+
repository: repositoryName,
301+
branchType: "feature",
302+
issueId: "JIRA-123",
303+
issueContext: "back--user",
304+
maxLength: 44, // This will truncate to "feature/JIRA-123-back--" and should become "feature/JIRA-123-back"
305+
},
306+
want: "feature/JIRA-123-back",
307+
},
308+
{
309+
name: "Does not end with dash when truncating at multiple consecutive dashes",
310+
args: args{
311+
repository: repositoryName,
312+
branchType: "feature",
313+
issueId: "JIRA-123",
314+
issueContext: "back---user-do-something",
315+
maxLength: 45, // This will truncate to "feature/JIRA-123-back---" and should become "feature/JIRA-123-back"
316+
},
317+
want: "feature/JIRA-123-back",
318+
},
286319
}
287320

288321
for _, tt := range tests {

0 commit comments

Comments
 (0)