Skip to content

Commit

Permalink
Link PR to issue when creating PR
Browse files Browse the repository at this point in the history
  • Loading branch information
janekbaraniewski committed Jul 23, 2023
1 parent 58b7220 commit 8fcecab
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
8 changes: 4 additions & 4 deletions pkg/backend_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (g *GitHub) CloseIssue(owner string, repo RepoConfigName, issueID IssueID)
return nil
}

func (g *GitHub) OpenPullRequest(owner string, repo RepoConfigName, title, body, baseBranch, headBranch string) error {
func (g *GitHub) OpenPullRequest(owner string, repo RepoConfigName, title, body, baseBranch, headBranch string) (*int, error) {
newPR := &github.NewPullRequest{
Title: github.String(title),
Head: github.String(headBranch),
Expand All @@ -67,12 +67,12 @@ func (g *GitHub) OpenPullRequest(owner string, repo RepoConfigName, title, body,
MaintainerCanModify: github.Bool(true),
}

_, _, err := g.client.PullRequests.Create(context.Background(), owner, string(repo), newPR)
pr, _, err := g.client.PullRequests.Create(context.Background(), owner, string(repo), newPR)
if err != nil {
return err
return nil, err
}

return nil
return pr.Number, nil
}

func (g *GitHub) LinkIssueToRepo(owner string, repo RepoConfigName, issueID IssueID, pullRequestID string) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/backend_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,20 @@ func (g *GitLab) CloseIssue(owner string, repo RepoConfigName, issueID IssueID)
return nil
}

func (g *GitLab) OpenPullRequest(owner string, repo RepoConfigName, title, body, baseBranch, headBranch string) error {
func (g *GitLab) OpenPullRequest(owner string, repo RepoConfigName, title, body, baseBranch, headBranch string) (*int, error) {
pullReqOpt := &gitlab.CreateMergeRequestOptions{
Title: gitlab.String(title),
Description: gitlab.String(body),
SourceBranch: gitlab.String(headBranch),
TargetBranch: gitlab.String(baseBranch),
}

_, _, err := g.client.MergeRequests.CreateMergeRequest(fmt.Sprintf("%s/%s", owner, repo), pullReqOpt)
mr, _, err := g.client.MergeRequests.CreateMergeRequest(fmt.Sprintf("%s/%s", owner, repo), pullReqOpt)
if err != nil {
return err
return nil, err
}

return nil
return &mr.ID, nil
}

func (g *GitLab) LinkIssueToRepo(owner string, repo RepoConfigName, issueID IssueID, pullRequestID string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type IssueBackend interface {
}

type RepositoryBackend interface {
OpenPullRequest(owner string, repo RepoConfigName, title, body, baseBranch, headBranch string) error
OpenPullRequest(owner string, repo RepoConfigName, title, body, baseBranch, headBranch string) (*int, error)
}

// getIssueBackendConfigurator prepares IssueBackend
Expand Down
12 changes: 11 additions & 1 deletion pkg/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/andygrunwald/go-jira"
Expand Down Expand Up @@ -175,14 +176,23 @@ func OpenPullRequest(issueID IssueID) error {
}

repo := config.GetRepository(profile.DefaultRepository)
return repoBackend.OpenPullRequest(
prId, err := repoBackend.OpenPullRequest(
repo.Owner,
repo.Name,
fmt.Sprintf("%v | %v", issue.ID, issue.Name),
fmt.Sprintf("Resolves #%v", issue.ID),
"master", // TODO: make configurable
issue.BranchName,
)
if err != nil {
return err
}

issueBackend, err := getIssueBackendConfigurator(config.GetBackend(profile.RepoBackend))
if err != nil {
return err
}
return issueBackend.LinkIssueToRepo(repo.Owner, repo.Name, issueID, strconv.Itoa(*prId))
}

// FinishWorkingOnIssue finishes work on an issue
Expand Down

0 comments on commit 8fcecab

Please sign in to comment.