-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (142 loc) · 4.5 KB
/
index.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
const core = require('@actions/core')
const fs = require('fs/promises')
const path = require('path')
const fetch = require('node-fetch')
const setCookieParser = require('set-cookie-parser')
const apiUrl = core.getInput('api_url')
const loginCredentials = {
username: core.getInput('username'),
password: core.getInput('password')
}
const project = core.getInput('project_name')
async function publishReport () {
try {
// login feature
const response = await fetch(`${apiUrl}/login`, {
method: 'POST',
body: JSON.stringify(loginCredentials),
headers: { 'Content-Type': 'application/json' }
})
const message = await response.json()
if (response.status !== 200) {
throw new Error(`Login failed! ${JSON.stringify(message, null, 2)}`)
}
const cookie = response.headers.raw()['set-cookie']
const cookies = setCookieParser.parse(cookie, {
decodeValues: true,
map: true,
silent: false
})
const finalCookie = [
`access_token_cookie=${cookies.access_token_cookie.value}`,
`csrf_access_token=${cookies.csrf_access_token.value}`,
`refresh_token_cookie=${cookies.refresh_token_cookie.value}`,
`csrf_refresh_token=${cookies.csrf_refresh_token.value}`
].join('; ')
const csrfAccessToken = cookies.csrf_access_token.value
// get projects
const resp = await fetch(`${apiUrl}/projects`, {
method: 'GET',
headers: { 'Content-Type': 'application/json', cookie: finalCookie }
})
const data = await resp.json()
if (resp.status !== 200) {
throw new Error(
`Failed to get projects. ${JSON.stringify(data, null, 2)}`
)
}
const projects = data.data.projects
const keys = Object.keys(projects)
const exists = keys.includes(project)
if (exists === false) {
// create project
const res = await fetch(`${apiUrl}/projects`, {
method: 'POST',
body: JSON.stringify({
id: project
}),
headers: {
'Content-Type': 'application/json',
cookie: finalCookie,
'X-CSRF-TOKEN': csrfAccessToken
}
})
const newProject = await res.json()
if (res.status !== 201) {
throw new Error(
`Failed to create project. ${JSON.stringify(newProject, null, 2)}`
)
}
}
const folderPath = core.getInput('results_path')
async function readdir () {
const file = await fs.readdir(folderPath)
return file
}
const files = await readdir()
const result = []
files.forEach((file) => {
const filePath = path.join(folderPath, file)
const extension = filePath.split('.').pop().toLowerCase()
if (extension === 'json' || extension === 'xml') {
result.push(filePath)
}
})
let encodedData = []
for (let i = 0; i < result.length; i++) {
const filePath = result[i]
const results = []
async function readfile () {
const fileData = await fs.readFile(filePath)
const buffer = Buffer.from(fileData, 'utf8')
const base64Data = buffer.toString('base64')
results.push({
content_base64: base64Data,
file_name: files[i]
})
return results
}
const data = await readfile()
encodedData = [...encodedData, ...data]
}
const object = {
results: encodedData
}
const filesData = JSON.stringify(object)
const res = await fetch(
`${apiUrl}/send-results?project_id=${project}&force_project_creation=false`,
{
method: 'POST',
body: filesData,
headers: {
'Content-Type': 'application/json',
cookie: finalCookie,
'X-CSRF-TOKEN': csrfAccessToken
}
}
)
const resultsApiData = await res.json()
if (res.status !== 200) {
throw new Error(
`Failed to send results. ${JSON.stringify(resultsApiData, null, 2)}`
)
}
const report = await fetch(
`${apiUrl}/generate-report?project_id=${project}`,
{
method: 'GET',
headers: { 'Content-Type': 'application/json', cookie: finalCookie }
}
)
const generatedReprot = await report.json()
if (report.status !== 200) {
throw new Error(
`Failed to generate report. ${JSON.stringify(generatedReprot, null, 2)}`
)
}
core.info(`Published report. API respoonse: ${JSON.stringify(generatedReprot, null, 2)}`)
} catch (error) {
core.setFailed(`Error occurred while hitting the API: ${error.message}`)
}
}
publishReport()