Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added links to printed prs #58

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type pullRequest struct {
repoName string
branchName string
number int
guiURL string
status domain.PullRequestStatus
}

Expand All @@ -99,6 +100,10 @@ func (pr pullRequest) Status() domain.PullRequestStatus {
return pr.status
}

func (pr pullRequest) URL() string {
return pr.guiURL
}

// ParseRepositoryReference parses a repository reference from the format "ownerName/repoName"
func ParseRepositoryReference(val string) (RepositoryReference, error) {
split := strings.Split(val, "/")
Expand Down Expand Up @@ -257,6 +262,7 @@ func (g Github) CreatePullRequest(ctx context.Context, repo domain.Repository, n
repoName: pr.GetBase().GetRepo().GetName(),
branchName: pr.GetHead().GetRef(),
number: pr.GetNumber(),
guiURL: pr.GetHTMLURL(),
}, nil
}

Expand Down Expand Up @@ -347,6 +353,7 @@ func (g Github) GetPullRequestStatuses(ctx context.Context, branchName string) (
repoName: repoName,
branchName: pr.GetHead().GetRef(),
number: pr.GetNumber(),
guiURL: pr.GetHTMLURL(),
status: status,
})
}
Expand Down
45 changes: 29 additions & 16 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ type pullRequest struct {
pid int
branchName string
iid int
webURL string
status domain.PullRequestStatus
}

Expand All @@ -106,6 +107,10 @@ func (pr pullRequest) Status() domain.PullRequestStatus {
return pr.status
}

func (pr pullRequest) URL() string {
return pr.webURL
}

// GetRepositories fetches repositories from all sources (groups/user/specific project)
func (g *Gitlab) GetRepositories(ctx context.Context) ([]domain.Repository, error) {
allProjects, err := g.getProjects(ctx)
Expand Down Expand Up @@ -262,6 +267,7 @@ func (g *Gitlab) CreatePullRequest(ctx context.Context, repo domain.Repository,
pid: r.pid,
branchName: newPR.Head,
iid: mr.IID,
webURL: mr.WebURL,
}, nil
}

Expand Down Expand Up @@ -289,57 +295,64 @@ func (g *Gitlab) GetPullRequestStatuses(ctx context.Context, branchName string)
return nil, err
}

prs := make([]domain.PullRequest, len(projects))
for i, project := range projects {
status, iid, err := g.getPullRequestInfo(ctx, branchName, project)
prs := []domain.PullRequest{}
for _, project := range projects {
mr, err := g.getPullRequest(ctx, branchName, project)
if err != nil {
return nil, err
}
if mr == nil {
continue
}

prs[i] = pullRequest{
prs = append(prs, pullRequest{
repoName: project.Path,
ownerName: project.Namespace.Path,
pid: project.ID,
branchName: branchName,
status: status,
iid: iid,
}
status: pullRequestStatus(mr),
iid: mr.IID,
webURL: mr.WebURL,
})
}

return prs, nil
}

func (g *Gitlab) getPullRequestInfo(ctx context.Context, branchName string, project *gitlab.Project) (status domain.PullRequestStatus, id int, err error) {
func (g *Gitlab) getPullRequest(ctx context.Context, branchName string, project *gitlab.Project) (*gitlab.MergeRequest, error) {
mrs, _, err := g.glClient.MergeRequests.ListProjectMergeRequests(project.ID, &gitlab.ListProjectMergeRequestsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 1,
},
SourceBranch: &branchName,
}, gitlab.WithContext(ctx))
if err != nil {
return domain.PullRequestStatusUnknown, 0, err
return nil, err
}

if len(mrs) == 0 {
return domain.PullRequestStatusUnknown, 0, nil
return nil, nil
}

mr, _, err := g.glClient.MergeRequests.GetMergeRequest(project.ID, mrs[0].IID, nil, gitlab.WithContext(ctx))
if err != nil {
return domain.PullRequestStatusUnknown, mrs[0].IID, err
return mrs[0], err
}
return mr, nil
}

func pullRequestStatus(mr *gitlab.MergeRequest) domain.PullRequestStatus {
switch {
case mr.MergedAt != nil:
return domain.PullRequestStatusMerged, mr.IID, nil
return domain.PullRequestStatusMerged
case mr.ClosedAt != nil:
return domain.PullRequestStatusClosed, mr.IID, nil
return domain.PullRequestStatusClosed
case mr.Pipeline == nil, mr.Pipeline.Status == "success":
return domain.PullRequestStatusSuccess, mr.IID, nil
return domain.PullRequestStatusSuccess
case mr.Pipeline.Status == "failed":
return domain.PullRequestStatusError, mr.IID, nil
return domain.PullRequestStatusError
default:
return domain.PullRequestStatusPending, mr.IID, nil
return domain.PullRequestStatusPending
}
}

Expand Down
11 changes: 10 additions & 1 deletion internal/multigitter/repocounter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync"

"github.com/lindell/multi-gitter/internal/domain"
"github.com/lindell/multi-gitter/internal/multigitter/terminal"
)

// Counter keeps track of succeeded and failed repositories
Expand Down Expand Up @@ -65,7 +66,11 @@ func (r *Counter) Info() string {
if len(r.successPullRequests) > 0 {
exitInfo += "Repositories with a successful run:\n"
for _, pr := range r.successPullRequests {
exitInfo += fmt.Sprintf(" %s\n", pr.String())
if urler, ok := pr.(urler); ok {
exitInfo += fmt.Sprintf(" %s\n", terminal.Link(pr.String(), urler.URL()))
} else {
exitInfo += fmt.Sprintf(" %s\n", pr.String())
}
}
}

Expand All @@ -78,3 +83,7 @@ func (r *Counter) Info() string {

return exitInfo
}

type urler interface {
URL() string
}
5 changes: 5 additions & 0 deletions internal/multigitter/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package multigitter

type urler interface {
URL() string
}
8 changes: 7 additions & 1 deletion internal/multigitter/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"io"

"github.com/lindell/multi-gitter/internal/multigitter/terminal"
)

// Statuser checks the statuses of pull requests
Expand All @@ -23,7 +25,11 @@ func (s Statuser) Statuses(ctx context.Context) error {
}

for _, pr := range prs {
fmt.Fprintf(s.Output, "%s: %s\n", pr.String(), pr.Status())
if urler, ok := pr.(urler); ok {
fmt.Fprintf(s.Output, "%s: %s\n", terminal.Link(pr.String(), urler.URL()), pr.Status())
} else {
fmt.Fprintf(s.Output, "%s: %s\n", pr.String(), pr.Status())
}
}

return nil
Expand Down
9 changes: 9 additions & 0 deletions internal/multigitter/terminal/terminal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package terminal

import "fmt"

// Link generates a link in that can be displayed in the terminal
// https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
func Link(text, url string) string {
return fmt.Sprintf("\x1B]8;;%s\a%s\x1B]8;;\a", url, text)
}