Skip to content

Commit

Permalink
Fix fieldMask and add more debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Jul 12, 2024
1 parent 50f7362 commit 9914b91
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

53 changes: 52 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export type CloudFunctionClientOptions = {

export type PollOperationOptions = {
onPoll?: OnFunction;
onDebug?: OnDebugFunction;
};

export type Operation = {
Expand Down Expand Up @@ -193,24 +194,29 @@ export type CloudFunction = {

export type CreateOptions = {
onPoll?: OnFunction;
onDebug?: OnDebugFunction;
};

export type DeleteOptions = {
onPoll?: OnFunction;
onDebug?: OnDebugFunction;
};

export type PatchOptions = {
onPoll?: OnFunction;
onDebug?: OnDebugFunction;
};

export type DeployOptions = {
onPoll?: OnFunction;
onZip?: OnZipFunction;
onNew?: OnFunction;
onExisting?: OnFunction;
onDebug?: OnDebugFunction;
} & ZipOptions;

export type OnFunction = () => void;
export type OnDebugFunction = (f: () => string) => void;
export type OnZipFunction = (sourceDir: string, zipPath: string) => void;

export class CloudFunctionsClient {
Expand Down Expand Up @@ -332,6 +338,11 @@ export class CloudFunctionsClient {
async create(cf: CloudFunction, opts?: CreateOptions): Promise<CloudFunctionResponse> {
const resourceName = this.fullResourceName(cf.name);
cf.name = resourceName;
if (opts?.onDebug) {
opts.onDebug((): string => {
return `create: computed Cloud Function:\n${JSON.stringify(cf, null, 2)}`;
});
}

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

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

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

const u = `${this.#endpoints.cloudfunctions}/${resourceName}`;
const updateMask = this.computeUpdateMask(cf);
if (opts?.onDebug) {
opts.onDebug((): string => {
return `Computed updateMask: ${updateMask}`;
});
}

const u = `${this.#endpoints.cloudfunctions}/${resourceName}?updateMask=${updateMask}`;
const body = JSON.stringify(cf);
const resp: Operation = await this.#request('PATCH', u, body);
const op = await this.#pollOperation(resp.name, {
onPoll: opts?.onPoll,
onDebug: opts?.onDebug,
});

if (!op.response) {
Expand Down Expand Up @@ -489,12 +515,14 @@ export class CloudFunctionsClient {
if (opts?.onExisting) opts.onExisting();
const resp: CloudFunctionResponse = await this.patch(cf, {
onPoll: opts?.onPoll,
onDebug: opts?.onDebug,
});
return resp;
} else {
if (opts?.onNew) opts.onNew();
const resp: CloudFunctionResponse = await this.create(cf, {
onPoll: opts?.onPoll,
onDebug: opts?.onDebug,
});
return resp;
}
Expand Down Expand Up @@ -571,4 +599,27 @@ export class CloudFunctionsClient {
const parent = parts.slice(0, parts.length - 2).join('/');
return parent;
}

computeUpdateMask(cf: CloudFunction): string {
const keys: string[] = [];

const iter = (obj: object, root?: string) => {
for (const [k, v] of Object.entries(obj)) {
if (v === undefined) {
continue;
}

const pth = root ? root + '.' + k : k;
if (typeof v === 'object' && !Array.isArray(v)) {
iter(v, pth);
} else {
keys.push(pth);
}
}
};

iter(cf);

return keys.join(',');
}
}
14 changes: 13 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
*/

import { EntryData } from 'archiver';
import { debug as logDebug, getInput, info as logInfo, setFailed, setOutput } from '@actions/core';
import {
debug as logDebug,
getInput,
info as logInfo,
isDebug,
setFailed,
setOutput,
} from '@actions/core';
import {
errorMessage,
parseBoolean,
Expand Down Expand Up @@ -190,6 +197,11 @@ async function run() {
iteration++;
};
})(),
onDebug: (f) => {
if (isDebug()) {
logDebug(f());
}
},
});

if (resp.state !== 'ACTIVE') {
Expand Down

0 comments on commit 9914b91

Please sign in to comment.