github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/issues/git_provider.go (about)

     1  package issues
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/olli-ai/jx/v2/pkg/gits"
     9  	"github.com/olli-ai/jx/v2/pkg/util"
    10  )
    11  
    12  type GitIssueProvider struct {
    13  	GitProvider gits.GitProvider
    14  	Owner       string
    15  	Repository  string
    16  }
    17  
    18  func CreateGitIssueProvider(gitProvider gits.GitProvider, owner string, repository string) (IssueProvider, error) {
    19  	if owner == "" {
    20  		return nil, fmt.Errorf("No owner specified")
    21  	}
    22  	if repository == "" {
    23  		return nil, fmt.Errorf("No owner specified")
    24  	}
    25  	return &GitIssueProvider{
    26  		GitProvider: gitProvider,
    27  		Owner:       owner,
    28  		Repository:  repository,
    29  	}, nil
    30  }
    31  
    32  func (i *GitIssueProvider) GetIssue(key string) (*gits.GitIssue, error) {
    33  	n, err := issueKeyToNumber(key)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	return i.GitProvider.GetIssue(i.Owner, i.Repository, n)
    38  }
    39  
    40  func (i *GitIssueProvider) SearchIssues(query string) ([]*gits.GitIssue, error) {
    41  	return i.GitProvider.SearchIssues(i.Owner, i.Repository, query)
    42  }
    43  
    44  func (i *GitIssueProvider) SearchIssuesClosedSince(t time.Time) ([]*gits.GitIssue, error) {
    45  	return i.GitProvider.SearchIssuesClosedSince(i.Owner, i.Repository, t)
    46  }
    47  
    48  func (i *GitIssueProvider) IssueURL(key string) string {
    49  	n, err := issueKeyToNumber(key)
    50  	if err != nil {
    51  		return ""
    52  	}
    53  	return i.GitProvider.IssueURL(i.Owner, i.Repository, n, false)
    54  }
    55  
    56  func issueKeyToNumber(key string) (int, error) {
    57  	n, err := strconv.Atoi(key)
    58  	if err != nil {
    59  		return n, fmt.Errorf("Failed to convert issue key '%s' to number: %s", key, err)
    60  	}
    61  	return n, nil
    62  }
    63  
    64  func (i *GitIssueProvider) CreateIssue(issue *gits.GitIssue) (*gits.GitIssue, error) {
    65  	return i.GitProvider.CreateIssue(i.Owner, i.Repository, issue)
    66  }
    67  
    68  func (i *GitIssueProvider) CreateIssueComment(key string, comment string) error {
    69  	n, err := issueKeyToNumber(key)
    70  	if err != nil {
    71  		return err
    72  	}
    73  	return i.GitProvider.CreateIssueComment(i.Owner, i.Repository, n, comment)
    74  }
    75  
    76  func (i *GitIssueProvider) HomeURL() string {
    77  	return util.UrlJoin(i.GitProvider.ServerURL(), i.Owner, i.Repository)
    78  }