Skip to content

Commit

Permalink
Release (#256)
Browse files Browse the repository at this point in the history
* fix: Jumping to wrong buffer (#261)
* fix: Go to last line and show warning when diagnostic is past the end of buffer (#262)
* fix: Get recent pipeline through other means (#266)
* feat: Add keymaps and linewise actions to layouts (#265)

This is a #MINOR release, because we are introducing new keybindings for the comment/note popups.

---------

Co-authored-by: Jakub F. Bortlík <[email protected]>
Co-authored-by: sunfuze <[email protected]>
  • Loading branch information
3 people authored Apr 15, 2024
1 parent 138b96b commit f906af0
Show file tree
Hide file tree
Showing 20 changed files with 356 additions and 101 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ require("gitlab").setup({
},
help = "g?", -- Opens a help popup for local keymaps when a relevant view is focused (popup, discussion panel, etc)
popup = { -- The popup for comment creation, editing, and replying
keymaps = {
next_field = "<Tab>", -- Cycle to the next field. Accepts count.
prev_field = "<S-Tab>", -- Cycle to the previous field. Accepts count.
},
perform_action = "<leader>s", -- Once in normal mode, does action (like saving comment or editing description, etc)
perform_linewise_action = "<leader>l", -- Once in normal mode, does the linewise action (see logs for this job, etc)
width = "40%",
Expand Down
2 changes: 1 addition & 1 deletion cmd/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (a *api) editComment(w http.ResponseWriter, r *http.Request) {
}

options := gitlab.UpdateMergeRequestDiscussionNoteOptions{}
options.Body = gitlab.String(editCommentRequest.Comment)
options.Body = gitlab.Ptr(editCommentRequest.Comment)

note, res, err := a.client.UpdateMergeRequestDiscussionNote(a.projectInfo.ProjectId, a.projectInfo.MergeId, editCommentRequest.DiscussionId, editCommentRequest.NoteId, &options)

Expand Down
24 changes: 20 additions & 4 deletions cmd/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

type GitProjectInfo struct {
RemoteUrl string
Namespace string
ProjectName string
BranchName string
RemoteUrl string
Namespace string
ProjectName string
BranchName string
GetLatestCommitOnRemote func(a *api) (string, error)
}

/*
Expand Down Expand Up @@ -108,3 +109,18 @@ func RefreshProjectInfo() error {

return nil
}

/*
The GetLatestCommitOnRemote function is attached during the createRouterAndApi call, since it needs to be called every time to get the latest commit.
*/
func GetLatestCommitOnRemote(a *api) (string, error) {
cmd := exec.Command("git", "log", "-1", "--format=%H", fmt.Sprintf("origin/%s", a.gitInfo.BranchName))

out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Failed to run `git log -1 --format=%%H " + fmt.Sprintf("origin/%s", a.gitInfo.BranchName))
}

commit := strings.TrimSpace(string(out))
return commit, nil
}
13 changes: 11 additions & 2 deletions cmd/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,17 @@ func TestExtractGitInfo_Success(t *testing.T) {
if err != nil {
t.Errorf("No error was expected, got %s", err)
}
if actual != tC.expected {
t.Errorf("\nExpected: %s\nActual: %s", tC.expected, actual)
if actual.RemoteUrl != tC.expected.RemoteUrl {
t.Errorf("\nExpected Remote URL: %s\nActual: %s", tC.expected.RemoteUrl, actual.RemoteUrl)
}
if actual.BranchName != tC.expected.BranchName {
t.Errorf("\nExpected Branch Name: %s\nActual: %s", tC.expected.BranchName, actual.BranchName)
}
if actual.ProjectName != tC.expected.ProjectName {
t.Errorf("\nExpected Project Name: %s\nActual: %s", tC.expected.ProjectName, actual.ProjectName)
}
if actual.Namespace != tC.expected.Namespace {
t.Errorf("\nExpected Namespace: %s\nActual: %s", tC.expected.Namespace, actual.Namespace)
}
})
}
Expand Down
45 changes: 36 additions & 9 deletions cmd/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
Expand All @@ -16,8 +17,8 @@ type RetriggerPipelineResponse struct {
}

type PipelineWithJobs struct {
Jobs []*gitlab.Job `json:"jobs"`
LatestPipeline *gitlab.Pipeline `json:"latest_pipeline"`
Jobs []*gitlab.Job `json:"jobs"`
LatestPipeline *gitlab.PipelineInfo `json:"latest_pipeline"`
}

type GetPipelineAndJobsResponse struct {
Expand All @@ -42,25 +43,51 @@ func (a *api) pipelineHandler(w http.ResponseWriter, r *http.Request) {
}
}

/* Gets the latest pipeline for a given commit, returns an error if there is no pipeline */
func (a *api) GetLastPipeline(commit string) (*gitlab.PipelineInfo, error) {

l := &gitlab.ListProjectPipelinesOptions{
SHA: gitlab.Ptr(commit),
Sort: gitlab.Ptr("desc"),
}

l.Page = 1
l.PerPage = 1

pipes, _, err := a.client.ListProjectPipelines(a.projectInfo.ProjectId, l)

if err != nil {
return nil, err
}

if len(pipes) == 0 {
return nil, errors.New("No pipeline running or available for commit " + commit)
}

return pipes[0], nil
}

/* Gets the latest pipeline and job information for the current branch */
func (a *api) GetPipelineAndJobs(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

pipeline, res, err := a.client.GetLatestPipeline(a.projectInfo.ProjectId, &gitlab.GetLatestPipelineOptions{
Ref: &a.gitInfo.BranchName,
})
commit, err := a.gitInfo.GetLatestCommitOnRemote(a)

if err != nil {
handleError(w, err, fmt.Sprintf("Gitlab failed to get latest pipeline for %s branch", a.gitInfo.BranchName), http.StatusInternalServerError)
fmt.Println(err)
handleError(w, err, "Error getting commit on remote branch", http.StatusInternalServerError)
return
}

if res.StatusCode >= 300 {
handleError(w, GenericError{endpoint: "/pipeline"}, fmt.Sprintf("Could not get latest pipeline for %s branch", a.gitInfo.BranchName), res.StatusCode)
pipeline, err := a.GetLastPipeline(commit)

if err != nil {
handleError(w, err, fmt.Sprintf("Gitlab failed to get latest pipeline for %s branch", a.gitInfo.BranchName), http.StatusInternalServerError)
return
}

if pipeline == nil {
handleError(w, GenericError{endpoint: "/pipeline"}, fmt.Sprintf("No pipeline found for %s branch", a.gitInfo.BranchName), res.StatusCode)
handleError(w, GenericError{endpoint: "/pipeline"}, fmt.Sprintf("No pipeline found for %s branch", a.gitInfo.BranchName), http.StatusInternalServerError)
return
}

Expand Down
56 changes: 33 additions & 23 deletions cmd/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,27 @@ func retryPipelineBuildNon200(pid interface{}, pipeline int, options ...gitlab.R
return nil, makeResponse(http.StatusSeeOther), nil
}

func getLatestPipeline200(pid interface{}, opts *gitlab.GetLatestPipelineOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
return &gitlab.Pipeline{ID: 1}, makeResponse(http.StatusOK), nil
func listProjectPipelines(pid interface{}, opt *gitlab.ListProjectPipelinesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.PipelineInfo, *gitlab.Response, error) {
return []*gitlab.PipelineInfo{
{ID: 12345},
}, makeResponse(http.StatusOK), nil
}

func withGitInfo(a *api) error {
a.gitInfo.GetLatestCommitOnRemote = func(a *api) (string, error) {
return "123abc", nil
}
a.gitInfo.BranchName = "some-feature"
return nil
}

func TestPipelineHandler(t *testing.T) {
t.Run("Gets all pipeline jobs", func(t *testing.T) {
request := makeRequest(t, http.MethodGet, "/pipeline", nil)
server, _ := createRouterAndApi(fakeClient{
listPipelineJobs: listPipelineJobs,
getLatestPipeline: getLatestPipeline200,
})
listPipelineJobs: listPipelineJobs,
listProjectPipelines: listProjectPipelines,
}, withGitInfo)
data := serveRequest(t, server, request, GetPipelineAndJobsResponse{})
assert(t, data.SuccessResponse.Message, "Pipeline retrieved")
assert(t, data.SuccessResponse.Status, http.StatusOK)
Expand All @@ -51,49 +61,49 @@ func TestPipelineHandler(t *testing.T) {
t.Run("Disallows non-GET, non-POST methods", func(t *testing.T) {
request := makeRequest(t, http.MethodPatch, "/pipeline", nil)
server, _ := createRouterAndApi(fakeClient{
listPipelineJobs: listPipelineJobs,
getLatestPipeline: getLatestPipeline200,
})
listPipelineJobs: listPipelineJobs,
listProjectPipelines: listProjectPipelines,
}, withGitInfo)
data := serveRequest(t, server, request, ErrorResponse{})
checkBadMethod(t, *data, http.MethodGet, http.MethodPost)
})

t.Run("Handles errors from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodGet, "/pipeline", nil)
server, _ := createRouterAndApi(fakeClient{
listPipelineJobs: listPipelineJobsErr,
getLatestPipeline: getLatestPipeline200,
})
listPipelineJobs: listPipelineJobsErr,
listProjectPipelines: listProjectPipelines,
}, withGitInfo)
data := serveRequest(t, server, request, ErrorResponse{})
checkErrorFromGitlab(t, *data, "Could not get pipeline jobs")
})

t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodGet, "/pipeline", nil)
server, _ := createRouterAndApi(fakeClient{
listPipelineJobs: listPipelineJobsNon200,
getLatestPipeline: getLatestPipeline200,
})
listPipelineJobs: listPipelineJobsNon200,
listProjectPipelines: listProjectPipelines,
}, withGitInfo)
data := serveRequest(t, server, request, ErrorResponse{})
checkNon200(t, *data, "Could not get pipeline jobs", "/pipeline")
})

t.Run("Handles errors from Gitlab client", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/pipeline/trigger/1", nil)
server, _ := createRouterAndApi(fakeClient{
retryPipelineBuild: retryPipelineBuildErr,
getLatestPipeline: getLatestPipeline200,
})
retryPipelineBuild: retryPipelineBuildErr,
listProjectPipelines: listProjectPipelines,
}, withGitInfo)
data := serveRequest(t, server, request, ErrorResponse{})
checkErrorFromGitlab(t, *data, "Could not retrigger pipeline")
})

t.Run("Retriggers pipeline", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/pipeline/trigger/1", nil)
server, _ := createRouterAndApi(fakeClient{
retryPipelineBuild: retryPipelineBuild,
getLatestPipeline: getLatestPipeline200,
})
retryPipelineBuild: retryPipelineBuild,
listProjectPipelines: listProjectPipelines,
}, withGitInfo)
data := serveRequest(t, server, request, GetPipelineAndJobsResponse{})
assert(t, data.SuccessResponse.Message, "Pipeline retriggered")
assert(t, data.SuccessResponse.Status, http.StatusOK)
Expand All @@ -102,9 +112,9 @@ func TestPipelineHandler(t *testing.T) {
t.Run("Handles non-200s from Gitlab client on retrigger", func(t *testing.T) {
request := makeRequest(t, http.MethodPost, "/pipeline/trigger/1", nil)
server, _ := createRouterAndApi(fakeClient{
retryPipelineBuild: retryPipelineBuildNon200,
getLatestPipeline: getLatestPipeline200,
})
retryPipelineBuild: retryPipelineBuildNon200,
listProjectPipelines: listProjectPipelines,
}, withGitInfo)
data := serveRequest(t, server, request, ErrorResponse{})
checkNon200(t, *data, "Could not retrigger pipeline", "/pipeline")
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/reply.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (a *api) replyHandler(w http.ResponseWriter, r *http.Request) {

now := time.Now()
options := gitlab.AddMergeRequestDiscussionNoteOptions{
Body: gitlab.String(replyRequest.Reply),
Body: gitlab.Ptr(replyRequest.Reply),
CreatedAt: &now,
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ func startServer(client *Client, projectInfo *ProjectInfo, gitInfo GitProjectInf
func(a *api) error {
err := attachEmojisToApi(a)
return err
},
func(a *api) error {
a.gitInfo.GetLatestCommitOnRemote = GetLatestCommitOnRemote
return nil
})

l := createListener()
server := &http.Server{Handler: m}

Expand Down Expand Up @@ -191,8 +194,8 @@ func (a *api) withMr(f func(w http.ResponseWriter, r *http.Request)) func(http.R
}

options := gitlab.ListProjectMergeRequestsOptions{
Scope: gitlab.String("all"),
State: gitlab.String("opened"),
Scope: gitlab.Ptr("all"),
State: gitlab.Ptr("opened"),
SourceBranch: &a.gitInfo.BranchName,
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type fakeClient struct {
listAllProjectMembers func(pid interface{}, opt *gitlab.ListProjectMembersOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectMember, *gitlab.Response, error)
retryPipelineBuild func(pid interface{}, pipeline int, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error)
listPipelineJobs func(pid interface{}, pipelineID int, opts *gitlab.ListJobsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Job, *gitlab.Response, error)
getLatestPipeline func(pid interface{}, opt *gitlab.GetLatestPipelineOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error)
listProjectPipelines func(pid interface{}, opt *gitlab.ListProjectPipelinesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.PipelineInfo, *gitlab.Response, error)
getTraceFile func(pid interface{}, jobID int, options ...gitlab.RequestOptionFunc) (*bytes.Reader, *gitlab.Response, error)
listLabels func(pid interface{}, opt *gitlab.ListLabelsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Label, *gitlab.Response, error)
listMergeRequestAwardEmojiOnNote func(pid interface{}, mergeRequestIID, noteID int, opt *gitlab.ListAwardEmojiOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.AwardEmoji, *gitlab.Response, error)
Expand Down Expand Up @@ -121,8 +121,8 @@ func (f fakeClient) ListPipelineJobs(pid interface{}, pipelineID int, opts *gitl
return f.listPipelineJobs(pid, pipelineID, opts, options...)
}

func (f fakeClient) GetLatestPipeline(pid interface{}, opts *gitlab.GetLatestPipelineOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error) {
return f.getLatestPipeline(pid, opts, options...)
func (f fakeClient) ListProjectPipelines(pid interface{}, opt *gitlab.ListProjectPipelinesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.PipelineInfo, *gitlab.Response, error) {
return f.listProjectPipelines(pid, opt, options...)
}

func (f fakeClient) GetTraceFile(pid interface{}, jobID int, options ...gitlab.RequestOptionFunc) (*bytes.Reader, *gitlab.Response, error) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type ClientInterface interface {
ListAllProjectMembers(pid interface{}, opt *gitlab.ListProjectMembersOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectMember, *gitlab.Response, error)
RetryPipelineBuild(pid interface{}, pipeline int, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error)
ListPipelineJobs(pid interface{}, pipelineID int, opts *gitlab.ListJobsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Job, *gitlab.Response, error)
GetLatestPipeline(pid interface{}, opt *gitlab.GetLatestPipelineOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Pipeline, *gitlab.Response, error)
ListProjectPipelines(pid interface{}, opt *gitlab.ListProjectPipelinesOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.PipelineInfo, *gitlab.Response, error)
GetTraceFile(pid interface{}, jobID int, options ...gitlab.RequestOptionFunc) (*bytes.Reader, *gitlab.Response, error)
ListLabels(pid interface{}, opt *gitlab.ListLabelsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Label, *gitlab.Response, error)
ListMergeRequestAwardEmojiOnNote(pid interface{}, mergeRequestIID int, noteID int, opt *gitlab.ListAwardEmojiOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.AwardEmoji, *gitlab.Response, error)
Expand Down
25 changes: 23 additions & 2 deletions doc/gitlab.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ you call this function with no values the defaults will be used:
},
help = "g?", -- Opens a help popup for local keymaps when a relevant view is focused (popup, discussion panel, etc)
popup = { -- The popup for comment creation, editing, and replying
keymaps = {
next_field = "<Tab>", -- Cycle to the next field. Accepts |count|.
prev_field = "<S-Tab>", -- Cycle to the previous field. Accepts |count|.
},
perform_action = "<leader>s", -- Once in normal mode, does action (like saving comment or editing description, etc)
perform_linewise_action = "<leader>l", -- Once in normal mode, does the linewise action (see logs for this job, etc)
width = "40%",
Expand Down Expand Up @@ -522,6 +526,7 @@ in normal mode):
vim.keymap.set("n", "glp", gitlab.pipeline)
vim.keymap.set("n", "glo", gitlab.open_in_browser)
vim.keymap.set("n", "glM", gitlab.merge)
vim.keymap.set("n", "glu", gitlab.copy_mr_url)
<

TROUBLESHOOTING *gitlab.nvim.troubleshooting*
Expand Down Expand Up @@ -633,7 +638,7 @@ After the comment is typed, submit it to Gitlab via the |settings.popup.perform_
keybinding, by default |<leader>l|

*gitlab.nvim.create_mr*
create_mr({opts}) ~
gitlab.create_mr({opts}) ~

Starts the process of creating an MR for the currently checked out branch.
>lua
Expand Down Expand Up @@ -662,6 +667,15 @@ Starts the process of creating an MR for the currently checked out branch.
{squash}: (bool) If true, the commits will be marked for
squashing.

After selecting all necessary details, you'll be presented with a confirmation
window. You can cycle through the individual fields with the keymaps defined
in `settings.popup.keymaps.next_field` and `settings.popup.keymaps.prev_field`.
Both keymaps accept a count, i.g., 2<Tab> goes to the 2nd next field.
In the "Delete source branch", "Squash commits", and "Target branch" fields,
you can use the `settings.popup.perform_linewise_action` keymap to either
toggle the Boolean value or to select a new target branch, respectively.
Use the `settings.popup.perform_action` keymap to POST the MR to Gitlab.

*gitlab.nvim.move_to_discussion_tree_from_diagnostic*
gitlab.move_to_discussion_tree_from_diagnostic() ~

Expand All @@ -681,7 +695,7 @@ tied to specific changes in an MR.
require("gitlab").create_note()

After the comment is typed, submit it to Gitlab via the `settings.popup.perform_action`
keybinding, by default |<leader>l|
keybinding, by default |<leader>s|.

*gitlab.nvim.toggle_discussions*
gitlab.toggle_discussions() ~
Expand Down Expand Up @@ -753,6 +767,13 @@ gitlab.open_in_browser() ~
Opens the current MR in your default web browser.
>lua
require("gitlab").open_in_browser()
<
*gitlab.nvim.copy_mr_url*
gitlab.copy_mr_url() ~

Copies the URL of the current MR to system clipboard.
>lua
require("gitlab").copy_mr_url()
<
*gitlab.nvim.merge*
gitlab.merge({opts}) ~
Expand Down
Loading

0 comments on commit f906af0

Please sign in to comment.