diff --git a/src/client/client.v b/src/client/client.v index 614b3dba..7446e91b 100644 --- a/src/client/client.v +++ b/src/client/client.v @@ -1,6 +1,7 @@ module client -import net.http +import net.http { Method } +import net.urllib import response { Response } import json @@ -17,24 +18,35 @@ pub fn new(address string, api_key string) Client { } } -// send_request is a convenience method for sending requests to the repos -// API. It mostly does string manipulation to create a query string containing -// the provided params. -fn (c &Client) send_request(method http.Method, url string, params map[string]string) ?Response { - mut full_url := '${c.address}$url' +// send_request just calls send_request_with_body with an empty body. +fn (c &Client) send_request(method Method, url string, params map[string]string) ?Response { + return c.send_request_with_body(method, url, params, '') +} + +// send_request_with_body is a convenience method for sending requests to +// the repos API. It mostly does string manipulation to create a query string +// containing the provided params. +fn (c &Client) send_request_with_body(method Method, url string, params map[string]string, body string) ?Response { + mut full_url := '$c.address$url' if params.len > 0 { - params_str := params.keys().map('$it=${params[it]}').join('&') + mut params_escaped := map[string]string{} + + // Escape each query param + for k, v in params { + params_escaped[k] = urllib.query_escape(v) + } + + params_str := params_escaped.keys().map('$it=${params[it]}').join('&') full_url = '$full_url?$params_str' } - mut req := http.new_request(method, full_url, '') ? - req.add_custom_header('X-API-Key', c.api_key) ? + mut req := http.new_request(method, full_url, body) ? + req.add_custom_header('X-Api-Key', c.api_key) ? res := req.do() ? data := json.decode(Response, res.text) ? return data } - diff --git a/src/client/git.v b/src/client/git.v index 5ed06200..7f4c27ad 100644 --- a/src/client/git.v +++ b/src/client/git.v @@ -1,19 +1,19 @@ module client -import db -import net.http +import db { GitRepo } +import net.http { Method } import response { Response } // get_repos returns the current list of repos. -pub fn (c &Client) get_git_repos() ?[]db.GitRepo { - data := c.send_request<[]db.GitRepo>(http.Method.get, '/api/repos', {}) ? +pub fn (c &Client) get_git_repos() ?[]GitRepo { + data := c.send_request<[]GitRepo>(Method.get, '/api/repos', {}) ? return data.data } // get_repo returns the repo for a specific ID. -pub fn (c &Client) get_git_repo(id int) ?db.GitRepo { - data := c.send_request(http.Method.get, '/api/repos/$id', {}) ? +pub fn (c &Client) get_git_repo(id int) ?GitRepo { + data := c.send_request(Method.get, '/api/repos/$id', {}) ? return data.data } @@ -30,14 +30,14 @@ pub fn (c &Client) add_git_repo(url string, branch string, repo string, arch []s params['arch'] = arch.join(',') } - data := c.send_request(http.Method.post, '/api/repos', params) ? + data := c.send_request(Method.post, '/api/repos', params) ? return data } // remove_repo removes the repo with the given ID from the server. pub fn (c &Client) remove_git_repo(id int) ?Response { - data := c.send_request(http.Method.delete, '/api/repos/$id', {}) ? + data := c.send_request(Method.delete, '/api/repos/$id', {}) ? return data } @@ -45,7 +45,7 @@ pub fn (c &Client) remove_git_repo(id int) ?Response { // patch_repo sends a PATCH request to the given repo with the params as // payload. pub fn (c &Client) patch_git_repo(id int, params map[string]string) ?Response { - data := c.send_request(http.Method.patch, '/api/repos/$id', params) ? + data := c.send_request(Method.patch, '/api/repos/$id', params) ? return data } diff --git a/src/client/logs.v b/src/client/logs.v new file mode 100644 index 00000000..9182f691 --- /dev/null +++ b/src/client/logs.v @@ -0,0 +1,42 @@ +module client + +import db { BuildLog } +import net.http { Method } +import response { Response } +import time + +pub fn (c &Client) get_build_logs() ?Response<[]BuildLog> { + data := c.send_request<[]BuildLog>(Method.get, '/api/logs', {}) ? + + return data +} + +pub fn (c &Client) get_build_logs_for_repo(repo_id int) ?Response<[]BuildLog> { + params := { + 'repo': repo_id.str() + } + + data := c.send_request<[]BuildLog>(Method.get, '/api/logs', params) ? + + return data +} + +pub fn (c &Client) get_build_log(id int) ?Response { + data := c.send_request(Method.get, '/api/logs/$id', {}) ? + + return data +} + +pub fn (c &Client) add_build_log(repo_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response { + params := { + 'repo': repo_id.str() + 'startTime': start_time.str() + 'endTime': end_time.str() + 'arch': arch + 'exitCode': exit_code.str() + } + + data := c.send_request_with_body(Method.post, '/api/logs', params, content) ? + + return data +} diff --git a/src/cron/daemon/daemon.v b/src/cron/daemon/daemon.v index 71fc5754..ade8fcbf 100644 --- a/src/cron/daemon/daemon.v +++ b/src/cron/daemon/daemon.v @@ -31,7 +31,7 @@ fn (r1 ScheduledBuild) < (r2 ScheduledBuild) bool { pub struct Daemon { mut: - client client.Client + client client.Client base_image string builder_images []string global_schedule CronExpression diff --git a/src/db/git.v b/src/db/git.v index 6eeecc8c..7aba2845 100644 --- a/src/db/git.v +++ b/src/db/git.v @@ -154,9 +154,7 @@ pub fn (db &VieterDb) update_git_repo_archs(repo_id int, archs []GitRepoArch) { } pub fn (db &VieterDb) git_repo_exists(repo_id int) bool { - db.get_git_repo(repo_id) or { - return false - } - + db.get_git_repo(repo_id) or { return false } + return true } diff --git a/src/db/logs.v b/src/db/logs.v index 589304e7..9ce28652 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -8,7 +8,7 @@ pub: repo_id int [nonull] start_time time.Time [nonull] end_time time.Time [nonull] - arch string [nonull] + arch string [nonull] exit_code int [nonull] } diff --git a/src/git/cli.v b/src/git/cli.v index bdc0479d..3bf78d10 100644 --- a/src/git/cli.v +++ b/src/git/cli.v @@ -160,7 +160,6 @@ fn patch(conf Config, id string, params map[string]string) ? { id_int := id.int() if id_int != 0 { - c := client.new(conf.address, conf.api_key) res := c.patch_git_repo(id_int, params) ? @@ -176,7 +175,7 @@ fn info(conf Config, id string) ? { return } - c := client.new(conf.address, conf.api_key) + c := client.new(conf.address, conf.api_key) repo := c.get_git_repo(id_int) ? println(repo) } diff --git a/src/server/logs.v b/src/server/logs.v index 10b734da..f53464dc 100644 --- a/src/server/logs.v +++ b/src/server/logs.v @@ -29,7 +29,7 @@ fn (mut app App) get_single_log(id int) web.Result { if !app.is_authorized() { return app.json(http.Status.unauthorized, new_response('Unauthorized.')) } - + log := app.db.get_build_log(id) or { return app.not_found() } return app.json(http.Status.ok, new_data_response(log)) @@ -43,7 +43,8 @@ fn (mut app App) get_log_contents(id int) web.Result { log := app.db.get_build_log(id) or { return app.not_found() } file_name := log.start_time.custom_format('YYYY-MM-DD_HH-mm-ss') - full_path := os.join_path(app.conf.data_dir, server.logs_dir_name, log.repo_id.str(), log.arch, file_name) + full_path := os.join_path(app.conf.data_dir, logs_dir_name, log.repo_id.str(), log.arch, + file_name) return app.file(full_path) }