Files
gitlab.nvim/cmd/client.go
Harrison Cramer b9538b1ce6 BUGFIX: Show all merge requests
We do not want to show only merge requests created by the current user,
   we want to show all merge requests for the current branch created by
   anyone.
2023-08-03 09:43:52 -04:00

80 lines
1.6 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"strconv"
"github.com/xanzy/go-gitlab"
)
type Client struct {
command string
projectId string
mergeId int
git *gitlab.Client
}
type Logger struct {
Active bool
}
func (l Logger) Printf(s string, args ...interface{}) {
logString := fmt.Sprintf(s+"\n", args...)
file, err := os.OpenFile("./logs", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.Write([]byte(logString))
}
/* This will initialize the client with the token and check for the basic project ID and command arguments */
func (c *Client) Init(branchName string) error {
if len(os.Args) < 2 {
return errors.New("Must provide project ID!")
}
projectId := os.Args[1]
c.projectId = projectId
if projectId == "" {
return errors.New("Project ID cannot be empty")
}
var l Logger
git, err := gitlab.NewClient(os.Getenv("GITLAB_TOKEN"), gitlab.WithCustomLogger(l))
if err != nil {
return fmt.Errorf("Failed to create client: %v", err)
}
options := gitlab.ListMergeRequestsOptions{
Scope: gitlab.String("all"),
State: gitlab.String("opened"),
SourceBranch: &branchName,
}
mergeRequests, _, err := git.MergeRequests.ListMergeRequests(&options)
if err != nil {
return fmt.Errorf("Failed to list merge requests: %w", err)
}
if len(mergeRequests) == 0 {
return errors.New("No merge requests found")
}
mergeId := strconv.Itoa(mergeRequests[0].IID)
mergeIdInt, err := strconv.Atoi(mergeId)
if err != nil {
return err
}
c.mergeId = mergeIdInt
c.git = git
return nil
}