Skip to content

Commit 1f2c876

Browse files
committed
Fix fieldMask and add more debug output
1 parent 50f7362 commit 1f2c876

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export type CloudFunctionClientOptions = {
5151

5252
export type PollOperationOptions = {
5353
onPoll?: OnFunction;
54+
onDebug?: OnDebugFunction;
5455
};
5556

5657
export type Operation = {
@@ -193,24 +194,29 @@ export type CloudFunction = {
193194

194195
export type CreateOptions = {
195196
onPoll?: OnFunction;
197+
onDebug?: OnDebugFunction;
196198
};
197199

198200
export type DeleteOptions = {
199201
onPoll?: OnFunction;
202+
onDebug?: OnDebugFunction;
200203
};
201204

202205
export type PatchOptions = {
203206
onPoll?: OnFunction;
207+
onDebug?: OnDebugFunction;
204208
};
205209

206210
export type DeployOptions = {
207211
onPoll?: OnFunction;
208212
onZip?: OnZipFunction;
209213
onNew?: OnFunction;
210214
onExisting?: OnFunction;
215+
onDebug?: OnDebugFunction;
211216
} & ZipOptions;
212217

213218
export type OnFunction = () => void;
219+
export type OnDebugFunction = (f: () => string) => void;
214220
export type OnZipFunction = (sourceDir: string, zipPath: string) => void;
215221

216222
export class CloudFunctionsClient {
@@ -332,6 +338,11 @@ export class CloudFunctionsClient {
332338
async create(cf: CloudFunction, opts?: CreateOptions): Promise<CloudFunctionResponse> {
333339
const resourceName = this.fullResourceName(cf.name);
334340
cf.name = resourceName;
341+
if (opts?.onDebug) {
342+
opts.onDebug((): string => {
343+
return `create: computed Cloud Function:\n${JSON.stringify(cf, null, 2)}`;
344+
});
345+
}
335346

336347
const parent = this.parentFromName(resourceName);
337348
const functionName = resourceName.split('/').at(-1);
@@ -342,6 +353,7 @@ export class CloudFunctionsClient {
342353
const resp: Operation = await this.#request('POST', u, body);
343354
const op = await this.#pollOperation(resp.name, {
344355
onPoll: opts?.onPoll,
356+
onDebug: opts?.onDebug,
345357
});
346358

347359
if (!op.response) {
@@ -361,6 +373,7 @@ export class CloudFunctionsClient {
361373
const resp: Operation = await this.#request('DELETE', u);
362374
return await this.#pollOperation(resp.name, {
363375
onPoll: opts?.onPoll,
376+
onDebug: opts?.onDebug,
364377
});
365378
}
366379

@@ -421,12 +434,25 @@ export class CloudFunctionsClient {
421434
async patch(cf: CloudFunction, opts?: PatchOptions): Promise<CloudFunctionResponse> {
422435
const resourceName = this.fullResourceName(cf.name);
423436
cf.name = resourceName;
437+
if (opts?.onDebug) {
438+
opts.onDebug((): string => {
439+
return `patch: computed Cloud Function:\n${JSON.stringify(cf, null, 2)}`;
440+
});
441+
}
424442

425-
const u = `${this.#endpoints.cloudfunctions}/${resourceName}`;
443+
const updateMask = this.computeUpdateMask(cf);
444+
if (opts?.onDebug) {
445+
opts.onDebug((): string => {
446+
return `Computed updateMask: ${updateMask}`;
447+
});
448+
}
449+
450+
const u = `${this.#endpoints.cloudfunctions}/${resourceName}?updateMask=${updateMask}`;
426451
const body = JSON.stringify(cf);
427452
const resp: Operation = await this.#request('PATCH', u, body);
428453
const op = await this.#pollOperation(resp.name, {
429454
onPoll: opts?.onPoll,
455+
onDebug: opts?.onDebug,
430456
});
431457

432458
if (!op.response) {
@@ -489,12 +515,14 @@ export class CloudFunctionsClient {
489515
if (opts?.onExisting) opts.onExisting();
490516
const resp: CloudFunctionResponse = await this.patch(cf, {
491517
onPoll: opts?.onPoll,
518+
onDebug: opts?.onDebug,
492519
});
493520
return resp;
494521
} else {
495522
if (opts?.onNew) opts.onNew();
496523
const resp: CloudFunctionResponse = await this.create(cf, {
497524
onPoll: opts?.onPoll,
525+
onDebug: opts?.onDebug,
498526
});
499527
return resp;
500528
}
@@ -571,4 +599,27 @@ export class CloudFunctionsClient {
571599
const parent = parts.slice(0, parts.length - 2).join('/');
572600
return parent;
573601
}
602+
603+
computeUpdateMask(cf: CloudFunction): string {
604+
const keys: string[] = [];
605+
606+
const iter = (obj: Object, root?: string) => {
607+
for (const [k, v] of Object.entries(obj)) {
608+
if (v === undefined) {
609+
continue;
610+
}
611+
612+
const pth = root ? root + '.' + k : k;
613+
if (typeof v === 'object' && !Array.isArray(v)) {
614+
iter(v, pth);
615+
} else {
616+
keys.push(pth);
617+
}
618+
}
619+
};
620+
621+
iter(cf);
622+
623+
return keys.join(',');
624+
}
574625
}

src/main.ts

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

1717
import { EntryData } from 'archiver';
18-
import { debug as logDebug, getInput, info as logInfo, setFailed, setOutput } from '@actions/core';
18+
import {
19+
debug as logDebug,
20+
getInput,
21+
info as logInfo,
22+
isDebug,
23+
setFailed,
24+
setOutput,
25+
} from '@actions/core';
1926
import {
2027
errorMessage,
2128
parseBoolean,
@@ -190,6 +197,11 @@ async function run() {
190197
iteration++;
191198
};
192199
})(),
200+
onDebug: (f) => {
201+
if (isDebug()) {
202+
logDebug(f());
203+
}
204+
},
193205
});
194206

195207
if (resp.state !== 'ACTIVE') {

0 commit comments

Comments
 (0)