Skip to content

Commit 2f6066a

Browse files
authored
Add optional gzip flag (#15)
1 parent 864317d commit 2f6066a

8 files changed

Lines changed: 56 additions & 13 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ The files will be uploaded to `gs://bucket-name/folder/file1`,`gs://bucket-name/
108108

109109
In the above example, the file will be uploaded to gs://bucket-name/prefix/file
110110

111+
- `gzip`: (Optional) Upload file(s) with gzip content encoding, defaults to true.
112+
113+
```yaml
114+
gzip: false
115+
```
116+
117+
In the above example, the file(s) will be uploaded without `gzip` content-encoding
118+
111119
- `credentials`: (Optional) [Google Service Account JSON][sa] credentials as JSON or base64 encoded string,
112120
typically sourced from a [GitHub Secret][gh-secret]. If unspecified, other
113121
authentication methods are attempted. See [Authorization](#Authorization) below.

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ inputs:
2020
GCS bucket name of form <bucketname> or with an optional prefix
2121
of form <bucketname>/<prefix>
2222
required: true
23+
gzip:
24+
description: |-
25+
Upload file(s) with gzip content encoding, defaults to true.
26+
required: false
2327

2428
runs:
2529
using: "node12"

src/client.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ export class Client {
6060
* @param prefix Optional prefix when uploading to GCS.
6161
* @returns List of uploaded file(s).
6262
*/
63-
async upload(destination: string, path: string): Promise<UploadResponse[]> {
63+
async upload(
64+
destination: string,
65+
path: string,
66+
gzip: boolean,
67+
): Promise<UploadResponse[]> {
6468
let bucketName = destination;
6569
let prefix = '';
6670
// If destination of the form my-bucket/subfolder get bucket and prefix.
@@ -73,12 +77,18 @@ export class Client {
7377
const stat = await fs.promises.stat(path);
7478
const uploader = new UploadHelper(this.storage);
7579
if (stat.isFile()) {
76-
const uploadedFile = await uploader.uploadFile(bucketName, path, prefix);
80+
const uploadedFile = await uploader.uploadFile(
81+
bucketName,
82+
path,
83+
gzip,
84+
prefix,
85+
);
7786
return [uploadedFile];
7887
} else {
7988
const uploadedFiles = await uploader.uploadDirectory(
8089
bucketName,
8190
path,
91+
gzip,
8292
prefix,
8393
);
8494
return uploadedFiles;

src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ async function run(): Promise<void> {
2121
try {
2222
const path = core.getInput('path', { required: true });
2323
const destination = core.getInput('destination', { required: true });
24+
const gzip =
25+
core.getInput('gzip', { required: false }) === 'false' ? false : true;
2426
const serviceAccountKey = core.getInput('credentials');
2527
const client = new Client({ credentials: serviceAccountKey });
26-
const uploadResponses = await client.upload(destination, path);
28+
const uploadResponses = await client.upload(destination, path, gzip);
2729

2830
core.setOutput(
2931
'uploaded',

src/upload-helper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ export class UploadHelper {
4646
async uploadFile(
4747
bucketName: string,
4848
filename: string,
49+
gzip: boolean,
4950
destination?: string,
5051
): Promise<UploadResponse> {
5152
interface UploadOptions {
5253
gzip: boolean;
5354
destination?: string;
5455
}
55-
const options: UploadOptions = { gzip: true };
56+
const options: UploadOptions = { gzip };
5657
if (destination) {
5758
// If obj prefix is set, then extract filename and append to prefix.
5859
options.destination = `${destination}/${path.posix.basename(filename)}`;
@@ -76,6 +77,7 @@ export class UploadHelper {
7677
async uploadDirectory(
7778
bucketName: string,
7879
directoryPath: string,
80+
gzip: boolean,
7981
prefix = '',
8082
): Promise<UploadResponse[]> {
8183
const pathDirName = path.posix.dirname(directoryPath);
@@ -96,6 +98,7 @@ export class UploadHelper {
9698
const uploadResp = await this.uploadFile(
9799
bucketName,
98100
filePath,
101+
gzip,
99102
destination,
100103
);
101104
return uploadResp;

tests/client.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ describe('Unit Test Client', function() {
5151
const path = './tests/testdata/test1.txt';
5252
const bucketName = 'foo';
5353
const prefix = 'test-prefix';
54-
await client.upload(`${bucketName}/${prefix}`, path);
54+
await client.upload(`${bucketName}/${prefix}`, path, true);
5555
expect(uploadFileStub.calledOnce);
5656
expect(uploadFileStub.firstCall.args[0]).to.equal(bucketName);
5757
expect(uploadFileStub.firstCall.args[1]).to.equal(path);
58-
expect(uploadFileStub.firstCall.args[2]).to.equal(prefix);
58+
expect(uploadFileStub.firstCall.args[2]).to.equal(true);
59+
expect(uploadFileStub.firstCall.args[3]).to.equal(prefix);
5960
});
6061

6162
it('calls uploadDirectory', async function() {
@@ -66,10 +67,11 @@ describe('Unit Test Client', function() {
6667
const client = new Client();
6768
const path = './tests/testdata';
6869
const bucketName = 'foo';
69-
await client.upload(bucketName, path);
70+
await client.upload(bucketName, path, true);
7071
expect(uploadDirSpy.calledOnce);
7172
expect(uploadDirSpy.firstCall.args[0]).to.equal(bucketName);
7273
expect(uploadDirSpy.firstCall.args[1]).to.equal(path);
73-
expect(uploadDirSpy.firstCall.args[2]).to.equal('');
74+
expect(uploadDirSpy.firstCall.args[2]).to.equal(true);
75+
expect(uploadDirSpy.firstCall.args[3]).to.equal('');
7476
});
7577
});

tests/upload-helper.int.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ describe('Integration Upload Helper', function() {
3131
const uploadResponse = await uploader.uploadFile(
3232
testBucket,
3333
'./tests/testdata/test1.txt',
34+
true,
3435
);
3536
expect(uploadResponse[0].name).eql('test1.txt');
3637
});
@@ -44,6 +45,7 @@ describe('Integration Upload Helper', function() {
4445
const uploadResponse = await uploader.uploadFile(
4546
testBucket,
4647
'./tests/testdata/test1.txt',
48+
true,
4749
'testprefix',
4850
);
4951
expect(uploadResponse[0].name).eql('testprefix/test1.txt');
@@ -58,6 +60,7 @@ describe('Integration Upload Helper', function() {
5860
const uploadResponse = await uploader.uploadFile(
5961
testBucket,
6062
'./tests/testdata/testfile',
63+
true,
6164
'testprefix',
6265
);
6366
expect(uploadResponse[0].name).eql('testprefix/testfile');
@@ -72,6 +75,7 @@ describe('Integration Upload Helper', function() {
7275
const uploadResponse = await uploader.uploadFile(
7376
testBucket,
7477
'./tests/testdata/🚀',
78+
true,
7579
'testprefix',
7680
);
7781
expect(uploadResponse[0].name).eql('testprefix/🚀');

tests/upload-helper.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,19 @@ describe('Unit Test uploadFile', function() {
4646

4747
it('uploads a single file', async function() {
4848
const uploader = new UploadHelper(new Storage());
49-
await uploader.uploadFile(EXAMPLE_BUCKET, EXAMPLE_FILE);
49+
await uploader.uploadFile(EXAMPLE_BUCKET, EXAMPLE_FILE, true);
5050
// Assert that upload method in storage library was called with right file.
5151
expect(this.uploadStub.firstCall.args[0]).eq(EXAMPLE_FILE);
5252
});
5353

5454
it('uploads a single file with prefix', async function() {
5555
const uploader = new UploadHelper(new Storage());
56-
await uploader.uploadFile(EXAMPLE_BUCKET, EXAMPLE_FILE, EXAMPLE_PREFIX);
56+
await uploader.uploadFile(
57+
EXAMPLE_BUCKET,
58+
EXAMPLE_FILE,
59+
true,
60+
EXAMPLE_PREFIX,
61+
);
5762
// Assert that upload method in storage library was called with right file
5863
// and right prefix.
5964
expect(this.uploadStub.firstCall.args[0]).eq(EXAMPLE_FILE);
@@ -83,7 +88,7 @@ describe('Unit Test uploadDir', function() {
8388

8489
it('uploads a dir', async function() {
8590
const uploader = new UploadHelper(new Storage());
86-
await uploader.uploadDirectory(EXAMPLE_BUCKET, EXAMPLE_DIR);
91+
await uploader.uploadDirectory(EXAMPLE_BUCKET, EXAMPLE_DIR, true);
8792
// Assert that uploadFile was called for each file in directory.
8893
expect(this.uploadFileStub.callCount).eq(FILES_IN_DIR.length);
8994
// Capture filename arguments passed to uploadFile.
@@ -97,7 +102,12 @@ describe('Unit Test uploadDir', function() {
97102

98103
it('uploads a dir with prefix', async function() {
99104
const uploader = new UploadHelper(new Storage());
100-
await uploader.uploadDirectory(EXAMPLE_BUCKET, EXAMPLE_DIR, EXAMPLE_PREFIX);
105+
await uploader.uploadDirectory(
106+
EXAMPLE_BUCKET,
107+
EXAMPLE_DIR,
108+
true,
109+
EXAMPLE_PREFIX,
110+
);
101111
// Assert that uploadFile was called for each file in directory.
102112
expect(this.uploadFileStub.callCount).eq(FILES_IN_DIR.length);
103113
// Capture filename arguments passed to uploadFile.
@@ -107,7 +117,7 @@ describe('Unit Test uploadDir', function() {
107117
);
108118
// Capture destination arguments passed to uploadFile.
109119
const destinations = uploadFileCalls.map(
110-
(uploadFileCall: sinon.SinonSpyCall) => uploadFileCall.args[2],
120+
(uploadFileCall: sinon.SinonSpyCall) => uploadFileCall.args[3],
111121
);
112122
// Assert uploadDir called uploadFile with right files.
113123
expect(filenames).to.have.members(FILES_IN_DIR);

0 commit comments

Comments
 (0)