WIP Adding GET /project/<projectName> endpoint.
This commit is contained in:
		| @@ -98,14 +98,13 @@ proc raiseEx*(reason: string): void = | |||||||
|   raise newException(Exception, reason) |   raise newException(Exception, reason) | ||||||
|  |  | ||||||
| # internal utils | # internal utils | ||||||
| let nullNode = newJNull() |  | ||||||
| proc getIfExists(n: JsonNode, key: string): JsonNode = | proc getIfExists(n: JsonNode, key: string): JsonNode = | ||||||
|   # convenience method to get a key from a JObject or return null |   ## convenience method to get a key from a JObject or return null | ||||||
|   result = if n.hasKey(key): n[key] |   result = if n.hasKey(key): n[key] | ||||||
|            else: nullNode |            else: newJNull() | ||||||
|  |  | ||||||
| proc getOrFail(n: JsonNode, key: string, objName: string = ""): JsonNode = | proc getOrFail(n: JsonNode, key: string, objName: string = ""): JsonNode = | ||||||
|   # convenience method to get a key from a JObject or raise an exception |   ## convenience method to get a key from a JObject or raise an exception | ||||||
|   if not n.hasKey(key): raiseEx objName & " missing key '" & key & "'" |   if not n.hasKey(key): raiseEx objName & " missing key '" & key & "'" | ||||||
|   return n[key] |   return n[key] | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,4 +1,4 @@ | |||||||
| import cliutils, logging, json, os, osproc, sequtils, streams, | import cliutils, logging, json, options, os, osproc, sequtils, streams, | ||||||
|   strtabs, strutils, tables |   strtabs, strutils, tables | ||||||
|  |  | ||||||
| import nre except toSeq | import nre except toSeq | ||||||
| @@ -93,6 +93,22 @@ proc setupProject(wksp: Workspace) = | |||||||
|   wksp.version = versionResult.output.strip |   wksp.version = versionResult.output.strip | ||||||
|   wksp.env["VERSION"] = wksp.version |   wksp.env["VERSION"] = wksp.version | ||||||
|  |  | ||||||
|  | proc listRuns*(cfg: StrawBossConfig, project: ProjectDef): seq[RunRequest] = | ||||||
|  |   let runsDir = cfg.artifactsRepo & "/" & project.name & "/runs" | ||||||
|  |   if not existsDir(runsDir): return @[] | ||||||
|  |  | ||||||
|  |   let runPaths = toSeq(walkFiles(runsDir & "/*.json")) | ||||||
|  |   return runPaths.mapIt(parseRunRequest(parseFile(it))) | ||||||
|  |  | ||||||
|  | proc getCurrentProjectConfig*(cfg: StrawBossConfig, project: ProjectDef): Option[ProjectConfig] = | ||||||
|  |   let projCfgFile = cfg.artifactsRepo & "/" & project.name & "/" & project.cfgFilePath | ||||||
|  |   if not existsFile(projCfgFile): result = none(ProjectConfig) | ||||||
|  |   else: | ||||||
|  |     try: | ||||||
|  |       let projectConfig: ProjectConfig = loadProjectConfig(projCfgFile) #ProjectConfig(name: "test") | ||||||
|  |       result = some(projectConfig) | ||||||
|  |     except: result = none(ProjectConfig) | ||||||
|  |  | ||||||
| proc runStep*(wksp: Workspace, step: Step) = | proc runStep*(wksp: Workspace, step: Step) = | ||||||
|  |  | ||||||
|   ## Lower-level method to execute a given step within the context of a project |   ## Lower-level method to execute a given step within the context of a project | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import algorithm, asyncdispatch, bcrypt, cliutils, jester, json, jwt, logging, | import algorithm, asyncdispatch, bcrypt, cliutils, jester, json, jwt, logging, | ||||||
|   os, osproc, sequtils, strutils, tempfile, times, unittest |   options, os, osproc, sequtils, strutils, tempfile, times, unittest | ||||||
|  |  | ||||||
| import ./configuration, ./core | import ./configuration, ./core | ||||||
|  |  | ||||||
| @@ -234,8 +234,22 @@ proc start*(cfg: StrawBossConfig): void = | |||||||
|  |  | ||||||
|       checkAuth(); if not authed: return true |       checkAuth(); if not authed: return true | ||||||
|  |  | ||||||
|       # TODO |       # Make sure we know about that project | ||||||
|       resp(Http501, makeJsonResp(Http501), JSON) |       var projDef: ProjectDef | ||||||
|  |       try: projDef = cfg.findProject(@"projectName") | ||||||
|  |       except: resp(Http404, makeJsonResp(Http404, getCurrentExceptionMsg()), JSON) | ||||||
|  |  | ||||||
|  |       # Get the project configuration. | ||||||
|  |       let projConf = getCurrentProjectConfig(cfg, projDef) | ||||||
|  |  | ||||||
|  |       var respObj = newJObject() | ||||||
|  |       respObj["definition"] = %projDef | ||||||
|  |  | ||||||
|  |       #if projConf.isSome(): | ||||||
|  |       #  let pc: ProjectConfig = projConf.get() | ||||||
|  |       #  respObj["configuration"] = %pc | ||||||
|  |  | ||||||
|  |       resp($respObj, JSON) | ||||||
|  |  | ||||||
|     get "/project/@projectName/runs": |     get "/project/@projectName/runs": | ||||||
|       ## List all runs |       ## List all runs | ||||||
| @@ -247,11 +261,7 @@ proc start*(cfg: StrawBossConfig): void = | |||||||
|       try: project = cfg.findProject(@"projectName") |       try: project = cfg.findProject(@"projectName") | ||||||
|       except: resp(Http404, makeJsonResp(Http404, getCurrentExceptionMsg()), JSON) |       except: resp(Http404, makeJsonResp(Http404, getCurrentExceptionMsg()), JSON) | ||||||
|  |  | ||||||
|       let runsDir = cfg.artifactsRepo & "/" & project.name & "/runs" |       let runRequests = listRuns(cfg, project) | ||||||
|       if not existsDir(runsDir): resp("[]", JSON) |  | ||||||
|  |  | ||||||
|       let runPaths = toSeq(walkFiles(runsDir & "/*.json")) |  | ||||||
|       let runRequests = runPaths.mapIt(parseRunRequest(parseFile(it))) |  | ||||||
|  |  | ||||||
|       resp($(%runRequests), JSON) |       resp($(%runRequests), JSON) | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user