Repository: actions/toolkit
Pull request #1497 (2023-08-17 13:38:43)

[Artifacts] Add tests for E2E artifact upload
See the output of: https://github.com/actions/toolkit/actions/runs/5892906514?pr=1497

<img width="1201" alt="image" src="https://github.com/actions/toolkit/assets/16109154/964ba1f6-00a6-4ed2-b19e-355c5ca11025">

Whenever making artifact related changes we want to run upload, list, download end-to-end to make sure we aren't breaking anything.

I've re-enabled `artifact-tests.yml` which was disabled once we started working on `v2.0.0` and these changes now make sure that upload successfully works end to end.

In a future PR I plan on calling the list artifacts API to make sure the uploaded artifacts show up successfully



Patch:
diff --git a/packages/artifact/package.json b/packages/artifact/package.json
index 25fbc98..9845862 100644
--- a/packages/artifact/package.json
+++ b/packages/artifact/package.json
@@ -10,8 +10,8 @@
   ],
   "homepage": "https://github.com/actions/toolkit/tree/main/packages/artifact",
   "license": "MIT",
-  "main": "lib/artifact-client.js",
-  "types": "lib/artifact-client.d.ts",
+  "main": "lib/artifact.js",
+  "types": "lib/artifact.d.ts",
   "directories": {
     "lib": "lib",
     "test": "__tests__"
diff --git a/packages/artifact/src/internal/shared/artifact-twirp-client.ts b/packages/artifact/src/internal/shared/artifact-twirp-client.ts
index a77d02a..444e3fa 100644
--- a/packages/artifact/src/internal/shared/artifact-twirp-client.ts
+++ b/packages/artifact/src/internal/shared/artifact-twirp-client.ts
@@ -1,6 +1,6 @@
 import {HttpClient, HttpClientResponse, HttpCodes} from '@actions/http-client'
 import {BearerCredentialHandler} from '@actions/http-client/lib/auth'
-import {info} from '@actions/core'
+import {info, debug} from '@actions/core'
 import {ArtifactServiceClientJSON} from '../../generated'
 import {getResultsServiceUrl, getRuntimeToken} from './config'
 
@@ -53,6 +53,7 @@ class ArtifactHttpClient implements Rpc {
     data: object | Uint8Array
   ): Promise<object | Uint8Array> {
     const url = `${this.baseUrl}/twirp/${service}/${method}`
+    debug(`Requesting ${url}`)
     const headers = {
       'Content-Type': contentType
     }


Test patch:
diff --git a/.github/workflows/artifact-tests.yml b/.github/workflows/artifact-tests.yml
index 1ebfa7c..d354198 100644
--- a/.github/workflows/artifact-tests.yml
+++ b/.github/workflows/artifact-tests.yml
@@ -31,11 +31,6 @@ jobs:
       with:
         node-version: 16.x
 
-    # In order to upload & download artifacts from a shell script, certain env variables need to be set that are only available in the
-    # node context. This runs a local action that gets and sets the necessary env variables that are needed
-    - name: Set env variables
-      uses: ./packages/artifact/__tests__/ci-test-action/
-
     # Need root node_modules because certain npm packages like jest are configured for the entire repository and it won't be possible
     # without these to just compile the artifacts package
     - name: Install root npm packages
@@ -50,48 +45,35 @@ jobs:
     - name: Set artifact file contents
       shell: bash
       run: |
-        echo "non-gzip-artifact-content=hello" >> $GITHUB_ENV
-        echo "gzip-artifact-content=Some large amount of text that has a compression ratio that is greater than 100%. If greater than 100%, gzip is used to upload the file" >> $GITHUB_ENV
-        echo "empty-artifact-content=_EMPTY_" >> $GITHUB_ENV
+        echo "file1=hello from file 1" >> $GITHUB_ENV
+        echo "file2=hello from file 2" >> $GITHUB_ENV
 
     - name: Create files that will be uploaded
       run: |
         mkdir artifact-path
-        echo '${{ env.non-gzip-artifact-content }}' > artifact-path/world.txt
-        echo '${{ env.gzip-artifact-content }}' > artifact-path/gzip.txt
-        touch artifact-path/empty.txt
+        echo '${{ env.file1 }}' > artifact-path/first.txt
+        echo '${{ env.file2 }}' > artifact-path/second.txt
 
-    # We're using node -e to call the functions directly available in the @actions/artifact package
-    - name: Upload artifacts using uploadArtifact()
-      run: |
-        node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-1',['artifact-path/world.txt'], process.argv[1]))" "${{ github.workspace }}"
-        node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-2',['artifact-path/gzip.txt'], process.argv[1]))" "${{ github.workspace }}"
-        node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().uploadArtifact('my-artifact-3',['artifact-path/empty.txt'], process.argv[1]))" "${{ github.workspace }}"
+    - uses: actions/github-script@v6
+      with:
+        script: |
+            const artifact = require('./packages/artifact/lib/artifact')
 
-    - name: Download artifacts using downloadArtifact()
-      run: |
-        mkdir artifact-1-directory
-        node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-1','artifact-1-directory'))"
-        mkdir artifact-2-directory
-        node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-2','artifact-2-directory'))"
-        mkdir artifact-3-directory
-        node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadArtifact('my-artifact-3','artifact-3-directory'))"
-
-    - name: Verify downloadArtifact()
-      shell: bash
-      run: |
-        packages/artifact/__tests__/test-artifact-file.sh "artifact-1-directory/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}"
-        packages/artifact/__tests__/test-artifact-file.sh "artifact-2-directory/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}"
-        packages/artifact/__tests__/test-artifact-file.sh "artifact-3-directory/artifact-path/empty.txt" "${{ env.empty-artifact-content }}"
+            const artifactName = 'my-artifact-${{ matrix.runs-on }}'
+            console.log('artifactName: ' + artifactName)
 
-    - name: Download artifacts using downloadAllArtifacts()
-      run: |
-        mkdir multi-artifact-directory
-        node -e "Promise.resolve(require('./packages/artifact/lib/artifact-client').create().downloadAllArtifacts('multi-artifact-directory'))"
+            const fileContents = ['artifact-path/first.txt','artifact-path/second.txt']
 
-    - name: Verify downloadAllArtifacts()
-      shell: bash
-      run: |
-        packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-1/artifact-path/world.txt" "${{ env.non-gzip-artifact-content }}"
-        packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-2/artifact-path/gzip.txt" "${{ env.gzip-artifact-content }}"
-        packages/artifact/__tests__/test-artifact-file.sh "multi-artifact-directory/my-artifact-3/artifact-path/empty.txt" "${{ env.empty-artifact-content }}"
+            const uploadResult = await artifact.create().uploadArtifact(artifactName, fileContents, './')
+            console.log(uploadResult)
+
+            const success = uploadResult.success
+            const size = uploadResult.size
+            const id = uploadResult.id
+
+            if (!success) {
+              throw new Error('Failed to upload artifact')
+            } else {
+              console.log(`Successfully uploaded artifact ${id}`)
+            }
+