WIP Adding GET /project/<projectName> endpoint.
This commit is contained in:
		| @@ -98,14 +98,13 @@ proc raiseEx*(reason: string): void = | ||||
|   raise newException(Exception, reason) | ||||
|  | ||||
| # internal utils | ||||
| let nullNode = newJNull() | ||||
| 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] | ||||
|            else: nullNode | ||||
|            else: newJNull() | ||||
|  | ||||
| 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 & "'" | ||||
|   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 | ||||
|  | ||||
| import nre except toSeq | ||||
| @@ -93,6 +93,22 @@ proc setupProject(wksp: Workspace) = | ||||
|   wksp.version = versionResult.output.strip | ||||
|   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) = | ||||
|  | ||||
|   ## 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, | ||||
|   os, osproc, sequtils, strutils, tempfile, times, unittest | ||||
|   options, os, osproc, sequtils, strutils, tempfile, times, unittest | ||||
|  | ||||
| import ./configuration, ./core | ||||
|  | ||||
| @@ -234,8 +234,22 @@ proc start*(cfg: StrawBossConfig): void = | ||||
|  | ||||
|       checkAuth(); if not authed: return true | ||||
|  | ||||
|       # TODO | ||||
|       resp(Http501, makeJsonResp(Http501), JSON) | ||||
|       # Make sure we know about that project | ||||
|       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": | ||||
|       ## List all runs | ||||
| @@ -247,11 +261,7 @@ proc start*(cfg: StrawBossConfig): void = | ||||
|       try: project = cfg.findProject(@"projectName") | ||||
|       except: resp(Http404, makeJsonResp(Http404, getCurrentExceptionMsg()), JSON) | ||||
|  | ||||
|       let runsDir = cfg.artifactsRepo & "/" & project.name & "/runs" | ||||
|       if not existsDir(runsDir): resp("[]", JSON) | ||||
|  | ||||
|       let runPaths = toSeq(walkFiles(runsDir & "/*.json")) | ||||
|       let runRequests = runPaths.mapIt(parseRunRequest(parseFile(it))) | ||||
|       let runRequests = listRuns(cfg, project) | ||||
|  | ||||
|       resp($(%runRequests), JSON) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user