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: PR labels (#3002) #3118

Merged
merged 3 commits into from
Dec 20, 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
1 change: 1 addition & 0 deletions docs/docs/35-references/10-promotion-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ requests.
| `targetBranch` | `string` | N | The branch to which the changes should be merged. |
| `createTargetBranch` | `boolean` | N | Indicates whether a new, empty orphaned branch should be created and pushed to the remote if the target branch does not already exist there. Default is `false`. |
| `title` | `string` | N | The title for the pull request. Kargo generates a title based on the commit messages if it is not explicitly specified. |
| `labels` | `[]string` | N | Labels to add to the pull request. |

#### `git-open-pr` Example

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
Expand Down
1 change: 1 addition & 0 deletions internal/directives/git_pr_opener.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (g *gitPROpener) runPromotionStep(
Base: cfg.TargetBranch,
Title: title,
Description: description,
Labels: cfg.Labels,
},
); err != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored},
Expand Down
9 changes: 9 additions & 0 deletions internal/directives/schemas/git-open-pr-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
"type": "string",
"description": "The title for the pull request. Kargo generates a title based on the commit messages if it is not explicitly specified.",
"minLength": 1
},
"labels": {
"type": "array",
"description": "Labels to add to the pull request.",
"items": {
"type": "string",
"description": "A pull request label",
"minLength": 1
}
}
},
"oneOf": [
Expand Down
2 changes: 2 additions & 0 deletions internal/directives/zz_config_types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/gitprovider/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strings"

"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
adocore "github.com/microsoft/azure-devops-go-api/azuredevops/v7/core"
adogit "github.com/microsoft/azure-devops-go-api/azuredevops/v7/git"
"k8s.io/utils/ptr"

Expand Down Expand Up @@ -94,6 +95,12 @@
return nil, fmt.Errorf("error getting repository %q: %w", p.repo, err)
}
repoID := ptr.To(repository.Id.String())
labels := make([]adocore.WebApiTagDefinition, 0, len(opts.Labels))
for _, label := range opts.Labels {
labels = append(labels, adocore.WebApiTagDefinition{
Name: &label,
})
}

Check warning on line 103 in internal/gitprovider/azure/azure.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L98-L103

Added lines #L98 - L103 were not covered by tests
krancour marked this conversation as resolved.
Show resolved Hide resolved
sourceRefName := ptr.To(fmt.Sprintf("refs/heads/%s", opts.Head))
targetRefName := ptr.To(fmt.Sprintf("refs/heads/%s", opts.Base))
adoPR, err := gitClient.CreatePullRequest(ctx, adogit.CreatePullRequestArgs{
Expand All @@ -102,6 +109,7 @@
GitPullRequestToCreate: &adogit.GitPullRequest{
Title: &opts.Title,
Description: &opts.Description,
Labels: &labels,

Check warning on line 112 in internal/gitprovider/azure/azure.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/azure/azure.go#L112

Added line #L112 was not covered by tests
SourceRefName: sourceRefName,
TargetRefName: targetRefName,
},
Expand Down
93 changes: 88 additions & 5 deletions internal/gitprovider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,42 @@
gitprovider.Register(ProviderName, registration)
}

type githubClient interface {
CreatePullRequest(
ctx context.Context,
owner string,
repo string,
pull *github.NewPullRequest,
) (*github.PullRequest, *github.Response, error)

ListPullRequests(
ctx context.Context,
owner string,
repo string,
opts *github.PullRequestListOptions,
) ([]*github.PullRequest, *github.Response, error)

GetPullRequests(
ctx context.Context,
owner string,
repo string,
number int,
) (*github.PullRequest, *github.Response, error)

AddLabelsToIssue(
ctx context.Context,
owner string,
repo string,
number int,
labels []string,
) ([]*github.Label, *github.Response, error)
}

// provider is a GitHub implementation of gitprovider.Interface.
type provider struct { // nolint: revive
owner string
repo string
client *github.Client
client githubClient
}

// NewProvider returns a GitHub-based implementation of gitprovider.Interface.
Expand Down Expand Up @@ -81,10 +112,51 @@
return &provider{
owner: owner,
repo: repo,
client: client,
client: &githubClientWrapper{client},

Check warning on line 115 in internal/gitprovider/github/github.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/github/github.go#L115

Added line #L115 was not covered by tests
}, nil
}

type githubClientWrapper struct {
client *github.Client
}

func (g githubClientWrapper) CreatePullRequest(
ctx context.Context,
owner string,
repo string,
pull *github.NewPullRequest,
) (*github.PullRequest, *github.Response, error) {
return g.client.PullRequests.Create(ctx, owner, repo, pull)

Check warning on line 129 in internal/gitprovider/github/github.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/github/github.go#L128-L129

Added lines #L128 - L129 were not covered by tests
}

func (g githubClientWrapper) ListPullRequests(
ctx context.Context,
owner string,
repo string,
opts *github.PullRequestListOptions,
) ([]*github.PullRequest, *github.Response, error) {
return g.client.PullRequests.List(ctx, owner, repo, opts)

Check warning on line 138 in internal/gitprovider/github/github.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/github/github.go#L137-L138

Added lines #L137 - L138 were not covered by tests
}

func (g githubClientWrapper) GetPullRequests(
ctx context.Context,
owner string,
repo string,
number int,
) (*github.PullRequest, *github.Response, error) {
return g.client.PullRequests.Get(ctx, owner, repo, number)

Check warning on line 147 in internal/gitprovider/github/github.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/github/github.go#L146-L147

Added lines #L146 - L147 were not covered by tests
}

func (g githubClientWrapper) AddLabelsToIssue(
ctx context.Context,
owner string,
repo string,
number int,
labels []string,
) ([]*github.Label, *github.Response, error) {
return g.client.Issues.AddLabelsToIssue(ctx, owner, repo, number, labels)

Check warning on line 157 in internal/gitprovider/github/github.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/github/github.go#L156-L157

Added lines #L156 - L157 were not covered by tests
}

// CreatePullRequest implements gitprovider.Interface.
func (p *provider) CreatePullRequest(
ctx context.Context,
Expand All @@ -93,7 +165,7 @@
if opts == nil {
opts = &gitprovider.CreatePullRequestOpts{}
}
ghPR, _, err := p.client.PullRequests.Create(ctx,
ghPR, _, err := p.client.CreatePullRequest(ctx,
p.owner,
p.repo,
&github.NewPullRequest{
Expand All @@ -111,6 +183,17 @@
return nil, fmt.Errorf("unexpected nil pull request")
}
pr := convertGithubPR(*ghPR)
if len(opts.Labels) > 0 {
_, _, err = p.client.AddLabelsToIssue(ctx,
p.owner,
p.repo,
int(pr.Number),
opts.Labels,
)
}
if err != nil {
return nil, err
}

Check warning on line 196 in internal/gitprovider/github/github.go

View check run for this annotation

Codecov / codecov/patch

internal/gitprovider/github/github.go#L195-L196

Added lines #L195 - L196 were not covered by tests
return &pr, nil
}

Expand All @@ -119,7 +202,7 @@
ctx context.Context,
id int64,
) (*gitprovider.PullRequest, error) {
ghPR, _, err := p.client.PullRequests.Get(ctx, p.owner, p.repo, int(id))
ghPR, _, err := p.client.GetPullRequests(ctx, p.owner, p.repo, int(id))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -160,7 +243,7 @@
}
prs := []gitprovider.PullRequest{}
for {
ghPRs, res, err := p.client.PullRequests.List(ctx, p.owner, p.repo, &listOpts)
ghPRs, res, err := p.client.ListPullRequests(ctx, p.owner, p.repo, &listOpts)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading