Fix MR Selection, Go Code Refactor (#358)

refactor: Refactors the Go codebase into a more modular and idiomatic approach
fix: require selection of specific MR when there are multiple targets for a given source branch
feat: Allows for the passing of Gitlab's filter options when choosing an MR, improves MR selection
feat: API to choose an MR from a list based on the provided username's involvement as an assignee/reviewer/author

This is a #MINOR release
This commit is contained in:
Harrison (Harry) Cramer
2024-09-08 16:45:09 -04:00
committed by GitHub
parent 6500ef1f2c
commit ea2b2b2f5c
81 changed files with 2559 additions and 3035 deletions

131
cmd/app/git/git.go Normal file
View File

@@ -0,0 +1,131 @@
package git
import (
"fmt"
"os/exec"
"regexp"
"strings"
)
type GitManager interface {
RefreshProjectInfo(remote string) error
GetProjectUrlFromNativeGitCmd(remote string) (url string, err error)
GetCurrentBranchNameFromNativeGitCmd() (string, error)
GetLatestCommitOnRemote(remote string, branchName string) (string, error)
}
type GitData struct {
RemoteUrl string
Namespace string
ProjectName string
BranchName string
}
type Git struct{}
/*
projectPath returns the Gitlab project full path, which isn't necessarily the same as its name.
See https://docs.gitlab.com/ee/api/rest/index.html#namespaced-path-encoding for more information.
*/
func (g GitData) ProjectPath() string {
return g.Namespace + "/" + g.ProjectName
}
/*
Extracts information about the current repository and returns
it to the client for initialization. The current directory must be a valid
Gitlab project and the branch must be a feature branch
*/
func NewGitData(remote string, g GitManager) (GitData, error) {
err := g.RefreshProjectInfo(remote)
if err != nil {
return GitData{}, fmt.Errorf("Could not get latest information from remote: %v", err)
}
url, err := g.GetProjectUrlFromNativeGitCmd(remote)
if err != nil {
return GitData{}, fmt.Errorf("Could not get project Url: %v", err)
}
/*
This should match following formats:
namespace: namespace, projectName: dummy-test-repo:
https://gitlab.com/namespace/dummy-test-repo.git
git@gitlab.com:namespace/dummy-test-repo.git
ssh://git@gitlab.com/namespace/dummy-test-repo.git
namespace: namespace/subnamespace, projectName: dummy-test-repo:
ssh://git@gitlab.com/namespace/subnamespace/dummy-test-repo
https://git@gitlab.com/namespace/subnamespace/dummy-test-repo.git
git@git@gitlab.com:namespace/subnamespace/dummy-test-repo.git
*/
re := regexp.MustCompile(`(?:^https?:\/\/|^ssh:\/\/|^git@)(?:[^\/:]+)(?::\d+)?[\/:](.*)\/([^\/]+?)(?:\.git)?$`)
matches := re.FindStringSubmatch(url)
if len(matches) != 3 {
return GitData{}, fmt.Errorf("Invalid Git URL format: %s", url)
}
namespace := matches[1]
projectName := matches[2]
branchName, err := g.GetCurrentBranchNameFromNativeGitCmd()
if err != nil {
return GitData{}, fmt.Errorf("Failed to get current branch: %v", err)
}
return GitData{
RemoteUrl: url,
Namespace: namespace,
ProjectName: projectName,
BranchName: branchName,
},
nil
}
/* Gets the current branch name */
func (g Git) GetCurrentBranchNameFromNativeGitCmd() (res string, e error) {
gitCmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
output, err := gitCmd.Output()
if err != nil {
return "", fmt.Errorf("Error running git rev-parse: %w", err)
}
branchName := strings.TrimSpace(string(output))
return branchName, nil
}
/* Gets the project SSH or HTTPS url */
func (g Git) GetProjectUrlFromNativeGitCmd(remote string) (string, error) {
cmd := exec.Command("git", "remote", "get-url", remote)
url, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Could not get remote")
}
return strings.TrimSpace(string(url)), nil
}
/* Pulls down latest commit information from Gitlab */
func (g Git) RefreshProjectInfo(remote string) error {
cmd := exec.Command("git", "fetch", remote)
_, err := cmd.Output()
if err != nil {
return fmt.Errorf("Failed to run `git fetch %s`: %v", remote, err)
}
return nil
}
func (g Git) GetLatestCommitOnRemote(remote string, branchName string) (string, error) {
cmd := exec.Command("git", "log", "-1", "--format=%H", fmt.Sprintf("%s/%s", remote, branchName))
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("Failed to run `git log -1 --format=%%H " + fmt.Sprintf("%s/%s", remote, branchName))
}
commit := strings.TrimSpace(string(out))
return commit, nil
}

219
cmd/app/git/git_test.go Normal file
View File

@@ -0,0 +1,219 @@
package git
import (
"errors"
"testing"
)
type FakeGitManager struct {
RemoteUrl string
BranchName string
ProjectName string
Namespace string
}
func (f FakeGitManager) RefreshProjectInfo(remote string) error {
return nil
}
func (f FakeGitManager) GetCurrentBranchNameFromNativeGitCmd() (string, error) {
return f.BranchName, nil
}
func (f FakeGitManager) GetLatestCommitOnRemote(remote string, branchName string) (string, error) {
return "", nil
}
func (f FakeGitManager) GetProjectUrlFromNativeGitCmd(string) (url string, err error) {
return f.RemoteUrl, nil
}
type TestCase struct {
desc string
branch string
projectName string
namespace string
remote string
}
func TestExtractGitInfo_Success(t *testing.T) {
testCases := []TestCase{
{
desc: "Project configured in SSH under a single folder",
remote: "git@custom-gitlab.com:namespace-1/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in SSH under a single folder without .git extension",
remote: "git@custom-gitlab.com:namespace-1/project-name",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in SSH under one nested folder",
remote: "git@custom-gitlab.com:namespace-1/namespace-2/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2",
},
{
desc: "Project configured in SSH under two nested folders",
remote: "git@custom-gitlab.com:namespace-1/namespace-2/namespace-3/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2/namespace-3",
},
{
desc: "Project configured in SSH:// under a single folder",
remote: "ssh://custom-gitlab.com/namespace-1/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in SSH:// under a single folder without .git extension",
remote: "ssh://custom-gitlab.com/namespace-1/project-name",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in SSH:// under two nested folders",
remote: "ssh://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2/namespace-3",
},
{
desc: "Project configured in SSH:// and have a custom port",
remote: "ssh://custom-gitlab.com:2222/namespace-1/project-name",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in HTTP and under a single folder without .git extension",
remote: "http://custom-gitlab.com/namespace-1/project-name",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in HTTPS and under a single folder",
remote: "https://custom-gitlab.com/namespace-1/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1",
},
{
desc: "Project configured in HTTPS and under a nested folder",
remote: "https://custom-gitlab.com/namespace-1/namespace-2/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2",
},
{
desc: "Project configured in HTTPS and under two nested folders",
remote: "https://custom-gitlab.com/namespace-1/namespace-2/namespace-3/project-name.git",
branch: "feature/abc",
projectName: "project-name",
namespace: "namespace-1/namespace-2/namespace-3",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
g := FakeGitManager{
Namespace: tC.namespace,
ProjectName: tC.projectName,
BranchName: tC.branch,
RemoteUrl: tC.remote,
}
data, err := NewGitData(tC.remote, g)
if err != nil {
t.Errorf("No error was expected, got %s", err)
}
if data.RemoteUrl != tC.remote {
t.Errorf("\nExpected Remote URL: %s\nActual: %s", tC.remote, data.RemoteUrl)
}
if data.BranchName != tC.branch {
t.Errorf("\nExpected Branch Name: %s\nActual: %s", tC.branch, data.BranchName)
}
if data.ProjectName != tC.projectName {
t.Errorf("\nExpected Project Name: %s\nActual: %s", tC.projectName, data.ProjectName)
}
if data.Namespace != tC.namespace {
t.Errorf("\nExpected Namespace: %s\nActual: %s", tC.namespace, data.Namespace)
}
})
}
}
type FailTestCase struct {
desc string
errMsg string
expectedErr string
}
type failingUrlManager struct {
errMsg string
FakeGitManager
}
func (f failingUrlManager) GetProjectUrlFromNativeGitCmd(string) (string, error) {
return "", errors.New(f.errMsg)
}
func TestExtractGitInfo_FailToGetProjectRemoteUrl(t *testing.T) {
tC := FailTestCase{
desc: "Error returned by function to get the project remote url",
errMsg: "Some error",
expectedErr: "Could not get project Url: Some error",
}
t.Run(tC.desc, func(t *testing.T) {
g := failingUrlManager{
errMsg: tC.errMsg,
}
_, err := NewGitData("", g)
if err == nil {
t.Errorf("Expected an error, got none")
}
if err.Error() != tC.expectedErr {
t.Errorf("\nExpected: %s\nActual: %s", tC.expectedErr, err.Error())
}
})
}
type failingBranchManager struct {
errMsg string
FakeGitManager
}
func (f failingBranchManager) GetCurrentBranchNameFromNativeGitCmd() (string, error) {
return "", errors.New(f.errMsg)
}
func TestExtractGitInfo_FailToGetCurrentBranchName(t *testing.T) {
tC := FailTestCase{
desc: "Error returned by function to get the project remote url",
errMsg: "Some error",
expectedErr: "Failed to get current branch: Some error",
}
t.Run(tC.desc, func(t *testing.T) {
g := failingBranchManager{
FakeGitManager: FakeGitManager{
RemoteUrl: "git@custom-gitlab.com:namespace-1/project-name.git",
},
errMsg: tC.errMsg,
}
_, err := NewGitData("", g)
if err == nil {
t.Errorf("Expected an error, got none")
}
if err.Error() != tC.expectedErr {
t.Errorf("\nExpected: %s\nActual: %s", tC.expectedErr, err.Error())
}
})
}