-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix for invalid characters in branch name (#1392)
The majority of the changes were merged in previous PRs?
- Loading branch information
Rhys Koedijk
authored
Oct 15, 2024
1 parent
905ae4d
commit 42e9ac9
Showing
2 changed files
with
76 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
extension/tasks/dependabotV2/utils/dependabot-cli/getBranchName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as crypto from 'crypto'; | ||
|
||
export function getBranchNameForUpdate( | ||
packageEcosystem: string, | ||
targetBranchName: string, | ||
directory: string, | ||
dependencyGroupName: string, | ||
dependencies: any, | ||
separator?: string, | ||
): string { | ||
// Based on dependabot-core implementation: | ||
// https://github.com/dependabot/dependabot-core/blob/main/common/lib/dependabot/pull_request_creator/branch_namer/solo_strategy.rb | ||
// https://github.com/dependabot/dependabot-core/blob/main/common/lib/dependabot/pull_request_creator/branch_namer/dependency_group_strategy.rb | ||
let branchName: string; | ||
const branchNameMightBeTooLong = dependencyGroupName || dependencies.length > 1; | ||
if (branchNameMightBeTooLong) { | ||
// Group/multi dependency update | ||
// e.g. dependabot/nuget/main/microsoft-3b49c54d9e | ||
const dependencyDigest = crypto | ||
.createHash('md5') | ||
.update(dependencies.map((d) => `${d['dependency-name']}-${d['dependency-version']}`).join(',')) | ||
.digest('hex') | ||
.substring(0, 10); | ||
branchName = `${dependencyGroupName || 'multi'}-${dependencyDigest}`; | ||
} else { | ||
// Single dependency update | ||
// e.g. dependabot/nuget/main/Microsoft.Extensions.Logging-1.0.0 | ||
const dependencyNames = dependencies | ||
.map((d) => d['dependency-name']) | ||
.join('-and-') | ||
.replace(/[:\[\]]/g, '-') // Replace `:` and `[]` with `-` | ||
.replace(/@/g, ''); // Remove `@` | ||
const versionSuffix = dependencies[0]?.['removed'] ? 'removed' : dependencies[0]?.['dependency-version']; | ||
branchName = `${dependencyNames}-${versionSuffix}`; | ||
} | ||
|
||
// TODO: Add config for the branch prefix? Task V1 supported this via DEPENDABOT_BRANCH_NAME_PREFIX | ||
return sanitizeRef(['dependabot', packageEcosystem, targetBranchName, directory, branchName], separator || '/'); | ||
} | ||
|
||
function sanitizeRef(refParts: string[], separator): string { | ||
// Based on dependabot-core implementation: | ||
// https://github.com/dependabot/dependabot-core/blob/fc31ae64f492dc977cfe6773ab13fb6373aabec4/common/lib/dependabot/pull_request_creator/branch_namer/base.rb#L99 | ||
|
||
// This isn't a complete implementation of git's ref validation, but it | ||
// covers most cases that crop up. Its list of allowed characters is a | ||
// bit stricter than git's, but that's for cosmetic reasons. | ||
return ( | ||
refParts | ||
// Join the parts with the separator, ignore empty parts | ||
.filter((p) => p?.trim()?.length > 0) | ||
.join(separator) | ||
// Remove forbidden characters (those not already replaced elsewhere) | ||
.replace(/[^A-Za-z0-9\/\-_.(){}]/g, '') | ||
// Slashes can't be followed by periods | ||
.replace(/\/\./g, '/dot-') | ||
// Squeeze out consecutive periods and slashes | ||
.replace(/\.+/g, '.') | ||
.replace(/\/+/g, '/') | ||
// Trailing periods are forbidden | ||
.replace(/\.$/, '') | ||
); | ||
} |