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

Pull Request ID as Issue ID #88

Merged
merged 6 commits into from
Jun 6, 2024
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
4 changes: 0 additions & 4 deletions internal/domain/providers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package domain

type GhCli interface {
Execute(result any, args []string) (err error)
}

type RepositoryProvider interface {
GetRepository() (repo *Repository, err error)
}
Expand Down
4 changes: 0 additions & 4 deletions internal/fakes/domain/fake_gh_cli.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package domain

import "github.com/InditexTech/gh-sherpa/internal/domain"

type FakeGhCli struct{}

var _ domain.GhCli = (*FakeGhCli)(nil)

func NewFakeGhCli() FakeGhCli {
return FakeGhCli{}
}
Expand Down
2 changes: 0 additions & 2 deletions internal/gh/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (

type Cli struct{}

var _ domain.GhCli = (*Cli)(nil)

func (c *Cli) GetRepository() (repo *domain.Repository, err error) {
baseCommand := []string{"repo", "view", "--json", "name,owner,nameWithOwner,defaultBranchRef"}

Expand Down
54 changes: 43 additions & 11 deletions internal/issue_trackers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,67 @@ var issuePattern = regexp.MustCompile(`^(?i:GH-)?(?P<issue_num>\d+)$`)

var ErrIssueNotFound = fmt.Errorf("the issue was not found")

var ErrIdIsPullRequestNumber = func(identifier string) error {
return fmt.Errorf("given identifier %s is a Pull Request number", identifier)
}

type githubCli interface {
Execute(result interface{}, command []string) error
domain.RepositoryProvider
}

type Github struct {
cfg Configuration
cli domain.GhCli
cli githubCli
}

type Configuration struct {
config.Github
}

type ghIssue struct {
Number int64
Title string
Body string
Labels []Label
Url string
Number int64
Title string
Body string
Labels []Label
Url string
PullRequest *ghPullRequest `json:"pull_request"`
}

func (i ghIssue) isPullRequest() bool {
return i.PullRequest != nil
}

type ghPullRequest map[string]any

type Label struct {
Id string
Id int
Name string
Description string
Color string
}

var newGhCli = func() githubCli {
return &gh.Cli{}
}

// New returns a new Github issue tracker with the given configuration
func New(cfg Configuration) (*Github, error) {

return &Github{
cfg: cfg,
cli: &gh.Cli{},
cli: newGhCli(),
}, nil
}

func (g *Github) GetIssue(identifier string) (issue domain.Issue, err error) {
command := []string{"issue", "view", identifier, "--json", "labels,number,title,body,url"}
repo, err := g.cli.GetRepository()
if err != nil {
return nil, err
}

apiPath := fmt.Sprintf("/repos/%s/issues/%s", repo.NameWithOwner, identifier)
command := []string{"api", apiPath}

result := ghIssue{}

Expand All @@ -63,12 +89,18 @@ func (g *Github) GetIssue(identifier string) (issue domain.Issue, err error) {
return
}

if result.isPullRequest() {
return nil, ErrIdIsPullRequestNumber(identifier)
}

labels := make([]domain.Label, len(result.Labels))

for i, label := range result.Labels {
labels[i] = domain.Label{
Id: label.Id,
Name: label.Name,
Id: fmt.Sprintf("%d", label.Id),
Name: label.Name,
Description: label.Description,
Color: label.Color,
}
}

Expand Down
Loading