* 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 <jakub.bortlik@proton.me>
Co-authored-by: sunfuze <sunfuze.1989@gmail.com>
This commit is contained in:
Harrison (Harry) Cramer
2024-04-15 09:56:21 -04:00
committed by GitHub
parent 138b96baea
commit f906af0c3a
20 changed files with 358 additions and 103 deletions

View File

@@ -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)

View File

@@ -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)
}
/*
@@ -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
}

View File

@@ -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)
}
})
}

View File

@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
@@ -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 {
@@ -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 {
fmt.Println(err)
handleError(w, err, "Error getting commit on remote branch", http.StatusInternalServerError)
return
}
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 res.StatusCode >= 300 {
handleError(w, GenericError{endpoint: "/pipeline"}, fmt.Sprintf("Could not get latest pipeline for %s branch", a.gitInfo.BranchName), res.StatusCode)
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
}

View File

@@ -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)
@@ -51,9 +61,9 @@ 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)
})
@@ -61,9 +71,9 @@ func TestPipelineHandler(t *testing.T) {
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")
})
@@ -71,9 +81,9 @@ func TestPipelineHandler(t *testing.T) {
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")
})
@@ -81,9 +91,9 @@ func TestPipelineHandler(t *testing.T) {
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")
})
@@ -91,9 +101,9 @@ func TestPipelineHandler(t *testing.T) {
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)
@@ -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")
})

View File

@@ -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,
}

View File

@@ -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}
@@ -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,
}

View File

@@ -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)
@@ -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) {

View File

@@ -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)