Skip to content

Commit 37557f0

Browse files
authored
bug: fix an issue where mime types are not preserved (#262)
1 parent c37b3dd commit 37557f0

10 files changed

Lines changed: 475 additions & 306 deletions

File tree

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ If `parent` is set to `false`, it wil be uploaded to `gs://bucket-name/folder2/f
294294
process_gcloudignore: true
295295
```
296296

297+
- `project_id` - (Optional) Google Cloud project ID to use for billing and API
298+
requests. By default, this is extracted from the credentials or the running
299+
environment.
300+
301+
```yaml
302+
project_id: 'my-project'
303+
```
304+
297305
- `credentials`: (**Deprecated**) This input is deprecated. See [auth
298306
section](#via-google-github-actionsauth) for more details. [Google Service
299307
Account JSON][sa] credentials as JSON or base64 encoded string, typically

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ inputs:
99
formatted private key which can be exported from the Cloud Console. The
1010
value can be raw or base64-encoded.
1111
required: false
12+
project_id:
13+
description: |-
14+
Project ID to use for making requests. By default, this is extracted from
15+
the credentials or the running environment.
16+
required: false
1217
path:
1318
description: |-
1419
The path to a file or folder inside the action's filesystem

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "ncc build -m src/main.ts",
8-
"lint": "eslint src/ tests/ --ext .ts,.tsx",
8+
"lint": "eslint src/ tests/ --ext .ts,.tsx --max-warnings 0",
99
"format": "eslint src/ tests/ --ext .ts,.tsx --fix",
1010
"test": "mocha -r ts-node/register -t 600s 'tests/**/*.test.ts'"
1111
},

src/client.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
} from '@google-github-actions/actions-utils';
2626

2727
import { Metadata } from './headers';
28-
import { parseBucketNameAndPrefix } from './util';
28+
import { deepClone, parseBucketNameAndPrefix } from './util';
2929

3030
// Do not listen to the linter - this can NOT be rewritten as an ES6 import statement.
3131
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -132,6 +132,7 @@ export class Client {
132132
if (opts?.credentials) {
133133
options.credentials = parseCredential(opts.credentials);
134134
}
135+
135136
this.storage = new Storage(options);
136137
}
137138

@@ -156,16 +157,20 @@ export class Client {
156157
const base = opts.includeParent ? path.basename(opts.root) : '';
157158
const destination = path.posix.join(prefix, base, file);
158159

159-
// Build options
160+
// Build options.
160161
const abs = path.resolve(opts.root, toPlatformPath(file));
161-
const uploadOpts = {
162+
163+
// Apparently the Cloud Storage SDK modifies this object, so we need to
164+
// make our own deep copy before passing it to upload. See #258 for more
165+
// information.
166+
const uploadOpts = deepClone({
162167
destination: destination,
163168
metadata: opts.metadata || {},
164169
gzip: opts.gzip,
165170
predefinedAcl: opts.predefinedAcl,
166171
resumable: opts.resumable,
167172
configPath: randomFilepath(),
168-
};
173+
});
169174

170175
// Execute callback if defined
171176
if (opts.onUploadObject) {

src/main.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export async function run(): Promise<void> {
6363
const processGcloudIgnore = core.getBooleanInput('process_gcloudignore');
6464
const metadata = headersInput === '' ? {} : parseHeadersInput(headersInput);
6565
const credentials = core.getInput('credentials');
66+
const projectID = core.getInput('project_id');
6667

6768
// Add warning if using credentials.
6869
if (credentials) {
@@ -88,7 +89,7 @@ export async function run(): Promise<void> {
8889
// - Format all files to be posix relative to input.path
8990
// - Filter out items that match
9091
if (processGcloudIgnore) {
91-
core.debug(`processing gcloudignore`);
92+
core.debug(`Processing gcloudignore`);
9293

9394
const ignores = ignore();
9495

@@ -98,8 +99,8 @@ export async function run(): Promise<void> {
9899
const ignoreList = await parseGcloudIgnore(gcloudIgnorePath);
99100

100101
if (ignoreList.length) {
101-
core.debug(`using .gcloudignore at: ${gcloudIgnorePath}`);
102-
core.debug(`parsed ignore list: ${JSON.stringify(ignoreList)}`);
102+
core.debug(`Using .gcloudignore at: ${gcloudIgnorePath}`);
103+
core.debug(`Parsed ignore list: ${JSON.stringify(ignoreList)}`);
103104

104105
ignores.add(ignoreList);
105106
}
@@ -108,27 +109,30 @@ export async function run(): Promise<void> {
108109
const name = files[i];
109110
try {
110111
if (ignores.ignores(name)) {
111-
core.debug(`ignoring ${name} because of ignore file`);
112+
core.debug(`Ignoring ${name} because of ignore file`);
112113
files.splice(i, 1);
113114
i--;
114115
}
115116
} catch (err) {
116117
const msg = errorMessage(err);
117-
core.error(`failed to process ignore for ${name}, skipping: ${msg}`);
118+
core.error(`Failed to process ignore for ${name}, skipping: ${msg}`);
118119
}
119120
}
120121
}
121122
}
122123

123-
core.debug(`uploading ${files.length} files: ${JSON.stringify(files)}`);
124+
core.debug(`Uploading ${files.length} files: ${JSON.stringify(files)}`);
124125

125126
// Emit a helpful warning in case people specify the wrong directory.
126127
if (files.length === 0) {
127128
core.warning(NO_FILES_WARNING);
128129
}
129130

130131
// Create the client and upload files.
131-
const client = new Client({ credentials: credentials });
132+
const client = new Client({
133+
credentials: credentials,
134+
projectID: projectID,
135+
});
132136
const uploadResponses = await client.upload({
133137
destination: destination,
134138
root: absoluteRoot,

src/util.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { promises as fs } from 'fs';
1818
import * as path from 'path';
19+
import * as v8 from 'v8';
1920

2021
import globby from 'globby';
2122
import { toPlatformPath, toPosixPath } from '@google-github-actions/actions-utils';
@@ -106,3 +107,13 @@ export async function expandGlob(directoryPath: string, glob: string): Promise<s
106107

107108
return filesList.sort();
108109
}
110+
111+
/**
112+
* deepClone makes a deep clone of the given object.
113+
*
114+
* @param obj T, object to clone
115+
* @return T a copy of the object
116+
*/
117+
export function deepClone<T>(obj: T): T {
118+
return v8.deserialize(v8.serialize(obj));
119+
}

0 commit comments

Comments
 (0)