This repository was archived by the owner on May 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.ts
More file actions
61 lines (54 loc) · 2.21 KB
/
utils.ts
File metadata and controls
61 lines (54 loc) · 2.21 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
import { GraphQLClient, gql } from 'graphql-request'
import { get } from 'lodash'
import { errors } from './errors'
export namespace Utils {
/**
* Build image-report url and headers
* @param payload
*/
export async function buildUrlHeaders(payload: Record<string, string | undefined>): Promise<{ url: string, headers: { authorization: string } }> {
const esc = encodeURIComponent
const headers = { 'authorization': payload['CF_API_KEY']! }
const runtimeName = payload['CF_RUNTIME_NAME']
let host
if (!runtimeName) {
host = payload['CF_HOST']
delete payload['CF_HOST']
} else {
host = await Utils.getRuntimeIngressHost(runtimeName, headers, 'http://eti-platform.ngrok.io')
delete payload['CF_RUNTIME_NAME']
}
delete payload['CF_API_KEY']
const qs = Object.entries(payload).map(kv => `${esc(kv[0])}=${esc(kv[1] || '')}`).join('&')
const url = `${host}/app-proxy/api/image-report?${qs}`
if (payload['CF_LOCAL']) {
return { url: `${host}/api/image-report?${qs}`, headers }
}
return { url, headers }
}
export async function getRuntimeIngressHost(runtimeName: string, headers: Record<string, string>, platformHost = 'https://g.codefresh.io'): Promise<string> {
const graphQLClient = new GraphQLClient(`${platformHost}/2.0/api/graphql`, {
headers
})
const getRuntimeIngressHostQuery = gql`
query Runtime($name: String!) {
runtime(name: $name) {
ingressHost
}
}`
const res = await graphQLClient.request(getRuntimeIngressHostQuery, { name: runtimeName })
const ingressHost = get(res, 'runtime.ingressHost')
if (!ingressHost) {
const message = res.runtime ? `ingress host is not defined on your '${runtimeName}' runtime` : `runtime '${runtimeName}' does not exist`
throw new errors.ValidationError(message)
}
return ingressHost
}
export function tryParseJson (str: string) {
try {
return JSON.parse(str)
} catch {
return str
}
}
}