-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathprocessRequest.mjs
More file actions
88 lines (73 loc) · 3.14 KB
/
processRequest.mjs
File metadata and controls
88 lines (73 loc) · 3.14 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
export const NETWORK_CONFIG = {
MAINNET: { chainId: 1, blockExplorer: 'etherscan.io' },
ARBITRUM_ONE: { chainId: 42161, blockExplorer: 'arbiscan.io' },
BASE: { chainId: 8453, blockExplorer: 'basescan.org' },
AVALANCHE: { chainId: 43114, blockExplorer: 'snowscan.xyz' },
POLYGON: { chainId: 137, blockExplorer: 'polygonscan.com' },
BNB: { chainId: 56, blockExplorer: 'bscscan.com' },
LENS: { chainId: 232, blockExplorer: 'explorer.lens.xyz' },
GNOSIS: { chainId: 100, blockExplorer: 'gnosisscan.io' },
LINEA: { chainId: 59144, blockExplorer: 'lineascan.build' },
PLASMA: { chainId: 9745, blockExplorer: 'plasmascan.to' },
INK: { chainId: 57073, blockExplorer: 'explorer.inkonchain.com' },
}
export const VALIDATION_RULES = {
addImage: ['network', 'url', 'address'],
addToken: ['network', 'symbol', 'name', 'url', 'decimals', 'address', 'reason'],
removeToken: ['network', 'reason', 'address'],
}
export const extractFieldValues = (body, fieldNames) => {
return fieldNames.reduce((acc, f) => {
// Create a regex for each field, with capturing group with the same name
const r = new RegExp(String.raw`${f}\s+(.*?)(\s+###|$)`, 'si')
// Build an object with the capturing group and value for each field
return { ...acc, [f.toLowerCase()]: body.match(r)?.[1].trim() }
}, {})
}
export const applyNetworkConfig = (values) => {
console.log(values)
const config = NETWORK_CONFIG[values.network]
if (!config) {
throw new Error('No valid network found.')
}
Object.assign(values, config)
}
export const generateImageUrls = (values) => {
const { chainId, address } = values
if (chainId && address) {
values.prImageUrl = `https://raw.githubusercontent.com/cowprotocol/token-lists/{0}/${chainId}_${address}/src/public/images/${chainId}/${address}/logo.png`
values.logoURI = `https://files.cow.fi/token-lists/images/${chainId}/${address}/logo.png`
}
}
export const getOperation = (labels) => {
const operation = labels.find((label) => Object.keys(VALIDATION_RULES).includes(label))
if (!operation) {
throw new Error('No valid operation label found')
}
return operation
}
export const validateFields = (operation, values) => {
const requiredFields = VALIDATION_RULES[operation]
const missingFields = requiredFields.filter((field) => !values[field])
if (missingFields.length > 0) {
throw new Error(`Missing required fields for ${operation}: ${missingFields.join(', ')}`)
}
if (values.symbol?.includes(' ')) {
throw new Error('Symbol cannot contain spaces')
}
}
export const processRequest = (context, core) => {
const { issue } = context.payload
const body = issue.body
const labels = issue.labels.map((label) => label.name)
const fieldNames = process.env.FIELD_NAMES.split(',')
const values = extractFieldValues(body, fieldNames)
values.address &&= values.address.toLowerCase()
applyNetworkConfig(values)
generateImageUrls(values)
const operation = getOperation(labels)
validateFields(operation, values)
core.setOutput('operation', operation)
core.setOutput('issueInfo', JSON.stringify(values))
core.setOutput('needsImageOptimization', ['addImage', 'addToken'].includes(operation))
}