Skip to content

Commit

Permalink
feat(github-action): retain spaces in extracted values (#32730)
Browse files Browse the repository at this point in the history
Signed-off-by: JP-Ellis <[email protected]>
Co-authored-by: Michael Kriese <[email protected]>
Co-authored-by: Rhys Arkins <[email protected]>
  • Loading branch information
3 people authored Nov 28, 2024
1 parent 43bd81b commit 66f1ef5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
53 changes: 53 additions & 0 deletions lib/modules/manager/github-actions/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,59 @@ describe('modules/manager/github-actions/extract', () => {
]);
});

it('maintains spaces between hash and comment', () => {
const yamlContent = `
jobs:
build:
steps:
# One space
- name: "test1"
uses: actions/setup-node@56337c425554a6be30cdef71bf441f15be286854 # tag=v3.1.1
- name: "test2"
uses: 'actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561' # tag=v3.1.1
- name: "test3"
uses: "actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561" # tag=v2.5.1
# Two space
- name: "test1"
uses: actions/setup-node@56337c425554a6be30cdef71bf441f15be286854 # tag=v3.1.1
- name: "test2"
uses: 'actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561' # tag=v3.1.1
- name: "test3"
uses: "actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561" # tag=v2.5.1
"`;

const res = extractPackageFile(yamlContent, 'workflow.yml');
expect(res).toMatchObject({
deps: [
{
replaceString:
'actions/setup-node@56337c425554a6be30cdef71bf441f15be286854 # tag=v3.1.1',
},
{
replaceString:
"'actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561' # tag=v3.1.1",
},
{
replaceString:
'"actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561" # tag=v2.5.1',
},
{
replaceString:
'actions/setup-node@56337c425554a6be30cdef71bf441f15be286854 # tag=v3.1.1',
},
{
replaceString:
"'actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561' # tag=v3.1.1",
},
{
replaceString:
'"actions/setup-node@1f8c6b94b26d0feae1e387ca63ccbdc44d27b561" # tag=v2.5.1',
},
],
});
});

it('extracts tags in different formats', () => {
const res = extractPackageFile(
Fixtures.get('workflow_4.yml'),
Expand Down
5 changes: 3 additions & 2 deletions lib/modules/manager/github-actions/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { Workflow } from './types';

const dockerActionRe = regEx(/^\s+uses\s*: ['"]?docker:\/\/([^'"]+)\s*$/);
const actionRe = regEx(
/^\s+-?\s+?uses\s*: (?<replaceString>['"]?(?<registryUrl>https:\/\/[.\w-]+\/)?(?<depName>[\w-]+\/[.\w-]+)(?<path>\/.*)?@(?<currentValue>[^\s'"]+)['"]?(?:\s+#\s*(((?:renovate\s*:\s*)?(?:pin\s+|tag\s*=\s*)?|(?:ratchet:[\w-]+\/[.\w-]+)?)@?(?<tag>([\w-]*-)?v?\d+(?:\.\d+(?:\.\d+)?)?)|(?:ratchet:exclude)))?)/,
/^\s+-?\s+?uses\s*: (?<replaceString>['"]?(?<registryUrl>https:\/\/[.\w-]+\/)?(?<depName>[\w-]+\/[.\w-]+)(?<path>\/.*)?@(?<currentValue>[^\s'"]+)['"]?(?:(?<commentWhiteSpaces>\s+)#\s*(((?:renovate\s*:\s*)?(?:pin\s+|tag\s*=\s*)?|(?:ratchet:[\w-]+\/[.\w-]+)?)@?(?<tag>([\w-]*-)?v?\d+(?:\.\d+(?:\.\d+)?)?)|(?:ratchet:exclude)))?)/,
);

// SHA1 or SHA256, see https://github.blog/2020-10-19-git-2-29-released/
Expand Down Expand Up @@ -72,6 +72,7 @@ function extractWithRegex(content: string): PackageDependency[] {
tag,
replaceString,
registryUrl = '',
commentWhiteSpaces = ' ',
} = tagMatch.groups;
let quotes = '';
if (replaceString.indexOf("'") >= 0) {
Expand All @@ -87,7 +88,7 @@ function extractWithRegex(content: string): PackageDependency[] {
versioning: dockerVersioning.id,
depType: 'action',
replaceString,
autoReplaceStringTemplate: `${quotes}${registryUrl}{{depName}}${path}@{{#if newDigest}}{{newDigest}}${quotes}{{#if newValue}} # {{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}${quotes}{{/unless}}`,
autoReplaceStringTemplate: `${quotes}${registryUrl}{{depName}}${path}@{{#if newDigest}}{{newDigest}}${quotes}{{#if newValue}}${commentWhiteSpaces}# {{newValue}}{{/if}}{{/if}}{{#unless newDigest}}{{newValue}}${quotes}{{/unless}}`,
...(registryUrl
? detectDatasource(registryUrl)
: customRegistryUrlsPackageDependency),
Expand Down

0 comments on commit 66f1ef5

Please sign in to comment.