From e0b1e2221d55f37d26239eb76e3bc37abff5d2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20T=C3=B6lle?= Date: Sat, 3 Aug 2024 03:00:36 +0200 Subject: [PATCH] feat: open pr --- cmd/rp/cmd/run.go | 16 +++++++++++++++- forge.go | 24 +++++++++++++++++++++++- releasepr.go | 2 ++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cmd/rp/cmd/run.go b/cmd/rp/cmd/run.go index f462e5f..dceb26c 100644 --- a/cmd/rp/cmd/run.go +++ b/cmd/rp/cmd/run.go @@ -232,7 +232,21 @@ func reconcileReleasePR(ctx context.Context, forge rp.Forge, changesets []rp.Cha logger.InfoContext(ctx, "file content is already up-to-date in remote branch, skipping push") } - // TODO Open PR + // Open/Update PR + if pr == nil { + pr = &rp.ReleasePullRequest{ + Title: releaseCommitMessage, + Description: "TODO", + Labels: nil, + Head: rpBranch, + } + + pr, err = forge.CreatePullRequest(ctx, pr) + if err != nil { + return err + } + logger.InfoContext(ctx, "opened pull request", "pr.title", pr.Title, "pr.id", pr.ID) + } return nil } diff --git a/forge.go b/forge.go index ded1c5f..b8ee226 100644 --- a/forge.go +++ b/forge.go @@ -48,6 +48,8 @@ type Forge interface { // PullRequestForBranch returns the open pull request between the branch and ForgeOptions.BaseBranch. If no open PR // exists, it returns nil. PullRequestForBranch(context.Context, string) (*ReleasePullRequest, error) + + CreatePullRequest(context.Context, *ReleasePullRequest) (*ReleasePullRequest, error) } type ForgeOptions struct { @@ -330,7 +332,7 @@ func (g *GitHub) PullRequestForBranch(ctx context.Context, branch string) (*Rele } for _, pr := range prs { - if pr.Base.GetLabel() == g.options.BaseBranch && pr.Head.GetLabel() == branch && pr.GetState() == GitHubPRStateOpen { + if pr.GetBase().GetLabel() == g.options.BaseBranch && pr.GetHead().GetLabel() == branch && pr.GetState() == GitHubPRStateOpen { labels := make([]string, 0, len(pr.Labels)) for _, label := range pr.Labels { labels = append(labels, label.GetName()) @@ -341,6 +343,7 @@ func (g *GitHub) PullRequestForBranch(ctx context.Context, branch string) (*Rele Title: pr.GetTitle(), Description: pr.GetBody(), Labels: labels, + Head: pr.GetHead().GetLabel(), }, nil } } @@ -354,6 +357,25 @@ func (g *GitHub) PullRequestForBranch(ctx context.Context, branch string) (*Rele return nil, nil } +func (g *GitHub) CreatePullRequest(ctx context.Context, pr *ReleasePullRequest) (*ReleasePullRequest, error) { + ghPR, _, err := g.client.PullRequests.Create( + ctx, g.options.Owner, g.options.Repo, + &github.NewPullRequest{ + Title: &pr.Title, + Head: &pr.Head, + Base: &g.options.BaseBranch, + Body: &pr.Description, + }, + ) + if err != nil { + return nil, err + } + + pr.ID = int(*ghPR.ID) // TODO: String ID? + + return pr, nil +} + func (g *GitHubOptions) autodiscover() { if apiToken := os.Getenv(GitHubEnvAPIToken); apiToken != "" { g.APIToken = apiToken diff --git a/releasepr.go b/releasepr.go index 7592ca7..b53d853 100644 --- a/releasepr.go +++ b/releasepr.go @@ -13,6 +13,8 @@ type ReleasePullRequest struct { Title string Description string Labels []string + + Head string } type ReleaseOverrides struct {