From bbe450472946813f4373c74a5b8321d6a8a78366 Mon Sep 17 00:00:00 2001 From: initdc Date: Sat, 15 Oct 2022 14:39:03 +0000 Subject: [PATCH] fix: add path key for pathObject --- .github/workflows/test-per-file.yml | 10 +++------ action.yml | 11 ++++++---- dist/index.js | 26 +++++++++++++++++------ src/upload-artifact.ts | 33 +++++++++++++++++++++++------ 4 files changed, 55 insertions(+), 25 deletions(-) diff --git a/.github/workflows/test-per-file.yml b/.github/workflows/test-per-file.yml index 1b56e1b..447e13f 100644 --- a/.github/workflows/test-per-file.yml +++ b/.github/workflows/test-per-file.yml @@ -64,14 +64,12 @@ jobs: tar -zvcf path/to/dir-3/all.gz path/to/dir-3/* - # Upload a single file artifact - name: 'Upload artifact #1' uses: ./ with: path: path/to/dir-1/file1.txt artifact-per-file: true - # Upload using a wildcard pattern, name should default to 'artifact' if not provided - name: 'Upload artifact #2' uses: ./ with: @@ -79,20 +77,18 @@ jobs: artifact-per-file: true artifact-name-rule: ${dir}-${base} - # Upload a directory that contains a file that will be uploaded with GZip - name: 'Upload artifact #3' uses: ./ with: path: path/to/dir-3/*.gz artifact-per-file: true - artifact-name-rule: ${dir}-${name}${ext} + artifact-name-rule: ${path}-${name}${ext} - # Upload a directory that contains a file that will be uploaded with GZip - name: 'Upload artifact #4' uses: ./ with: path: | - path/**/dir*/ + path/**/dir-1/ !path/to/dir-3/*.gz artifact-per-file: true - artifact-name-rule: ${{ matrix.runs-on }}-${base} + artifact-name-rule: ${{ matrix.runs-on }}-${name} diff --git a/action.yml b/action.yml index 1d21f6f..188957d 100644 --- a/action.yml +++ b/action.yml @@ -28,25 +28,28 @@ inputs: default: "false" artifact-name-rule: description: > - https://nodejs.org/docs/latest-v16.x/api/path.html#pathparsepath + // https://nodejs.org/docs/latest-v16.x/api/path.html#pathparsepath + // Modified from path.parse() path.parse('/home/user/dir/file.txt'); // Returns: // { root: '/', // dir: '/home/user/dir', + // path: 'home/user/dir' // base: 'file.txt', // ext: '.txt', // name: 'file' } ┌─────────────────────┬────────────┐ - │ dir │ base │ - ├──────┬ ├──────┬─────┤ - │ root │ │ name │ ext │ + │ dir sep base │ + ├──────┬──────────────┼──────┬─────┤ + │ root │ path │ name │ ext │ " / home/user/dir / file .txt " └──────┴──────────────┴──────┴─────┘ (All spaces in the "" line should be ignored. They are purely for formatting.) Every key need in wrapper: ${} + sep just for prompt, can't be used default: ${base} runs: using: 'node16' diff --git a/dist/index.js b/dist/index.js index 68e2cd2..3dd6b32 100644 --- a/dist/index.js +++ b/dist/index.js @@ -4781,8 +4781,10 @@ function run() { } const artifactsName = inputs['artifactsName'] || 'artifacts'; const artifactPerFile = inputs['artifactPerFile'] || false; + const rootDirectory = searchResult.rootDirectory; + core.info('rootDirectory: ' + rootDirectory); if (!artifactPerFile) { - const uploadResponse = yield artifactClient.uploadArtifact(artifactsName, searchResult.filesToUpload, searchResult.rootDirectory, options); + const uploadResponse = yield artifactClient.uploadArtifact(artifactsName, searchResult.filesToUpload, rootDirectory, options); if (uploadResponse.failedItems.length > 0) { core.setFailed(`An error was encountered when uploading ${uploadResponse.artifactName}. There were ${uploadResponse.failedItems.length} items that failed to upload.`); } @@ -4797,8 +4799,14 @@ function run() { const artifactNameRule = inputs['artifactNameRule']; for (let i = 0; i < filesToUpload.length; i++) { const file = filesToUpload[i]; - core.info(file); - const pathObject = path_1.default.parse(file); + core.info('file: ' + file); + const pathObject = Object.assign({}, path_1.default.parse(file)); + const pathBase = pathObject.base; + const pathRoot = path_1.default.parse(rootDirectory).dir; + pathObject.root = pathRoot; + core.info('root: ' + pathRoot); + pathObject['path'] = file.slice(pathRoot.length, file.length - path_1.default.sep.length - pathBase.length); + core.info('path: ' + pathObject['path']); let artifactName = artifactNameRule; for (const key of Object.keys(pathObject)) { const re = `$\{${key}}`; @@ -4807,15 +4815,19 @@ function run() { artifactName = artifactName.replace(re, value); } } - if (artifactName.includes(path_1.default.sep)) { - core.warning(`${artifactName} includes ${path_1.default.sep}`); - artifactName = artifactName.split(path_1.default.sep).join('_'); + if (artifactName.startsWith(path_1.default.sep)) { + core.warning(`${artifactName} startsWith ${path_1.default.sep}`); + artifactName = artifactName.slice(path_1.default.sep.length); } if (artifactName.includes(':')) { core.warning(`${artifactName} includes :`); artifactName = artifactName.split(':').join('-'); } - core.info(artifactName); + if (artifactName.includes(path_1.default.sep)) { + core.warning(`${artifactName} includes ${path_1.default.sep}`); + artifactName = artifactName.split(path_1.default.sep).join('_'); + } + core.debug(artifactName); const artifactItemExist = SuccessedItems.includes(artifactName); if (artifactItemExist) { const oldArtifactName = artifactName; diff --git a/src/upload-artifact.ts b/src/upload-artifact.ts index b09e33e..1714a85 100644 --- a/src/upload-artifact.ts +++ b/src/upload-artifact.ts @@ -55,11 +55,14 @@ async function run(): Promise { const artifactsName = inputs['artifactsName'] || 'artifacts' const artifactPerFile = inputs['artifactPerFile'] || false + const rootDirectory = searchResult.rootDirectory + core.info('rootDirectory: ' + rootDirectory) + if (!artifactPerFile) { const uploadResponse = await artifactClient.uploadArtifact( artifactsName, searchResult.filesToUpload, - searchResult.rootDirectory, + rootDirectory, options ) @@ -80,9 +83,20 @@ async function run(): Promise { const artifactNameRule = inputs['artifactNameRule'] for (let i = 0; i < filesToUpload.length; i++) { const file = filesToUpload[i] - core.info(file) + core.info('file: ' + file) + + const pathObject = Object.assign({}, path.parse(file)) + const pathBase = pathObject.base + const pathRoot = path.parse(rootDirectory).dir + pathObject.root = pathRoot + core.info('root: ' + pathRoot) + + pathObject['path'] = file.slice( + pathRoot.length, + file.length - path.sep.length - pathBase.length + ) + core.info('path: ' + pathObject['path']) - const pathObject = path.parse(file) let artifactName = artifactNameRule for (const key of Object.keys(pathObject)) { const re = `$\{${key}}` @@ -91,15 +105,20 @@ async function run(): Promise { artifactName = artifactName.replace(re, value) } } - if (artifactName.includes(path.sep)) { - core.warning(`${artifactName} includes ${path.sep}`) - artifactName = artifactName.split(path.sep).join('_') + + if (artifactName.startsWith(path.sep)) { + core.warning(`${artifactName} startsWith ${path.sep}`) + artifactName = artifactName.slice(path.sep.length) } if (artifactName.includes(':')) { core.warning(`${artifactName} includes :`) artifactName = artifactName.split(':').join('-') } - core.info(artifactName) + if (artifactName.includes(path.sep)) { + core.warning(`${artifactName} includes ${path.sep}`) + artifactName = artifactName.split(path.sep).join('_') + } + core.debug(artifactName) const artifactItemExist = SuccessedItems.includes(artifactName) if (artifactItemExist) {