Implemented GET on /projects/<proj-id> and started unit tests.
This commit is contained in:
@ -79,7 +79,7 @@ proc getOrFail(n: JsonNode, key: string, objName: string = ""): JsonNode =
|
||||
return n[key]
|
||||
|
||||
# Configuration parsing code
|
||||
|
||||
|
||||
proc parseProjectDef*(pJson: JsonNode): ProjectDef =
|
||||
var envVars = newStringTable(modeCaseSensitive)
|
||||
for k, v in pJson.getIfExists("envVars").getFields: envVars[k] = v.getStr("")
|
||||
@ -186,13 +186,28 @@ proc `%`*(req: RunRequest): JsonNode =
|
||||
"workspaceDir": req.workspaceDir,
|
||||
"forceRebuild": req.forceRebuild }
|
||||
|
||||
proc `%`*(user: User): JsonNode =
|
||||
result = %* {
|
||||
"name": user.name,
|
||||
"hashedPwd": user.hashedPwd }
|
||||
|
||||
proc `%`*(cfg: StrawBossConfig): JsonNode =
|
||||
result = %* {
|
||||
"artifactsRepo": cfg.artifactsRepo,
|
||||
"authSecret": cfg.authSecret,
|
||||
"debug": cfg.debug,
|
||||
"projects": %cfg.projects,
|
||||
"pwdCost": cfg.pwdCost,
|
||||
"users": %cfg.users }
|
||||
|
||||
proc `$`*(s: BuildStatus): string = result = pretty(%s)
|
||||
proc `$`*(req: RunRequest): string = result = pretty(%req)
|
||||
proc `$`*(pd: ProjectDef): string = result = pretty(%pd)
|
||||
proc `$`*(cfg: StrawBossConfig): string = result = pretty(%cfg)
|
||||
|
||||
# TODO: maybe a macro for more general-purpose, shallow object comparison?
|
||||
#proc `==`*(a, b: ProjectDef): bool =
|
||||
|
||||
|
||||
template shallowEquals(a, b: RootObj): bool =
|
||||
if type(a) != type(b): return false
|
||||
var anyB = toAny(b)
|
||||
|
@ -140,7 +140,10 @@ proc runStep*(wksp: Workspace, step: Step) =
|
||||
let artifactPath = a.resolveEnvVars(wksp.env)
|
||||
let artifactName = artifactPath[(artifactPath.rfind("/")+1)..^1]
|
||||
try:
|
||||
wksp.outputHandler.sendMsg "copy " & wksp.dir & "/repo/" & step.workingDir & "/" & artifactPath & " -> " & wksp.artifactsDir & "/" & artifactName
|
||||
wksp.outputHandler.sendMsg "copy " & wksp.dir & "/repo/" &
|
||||
step.workingDir & "/" & artifactPath & " -> " &
|
||||
wksp.artifactsDir & "/" & artifactName
|
||||
|
||||
copyFile(wksp.dir & "/repo/" & step.workingDir & "/" & artifactPath,
|
||||
wksp.artifactsDir & "/" & artifactName)
|
||||
except:
|
||||
@ -205,6 +208,13 @@ proc runStep*(cfg: StrawBossConfig, req: RunRequest,
|
||||
"cloning project repo and preparing to run '" & req.stepName & "'")
|
||||
wksp.setupProject()
|
||||
|
||||
# Update our cache of project configurations by copying the configuration
|
||||
# file to our artifacts directory.
|
||||
copyFile(
|
||||
wksp.dir & "/repo/" & wksp.projectDef.cfgFilePath,
|
||||
cfg.artifactsRepo & "/" & wksp.project.name & "/configuration." &
|
||||
wksp.version & ".json")
|
||||
|
||||
# Find the requested step
|
||||
if not wksp.project.steps.hasKey(req.stepName):
|
||||
raiseEx "no step name '" & req.stepName & "' for " & req.projectName
|
||||
|
@ -1,5 +1,5 @@
|
||||
import asyncdispatch, bcrypt, jester, json, jwt, os, osproc, sequtils,
|
||||
strutils, tempfile, times, unittest
|
||||
import algorithm, asyncdispatch, bcrypt, jester, json, jwt, os, osproc,
|
||||
sequtils, strutils, tempfile, times, unittest
|
||||
|
||||
import logging
|
||||
import ./configuration, ./core, private/util
|
||||
@ -143,7 +143,60 @@ proc start*(cfg: StrawBossConfig): void =
|
||||
resp(Http200, $(%*{ "username": session.user.name }), JSON)
|
||||
|
||||
get "/projects": withSession:
|
||||
resp($(%(givenCfg.projects)), "application/json")
|
||||
# List project summaries (ProjectDefs only)
|
||||
resp($(%(cfg.projects)), JSON)
|
||||
|
||||
post "/projects": withSession:
|
||||
# Create a new project definition
|
||||
resp(Http501, makeJsonResp(Http501), JSON)
|
||||
|
||||
get "/project/@projectName/@version?": withSession:
|
||||
## Get a detailed project record including step definitions (ProjectConfig).
|
||||
|
||||
# Make sure we know about that project
|
||||
var project: ProjectDef
|
||||
try: project = cfg.findProject(@"projectName")
|
||||
except: resp(Http404, makeJsonResp(Http404, getCurrentExceptionMsg()), JSON)
|
||||
|
||||
# Given version
|
||||
|
||||
var cachedFilePath: string
|
||||
if @"version" != "":
|
||||
cachedFilePath = cfg.artifactsRepo & "/" & project.name &
|
||||
"/configuration." & @"version" & ".json"
|
||||
|
||||
if not existsFile(cachedFilePath):
|
||||
resp(Http404,
|
||||
makeJsonResp(Http404, "I have never built version " & @"version"),
|
||||
JSON)
|
||||
|
||||
# No version requested, use "latest"
|
||||
else:
|
||||
let confFilePaths = toSeq(walkFiles("configuration.*.json"))
|
||||
if confFilePaths.len == 0:
|
||||
resp(Http404, makeJsonResp(Http404, "I have not built any versions of " & project.name), JSON)
|
||||
let modTimes = confFilePaths.mapIt(it.getLastModificationTime)
|
||||
cachedFilePath = sorted(zip(confFilePaths, modTimes),
|
||||
proc (a, b: tuple): int = cmp(a.b, b.b))[0].a
|
||||
|
||||
try: resp(readFile(cachedFilePath), JSON)
|
||||
except:
|
||||
debug "Could not serve cached project configuration at: " &
|
||||
cachedFilePath & "\n\t Reason: " & getCurrentExceptionMsg()
|
||||
resp(Http500, makeJsonResp(Http500, "could not read cached project configuration"), JSON)
|
||||
|
||||
get "/api/project/@projectName/active": withSession:
|
||||
# List all currently active runs
|
||||
resp(Http501, makeJsonResp(Http501), JSON)
|
||||
|
||||
get "/api/project/@projectName/@stepName": withSession:
|
||||
|
||||
# Get step details including runs.
|
||||
resp(Http501, makeJsonResp(Http501), JSON)
|
||||
|
||||
get "/api/project/@projectName/@stepName/run/@buildRef": withSession:
|
||||
# Get detailed information about a run
|
||||
resp(Http501, makeJsonResp(Http501), JSON)
|
||||
|
||||
post "/project/@projectName/@stepName/run/@buildRef?":
|
||||
# Kick off a run
|
||||
@ -159,4 +212,12 @@ proc start*(cfg: StrawBossConfig): void =
|
||||
callSoon(proc(): void = complete(stopFuture))
|
||||
resp($(%*"shutting down"), JSON)
|
||||
|
||||
#[
|
||||
get re".*":
|
||||
resp(Http404, makeJsonResp(Http404), JSON)
|
||||
|
||||
post re".*":
|
||||
resp(Http404, makeJsonResp(Http404), JSON)
|
||||
]#
|
||||
|
||||
waitFor(stopFuture)
|
||||
|
Reference in New Issue
Block a user