@@ -48,14 +48,15 @@ type GitHubClientFactory func(token string, repo *github.Repository) (GitHubClie
4848type ContainerClientFactory func (workRoot , image , userUID , userGID string ) (ContainerClient , error )
4949
5050type commitInfo struct {
51- cfg * config.Config
52- state * config.LibrarianState
53- repo gitrepo.Repository
54- ghClient GitHubClient
55- idToCommits map [string ]string
56- failedLibraries []string
57- commitMessage string
58- prType string
51+ cfg * config.Config
52+ state * config.LibrarianState
53+ repo gitrepo.Repository
54+ ghClient GitHubClient
55+ idToCommits map [string ]string
56+ failedLibraries []string
57+ commitMessage string
58+ prType string
59+ pullRequestLabels []string
5960}
6061
6162type commandRunner struct {
@@ -328,8 +329,7 @@ func copyLibrary(dst, src string, library *config.LibraryState) error {
328329 return nil
329330}
330331
331- // commitAndPush creates a commit and push request to GitHub for the generated
332- // changes.
332+ // commitAndPush creates a commit and push request to GitHub for the generated changes.
333333// It uses the GitHub client to create a PR with the specified branch, title, and
334334// description to the repository.
335335func commitAndPush (ctx context.Context , info * commitInfo ) error {
@@ -380,10 +380,28 @@ func commitAndPush(ctx context.Context, info *commitInfo) error {
380380 return fmt .Errorf ("failed to create pull request body: %w" , err )
381381 }
382382
383- if _ , err = info .ghClient .CreatePullRequest (ctx , gitHubRepo , branch , cfg .Branch , title , prBody ); err != nil {
383+ pullRequestMetadata , err := info .ghClient .CreatePullRequest (ctx , gitHubRepo , branch , cfg .Branch , title , prBody )
384+ if err != nil {
384385 return fmt .Errorf ("failed to create pull request: %w" , err )
385386 }
386387
388+ return addLabelsToPullRequest (ctx , info .ghClient , info .pullRequestLabels , pullRequestMetadata )
389+ }
390+
391+ // addLabelsToPullRequest adds a list of labels to a single pull request (specified by the id number).
392+ // Should only be called on a valid Github pull request.
393+ // Passing in `nil` for labels will no-op and an empty list for labels will clear all labels on the PR.
394+ // TODO: Consolidate the params to a potential PullRequestInfo struct.
395+ func addLabelsToPullRequest (ctx context.Context , ghClient GitHubClient , pullRequestLabels []string , prMetadata * github.PullRequestMetadata ) error {
396+ // Do not update if there are aren't labels provided
397+ if pullRequestLabels == nil {
398+ return nil
399+ }
400+ // Github API treats Issues and Pull Request the same
401+ // https://docs.github.com/en/rest/issues/labels#add-labels-to-an-issue
402+ if err := ghClient .AddLabelsToIssue (ctx , prMetadata .Repo , prMetadata .Number , pullRequestLabels ); err != nil {
403+ return fmt .Errorf ("failed to add labels to pull request: %w" , err )
404+ }
387405 return nil
388406}
389407
0 commit comments