This repository has been archived by the owner on Nov 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathentrypoint.js
90 lines (74 loc) · 2.7 KB
/
entrypoint.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
const axios = require('axios')
const yaml = require('node-yaml')
const fs = require('fs')
const { execSync } = require('child_process')
const { Toolkit } = require('actions-toolkit')
Toolkit.run(async tools => {
tools.log.config({
logLevel: tools.inputs.logLevel
})
const url = tools.inputs.url
const outputFile = tools.inputs.outputFile
const accept = tools.inputs.accept
const options = {
headers: {
Authorization: `bearer ${tools.token}`
}
}
tools.log.debug(`url: ${url}`)
tools.log.debug(`outputFile: ${outputFile}`)
tools.log.debug(`accept: ${accept}`)
let body = {}
let yamlContent = yaml.parse(tools.getFile(tools.inputs.query))
body.query = yamlContent.query
tools.log.debug(`query: ${body.query}`)
if (yamlContent.variables) {
body.variables = yamlContent.variables
// Looking at the variables values to check if they are scalar or pointers to args/jq
for (const key in body.variables) {
let value = body.variables[key]
tools.log.debug(`GraphQL param ${key} found`)
if (typeof value === 'object') {
if (value.type === 'arg') {
// Value is coming from the command line
body.variables[key] = tools.inputs[value.name]
} else if (value.type === 'jq') {
// Need to apply jq to a file to retrieve a value
let jsonFile = value.file
let jqQuery = value.query
let result = execSync(`cat ${tools.workspace}/${jsonFile} | jq -j '${jqQuery}'`, {stdio: [this.stdin, this.stdout, this.stderr]})
if (value.cast === 'Int') {
body.variables[key] = parseInt(result)
} else if (value.cast === 'Float') {
body.variables[key] = parseFloat(result)
} else if (value.cast === 'Boolean') {
body.variables[key] = (result.toLowerCase() == 'true');
} else {
body.variables[key] = result.toString()
}
}
}
}
tools.log.info(`GraphQL Variables:\n ${JSON.stringify(body.variables)}`)
}
if (accept) {
options.headers.Accept = accept
}
const response = await axios.post(url, body, options)
const jsonStringData = JSON.stringify(response.data)
tools.runInWorkspace('echo', `::set-output name=queryResult::${jsonStringData}`)
tools.log.debug(`Output variable queryResult set to ${jsonStringData}`)
if (outputFile) {
fs.writeFile(`${tools.workspace}/${outputFile}`, jsonStringData, (err) => {
if (err) {
tools.exit.failure(err)
}
tools.log.debug(`GraphQL response saved to ${outputFile}`)
tools.exit.success('Sweet success')
})
} else {
tools.exit.success('Sweet success')
}
}, {
secrets: ['GITHUB_TOKEN']
})