forked from helaili/github-graphql-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.js
More file actions
91 lines (77 loc) · 2.57 KB
/
entrypoint.js
File metadata and controls
91 lines (77 loc) · 2.57 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
89
90
91
const axios = require('axios')
const yaml = require('node-yaml')
const fs = require('fs')
const { execSync } = require('child_process')
const { Toolkit } = require('actions-toolkit')
const tools = new Toolkit()
let options = {
headers: {
Authorization: `bearer ${tools.token}`
}
}
let url = 'https://api.github.com/graphql'
if (tools.arguments.url) {
url = tools.arguments.url
}
let outputFile = 'github-graphql-action.json'
if (tools.arguments.output) {
outputFile = tools.arguments.output
}
let body = {}
if (!tools.arguments.query) {
console.error('Configuration error: missing query argument')
process.exit(-1)
} else {
let yamlContent = yaml.parse(tools.getFile(tools.arguments.query))
body.query = yamlContent.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]
if (typeof value === 'object') {
if (value.type === 'arg') {
// Value is coming from the command line
body.variables[key] = tools.arguments[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()
}
}
}
}
if (tools.arguments.log) {
console.log(`GraphQL Variables:\n ${JSON.stringify(body.variables)}`)
}
}
}
if (tools.arguments.accept) {
options.headers.Accept = tools.arguments.accept
}
axios.post(url, body, options)
.then(function (response) {
let jsonStringData = JSON.stringify(response.data)
fs.writeFile(`${tools.workspace}/${outputFile}`, jsonStringData, (err) => {
if (err) throw err
if (tools.arguments.log) {
console.log(`GraphQL response saved to ${outputFile}`)
}
})
if (tools.arguments.log) {
console.log(`GraphQL Query response:\n ${jsonStringData}`)
}
})
.catch(function (error) {
console.log(error)
process.exit(-1)
})