Skip to content

Commit a2ee322

Browse files
bharathkkbhideo54
andauthored
feat: Add predefinedAcl option (#28) (#31)
* Make predefinedAcl parameter * Write README * lint --fix * Update action.yml * Build * Revert "Build" This reverts commit 27d78ae. * Change predefinedAcl example Co-authored-by: Hideo Yasumoto <hideo54@hideo54.com>
1 parent a34ccd4 commit a2ee322

5 files changed

Lines changed: 43 additions & 4 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ The files will be uploaded to `gs://bucket-name/folder/file1`,`gs://bucket-name/
116116

117117
In the above example, the file(s) will be uploaded without `gzip` content-encoding
118118

119+
- `predefinedAcl`: (Optional) Apply a predefined set of access controls to the file(s).
120+
121+
```yaml
122+
predefinedAcl: projectPrivate
123+
```
124+
125+
In the above example, project team members get access to the uploaded file(s) according to their roles.
126+
127+
Acceptable values are: `authenticatedRead`, `bucketOwnerFullControl`, `bucketOwnerRead`, `private`, `projectPrivate`, `publicRead`. See [the document](https://googleapis.dev/nodejs/storage/latest/global.html#UploadOptions) for details.
128+
119129
- `credentials`: (Optional) [Google Service Account JSON][sa] credentials as JSON or base64 encoded string,
120130
typically sourced from a [GitHub Secret][gh-secret]. If unspecified, other
121131
authentication methods are attempted. See [Authorization](#Authorization) below.

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ inputs:
2424
description: |-
2525
Upload file(s) with gzip content encoding, defaults to true.
2626
required: false
27+
predefinedAcl:
28+
description: |-
29+
Apply a predefined set of access controls to the file(s).
30+
required: false
2731

2832
runs:
2933
using: "node12"

src/client.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
import * as fs from 'fs';
1818
import { UploadHelper } from './upload-helper';
19-
import { Storage, UploadResponse, StorageOptions } from '@google-cloud/storage';
19+
import {
20+
Storage,
21+
UploadResponse,
22+
StorageOptions,
23+
PredefinedAcl,
24+
} from '@google-cloud/storage';
2025

2126
/**
2227
* Available options to create the client.
@@ -64,6 +69,7 @@ export class Client {
6469
destination: string,
6570
path: string,
6671
gzip: boolean,
72+
predefinedAcl?: PredefinedAcl,
6773
): Promise<UploadResponse[]> {
6874
let bucketName = destination;
6975
let prefix = '';
@@ -82,6 +88,7 @@ export class Client {
8288
path,
8389
gzip,
8490
prefix,
91+
predefinedAcl,
8592
);
8693
return [uploadedFile];
8794
} else {
@@ -90,6 +97,7 @@ export class Client {
9097
path,
9198
gzip,
9299
prefix,
100+
predefinedAcl,
93101
);
94102
return uploadedFiles;
95103
}

src/main.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import * as core from '@actions/core';
18+
import { PredefinedAcl } from '@google-cloud/storage';
1819
import { Client } from './client';
1920

2021
async function run(): Promise<void> {
@@ -23,9 +24,21 @@ async function run(): Promise<void> {
2324
const destination = core.getInput('destination', { required: true });
2425
const gzip =
2526
core.getInput('gzip', { required: false }) === 'false' ? false : true;
27+
const predefinedAclInput = core.getInput('predefinedAcl', {
28+
required: false,
29+
});
30+
const predefinedAcl =
31+
predefinedAclInput === ''
32+
? undefined
33+
: (predefinedAclInput as PredefinedAcl);
2634
const serviceAccountKey = core.getInput('credentials');
2735
const client = new Client({ credentials: serviceAccountKey });
28-
const uploadResponses = await client.upload(destination, path, gzip);
36+
const uploadResponses = await client.upload(
37+
destination,
38+
path,
39+
gzip,
40+
predefinedAcl,
41+
);
2942

3043
core.setOutput(
3144
'uploaded',

src/upload-helper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { Storage, UploadResponse } from '@google-cloud/storage';
17+
import { PredefinedAcl, Storage, UploadResponse } from '@google-cloud/storage';
1818
import * as path from 'path';
1919
import { getFiles } from './util';
2020

@@ -48,12 +48,14 @@ export class UploadHelper {
4848
filename: string,
4949
gzip: boolean,
5050
destination?: string,
51+
predefinedAcl?: PredefinedAcl,
5152
): Promise<UploadResponse> {
5253
interface UploadOptions {
5354
gzip: boolean;
5455
destination?: string;
56+
predefinedAcl?: PredefinedAcl;
5557
}
56-
const options: UploadOptions = { gzip };
58+
const options: UploadOptions = { gzip, predefinedAcl };
5759
if (destination) {
5860
// If obj prefix is set, then extract filename and append to prefix.
5961
options.destination = `${destination}/${path.posix.basename(filename)}`;
@@ -79,6 +81,7 @@ export class UploadHelper {
7981
directoryPath: string,
8082
gzip: boolean,
8183
prefix = '',
84+
predefinedAcl?: PredefinedAcl,
8285
): Promise<UploadResponse[]> {
8386
const pathDirName = path.posix.dirname(directoryPath);
8487
// Get list of files in the directory.
@@ -100,6 +103,7 @@ export class UploadHelper {
100103
filePath,
101104
gzip,
102105
destination,
106+
predefinedAcl,
103107
);
104108
return uploadResp;
105109
}),

0 commit comments

Comments
 (0)