-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathcontent.js
166 lines (162 loc) · 4.93 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import fs from "node:fs";
import path from "node:path";
import github from "@actions/github";
import { flatten } from "flat";
import yaml from "js-yaml";
import markup from "markup-js";
import Config from "./config.js";
import SlackError from "./errors.js";
/**
* The parsed payload provided to the action and passed to the preferred method
* of sending a payload.
*/
export default class Content {
/**
* Gather content from the provided payload or payload file path with parsings.
* @param {Config} config
* @returns {this} - An instance of this Content class.
*/
get(config) {
switch (true) {
case !!config.inputs.payload && !!config.inputs.payloadFilePath:
throw new SlackError(
config.core,
"Invalid input! Just the payload or payload file path is required.",
);
case !!config.inputs.payload:
this.values = this.getContentPayload(config);
break;
case !!config.inputs.payloadFilePath:
this.values = this.getContentPayloadFilePath(config);
break;
default:
config.core.debug(
"Missing payload so gathering inputs from action context.",
);
this.values = github.context;
break;
}
if (config.inputs.payloadDelimiter) {
this.values = flatten(this.values, {
delimiter: config.inputs.payloadDelimiter,
});
for (const key of Object.keys(this.values)) {
this.values[key] = `${this.values[key]}`;
}
}
return this;
}
/**
* Format request content from payload values for use in the request.
* @param {Config} config
* @throws if the input payload or payload file path is invalid JSON.
* @returns {Content} - the parsed JSON payload to use in requests.
*/
getContentPayload(config) {
if (!config.inputs.payload) {
throw new SlackError(
config.core,
"Invalid input! No payload content was provided",
);
}
try {
const input = this.templatize(config, config.inputs.payload);
const content = /** @type {Content} */ (
yaml.load(input, {
schema: yaml.JSON_SCHEMA,
})
);
return /** @type {Content} */ (content);
} catch (error) {
if (error instanceof Error) {
config.core.debug("Failed to parse input payload as YAML");
config.core.debug(error.message);
}
}
try {
const trimmed = config.inputs.payload.trim();
if (
!config.inputs.payload.startsWith("{") &&
!config.inputs.payload.endsWith("}")
) {
config.core.debug(
"Wrapping input payload in braces to create valid JSON",
);
const comma = trimmed.replace(/,$/, ""); // remove trailing comma
const wrap = `{${comma}}`;
return JSON.parse(wrap);
}
return JSON.parse(trimmed);
} catch (/** @type {any} */ error) {
throw new SlackError(
config.core,
"Invalid input! Failed to parse contents of the provided payload",
{
cause: error,
},
);
}
}
/**
* Format request content from the payload file path for use in the request.
* @param {Config} config
* @throws if the input payload or payload file path is invalid JSON.
* @returns {Content} - the parsed JSON payload to use in requests.
*/
getContentPayloadFilePath(config) {
if (!config.inputs.payloadFilePath) {
throw new SlackError(
config.core,
"Invalid input! No payload found for content",
);
}
try {
const input = fs.readFileSync(
path.resolve(config.inputs.payloadFilePath),
"utf-8",
);
const content = this.templatize(config, input);
if (
config.inputs.payloadFilePath.endsWith("yaml") ||
config.inputs.payloadFilePath.endsWith("yml")
) {
const load = yaml.load(content, {
schema: yaml.JSON_SCHEMA,
});
return /** @type {Content} */ (load);
}
if (config.inputs.payloadFilePath.endsWith("json")) {
return JSON.parse(content);
}
throw new SlackError(
config.core,
`Invalid input! Failed to parse file extension ${config.inputs.payloadFilePath}`,
);
} catch (/** @type {any} */ error) {
throw new SlackError(
config.core,
"Invalid input! Failed to parse contents of the provided payload file",
{
cause: error,
},
);
}
}
/**
* Replace templated variables in the provided content if requested.
* @param {Config} config
* @param {string} input - The initial value of the content.
* @returns {string} Content with templatized variables replaced.
*/
templatize(config, input) {
if (!config.inputs.payloadTemplated) {
return input;
}
const template = input.replace(/\$\{\{/g, "{{"); // swap ${{ for {{
const context = {
env: process.env,
github: github.context,
};
return markup.up(template, context);
}
}