Added reference/component parsing of OpenAPI

This commit is contained in:
frikky
2021-04-11 10:34:48 +02:00
parent 9190ba6ba1
commit e72ba27fb6
8 changed files with 225 additions and 52 deletions
+12 -3
View File
@@ -17,6 +17,7 @@ const Workflow = (props) => {
const [requiredTriggers, setRequiredTriggers] = React.useState([])
const [previousAuth, setPreviousAuth] = React.useState(appAuthentication)
const [firstLoad, setFirstLoad] = React.useState("")
const [itemChanged, setItemChanged] = React.useState(false)
var finished = false
if (workflow === undefined || workflow === null) {
@@ -216,6 +217,7 @@ const Workflow = (props) => {
newWebhook(workflow.triggers[trigger.index])
saveWorkflow(workflow)
setItemChanged(true)
}}>
{trigger.status !== "running" ? "Start" : "Running"}
</Button>
@@ -232,6 +234,7 @@ const Workflow = (props) => {
submitSchedule(workflow.triggers[trigger.index], trigger.index)
saveWorkflow(workflow)
setItemChanged(true)
}}>
{trigger.status !== "running" ? "Start" : "Running"}
</Button>
@@ -350,6 +353,7 @@ const Workflow = (props) => {
<CircularProgress />
:
<Button color="primary" variant="contained" onClick={() => {
setItemChanged(true)
setSelectedAction(action.action)
setSelectedApp(action.app)
setAuthenticationModalOpen(true)
@@ -359,8 +363,9 @@ const Workflow = (props) => {
:
null}
{action.must_activate ?
<Button color="primary" variant="contained" onClick={() => {
<Button disabled={true} color="primary" variant="contained" onClick={() => {
console.log("SHOULD ACTIVATE: ", action)
setItemChanged(true)
}}>
Activate
</Button>
@@ -426,8 +431,12 @@ const Workflow = (props) => {
*/}
<Button color="primary" variant={"contained"} style={{
}} onClick={() => {
saveWorkflow(workflow)
window.location.reload()
if (itemChanged) {
saveWorkflow(workflow)
window.location.reload()
} else {
setConfigureWorkflowModalOpen(false)
}
}}>
Finish setup
</Button>
+28 -9
View File
@@ -87,17 +87,21 @@ const ParsedAction = (props) => {
responseJson.loop_versions = selectedApp.loop_versions
}
var foundAction = responseJson.actions.find(action => action.name === selectedAction.name)
var foundAction = responseJson.actions.find(action => action.name.toLowerCase() === selectedAction.name.toLowerCase())
console.log("FOUNDACTION: ", foundAction)
if (foundAction !== null && foundAction !== undefined) {
var foundparams = []
for (var paramkey in foundAction.parameters) {
const param = foundAction.parameters[paramkey]
const foundParam = selectedAction.parameters.find(item => item.name === param.name)
const foundParam = selectedAction.parameters.find(item => item.name.toLowerCase() === param.name.toLowerCase())
if (foundParam === undefined) {
//console.log("COULDNT find Param: ", param)
console.log("COULDNT find Param: ", param)
} else {
foundAction.parameters[paramkey] = foundParam
}
//foundparams.push(param.name)
}
} else {
alert.error("Couldn't find action "+selectedAction.name)
@@ -285,6 +289,8 @@ const ParsedAction = (props) => {
"value": event.target.value,
}]
console.log("IN IF: ", paramcheck)
} else {
const subparamindex = paramcheck["value_replace"].findIndex(param => param.key === data.name)
if (subparamindex === -1) {
@@ -295,11 +301,25 @@ const ParsedAction = (props) => {
} else {
paramcheck["value_replace"][subparamindex]["value"] = event.target.value
}
console.log("IN ELSE: ", paramcheck)
}
//console.log("PARAM: ", paramcheck)
selectedActionParameters[count]["value_replace"] = paramcheck
selectedAction.parameters[count]["value_replace"] = paramcheck
//if (paramcheck.id === undefined) {
// console.log("Normal paramcheck")
//} else {
// selectedActionParameters[count]["value_replace"] = paramcheck
// selectedAction.parameters[count]["value_replace"] = paramcheck
//}
if (paramcheck["value_replace"] === undefined) {
selectedActionParameters[count]["value_replace"] = paramcheck
selectedAction.parameters[count]["value_replace"] = paramcheck
} else {
selectedActionParameters[count]["value_replace"] = paramcheck["value_replace"]
selectedAction.parameters[count]["value_replace"] = paramcheck["value_replace"]
}
console.log("RESULT: ", selectedAction)
setSelectedAction(selectedAction)
//setUpdate(Math.random())
return
@@ -718,7 +738,6 @@ const ParsedAction = (props) => {
value={selectedActionParameters[count].value}
fullWidth
onChange={(e) => {
console.log("VAL: ", e.target.value)
changeActionParameter(e, count, data)
setUpdate(Math.random())
}}
@@ -1234,7 +1253,7 @@ const ParsedAction = (props) => {
}}
fullWidth
onChange={(e) => {
console.log("CHOSE AN AUTHENTICATION OPTION: ", e.target.value)
//console.log("CHOSE AN AUTHENTICATION OPTION: ", e.target.value)
selectedAction.selectedAuthentication = e.target.value
selectedAction.authentication_id = e.target.value.id
setSelectedAction(selectedAction)
@@ -1243,7 +1262,7 @@ const ParsedAction = (props) => {
style={{backgroundColor: theme.palette.inputColor, color: "white", height: 50, maxWidth: rightsidebarStyle.maxWidth-80,}}
>
{selectedAction.authentication.map(data => {
console.log("DATA: ", data)
//console.log("AUTH DATA: ", data)
return(
<MenuItem key={data.id} style={{backgroundColor: theme.palette.inputColor, color: "white"}} value={data}>
{data.label} - ({data.app.app_version})
+1 -1
View File
@@ -5730,7 +5730,7 @@ const AngularWorkflow = (props) => {
const timestamp = new Date(data.started_at*1000).toISOString().split('.')[0].split("T").join(" ")
var calculatedResult = data.workflow.actions.length
var calculatedResult = data.workflow.actions !== undefined && data.workflow.actions !== null ? data.workflow.actions.length : 0
for (var key in data.workflow.triggers) {
const trigger = data.workflow.triggers[key]
if ((trigger.app_name === "User Input" && trigger.trigger_type === "USERINPUT") || (trigger.app_name === "Shuffle Workflow" && trigger.trigger_type === "SUBFLOW")) {
+124 -2
View File
@@ -338,7 +338,6 @@ const AppCreator = (props) => {
throw new Error("NOT 200 :O")
}
//console.log("DATA: ", response.text())
return response.json()
})
.then((responseJson) => {
@@ -373,6 +372,7 @@ const AppCreator = (props) => {
const handleGetRef = (parameter, data) => {
if (parameter["$ref"] === undefined) {
console.log("$ref not found in getref: ")
return parameter
}
@@ -530,6 +530,8 @@ const AppCreator = (props) => {
"example_response": "",
}
//console.log("Schema is application/json: ", methodvalue)
//console.log("DATA", data)
if (methodvalue["requestBody"] !== undefined) {
//console.log("Handle requestbody: ", methodvalue["requestBody"])
if (methodvalue["requestBody"]["content"] !== undefined) {
@@ -541,12 +543,22 @@ const AppCreator = (props) => {
tmpobject[prop] = `\$\{${prop}\}`
}
//console.log("Data: ", data)
for (var subkey in methodvalue["requestBody"]["content"]["application/json"]["schema"]["required"]) {
const tmpitem = methodvalue["requestBody"]["content"]["application/json"]["schema"]["required"][subkey]
tmpobject[tmpitem] = `\$\{${tmpitem}\}`
}
newaction["body"] = JSON.stringify(tmpobject, null, 2)
} else if (methodvalue["requestBody"]["content"]["application/json"]["schema"]["$ref"] !== undefined && methodvalue["requestBody"]["content"]["application/json"]["schema"]["$ref"] !== null) {
const retRef = handleGetRef(methodvalue["requestBody"]["content"]["application/json"]["schema"], data)
var newbody = {}
// Can handle default, required, description and type
for (var propkey in retRef.properties) {
const parsedkey = propkey.replaceAll(" ", "_").toLowerCase()
newbody[parsedkey] = "${"+parsedkey+"}"
}
newaction["body"] = JSON.stringify(newbody, null, 2)
}
}
} else if (methodvalue["requestBody"]["content"]["application/xml"] !== undefined) {
@@ -603,6 +615,117 @@ const AppCreator = (props) => {
}
}
}
} else {
var selectedReturn = ""
if (methodvalue.responses["200"] !== undefined) {
selectedReturn = "200"
} else if (methodvalue.responses["201"] !== undefined) {
selectedReturn = "201"
}
// Parsing examples. This should be standardized lol
if (methodvalue.responses[selectedReturn] !== undefined) {
const selectedExample = methodvalue.responses[selectedReturn]
if (selectedExample["content"] !== undefined) {
if (selectedExample["content"]["application/json"] !== undefined) {
if (selectedExample["content"]["application/json"]["schema"] !== undefined) {
if (selectedExample["content"]["application/json"]["schema"]["$ref"] !== undefined) {
//console.log("REF EXAMPLE: ", selectedExample["content"]["application/json"]["schema"])
const parameter = handleGetRef(selectedExample["content"]["application/json"]["schema"], data)
//console.log("GOT REF RETURN AS EXAMPLE: ", parameter)
if (parameter.properties !== undefined && parameter["type"] === "object") {
var newbody = {}
for (var propkey in parameter.properties) {
const parsedkey = propkey.replaceAll(" ", "_").toLowerCase()
if (parameter.properties[propkey].type === "string") {
if (parameter.properties[propkey].description !== undefined) {
newbody[parsedkey] = parameter.properties[propkey].description
} else {
newbody[parsedkey] = ""
}
} else if (parameter.properties[propkey].type.includes("int")) {
newbody[parsedkey] = 0
} else {
console.log("CANT HANDLE TYPE ", parameter.properties[propkey].type)
newbody[parsedkey] = []
}
}
newaction.example_response = JSON.stringify(newbody, null, 2)
} else {
console.log("CANT HANDLE PARAM: (1) ", parameter.properties)
}
} else {
// Just selecting the first one. bleh.
if (selectedExample["content"]["application/json"]["schema"]["allOf"] !== undefined) {
//console.log("ALLOF: ", selectedExample["content"]["application/json"]["schema"]["allOf"])
//console.log("BAD EXAMPLE: (SKIP ALLOF) ", selectedExample["content"]["application/json"]["schema"]["allOf"])
var selectedComponent = selectedExample["content"]["application/json"]["schema"]["allOf"]
if (selectedComponent.length >= 1) {
selectedComponent = selectedComponent[0]
const parameter = handleGetRef(selectedComponent, data)
if (parameter.properties !== undefined && parameter["type"] === "object") {
var newbody = {}
for (var propkey in parameter.properties) {
const parsedkey = propkey.replaceAll(" ", "_").toLowerCase()
if (parameter.properties[propkey].type === "string") {
if (parameter.properties[propkey].description !== undefined) {
newbody[parsedkey] = parameter.properties[propkey].description
} else {
newbody[parsedkey] = ""
}
} else if (parameter.properties[propkey].type.includes("int")) {
newbody[parsedkey] = 0
} else {
console.log("CANT HANDLE TYPE ", parameter.properties[propkey].type)
newbody[parsedkey] = []
}
}
newaction.example_response = JSON.stringify(newbody, null, 2)
//newaction.example_response = JSON.stringify(parameter.properties, null, 2)
} else {
//newaction.example_response = parameter.properties
console.log("CANT HANDLE PARAM: (3) ", parameter.properties)
}
} else {
}
} else if (selectedExample["content"]["application/json"]["schema"]["properties"] !== undefined) {
if (selectedExample["content"]["application/json"]["schema"]["properties"]["data"] !== undefined) {
const parameter = handleGetRef(selectedExample["content"]["application/json"]["schema"]["properties"]["data"], data)
if (parameter.properties !== undefined && parameter["type"] === "object") {
var newbody = {}
for (var propkey in parameter.properties) {
const parsedkey = propkey.replaceAll(" ", "_").toLowerCase()
if (parameter.properties[propkey].type === "string") {
if (parameter.properties[propkey].description !== undefined) {
newbody[parsedkey] = parameter.properties[propkey].description
} else {
newbody[parsedkey] = ""
}
} else if (parameter.properties[propkey].type.includes("int")) {
newbody[parsedkey] = 0
} else {
console.log("CANT HANDLE TYPE ", parameter.properties[propkey].type)
newbody[parsedkey] = []
}
}
newaction.example_response = JSON.stringify(newbody, null, 2)
//newaction.example_response = JSON.stringify(parameter.properties, null, 2)
} else {
//newaction.example_response = parameter.properties
console.log("CANT HANDLE PARAM: (2) ", parameter.properties)
}
}
}
}
}
}
}
}
}
}
@@ -727,7 +850,6 @@ const AppCreator = (props) => {
}
}
console.log("INVALID9: ", data)
if (securitySchemes !== undefined) {
for (const [key, value] of Object.entries(securitySchemes)) {
if (value.scheme === "bearer") {
+22 -6
View File
@@ -136,6 +136,7 @@ export const validateJson = (showResult) => {
const Workflows = (props) => {
const { globalUrl, isLoggedIn, isLoaded, removeCookie, cookies, userdata} = props;
document.title = "Shuffle - Workflows"
const referenceUrl = globalUrl+"/api/v1/hooks/"
const alert = useAlert()
const classes = useStyles();
@@ -544,9 +545,7 @@ const Workflows = (props) => {
}
}
const sanitizeWorkflow = (data) => {
data["owner"] = ""
console.log("Sanitize start: ", data)
const deduplicateIds = (data) => {
if (data.triggers !== null && data.triggers !== undefined) {
for (var key in data.triggers) {
const trigger = data.triggers[key]
@@ -561,6 +560,17 @@ const Workflows = (props) => {
}
const newId = uuid.v4()
if (trigger.trigger_type === "WEBHOOK") {
//"http://localhost:5002/api/v1/hooks/webhook_db179eb0-cb0c-4d2a-9c4b-53d2625a5008"
const hookname = "webhook_"+newId
if (trigger.parameters.length === 2) {
trigger.parameters[0].value = referenceUrl+"webhook_"+trigger.id
trigger.parameters[1].value = "webhook_"+trigger.id
} else {
alert.info("Something is wrong with the webhook in the copy")
}
}
for (var branchkey in data.branches) {
const branch = data.branches[branchkey]
if (branch.source_id === trigger.id) {
@@ -626,8 +636,13 @@ const Workflows = (props) => {
}
}
//console.log(data)
//return
return data
}
const sanitizeWorkflow = (data) => {
data["owner"] = ""
console.log("Sanitize start: ", data)
data = deduplicateIds(data)
data["org"] = []
data["org_id"] = ""
@@ -701,7 +716,8 @@ const Workflows = (props) => {
alert.success("Copying workflow "+data.name)
data.id = ""
data.name = data.name+"_copy"
console.log("COPIED DATA: ", data)
data = deduplicateIds(data)
//console.log("COPIED DATA: ", data)
//return
fetch(globalUrl+"/api/v1/workflows", {