From 5f5f5d63c08e192f6a347aafc610a278ff3f3b12 Mon Sep 17 00:00:00 2001 From: Harrison Cramer Date: Sun, 6 Aug 2023 15:30:17 -0400 Subject: [PATCH] Bugfix: Fixed issue with JSON marshalling/unmarshalling --- cmd/info.go | 25 ++++++++++++++++++++++++- lua/gitlab/init.lua | 2 +- lua/gitlab/job.lua | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/cmd/info.go b/cmd/info.go index 0cbd5e4..a2f9af7 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -1,14 +1,22 @@ package main import ( + "encoding/json" "errors" "fmt" "io" "net/http" + + "github.com/xanzy/go-gitlab" ) const mrUrl = "%s/api/v4/projects/%s/merge_requests/%d" +type InfoResponse struct { + SuccessResponse + Info *gitlab.MergeRequest `json:"info"` +} + func (c *Client) Info() ([]byte, error) { url := fmt.Sprintf(mrUrl, c.gitlabInstance, c.projectId, c.mergeId) @@ -58,6 +66,21 @@ func InfoHandler(w http.ResponseWriter, r *http.Request) { return } + var mergeRequest *gitlab.MergeRequest + err = json.Unmarshal(msg, &mergeRequest) + if err != nil { + c.handleError(w, err, "Could not unmarshal data from merge requests", http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusOK) - w.Write(msg) + response := InfoResponse{ + SuccessResponse: SuccessResponse{ + Message: "Merge requests retrieved", + Status: http.StatusOK, + }, + Info: mergeRequest, + } + + json.NewEncoder(w).Encode(response) } diff --git a/lua/gitlab/init.lua b/lua/gitlab/init.lua index 6eb8ea0..b7ebb99 100644 --- a/lua/gitlab/init.lua +++ b/lua/gitlab/init.lua @@ -52,7 +52,7 @@ M.setup = function(args) return else job.run_job("info", "GET", nil, function(data) - state.INFO = data + state.INFO = data.info keymaps.set_keymap_keys(args.keymaps) keymaps.set_keymaps() end) diff --git a/lua/gitlab/job.lua b/lua/gitlab/job.lua index d7ee6dd..3135f4d 100644 --- a/lua/gitlab/job.lua +++ b/lua/gitlab/job.lua @@ -20,7 +20,7 @@ M.run_job = function(endpoint, method, body, callback) vim.defer_fn(function() local data_ok, data = pcall(vim.json.decode, output) if data_ok and data ~= nil then - local status = (data.status >= 200 and data.status < 300) and "success" or "error" + local status = (tonumber(data.status) >= 200 and tonumber(data.status) < 300) and "success" or "error" if status == "success" and callback ~= nil then callback(data) elseif status == "success" then