@@ -5,6 +5,7 @@ import { escapeRegExp } from '@socketsecurity/registry/lib/regexps'
55import { spawn } from '@socketsecurity/registry/lib/spawn'
66
77import constants from '../../constants.mts'
8+ import { getPurlObject } from '../../utils/purl.mts'
89import {
910 getPkgFullNameFromPurlObj ,
1011 getSocketDevPackageOverviewUrlFromPurl ,
@@ -13,11 +14,6 @@ import {
1314import type { CResult } from '../../types.mts'
1415import type { SpawnOptions } from '@socketsecurity/registry/lib/spawn'
1516
16- export type GetSocketPrTitlePatternOptions = {
17- purl ?: string | undefined
18- workspace ?: string | undefined
19- }
20-
2117export type GitCreateAndPushBranchOptions = {
2218 cwd ?: string | undefined
2319 email ?: string | undefined
@@ -42,72 +38,120 @@ export function getBaseGitBranch(): string {
4238}
4339
4440export function getSocketBranchName (
45- purl : string ,
41+ purl : string | PackageURL ,
4642 newVersion : string ,
47- workspaceName ?: string | undefined ,
43+ workspace ?: string | undefined ,
4844) : string {
49- const purlObj = PackageURL . fromString ( purl )
50- const maybeWorkspaceName = workspaceName
51- ? `${ formatBranchName ( workspaceName ) } -`
52- : ''
53- const maybeNamespace = purlObj . namespace
54- ? `${ formatBranchName ( purlObj . namespace ) } -`
45+ const purlObj = getPurlObject ( purl )
46+ const fmtType = formatBranchName ( purlObj . type )
47+ const fmtWorkspace = workspace ? `${ formatBranchName ( workspace ) } ` : 'root'
48+ const fmtMaybeNamespace = purlObj . namespace
49+ ? `${ formatBranchName ( purlObj . namespace ) } --`
5550 : ''
56- const fullName = `${ maybeWorkspaceName } ${ maybeNamespace } ${ formatBranchName ( purlObj . name ) } `
57- return `socket/${ fullName } -${ formatBranchName ( newVersion ) } `
51+ const fmtFullName = `${ fmtMaybeNamespace } ${ formatBranchName ( purlObj . name ) } `
52+ const fmtVersion = formatBranchName ( purlObj . version ! )
53+ const fmtNewVersion = formatBranchName ( newVersion )
54+ return `socket/${ fmtType } _${ fmtWorkspace } _${ fmtFullName } _${ fmtVersion } _${ fmtNewVersion } `
5855}
5956
60- export function getSocketPrTitlePattern (
61- options ?: GetSocketPrTitlePatternOptions | undefined ,
57+ export type SocketBranchPatternOptions = {
58+ newVersion ?: string | undefined
59+ purl ?: string | undefined
60+ workspace ?: string | undefined
61+ }
62+
63+ export function getSocketBranchPattern (
64+ options ?: SocketBranchPatternOptions | undefined ,
6265) : RegExp {
63- const { purl, workspace } = {
66+ const { newVersion , purl, workspace } = {
6467 __proto__ : null ,
6568 ...options ,
66- } as GetSocketPrTitlePatternOptions
67- const purlObj = purl ? PackageURL . fromString ( purl ) : null
68- const escapedPkgFullName = purlObj
69- ? escapeRegExp ( getPkgFullNameFromPurlObj ( purlObj ) )
70- : '\\S+'
71- const escapedPkgVersion = purlObj ? escapeRegExp ( purlObj . version ! ) : '\\S+ '
72- const escapedWorkspaceDetails = workspace
73- ? ` in ${ escapeRegExp ( workspace ) } `
69+ } as SocketBranchPatternOptions
70+ const purlObj = purl ? getPurlObject ( purl ) : null
71+ const escType = purlObj ? escapeRegExp ( purlObj . type ) : '[^_]+'
72+ const escWorkspace = workspace
73+ ? ` ${ escapeRegExp ( formatBranchName ( workspace ) ) } `
74+ : 'root '
75+ const escMaybeNamespace = purlObj ?. namespace
76+ ? `${ escapeRegExp ( formatBranchName ( purlObj . namespace ) ) } -- `
7477 : ''
78+ const escFullName = purlObj
79+ ? `${ escMaybeNamespace } ${ escapeRegExp ( formatBranchName ( purlObj . name ) ) } `
80+ : '[^_]+'
81+ const escVersion = purlObj
82+ ? escapeRegExp ( formatBranchName ( purlObj . version ! ) )
83+ : '[^_]+'
84+ const escNewVersion = newVersion
85+ ? escapeRegExp ( formatBranchName ( newVersion ) )
86+ : '[^_]+'
7587 return new RegExp (
76- `Bump ${ escapedPkgFullName } from ${ escapedPkgVersion } to \\S+ ${ escapedWorkspaceDetails } ` ,
88+ `^socket/( ${ escType } )_( ${ escWorkspace } )_( ${ escFullName } )_( ${ escVersion } )_( ${ escNewVersion } )$ ` ,
7789 )
7890}
7991
92+ export type SocketBranchParser = (
93+ branch : string ,
94+ ) => SocketBranchParseResult | null
95+
96+ export type SocketBranchParseResult = {
97+ newVersion : string
98+ purl : PackageURL
99+ workspace : string
100+ }
101+
102+ export function createSocketBranchParser (
103+ options ?: SocketBranchPatternOptions | undefined ,
104+ ) : SocketBranchParser {
105+ const pattern = getSocketBranchPattern ( options )
106+ return function parse ( branch : string ) : SocketBranchParseResult | null {
107+ const match = pattern . exec ( branch )
108+ if ( ! match ) {
109+ return null
110+ }
111+ const {
112+ 0 : type ,
113+ 1 : workspace ,
114+ 2 : fullName ,
115+ 3 : version ,
116+ 4 : newVersion ,
117+ } = match
118+ return {
119+ newVersion,
120+ purl : getPurlObject ( `pkg:${ type } /${ fullName } @${ version } ` ) ,
121+ workspace,
122+ } as SocketBranchParseResult
123+ }
124+ }
125+
80126export function getSocketPullRequestTitle (
81- purl : string ,
82- toVersion : string ,
127+ purl : string | PackageURL ,
128+ newVersion : string ,
83129 workspace ?: string | undefined ,
84130) : string {
85- const purlObj = PackageURL . fromString ( purl )
86- const pkgFullName = getPkgFullNameFromPurlObj ( purlObj )
87- const workspaceDetails = workspace ? ` in ${ workspace } ` : ''
88- return `Bump ${ pkgFullName } from ${ purlObj . version } to ${ toVersion } ${ workspaceDetails } `
131+ const purlObj = getPurlObject ( purl )
132+ const fullName = getPkgFullNameFromPurlObj ( purlObj )
133+ return `Bump ${ fullName } from ${ purlObj . version } to ${ newVersion } ${ workspace ? ` in ${ workspace } ` : '' } `
89134}
90135
91136export function getSocketPullRequestBody (
92- purl : string ,
137+ purl : string | PackageURL ,
93138 newVersion : string ,
94- workspaceName ?: string | undefined ,
139+ workspace ?: string | undefined ,
95140) : string {
96- const purlObj = PackageURL . fromString ( purl )
97- const pkgFullName = getPkgFullNameFromPurlObj ( purlObj )
98- const workspaceDetails = workspaceName ? ` in ${ workspaceName } ` : ''
99- return `Bump [${ pkgFullName } ](${ getSocketDevPackageOverviewUrlFromPurl ( purlObj ) } ) from ${ purlObj . version } to ${ newVersion } ${ workspaceDetails } .`
141+ const purlObj = getPurlObject ( purl )
142+ const fullName = getPkgFullNameFromPurlObj ( purlObj )
143+ const pkgOverviewUrl = getSocketDevPackageOverviewUrlFromPurl ( purlObj )
144+ return `Bump [${ fullName } ](${ pkgOverviewUrl } ) from ${ purlObj . version } to ${ newVersion } ${ workspace ? ` in ${ workspace } ` : '' } .`
100145}
101146
102147export function getSocketCommitMessage (
103- purl : string ,
148+ purl : string | PackageURL ,
104149 newVersion : string ,
105- workspaceName ?: string | undefined ,
150+ workspace ?: string | undefined ,
106151) : string {
107- const purlObj = PackageURL . fromString ( purl )
108- const pkgFullName = getPkgFullNameFromPurlObj ( purlObj )
109- const workspaceDetails = workspaceName ? ` in ${ workspaceName } ` : ''
110- return `socket: Bump ${ pkgFullName } from ${ purlObj . version } to ${ newVersion } ${ workspaceDetails } `
152+ const purlObj = getPurlObject ( purl )
153+ const fullName = getPkgFullNameFromPurlObj ( purlObj )
154+ return `socket: Bump ${ fullName } from ${ purlObj . version } to ${ newVersion } ${ workspace ? ` in ${ workspace } ` : '' } `
111155}
112156
113157export async function gitCleanFdx ( cwd = process . cwd ( ) ) : Promise < void > {
0 commit comments