Skip to content

Commit bd4b2c1

Browse files
other (#25)
* extract main module * bump Co-authored-by: saffi <[email protected]>
1 parent 275ee87 commit bd4b2c1

File tree

4 files changed

+77
-57
lines changed

4 files changed

+77
-57
lines changed

action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ inputs:
44
VERSION:
55
description: specify client version
66
required: false
7-
default: 0.0.29
7+
default: 0.0.30
88
CF_API_KEY:
99
description: "Codefresh API KEY"
1010
required: true

service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
name: csdp-report-image
2-
version: 0.0.29
2+
version: 0.0.30

src/index.ts

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,3 @@
1-
import EventSource from 'eventsource'
1+
import { mainErrorHandling } from './main'
22

3-
import { validate } from './validate'
4-
import { buildUrlHeaders } from './request-builder'
5-
6-
/**
7-
* Take (CF_ prefixed) Env variables and perform http/s request (SSE) to app-proxy for image-report with CF_ENRICHERS
8-
*/
9-
async function main(): Promise<void> {
10-
const verbose = process.argv.includes('verbose') || process.env['VERBOSE']
11-
if (verbose) {
12-
console.debug('Running with verbose log')
13-
}
14-
const payload = validate(process.env)
15-
const { url, headers } = buildUrlHeaders(payload)
16-
if (verbose) {
17-
console.debug(`Payload: ${JSON.stringify(payload, null, 2)}`)
18-
console.debug(`Sending request: ${url}, headers: ${JSON.stringify(headers)}`)
19-
}
20-
if (payload['CF_CI_TYPE'] && payload['CF_WORKFLOW_URL']) {
21-
console.info(`Running ${payload['CF_CI_TYPE']} URL: ${payload['CF_WORKFLOW_URL']}`)
22-
}
23-
const waitFor = new Promise((resolve, reject) => {
24-
const eventSource = new EventSource(url, { headers })
25-
eventSource.addEventListener('report', function (event) {
26-
console.info(`report =>`, JSON.stringify(JSON.parse(event.data), null, 2))
27-
})
28-
eventSource.addEventListener('info', function (event) {
29-
console.info(`\t\t${JSON.stringify(event.data)}`)
30-
})
31-
eventSource.addEventListener('warn', function (event) {
32-
console.warn(`Warning:\t${JSON.stringify(event.data)}`)
33-
})
34-
eventSource.addEventListener('error', (err) => {
35-
eventSource.close()
36-
reject(err)
37-
})
38-
eventSource.addEventListener('end', (ev) => {
39-
eventSource.close()
40-
resolve(ev)
41-
})
42-
})
43-
await waitFor
44-
}
45-
46-
main().then(() => { return }).catch((error) => {
47-
console.error(error)
48-
if (error.type=='error' && error.status===500) {
49-
const tokenStart = (process.env['CF_API_KEY'] || '').slice(0, 6)
50-
const tokenEnd = (process.env['CF_API_KEY'] || '').slice(-6)
51-
console.info(`Error 500 are usually caused by providing an invalid CF_API_KEY, please check that the validity of the provided codefresh api token ${tokenStart}..${tokenEnd}`)
52-
}
53-
// Catchall for general errors
54-
process.exit(1)
55-
}
56-
)
3+
mainErrorHandling()

src/main.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import EventSource from 'eventsource'
2+
3+
import { validate } from './validate'
4+
import { buildUrlHeaders } from './request-builder'
5+
6+
/**
7+
* Take (CF_ prefixed) Env variables and perform http/s request (SSE) to app-proxy for image-report with CF_ENRICHERS
8+
*/
9+
async function mainProcess(argv, env): Promise<void> {
10+
const verbose = argv.includes('verbose') || env['VERBOSE']
11+
if (verbose) {
12+
console.debug('Running with verbose log')
13+
}
14+
const payload = validate(env)
15+
const { url, headers } = buildUrlHeaders(payload)
16+
if (verbose) {
17+
console.debug(`Payload: ${JSON.stringify(payload, null, 2)}`)
18+
console.debug(`Sending request: ${url}, headers: ${JSON.stringify(headers)}`)
19+
}
20+
if (payload['CF_CI_TYPE'] && payload['CF_WORKFLOW_URL']) {
21+
console.info(`Running ${payload['CF_CI_TYPE']} URL: ${payload['CF_WORKFLOW_URL']}`)
22+
}
23+
const waitFor = new Promise((resolve, reject) => {
24+
const eventSource = new EventSource(url, { headers })
25+
eventSource.addEventListener('report', function (event) {
26+
console.info(`report =>`, JSON.stringify(JSON.parse(event.data), null, 2))
27+
})
28+
eventSource.addEventListener('info', function (event) {
29+
console.info(`\t\t${JSON.stringify(event.data)}`)
30+
})
31+
eventSource.addEventListener('warn', function (event) {
32+
console.warn(`Warning:\t${JSON.stringify(event.data)}`)
33+
})
34+
eventSource.addEventListener('error', (err) => {
35+
eventSource.close()
36+
reject(err)
37+
})
38+
eventSource.addEventListener('end', (ev) => {
39+
eventSource.close()
40+
resolve(ev)
41+
})
42+
})
43+
await waitFor
44+
}
45+
46+
async function main(argv, env): Promise<void> {
47+
try {
48+
await mainProcess(argv, env)
49+
} catch (error) {
50+
console.error(error)
51+
if (error.type=='error' && error.status===500) {
52+
const tokenStart = (env['CF_API_KEY'] || '').slice(0, 6)
53+
const tokenEnd = (env['CF_API_KEY'] || '').slice(-6)
54+
console.info(`Error 500 are usually caused by providing an invalid CF_API_KEY, please check that the validity of the provided codefresh api token ${tokenStart}..${tokenEnd}`)
55+
}
56+
throw error
57+
}
58+
}
59+
60+
/**
61+
* calling main with process argv and env. Exit code 1 on error
62+
*/
63+
export async function mainErrorHandling() {
64+
try {
65+
await main(process.argv, process.env)
66+
} catch {
67+
// Catchall for general errors
68+
process.exit(1)
69+
}
70+
}
71+
72+
73+

0 commit comments

Comments
 (0)