Allow {} to represent any value in event payloads

This commit is contained in:
Josh Gross
2023-01-23 11:35:09 -05:00
parent 378d2ab799
commit e8bf17771c
2 changed files with 12 additions and 17 deletions
@@ -1,10 +1,7 @@
{
"action": "on-demand-test",
"branch": "master",
"client_payload": {
"unit": false,
"integration": true
},
"client_payload": {},
"repository": {
"id": 17273051,
"node_id": "MDEwOlJlcG9zaXRvcnkxNzI3MzA1MQ==",
@@ -89,26 +89,24 @@ function getEventContext(workflowContext: WorkflowContext): ExpressionData {
for (const e of events) {
const payload = eventPayloads[e];
if (payload) {
const anyKeys = ANY_KEYS[e] || [];
merge(d, payload, anyKeys);
merge(d, payload);
}
}
return d;
}
// These events have a top-level object that can be any type
const ANY_KEYS: Record<string, string[]> = {
repository_dispatch: ["client_payload"]
};
function merge(d: data.Dictionary, toAdd: Object, anyKeys: string[]): data.Dictionary {
function merge(d: data.Dictionary, toAdd: Object): data.Dictionary {
for (const [key, value] of Object.entries(toAdd)) {
if (anyKeys.includes(key)) {
d.add(key, new data.Null());
} else if (value && typeof value === "object" && !d.get(key)) {
// Only use anyKeys for the top-level object
d.add(key, merge(new data.Dictionary(), value, []));
if (value && typeof value === "object" && !d.get(key)) {
if (!Array.isArray(value) && Object.entries(value).length === 0) {
// Allow an empty object to be any value
d.add(key, new data.Null());
continue;
}
d.add(key, merge(new data.Dictionary(), value));
} else {
d.add(key, new data.Null());
}