Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (79 loc) · 2.26 KB
/
Copy pathindex.js
File metadata and controls
89 lines (79 loc) · 2.26 KB
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
import * as core from '@actions/core'
import kv from './cloudflare.js'
// KV often holds tokens. Mask the value, each of its lines (the runner masks
// per line) and each string or number inside parsed JSON, so that
// fromJSON(...).token is masked too.
function maskValue(value) {
const secrets = new Set()
const add = (text) => {
secrets.add(text)
for (const line of text.split('\n')) {
secrets.add(line.trim())
}
}
// Explicit stack: deeply nested JSON must not overflow the call stack.
const addLeaves = (root) => {
const stack = [root]
while (stack.length > 0) {
const node = stack.pop()
if (typeof node === 'string') {
add(node)
// toJSON prints quotes and backslashes escaped.
const escaped = JSON.stringify(node).slice(1, -1)
if (escaped !== node) {
add(escaped)
}
} else if (typeof node === 'number') {
add(String(node))
} else if (node && typeof node === 'object') {
for (const child of Object.values(node)) {
stack.push(child)
}
}
}
}
if (typeof value === 'string') {
add(value)
} else {
add(JSON.stringify(value))
addLeaves(value)
}
for (const secret of secrets) {
if (secret) {
core.setSecret(secret)
}
}
}
async function run() {
try {
const accountId = process.env.CLOUDFLARE_ACCOUNT_ID
const apiKey = process.env.CLOUDFLARE_API_KEY
const accountEmail = process.env.CLOUDFLARE_ACCOUNT_EMAIL
const namespaceId = core.getInput('namespace_identifier')
const key = core.getInput('key_name')
const value = core.getInput('value')
const expirationTtl = core.getInput('expiration_ttl')
const expiration = core.getInput('expiration')
const operation = core.getInput('operation').toLowerCase()
const failOnMissing = core.getBooleanInput('fail_on_missing')
const returnValue = await kv(
accountId,
accountEmail,
apiKey,
namespaceId,
key,
value,
expiration,
expirationTtl,
operation,
failOnMissing
)
if (returnValue !== null && returnValue !== undefined) {
maskValue(returnValue)
core.setOutput('value', returnValue)
}
} catch (error) {
core.setFailed(error.message)
}
}
run()