Add/Show/Delete Emojis on Notes (#181)
This MR adds the ability to view, add, and delete emojis from notes and comments. This action can be performed by default with the `Ea` (emoji add) keybinding, and the `Ed` (emoji delete) keybinding. Only emojis added by the current user are eligible for deletion. The MR also implements a popup functionality which shows the user who added emojis on hover. Implements #179
This commit is contained in:
committed by
GitHub
parent
99741178f9
commit
baee20b279
@@ -136,6 +136,8 @@ require("gitlab").setup({
|
||||
delete_comment = "dd", -- Delete comment
|
||||
reply = "r", -- Reply to comment
|
||||
toggle_node = "t", -- Opens or closes the discussion
|
||||
add_emoji = "Ea" -- Add an emoji to the note/comment
|
||||
add_emoji = "Ed" -- Remove an emoji from a note/comment
|
||||
toggle_all_discussions = "T", -- Open or close separately both resolved and unresolved discussions
|
||||
toggle_resolved_discussions = "R", -- Open or close all resolved discussions
|
||||
toggle_unresolved_discussions = "U", -- Open or close all unresolved discussions
|
||||
|
||||
@@ -6,8 +6,8 @@ syntax match Username "@\S*"
|
||||
syntax match Date "\v\d+\s+\w+\s+ago"
|
||||
syntax match ChevronDown ""
|
||||
syntax match ChevronRight ""
|
||||
syntax match Resolved "✓$"
|
||||
syntax match Unresolved "-$"
|
||||
syntax match Resolved /\s✓\s\?/
|
||||
syntax match Unresolved /\s-\s\?/
|
||||
|
||||
highlight link Username GitlabUsername
|
||||
highlight link Date GitlabDate
|
||||
|
||||
@@ -33,6 +33,8 @@ type Client struct {
|
||||
*gitlab.JobsService
|
||||
*gitlab.PipelinesService
|
||||
*gitlab.LabelsService
|
||||
*gitlab.AwardEmojiService
|
||||
*gitlab.UsersService
|
||||
}
|
||||
|
||||
/* initGitlabClient parses and validates the project settings and initializes the Gitlab client. */
|
||||
@@ -89,6 +91,8 @@ func initGitlabClient() (error, *Client) {
|
||||
JobsService: client.Jobs,
|
||||
PipelinesService: client.Pipelines,
|
||||
LabelsService: client.Labels,
|
||||
AwardEmojiService: client.AwardEmoji,
|
||||
UsersService: client.Users,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ type CommentResponse struct {
|
||||
|
||||
/* commentHandler creates, edits, and deletes discussions (comments, multi-line comments) */
|
||||
func (a *api) commentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
a.postComment(w, r)
|
||||
@@ -63,7 +64,6 @@ func (a *api) commentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
case http.MethodDelete:
|
||||
a.deleteComment(w, r)
|
||||
default:
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Header().Set("Access-Control-Allow-Methods", fmt.Sprintf("%s, %s, %s", http.MethodDelete, http.MethodPost, http.MethodPatch))
|
||||
handleError(w, InvalidRequestError{}, "Expected DELETE, POST or PATCH", http.StatusMethodNotAllowed)
|
||||
}
|
||||
@@ -71,7 +71,6 @@ func (a *api) commentHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
/* deleteComment deletes a note, multiline comment, or comment, which are all considered discussion notes. */
|
||||
func (a *api) deleteComment(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
||||
@@ -113,8 +112,6 @@ func (a *api) deleteComment(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
/* postComment creates a note, multiline comment, or comment. */
|
||||
func (a *api) postComment(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
||||
@@ -208,7 +205,6 @@ func (a *api) postComment(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
/* editComment changes the text of a comment or changes it's resolved status. */
|
||||
func (a *api) editComment(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
body, err := io.ReadAll(r.Body)
|
||||
|
||||
if err != nil {
|
||||
|
||||
234
cmd/emoji.go
Normal file
234
cmd/emoji.go
Normal file
@@ -0,0 +1,234 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type Emoji struct {
|
||||
Unicode string `json:"unicode"`
|
||||
UnicodeAlternates []string `json:"unicode_alternates"`
|
||||
Name string `json:"name"`
|
||||
Shortname string `json:"shortname"`
|
||||
Category string `json:"category"`
|
||||
Aliases []string `json:"aliases"`
|
||||
AliasesASCII []string `json:"aliases_ascii"`
|
||||
Keywords []string `json:"keywords"`
|
||||
Moji string `json:"moji"`
|
||||
}
|
||||
|
||||
type EmojiMap map[string]Emoji
|
||||
|
||||
type CreateNoteEmojiPost struct {
|
||||
Emoji string `json:"emoji"`
|
||||
NoteId int `json:"note_id"`
|
||||
}
|
||||
|
||||
type CreateEmojiResponse struct {
|
||||
SuccessResponse
|
||||
Emoji *gitlab.AwardEmoji
|
||||
}
|
||||
|
||||
/*
|
||||
attachEmojisToApi reads the emojis from our external JSON file
|
||||
and attaches them to the API so that they can be looked up later
|
||||
*/
|
||||
func attachEmojisToApi(a *api) error {
|
||||
|
||||
e, err := os.Executable()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
binPath := path.Dir(e)
|
||||
filePath := fmt.Sprintf("%s/config/emojis.json", binPath)
|
||||
|
||||
reader, err := a.fileReader.ReadFile(filePath)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("Could not find emojis at %s", filePath)
|
||||
}
|
||||
|
||||
bytes, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return errors.New("Could not read emoji file")
|
||||
}
|
||||
|
||||
var emojiMap EmojiMap
|
||||
err = json.Unmarshal(bytes, &emojiMap)
|
||||
if err != nil {
|
||||
return errors.New("Could not unmarshal emojis")
|
||||
}
|
||||
|
||||
a.emojiMap = emojiMap
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
Fetches emojis for a set of notes and comments in parallel and returns a map of note IDs to their emojis.
|
||||
Gitlab's API does not allow for fetching notes for an entire discussion thread so we have to do it per-note.
|
||||
*/
|
||||
func (a *api) fetchEmojisForNotesAndComments(noteIDs []int) (map[int][]*gitlab.AwardEmoji, error) {
|
||||
var wg sync.WaitGroup
|
||||
|
||||
emojis := make(map[int][]*gitlab.AwardEmoji)
|
||||
mu := &sync.Mutex{}
|
||||
errs := make(chan error, len(noteIDs))
|
||||
emojiChan := make(chan struct {
|
||||
noteID int
|
||||
emojis []*gitlab.AwardEmoji
|
||||
}, len(noteIDs))
|
||||
|
||||
for _, noteID := range noteIDs {
|
||||
wg.Add(1)
|
||||
go func(noteID int) {
|
||||
defer wg.Done()
|
||||
emojis, _, err := a.client.ListMergeRequestAwardEmojiOnNote(a.projectInfo.ProjectId, a.projectInfo.MergeId, noteID, &gitlab.ListAwardEmojiOptions{})
|
||||
if err != nil {
|
||||
errs <- err
|
||||
return
|
||||
}
|
||||
emojiChan <- struct {
|
||||
noteID int
|
||||
emojis []*gitlab.AwardEmoji
|
||||
}{noteID, emojis}
|
||||
}(noteID)
|
||||
}
|
||||
|
||||
/* Close the channels when all goroutines finish */
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
close(emojiChan)
|
||||
}()
|
||||
|
||||
/* Collect emojis */
|
||||
for e := range emojiChan {
|
||||
mu.Lock()
|
||||
emojis[e.noteID] = e.emojis
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
/* Check if any errors occurred */
|
||||
if len(errs) > 0 {
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return emojis, nil
|
||||
}
|
||||
|
||||
func (a *api) emojiNoteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
a.postEmojiOnNote(w, r)
|
||||
case http.MethodDelete:
|
||||
a.deleteEmojiFromNote(w, r)
|
||||
default:
|
||||
w.Header().Set("Access-Control-Allow-Methods", fmt.Sprintf("%s, %s", http.MethodDelete, http.MethodPost))
|
||||
handleError(w, InvalidRequestError{}, "Expected DELETE or POST", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
/* deleteEmojiFromNote deletes an emoji from a note based on the emoji (awardable) ID and the note's ID */
|
||||
func (a *api) deleteEmojiFromNote(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
suffix := strings.TrimPrefix(r.URL.Path, "/mr/awardable/note/")
|
||||
ids := strings.Split(suffix, "/")
|
||||
|
||||
noteId, err := strconv.Atoi(ids[0])
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not convert note ID to integer", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
awardableId, err := strconv.Atoi(ids[1])
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not convert awardable ID to integer", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := a.client.DeleteMergeRequestAwardEmojiOnNote(a.projectInfo.ProjectId, a.projectInfo.MergeId, noteId, awardableId)
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not delete awardable", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, GenericError{endpoint: "/pipeline"}, "Could not delete awardable", res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := SuccessResponse{
|
||||
Message: "Emoji deleted",
|
||||
Status: http.StatusOK,
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
/* postEmojiOnNote adds an emojis to a note based on the note's ID */
|
||||
func (a *api) postEmojiOnNote(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
var emojiPost CreateNoteEmojiPost
|
||||
err = json.Unmarshal(body, &emojiPost)
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not unmarshal request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
awardEmoji, res, err := a.client.CreateMergeRequestAwardEmojiOnNote(a.projectInfo.ProjectId, a.projectInfo.MergeId, emojiPost.NoteId, &gitlab.CreateAwardEmojiOptions{
|
||||
Name: emojiPost.Emoji,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not post emoji", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, GenericError{endpoint: "/mr/awardable/note"}, "Could not post emoji", res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := CreateEmojiResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
Message: "Merge requests retrieved",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
Emoji: awardEmoji,
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,9 @@ type DiscussionsRequest struct {
|
||||
|
||||
type DiscussionsResponse struct {
|
||||
SuccessResponse
|
||||
Discussions []*gitlab.Discussion `json:"discussions"`
|
||||
UnlinkedDiscussions []*gitlab.Discussion `json:"unlinked_discussions"`
|
||||
Discussions []*gitlab.Discussion `json:"discussions"`
|
||||
UnlinkedDiscussions []*gitlab.Discussion `json:"unlinked_discussions"`
|
||||
Emojis map[int][]*gitlab.AwardEmoji `json:"emojis"`
|
||||
}
|
||||
|
||||
type SortableDiscussions []*gitlab.Discussion
|
||||
@@ -83,6 +84,7 @@ func (a *api) listDiscussionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
and system discussions, then return them sorted by created date */
|
||||
var unlinkedDiscussions []*gitlab.Discussion
|
||||
var linkedDiscussions []*gitlab.Discussion
|
||||
|
||||
for _, discussion := range discussions {
|
||||
if discussion.Notes == nil || len(discussion.Notes) == 0 || Contains(requestBody.Blacklist, discussion.Notes[0].Author.Username) > -1 {
|
||||
continue
|
||||
@@ -98,17 +100,26 @@ func (a *api) listDiscussionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Collect IDs in order to fetch emojis */
|
||||
var noteIds []int
|
||||
for _, discussion := range discussions {
|
||||
for _, note := range discussion.Notes {
|
||||
noteIds = append(noteIds, note.ID)
|
||||
}
|
||||
}
|
||||
|
||||
emojis, err := a.fetchEmojisForNotesAndComments(noteIds)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not fetch emojis", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
sortedLinkedDiscussions := SortableDiscussions(linkedDiscussions)
|
||||
sortedUnlinkedDiscussions := SortableDiscussions(unlinkedDiscussions)
|
||||
|
||||
sort.Sort(sortedLinkedDiscussions)
|
||||
sort.Sort(sortedUnlinkedDiscussions)
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not list discussions", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := DiscussionsResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
@@ -117,6 +128,7 @@ func (a *api) listDiscussionsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
},
|
||||
Discussions: linkedDiscussions,
|
||||
UnlinkedDiscussions: unlinkedDiscussions,
|
||||
Emojis: emojis,
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
|
||||
@@ -47,10 +47,18 @@ func listMergeRequestDiscussionsNon200(pid interface{}, mergeRequest int, opt *g
|
||||
return nil, makeResponse(http.StatusSeeOther), nil
|
||||
}
|
||||
|
||||
func listMergeRequestAwardEmojiOnNote(pid interface{}, mr int, noteID int, opt *gitlab.ListAwardEmojiOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
|
||||
return []*gitlab.AwardEmoji{}, makeResponse(http.StatusOK), nil
|
||||
}
|
||||
|
||||
func listMergeRequestAwardEmojiOnNoteFailure(pid interface{}, mr int, noteID int, opt *gitlab.ListAwardEmojiOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
|
||||
return nil, makeResponse(http.StatusBadRequest), errors.New("Some error from Gitlab")
|
||||
}
|
||||
|
||||
func TestListDiscussionsHandler(t *testing.T) {
|
||||
t.Run("Returns sorted discussions", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/mr/discussions/list", DiscussionsRequest{})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussions})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussions, listMergeRequestAwardEmojiOnNote: listMergeRequestAwardEmojiOnNote})
|
||||
data := serveRequest(t, server, request, DiscussionsResponse{})
|
||||
assert(t, data.SuccessResponse.Message, "Discussions retrieved")
|
||||
assert(t, data.SuccessResponse.Status, http.StatusOK)
|
||||
@@ -60,7 +68,7 @@ func TestListDiscussionsHandler(t *testing.T) {
|
||||
|
||||
t.Run("Uses blacklist to filter unwanted authors", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/mr/discussions/list", DiscussionsRequest{Blacklist: []string{"hcramer"}})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussions})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussions, listMergeRequestAwardEmojiOnNote: listMergeRequestAwardEmojiOnNote})
|
||||
data := serveRequest(t, server, request, DiscussionsResponse{})
|
||||
assert(t, data.SuccessResponse.Message, "Discussions retrieved")
|
||||
assert(t, data.SuccessResponse.Status, http.StatusOK)
|
||||
@@ -70,22 +78,29 @@ func TestListDiscussionsHandler(t *testing.T) {
|
||||
|
||||
t.Run("Disallows non-POST method", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPatch, "/mr/discussions/list", DiscussionsRequest{})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussions})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussions, listMergeRequestAwardEmojiOnNote: listMergeRequestAwardEmojiOnNote})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkBadMethod(t, *data, http.MethodPost)
|
||||
})
|
||||
|
||||
t.Run("Handles errors from Gitlab client", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/mr/discussions/list", DiscussionsRequest{})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussionsErr})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussionsErr, listMergeRequestAwardEmojiOnNote: listMergeRequestAwardEmojiOnNote})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkErrorFromGitlab(t, *data, "Could not list discussions")
|
||||
})
|
||||
|
||||
t.Run("Handles non-200s from Gitlab client", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/mr/discussions/list", DiscussionsRequest{})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussionsNon200})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussionsNon200, listMergeRequestAwardEmojiOnNote: listMergeRequestAwardEmojiOnNote})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkNon200(t, *data, "Could not list discussions", "/mr/discussions/list")
|
||||
})
|
||||
|
||||
t.Run("Handles error from emoji service", func(t *testing.T) {
|
||||
request := makeRequest(t, http.MethodPost, "/mr/discussions/list", DiscussionsRequest{})
|
||||
server, _ := createRouterAndApi(fakeClient{listMergeRequestDiscussions: listMergeRequestDiscussions, listMergeRequestAwardEmojiOnNote: listMergeRequestAwardEmojiOnNoteFailure})
|
||||
data := serveRequest(t, server, request, ErrorResponse{})
|
||||
checkErrorFromGitlab(t, *data, "Could not fetch emojis")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -31,6 +31,10 @@ func startServer(client *Client, projectInfo *ProjectInfo, gitInfo GitProjectInf
|
||||
func(a *api) error {
|
||||
a.gitInfo = &gitInfo
|
||||
return nil
|
||||
},
|
||||
func(a *api) error {
|
||||
err := attachEmojisToApi(a)
|
||||
return err
|
||||
})
|
||||
|
||||
l := createListener()
|
||||
@@ -82,6 +86,7 @@ type api struct {
|
||||
projectInfo *ProjectInfo
|
||||
gitInfo *GitProjectInfo
|
||||
fileReader FileReader
|
||||
emojiMap EmojiMap
|
||||
sigCh chan os.Signal
|
||||
}
|
||||
|
||||
@@ -100,6 +105,7 @@ func createRouterAndApi(client ClientInterface, optFuncs ...optFunc) (*http.Serv
|
||||
projectInfo: &ProjectInfo{},
|
||||
gitInfo: &GitProjectInfo{},
|
||||
fileReader: nil,
|
||||
emojiMap: EmojiMap{},
|
||||
sigCh: make(chan os.Signal, 1),
|
||||
}
|
||||
|
||||
@@ -124,7 +130,9 @@ func createRouterAndApi(client ClientInterface, optFuncs ...optFunc) (*http.Serv
|
||||
m.HandleFunc("/mr/reply", a.withMr(a.replyHandler))
|
||||
m.HandleFunc("/mr/label", a.withMr(a.labelHandler))
|
||||
m.HandleFunc("/mr/revoke", a.withMr(a.revokeHandler))
|
||||
m.HandleFunc("/mr/awardable/note/", a.withMr(a.emojiNoteHandler))
|
||||
|
||||
m.HandleFunc("/users/me", a.meHandler)
|
||||
m.HandleFunc("/attachment", a.attachmentHandler)
|
||||
m.HandleFunc("/create_mr", a.createMr)
|
||||
m.HandleFunc("/job", a.jobHandler)
|
||||
|
||||
105
cmd/test.go
105
cmd/test.go
@@ -18,25 +18,28 @@ The FakeHandlerClient is used to create a fake gitlab client for testing our han
|
||||
*/
|
||||
|
||||
type fakeClient struct {
|
||||
createMrFn func(pid interface{}, opt *gitlab.CreateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
getMergeRequestFn func(pid interface{}, mergeRequest int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
updateMergeRequestFn func(pid interface{}, mergeRequest int, opt *gitlab.UpdateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
acceptAndMergeFn func(pid interface{}, mergeRequest int, opt *gitlab.AcceptMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
unapprorveMergeRequestFn func(pid interface{}, mr int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
uploadFile func(pid interface{}, content io.Reader, filename string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectFile, *gitlab.Response, error)
|
||||
getMergeRequestDiffVersions func(pid interface{}, mergeRequest int, opt *gitlab.GetMergeRequestDiffVersionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequestDiffVersion, *gitlab.Response, error)
|
||||
approveMergeRequest func(pid interface{}, mr int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error)
|
||||
listMergeRequestDiscussions func(pid interface{}, mergeRequest int, opt *gitlab.ListMergeRequestDiscussionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Discussion, *gitlab.Response, error)
|
||||
resolveMergeRequestDiscussion func(pid interface{}, mergeRequest int, discussion string, opt *gitlab.ResolveMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
createMergeRequestDiscussion func(pid interface{}, mergeRequest int, opt *gitlab.CreateMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
updateMergeRequestDiscussionNote func(pid interface{}, mergeRequest int, discussion string, note int, opt *gitlab.UpdateMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
deleteMergeRequestDiscussionNote func(pid interface{}, mergeRequest int, discussion string, note int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
addMergeRequestDiscussionNote func(pid interface{}, mergeRequest int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
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)
|
||||
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)
|
||||
createMrFn func(pid interface{}, opt *gitlab.CreateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
getMergeRequestFn func(pid interface{}, mergeRequestIID int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
updateMergeRequestFn func(pid interface{}, mergeRequestIID int, opt *gitlab.UpdateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
acceptAndMergeFn func(pid interface{}, mergeRequestIID int, opt *gitlab.AcceptMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
unapprorveMergeRequestFn func(pid interface{}, mergeRequestIID int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
uploadFile func(pid interface{}, content io.Reader, filename string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectFile, *gitlab.Response, error)
|
||||
getMergeRequestDiffVersions func(pid interface{}, mergeRequestIID int, opt *gitlab.GetMergeRequestDiffVersionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequestDiffVersion, *gitlab.Response, error)
|
||||
approveMergeRequest func(pid interface{}, mergeRequestIID int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error)
|
||||
listMergeRequestDiscussions func(pid interface{}, mergeRequestIID int, opt *gitlab.ListMergeRequestDiscussionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Discussion, *gitlab.Response, error)
|
||||
resolveMergeRequestDiscussion func(pid interface{}, mergeRequestIID int, discussion string, opt *gitlab.ResolveMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
createMergeRequestDiscussion func(pid interface{}, mergeRequestIID int, opt *gitlab.CreateMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
updateMergeRequestDiscussionNote func(pid interface{}, mergeRequestIID int, discussion string, note int, opt *gitlab.UpdateMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
deleteMergeRequestDiscussionNote func(pid interface{}, mergeRequestIID int, discussion string, note int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
addMergeRequestDiscussionNote func(pid interface{}, mergeRequestIID int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
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)
|
||||
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)
|
||||
deleteMergeRequestAwardEmojiOnNote func(pid interface{}, mergeRequestIID, noteID, awardID int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
currentUser func(options ...gitlab.RequestOptionFunc) (*gitlab.User, *gitlab.Response, error)
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
@@ -53,56 +56,56 @@ func (f fakeClient) CreateMergeRequest(pid interface{}, opt *gitlab.CreateMergeR
|
||||
return f.createMrFn(pid, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) AcceptMergeRequest(pid interface{}, mergeRequest int, opt *gitlab.AcceptMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return f.acceptAndMergeFn(pid, mergeRequest, opt, options...)
|
||||
func (f fakeClient) AcceptMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.AcceptMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return f.acceptAndMergeFn(pid, mergeRequestIID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) GetMergeRequest(pid interface{}, mergeRequest int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return f.getMergeRequestFn(pid, mergeRequest, opt, options...)
|
||||
func (f fakeClient) GetMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return f.getMergeRequestFn(pid, mergeRequestIID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) UpdateMergeRequest(pid interface{}, mergeRequest int, opt *gitlab.UpdateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return f.updateMergeRequestFn(pid, mergeRequest, opt, options...)
|
||||
func (f fakeClient) UpdateMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.UpdateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return f.updateMergeRequestFn(pid, mergeRequestIID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) UnapproveMergeRequest(pid interface{}, mr int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
|
||||
return f.unapprorveMergeRequestFn(pid, mr, options...)
|
||||
func (f fakeClient) UnapproveMergeRequest(pid interface{}, mergeRequestIID int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
|
||||
return f.unapprorveMergeRequestFn(pid, mergeRequestIID, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) UploadFile(pid interface{}, content io.Reader, filename string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectFile, *gitlab.Response, error) {
|
||||
return f.uploadFile(pid, content, filename, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) GetMergeRequestDiffVersions(pid interface{}, mergeRequest int, opt *gitlab.GetMergeRequestDiffVersionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequestDiffVersion, *gitlab.Response, error) {
|
||||
return f.getMergeRequestDiffVersions(pid, mergeRequest, opt, options...)
|
||||
func (f fakeClient) GetMergeRequestDiffVersions(pid interface{}, mergeRequestIID int, opt *gitlab.GetMergeRequestDiffVersionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequestDiffVersion, *gitlab.Response, error) {
|
||||
return f.getMergeRequestDiffVersions(pid, mergeRequestIID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) ApproveMergeRequest(pid interface{}, mr int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error) {
|
||||
return f.approveMergeRequest(pid, mr, opt, options...)
|
||||
func (f fakeClient) ApproveMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error) {
|
||||
return f.approveMergeRequest(pid, mergeRequestIID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) ListMergeRequestDiscussions(pid interface{}, mergeRequest int, opt *gitlab.ListMergeRequestDiscussionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Discussion, *gitlab.Response, error) {
|
||||
return f.listMergeRequestDiscussions(pid, mergeRequest, opt, options...)
|
||||
func (f fakeClient) ListMergeRequestDiscussions(pid interface{}, mergeRequestIID int, opt *gitlab.ListMergeRequestDiscussionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Discussion, *gitlab.Response, error) {
|
||||
return f.listMergeRequestDiscussions(pid, mergeRequestIID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) ResolveMergeRequestDiscussion(pid interface{}, mergeRequest int, discussion string, opt *gitlab.ResolveMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
|
||||
return f.resolveMergeRequestDiscussion(pid, mergeRequest, discussion, opt, options...)
|
||||
func (f fakeClient) ResolveMergeRequestDiscussion(pid interface{}, mergeRequestIID int, discussion string, opt *gitlab.ResolveMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
|
||||
return f.resolveMergeRequestDiscussion(pid, mergeRequestIID, discussion, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) CreateMergeRequestDiscussion(pid interface{}, mergeRequest int, opt *gitlab.CreateMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
|
||||
return f.createMergeRequestDiscussion(pid, mergeRequest, opt, options...)
|
||||
func (f fakeClient) CreateMergeRequestDiscussion(pid interface{}, mergeRequestIID int, opt *gitlab.CreateMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error) {
|
||||
return f.createMergeRequestDiscussion(pid, mergeRequestIID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) UpdateMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, note int, opt *gitlab.UpdateMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
|
||||
return f.updateMergeRequestDiscussionNote(pid, mergeRequest, discussion, note, opt, options...)
|
||||
func (f fakeClient) UpdateMergeRequestDiscussionNote(pid interface{}, mergeRequestIID int, discussion string, note int, opt *gitlab.UpdateMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
|
||||
return f.updateMergeRequestDiscussionNote(pid, mergeRequestIID, discussion, note, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) DeleteMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, note int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
|
||||
return f.deleteMergeRequestDiscussionNote(pid, mergeRequest, discussion, note, options...)
|
||||
func (f fakeClient) DeleteMergeRequestDiscussionNote(pid interface{}, mergeRequestIID int, discussion string, note int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
|
||||
return f.deleteMergeRequestDiscussionNote(pid, mergeRequestIID, discussion, note, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) AddMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
|
||||
return f.addMergeRequestDiscussionNote(pid, mergeRequest, discussion, opt, options...)
|
||||
func (f fakeClient) AddMergeRequestDiscussionNote(pid interface{}, mergeRequestIID int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error) {
|
||||
return f.addMergeRequestDiscussionNote(pid, mergeRequestIID, discussion, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) ListAllProjectMembers(pid interface{}, opt *gitlab.ListProjectMembersOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.ProjectMember, *gitlab.Response, error) {
|
||||
@@ -125,11 +128,27 @@ func (f fakeClient) ListLabels(pid interface{}, opt *gitlab.ListLabelsOptions, o
|
||||
return f.listLabels(pid, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) ListMergeRequestAwardEmojiOnNote(pid interface{}, mergeRequestIID int, noteID int, opt *gitlab.ListAwardEmojiOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.AwardEmoji, *gitlab.Response, error) {
|
||||
return f.listMergeRequestAwardEmojiOnNote(pid, mergeRequestIID, noteID, opt, options...)
|
||||
}
|
||||
|
||||
func (f fakeClient) DeleteMergeRequestAwardEmojiOnNote(pid interface{}, mergeRequestIID, noteID, awardID int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error) {
|
||||
return f.deleteMergeRequestAwardEmojiOnNote(pid, mergeRequestIID, noteID, awardID)
|
||||
}
|
||||
|
||||
func (f fakeClient) CurrentUser(options ...gitlab.RequestOptionFunc) (*gitlab.User, *gitlab.Response, error) {
|
||||
return f.currentUser()
|
||||
}
|
||||
|
||||
/* This middleware function needs to return an ID for the rest of the handlers */
|
||||
func (f fakeClient) ListProjectMergeRequests(pid interface{}, opt *gitlab.ListProjectMergeRequestsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error) {
|
||||
return []*gitlab.MergeRequest{{ID: 1}}, &gitlab.Response{}, nil
|
||||
}
|
||||
|
||||
func (f fakeClient) CreateMergeRequestAwardEmojiOnNote(pid interface{}, mergeRequestIID, noteID int, opt *gitlab.CreateAwardEmojiOptions, options ...gitlab.RequestOptionFunc) (*gitlab.AwardEmoji, *gitlab.Response, error) {
|
||||
return &gitlab.AwardEmoji{}, &gitlab.Response{}, nil
|
||||
}
|
||||
|
||||
/* The assert function is a helper function used to check two comparables */
|
||||
func assert[T comparable](t *testing.T, got T, want T) {
|
||||
t.Helper()
|
||||
|
||||
28
cmd/types.go
28
cmd/types.go
@@ -37,22 +37,26 @@ func (e InvalidRequestError) Error() string {
|
||||
type ClientInterface interface {
|
||||
CreateMergeRequest(pid interface{}, opt *gitlab.CreateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
ListProjectMergeRequests(pid interface{}, opt *gitlab.ListProjectMergeRequestsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
GetMergeRequest(pid interface{}, mr int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
AcceptMergeRequest(pid interface{}, mergeRequest int, opt *gitlab.AcceptMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
UpdateMergeRequest(pid interface{}, mr int, opt *gitlab.UpdateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
GetMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.GetMergeRequestsOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
AcceptMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.AcceptMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
UpdateMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.UpdateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
|
||||
UploadFile(pid interface{}, content io.Reader, filename string, options ...gitlab.RequestOptionFunc) (*gitlab.ProjectFile, *gitlab.Response, error)
|
||||
GetMergeRequestDiffVersions(pid interface{}, mr int, opt *gitlab.GetMergeRequestDiffVersionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequestDiffVersion, *gitlab.Response, error)
|
||||
ApproveMergeRequest(pid interface{}, mr int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error)
|
||||
UnapproveMergeRequest(pid interface{}, mr int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
ListMergeRequestDiscussions(pid interface{}, mergeRequest int, opt *gitlab.ListMergeRequestDiscussionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Discussion, *gitlab.Response, error)
|
||||
ResolveMergeRequestDiscussion(pid interface{}, mergeRequest int, discussion string, opt *gitlab.ResolveMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
CreateMergeRequestDiscussion(pid interface{}, mergeRequest int, opt *gitlab.CreateMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
UpdateMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, note int, opt *gitlab.UpdateMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
DeleteMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, note int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
AddMergeRequestDiscussionNote(pid interface{}, mergeRequest int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
GetMergeRequestDiffVersions(pid interface{}, mergeRequestIID int, opt *gitlab.GetMergeRequestDiffVersionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.MergeRequestDiffVersion, *gitlab.Response, error)
|
||||
ApproveMergeRequest(pid interface{}, mergeRequestIID int, opt *gitlab.ApproveMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequestApprovals, *gitlab.Response, error)
|
||||
UnapproveMergeRequest(pid interface{}, mergeRequestIID int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
ListMergeRequestDiscussions(pid interface{}, mergeRequestIID int, opt *gitlab.ListMergeRequestDiscussionsOptions, options ...gitlab.RequestOptionFunc) ([]*gitlab.Discussion, *gitlab.Response, error)
|
||||
ResolveMergeRequestDiscussion(pid interface{}, mergeRequestIID int, discussion string, opt *gitlab.ResolveMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
CreateMergeRequestDiscussion(pid interface{}, mergeRequestIID int, opt *gitlab.CreateMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
UpdateMergeRequestDiscussionNote(pid interface{}, mergeRequestIID int, discussion string, note int, opt *gitlab.UpdateMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
DeleteMergeRequestDiscussionNote(pid interface{}, mergeRequestIID int, discussion string, note int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
AddMergeRequestDiscussionNote(pid interface{}, mergeRequestIID int, discussion string, opt *gitlab.AddMergeRequestDiscussionNoteOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Note, *gitlab.Response, error)
|
||||
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)
|
||||
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)
|
||||
CreateMergeRequestAwardEmojiOnNote(pid interface{}, mergeRequestIID int, noteID int, opt *gitlab.CreateAwardEmojiOptions, options ...gitlab.RequestOptionFunc) (*gitlab.AwardEmoji, *gitlab.Response, error)
|
||||
DeleteMergeRequestAwardEmojiOnNote(pid interface{}, mergeRequestIID, noteID, awardID int, options ...gitlab.RequestOptionFunc) (*gitlab.Response, error)
|
||||
CurrentUser(options ...gitlab.RequestOptionFunc) (*gitlab.User, *gitlab.Response, error)
|
||||
}
|
||||
|
||||
47
cmd/user.go
Normal file
47
cmd/user.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type UserResponse struct {
|
||||
SuccessResponse
|
||||
User *gitlab.User `json:"user"`
|
||||
}
|
||||
|
||||
func (a *api) meHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Access-Control-Allow-Methods", http.MethodGet)
|
||||
handleError(w, InvalidRequestError{}, "Expected GET", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
user, res, err := a.client.CurrentUser()
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Failed to get current user", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, err, "User API returned non-200 status", res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
response := UserResponse{
|
||||
SuccessResponse: SuccessResponse{
|
||||
Message: "User fetched successfully",
|
||||
Status: http.StatusOK,
|
||||
},
|
||||
User: user,
|
||||
}
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
10878
config/emojis.json
Normal file
10878
config/emojis.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -14,6 +14,7 @@ Table of Contents *gitlab.nvim.table-of-contents*
|
||||
- Discussions and Notes |gitlab.nvim.discussions-and-notes|
|
||||
- Labels |gitlab.nvim.labels|
|
||||
- Signs and diagnostics |gitlab.nvim.signs-and-diagnostics|
|
||||
- Emojis gitlab.nvim.emojis
|
||||
- Uploading Files |gitlab.nvim.uploading-files|
|
||||
- MR Approvals |gitlab.nvim.mr-approvals|
|
||||
- Merging an MR |gitlab.nvim.merging-an-mr|
|
||||
@@ -425,6 +426,14 @@ of diagnostic, where the `discussion_sign.text` is shown, otherwise
|
||||
`vim.diagnostic.show` and `move_to_discussion_tree_from_diagnostic` will not
|
||||
work.
|
||||
|
||||
EMOJIS *gitlab.nvim.emojis*
|
||||
|
||||
You can add or remove emojis from a note or comment in the discussion tree.
|
||||
|
||||
To see who has reacted with an emoji, hover over the emoji. A popup will
|
||||
appear with anyone who has responded with that emoji. You can only delete
|
||||
emojis that you have responded with.
|
||||
|
||||
|
||||
UPLOADING FILES *gitlab.nvim.uploading-files*
|
||||
|
||||
|
||||
@@ -66,6 +66,18 @@
|
||||
---@field discussions Discussion[]
|
||||
---@field unlinked_discussions UnlinkedDiscussion[]
|
||||
|
||||
---@class EmojiMap: table<string, Emoji>
|
||||
---@class Emoji
|
||||
---@field unicode string
|
||||
---@field unicodeAlternates string[]
|
||||
---@field name string
|
||||
---@field shortname string
|
||||
---@field category string
|
||||
---@field aliases string[]
|
||||
---@field aliasesASCII string[]
|
||||
---@field keywords string[]
|
||||
---@field moji string
|
||||
|
||||
---@class WinbarTable
|
||||
---@field name string
|
||||
---@field resolvable_discussions number
|
||||
|
||||
@@ -14,6 +14,7 @@ local discussions_tree = require("gitlab.actions.discussions.tree")
|
||||
local signs = require("gitlab.actions.discussions.signs")
|
||||
local winbar = require("gitlab.actions.discussions.winbar")
|
||||
local help = require("gitlab.actions.help")
|
||||
local emoji = require("gitlab.emoji")
|
||||
|
||||
local M = {
|
||||
split_visible = false,
|
||||
@@ -24,6 +25,8 @@ local M = {
|
||||
discussions = {},
|
||||
---@type UnlinkedDiscussion[]
|
||||
unlinked_discussions = {},
|
||||
---@type EmojiMap
|
||||
emojis = {},
|
||||
---@type number
|
||||
linked_bufnr = nil,
|
||||
---@type number
|
||||
@@ -40,6 +43,7 @@ M.load_discussions = function(callback)
|
||||
job.run_job("/mr/discussions/list", "POST", { blacklist = state.settings.discussion_tree.blacklist }, function(data)
|
||||
M.discussions = data.discussions ~= vim.NIL and data.discussions or {}
|
||||
M.unlinked_discussions = data.unlinked_discussions ~= vim.NIL and data.unlinked_discussions or {}
|
||||
M.emojis = data.emojis or {}
|
||||
if type(callback) == "function" then
|
||||
callback(data)
|
||||
end
|
||||
@@ -100,7 +104,7 @@ M.toggle = function(callback)
|
||||
|
||||
M.load_discussions(function()
|
||||
if type(M.discussions) ~= "table" and type(M.unlinked_discussions) ~= "table" then
|
||||
vim.notify("No discussions or notes for this MR", vim.log.levels.WARN)
|
||||
u.notify("No discussions or notes for this MR", vim.log.levels.WARN)
|
||||
vim.api.nvim_buf_set_lines(split.bufnr, 0, -1, false, { "" })
|
||||
return
|
||||
end
|
||||
@@ -159,7 +163,7 @@ M.move_to_discussion_tree = function()
|
||||
local discussion_id = diagnostic.user_data.discussion_id
|
||||
local discussion_node, line_number = M.discussion_tree:get_node("-" .. discussion_id)
|
||||
if discussion_node == {} or discussion_node == nil then
|
||||
vim.notify("Discussion not found", vim.log.levels.WARN)
|
||||
u.notify("Discussion not found", vim.log.levels.WARN)
|
||||
return
|
||||
end
|
||||
if not discussion_node:is_expanded() then
|
||||
@@ -181,7 +185,7 @@ M.move_to_discussion_tree = function()
|
||||
end
|
||||
|
||||
if #diagnostics == 0 then
|
||||
vim.notify("No diagnostics for this line", vim.log.levels.WARN)
|
||||
u.notify("No diagnostics for this line", vim.log.levels.WARN)
|
||||
return
|
||||
elseif #diagnostics > 1 then
|
||||
vim.ui.select(diagnostics, {
|
||||
@@ -479,7 +483,6 @@ local function nui_tree_prepare_node(node)
|
||||
end
|
||||
|
||||
local texts = node.text
|
||||
|
||||
if type(node.text) ~= "table" or node.text.content then
|
||||
texts = { node.text }
|
||||
end
|
||||
@@ -502,6 +505,24 @@ local function nui_tree_prepare_node(node)
|
||||
|
||||
line:append(text, node.text_hl)
|
||||
|
||||
local note_id = tostring(node.is_root and node.root_note_id or node.id)
|
||||
|
||||
local e = require("gitlab.emoji")
|
||||
|
||||
---@type Emoji[]
|
||||
local emojis = M.emojis[note_id]
|
||||
local placed_emojis = {}
|
||||
if emojis ~= nil then
|
||||
for _, v in ipairs(emojis) do
|
||||
local icon = e.emoji_map[v.name]
|
||||
if icon ~= nil and not u.contains(placed_emojis, icon.moji) then
|
||||
line:append(" ")
|
||||
line:append(icon.moji)
|
||||
table.insert(placed_emojis, icon.moji)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(lines, line)
|
||||
end
|
||||
|
||||
@@ -689,7 +710,15 @@ M.set_tree_keymaps = function(tree, bufnr, unlinked)
|
||||
end, { buffer = bufnr, desc = "Open the note in your browser" })
|
||||
vim.keymap.set("n", "<leader>p", function()
|
||||
M.print_node(tree)
|
||||
end, { buffer = bufnr, desc = "dev_ Print current node (for debugging)" })
|
||||
end, { buffer = bufnr, desc = "Print current node (for debugging)" })
|
||||
vim.keymap.set("n", state.settings.discussion_tree.add_emoji, function()
|
||||
M.add_emoji_to_note(tree, unlinked)
|
||||
end, { buffer = bufnr, desc = "Add an emoji reaction to the note/comment" })
|
||||
vim.keymap.set("n", state.settings.discussion_tree.delete_emoji, function()
|
||||
M.delete_emoji_from_note(tree, unlinked)
|
||||
end, { buffer = bufnr, desc = "Remove an emoji reaction from the note/comment" })
|
||||
|
||||
emoji.init_popup(tree, bufnr)
|
||||
end
|
||||
|
||||
M.redraw_resolved_status = function(tree, note, mark_resolved)
|
||||
@@ -829,6 +858,76 @@ M.open_in_browser = function(tree)
|
||||
u.open_in_browser(url)
|
||||
end
|
||||
|
||||
M.add_emoji_to_note = function(tree, unlinked)
|
||||
local node = tree:get_node()
|
||||
local note_node = M.get_note_node(tree, node)
|
||||
local root_node = M.get_root_node(tree, node)
|
||||
local note_id = tonumber(note_node.is_root and root_node.root_note_id or note_node.id)
|
||||
local note_id_str = tostring(note_id)
|
||||
local emojis = require("gitlab.emoji").emoji_list
|
||||
emoji.pick_emoji(emojis, function(name)
|
||||
local body = { emoji = name, note_id = note_id }
|
||||
job.run_job("/mr/awardable/note/", "POST", body, function(data)
|
||||
if M.emojis[note_id_str] == nil then
|
||||
M.emojis[note_id_str] = {}
|
||||
table.insert(M.emojis[note_id_str], data.Emoji)
|
||||
else
|
||||
table.insert(M.emojis[note_id_str], data.Emoji)
|
||||
end
|
||||
if unlinked then
|
||||
M.rebuild_unlinked_discussion_tree()
|
||||
else
|
||||
M.rebuild_discussion_tree()
|
||||
end
|
||||
u.notify("Emoji added", vim.log.levels.INFO)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
M.delete_emoji_from_note = function(tree, unlinked)
|
||||
local node = tree:get_node()
|
||||
local note_node = M.get_note_node(tree, node)
|
||||
local root_node = M.get_root_node(tree, node)
|
||||
local note_id = tonumber(note_node.is_root and root_node.root_note_id or note_node.id)
|
||||
local note_id_str = tostring(note_id)
|
||||
|
||||
local e = require("gitlab.emoji")
|
||||
|
||||
local emojis = {}
|
||||
local current_emojis = M.emojis[note_id_str]
|
||||
for _, current_emoji in ipairs(current_emojis) do
|
||||
if state.USER.id == current_emoji.user.id then
|
||||
table.insert(emojis, e.emoji_map[current_emoji.name])
|
||||
end
|
||||
end
|
||||
|
||||
emoji.pick_emoji(emojis, function(name)
|
||||
local awardable_id
|
||||
for _, current_emoji in ipairs(current_emojis) do
|
||||
if current_emoji.name == name and current_emoji.user.id == state.USER.id then
|
||||
awardable_id = current_emoji.id
|
||||
break
|
||||
end
|
||||
end
|
||||
job.run_job(string.format("/mr/awardable/note/%d/%d", note_id, awardable_id), "DELETE", nil, function(_)
|
||||
local keep = {} -- Emojis to keep after deletion in the UI
|
||||
for _, saved in ipairs(M.emojis[note_id_str]) do
|
||||
if saved.name ~= name or saved.user.id ~= state.USER.id then
|
||||
table.insert(keep, saved)
|
||||
end
|
||||
end
|
||||
M.emojis[note_id_str] = keep
|
||||
if unlinked then
|
||||
M.rebuild_unlinked_discussion_tree()
|
||||
else
|
||||
M.rebuild_discussion_tree()
|
||||
end
|
||||
e.init_popup(tree, unlinked and M.unlinked_bufnr or M.linked_bufnr)
|
||||
u.notify("Emoji removed", vim.log.levels.INFO)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
-- For developers!
|
||||
M.print_node = function(tree)
|
||||
local current_node = tree:get_node()
|
||||
|
||||
@@ -267,7 +267,6 @@ M.parse_signs_from_discussions = function(discussions)
|
||||
start_line = start_old_line
|
||||
end_line = end_old_line
|
||||
else
|
||||
vim.print(start_type == "")
|
||||
return {}, {}, string.format("Unsupported line range type found for discussion %s", discussion.id)
|
||||
end
|
||||
|
||||
|
||||
@@ -245,7 +245,6 @@ M.color_labels = function(bufnr)
|
||||
for i, v in ipairs(state.settings.info.fields) do
|
||||
if v == "labels" then
|
||||
local line_content = u.get_line_content(bufnr, i)
|
||||
vim.print(line_content)
|
||||
for j, label in ipairs(state.LABELS) do
|
||||
local start_idx, end_idx = line_content:find(label.Name)
|
||||
if start_idx ~= nil and end_idx ~= nil then
|
||||
|
||||
140
lua/gitlab/emoji.lua
Normal file
140
lua/gitlab/emoji.lua
Normal file
@@ -0,0 +1,140 @@
|
||||
local u = require("gitlab.utils")
|
||||
local state = require("gitlab.state")
|
||||
|
||||
local M = {
|
||||
---@type EmojiMap|nil
|
||||
emoji_map = {},
|
||||
---@type Emoji[]
|
||||
emoji_list = {},
|
||||
}
|
||||
|
||||
M.init = function()
|
||||
local bin_path = state.settings.bin_path
|
||||
local emoji_path = bin_path
|
||||
.. state.settings.file_separator
|
||||
.. "config"
|
||||
.. state.settings.file_separator
|
||||
.. "emojis.json"
|
||||
local emojis = u.read_file(emoji_path)
|
||||
if emojis == nil then
|
||||
u.notify("Could not read emoji file at " .. emoji_path, vim.log.levels.WARN)
|
||||
end
|
||||
|
||||
local data_ok, data = pcall(vim.json.decode, emojis)
|
||||
if not data_ok then
|
||||
u.notify("Could not parse emoji file at " .. emoji_path, vim.log.levels.WARN)
|
||||
end
|
||||
|
||||
M.emoji_map = data
|
||||
M.emoji_list = {}
|
||||
for _, v in pairs(M.emoji_map) do
|
||||
table.insert(M.emoji_list, v)
|
||||
end
|
||||
end
|
||||
|
||||
-- Define the popup window options
|
||||
M.popup_opts = {
|
||||
relative = "cursor",
|
||||
row = -2,
|
||||
col = 0,
|
||||
width = 2, -- Width set dynamically later
|
||||
height = 1,
|
||||
style = "minimal",
|
||||
border = "single",
|
||||
}
|
||||
|
||||
M.show_popup = function(char)
|
||||
-- Close existing popup if it's open
|
||||
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
|
||||
vim.api.nvim_win_close(M.popup_win_id, true)
|
||||
end
|
||||
|
||||
-- Create a buffer for the popup window
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
|
||||
-- Set the content of the popup buffer to the character
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { char })
|
||||
|
||||
-- Open the popup window and store its ID
|
||||
M.popup_win_id = vim.api.nvim_open_win(buf, false, M.popup_opts)
|
||||
end
|
||||
|
||||
M.close_popup = function()
|
||||
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
|
||||
vim.api.nvim_win_close(M.popup_win_id, true)
|
||||
M.popup_win_id = nil -- Reset the window ID
|
||||
end
|
||||
end
|
||||
|
||||
M.init_popup = function(tree, bufnr)
|
||||
vim.api.nvim_create_autocmd({ "CursorHold" }, {
|
||||
callback = function()
|
||||
local node = tree:get_node()
|
||||
if node == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local note_node = require("gitlab.actions.discussions").get_note_node(tree, node)
|
||||
local root_node = require("gitlab.actions.discussions").get_root_node(tree, node)
|
||||
local note_id_str = tostring(note_node.is_root and root_node.root_note_id or note_node.id)
|
||||
|
||||
local emojis = require("gitlab.actions.discussions").emojis
|
||||
local note_emojis = emojis[note_id_str]
|
||||
if note_emojis == nil then
|
||||
return
|
||||
end
|
||||
|
||||
local cursor_pos = vim.api.nvim_win_get_cursor(0)
|
||||
vim.api.nvim_command('normal! "zyiw')
|
||||
vim.api.nvim_win_set_cursor(0, cursor_pos)
|
||||
local word = vim.fn.getreg("z")
|
||||
|
||||
for k, v in pairs(M.emoji_map) do
|
||||
if v.moji == word then
|
||||
local names = M.get_users_who_reacted_with_emoji(k, note_emojis)
|
||||
M.popup_opts.width = string.len(names)
|
||||
if M.popup_opts.width > 0 then
|
||||
M.show_popup(names)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
buffer = bufnr,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
|
||||
callback = function()
|
||||
M.close_popup()
|
||||
end,
|
||||
buffer = bufnr,
|
||||
})
|
||||
end
|
||||
|
||||
---@param name string
|
||||
---@return string
|
||||
M.get_users_who_reacted_with_emoji = function(name, note_emojis)
|
||||
local result = ""
|
||||
for _, v in pairs(note_emojis) do
|
||||
if v.name == name then
|
||||
result = result .. v.user.name .. ", "
|
||||
end
|
||||
end
|
||||
return string.len(result) > 3 and result:sub(1, -3) or result
|
||||
end
|
||||
|
||||
M.pick_emoji = function(options, cb)
|
||||
vim.ui.select(options, {
|
||||
prompt = "Choose emoji",
|
||||
format_item = function(val)
|
||||
return string.format("%s %s", val.moji, val.name)
|
||||
end,
|
||||
}, function(choice)
|
||||
if not choice then
|
||||
return
|
||||
end
|
||||
local name = choice.shortname:sub(2, -2)
|
||||
cb(name, choice)
|
||||
end)
|
||||
end
|
||||
|
||||
return M
|
||||
78
lua/gitlab/hover.lua
Normal file
78
lua/gitlab/hover.lua
Normal file
@@ -0,0 +1,78 @@
|
||||
local M = {}
|
||||
|
||||
M.show_popup = function(char)
|
||||
-- Close existing popup if it's open
|
||||
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
|
||||
vim.api.nvim_win_close(M.popup_win_id, true)
|
||||
end
|
||||
|
||||
-- Create a buffer for the popup window
|
||||
local buf = vim.api.nvim_create_buf(false, true)
|
||||
|
||||
-- Set the content of the popup buffer to the character
|
||||
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { char })
|
||||
|
||||
-- Define the popup window options
|
||||
local opts = {
|
||||
relative = "cursor",
|
||||
row = -2,
|
||||
col = 0,
|
||||
width = 2, -- Width set to 2 to accommodate the border
|
||||
height = 1,
|
||||
style = "minimal",
|
||||
border = "single",
|
||||
}
|
||||
|
||||
-- Open the popup window and store its ID
|
||||
M.popup_win_id = vim.api.nvim_open_win(buf, false, opts)
|
||||
end
|
||||
|
||||
M.close_popup = function()
|
||||
if M.popup_win_id and vim.api.nvim_win_is_valid(M.popup_win_id) then
|
||||
vim.api.nvim_win_close(M.popup_win_id, true)
|
||||
M.popup_win_id = nil -- Reset the window ID
|
||||
end
|
||||
end
|
||||
|
||||
M.init = function()
|
||||
-- Set up autocommands
|
||||
vim.api.nvim_create_autocmd({ "CursorHold" }, {
|
||||
callback = function()
|
||||
-- Get the current cursor position
|
||||
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
row = row - 1 -- Adjust row because Lua is 1-indexed
|
||||
col = col - 2 -- Adjust to account for > at front of line
|
||||
|
||||
if col < 1 then
|
||||
return
|
||||
end
|
||||
|
||||
-- Get the text of the current line
|
||||
local line = vim.api.nvim_buf_get_lines(0, row, row + 1, false)[1]
|
||||
|
||||
-- Correctly handle multi-byte characters, such as emojis
|
||||
local byteIndexStart = vim.str_byteindex(line, col)
|
||||
local byteIndexEnd = vim.str_byteindex(line, col + 1)
|
||||
|
||||
-- Extract the character (or emoji) under the cursor
|
||||
local char = line:sub(byteIndexStart + 1, byteIndexEnd)
|
||||
|
||||
-- Proceed only if char is not empty (to avoid empty popups)
|
||||
if char == "" then
|
||||
return
|
||||
end
|
||||
|
||||
M.show_popup(char)
|
||||
end,
|
||||
buffer = vim.api.nvim_get_current_buf(),
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
|
||||
callback = function()
|
||||
M.close_popup()
|
||||
end,
|
||||
buffer = vim.api.nvim_get_current_buf(),
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -1,6 +1,7 @@
|
||||
local u = require("gitlab.utils")
|
||||
local async = require("gitlab.async")
|
||||
local server = require("gitlab.server")
|
||||
local emoji = require("gitlab.emoji")
|
||||
local state = require("gitlab.state")
|
||||
local reviewer = require("gitlab.reviewer")
|
||||
local discussions = require("gitlab.actions.discussions")
|
||||
@@ -13,6 +14,7 @@ local create_mr = require("gitlab.actions.create_mr")
|
||||
local approvals = require("gitlab.actions.approvals")
|
||||
local labels = require("gitlab.actions.labels")
|
||||
|
||||
local user = state.dependencies.user
|
||||
local info = state.dependencies.info
|
||||
local labels_dep = state.dependencies.labels
|
||||
local project_members = state.dependencies.project_members
|
||||
@@ -28,6 +30,7 @@ return {
|
||||
require("gitlab.colors") -- Sets colors
|
||||
reviewer.init()
|
||||
discussions.initialize_discussions() -- place signs / diagnostics for discussions in reviewer
|
||||
emoji.init() -- Read in emojis for lookup purposes
|
||||
end,
|
||||
-- Global Actions 🌎
|
||||
summary = async.sequence({ u.merge(info, { refresh = true }), labels_dep }, summary.summary),
|
||||
@@ -45,7 +48,7 @@ return {
|
||||
move_to_discussion_tree_from_diagnostic = async.sequence({}, discussions.move_to_discussion_tree),
|
||||
create_note = async.sequence({ info }, comment.create_note),
|
||||
create_mr = async.sequence({}, create_mr.start),
|
||||
review = async.sequence({ u.merge(info, { refresh = true }), revisions }, function()
|
||||
review = async.sequence({ u.merge(info, { refresh = true }), revisions, user }, function()
|
||||
reviewer.open()
|
||||
end),
|
||||
close_review = function()
|
||||
@@ -54,7 +57,7 @@ return {
|
||||
pipeline = async.sequence({ info }, pipeline.open),
|
||||
merge = async.sequence({ u.merge(info, { refresh = true }) }, merge.merge),
|
||||
-- Discussion Tree Actions 🌴
|
||||
toggle_discussions = async.sequence({ info }, discussions.toggle),
|
||||
toggle_discussions = async.sequence({ info, user }, discussions.toggle),
|
||||
edit_comment = async.sequence({ info }, discussions.edit_comment),
|
||||
delete_comment = async.sequence({ info }, discussions.delete_comment),
|
||||
toggle_resolved = async.sequence({ info }, discussions.toggle_discussion_resolved),
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
local u = require("gitlab.utils")
|
||||
local M = {}
|
||||
|
||||
M.emoji_map = nil
|
||||
|
||||
-- These are the default settings for the plugin
|
||||
M.settings = {
|
||||
port = nil, -- choose random port
|
||||
@@ -49,6 +51,8 @@ M.settings = {
|
||||
open_in_browser = "b",
|
||||
reply = "r",
|
||||
toggle_node = "t",
|
||||
add_emoji = "Ea",
|
||||
delete_emoji = "Ed",
|
||||
toggle_all_discussions = "T",
|
||||
toggle_resolved_discussions = "R",
|
||||
toggle_unresolved_discussions = "U",
|
||||
@@ -311,6 +315,7 @@ end
|
||||
-- for each of the actions to occur. This is necessary because some Gitlab behaviors (like
|
||||
-- adding a reviewer) requires some initial state.
|
||||
M.dependencies = {
|
||||
user = { endpoint = "/users/me", key = "user", state = "USER", refresh = false },
|
||||
info = { endpoint = "/mr/info", key = "info", state = "INFO", refresh = false },
|
||||
labels = { endpoint = "/mr/label", key = "labels", state = "LABELS", refresh = false },
|
||||
revisions = { endpoint = "/mr/revisions", key = "Revisions", state = "MR_REVISIONS", refresh = false },
|
||||
|
||||
@@ -38,6 +38,15 @@ M.filter = function(input_table, value_to_remove)
|
||||
return resultTable
|
||||
end
|
||||
|
||||
M.filter_by_key_value = function(input_table, target_key, target_value)
|
||||
local result_table = {}
|
||||
for _, v in ipairs(input_table) do
|
||||
if v[target_key] ~= target_value then
|
||||
table.insert(result_table, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Merges two deeply nested tables together, overriding values from the first with conflicts
|
||||
---@param defaults table The first table
|
||||
---@param overrides table The second table
|
||||
|
||||
Reference in New Issue
Block a user