Skip to content

Commit 6f6d6d6

Browse files
authored
Labels (#248)
Adds label support for PRs for GitHub and GitLab
1 parent 08d823a commit 6f6d6d6

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

digestabotctl/cmd/files.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ func handlePRForPlatform(platform string, checkout versioncontrol.CheckoutRespon
100100
Diff: commit,
101101
Base: viper.GetString("base"),
102102
Head: viper.GetString("branch"),
103+
Labels: viper.GetStringSlice("labels"),
103104
RepoData: platforms.RepoData{
104105
Repo: viper.GetString("repo"),
105106
Owner: viper.GetString("owner"),

digestabotctl/cmd/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func bindPRFlags(cmd *cobra.Command) {
5757
viper.BindPFlag("signing-token", cmd.Flags().Lookup("signing-token"))
5858
viper.BindPFlag("name", cmd.Flags().Lookup("name"))
5959
viper.BindPFlag("email", cmd.Flags().Lookup("email"))
60+
viper.BindPFlag("labels", cmd.Flags().Lookup("labels"))
6061
}
6162

6263
// prFlags adds the pr flags to the passed in command
@@ -74,4 +75,5 @@ func prFlags(cmd *cobra.Command) {
7475
cmd.PersistentFlags().String("signing-token", "", "OIDC token for signing commit")
7576
cmd.PersistentFlags().String("name", "digestabotctl", "Name for commit")
7677
cmd.PersistentFlags().String("email", "", "Email for commit")
78+
cmd.PersistentFlags().StringSlice("labels", []string{"automated pr", "kind/cleanup", "release-note-none"}, "Labels to apply to the PR")
7779
}

digestabotctl/platforms/github.go

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ type GitHub struct {
1717
}
1818

1919
type GitHubPR struct {
20-
Title string `json:"title"`
21-
Body string `json:"body"`
22-
Head string `json:"head"`
23-
Base string `json:"base"`
20+
Title string `json:"title"`
21+
Body string `json:"body"`
22+
Head string `json:"head"`
23+
Base string `json:"base"`
24+
Labels []string
25+
}
26+
27+
type GitHubPRResponse struct {
28+
URL string `json:"url"`
29+
ID int `json:"id"`
30+
Number int `json:"number"`
31+
}
32+
33+
type GitHubPRLabelRequest struct {
34+
Labels []string `json:"labels"`
2435
}
2536

2637
func (g GitHub) CreatePR(logger *slog.Logger) error {
@@ -39,6 +50,43 @@ func (g GitHub) CreatePR(logger *slog.Logger) error {
3950
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
4051
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", g.Token))
4152

53+
resp, err := sendRequest(req)
54+
if err != nil {
55+
return err
56+
}
57+
logger.Debug(string(resp))
58+
var gr GitHubPRResponse
59+
if err := json.Unmarshal(resp, &gr); err != nil {
60+
return err
61+
}
62+
63+
if len(g.GitHubPR.Labels) == 0 {
64+
return nil
65+
}
66+
67+
return g.CreateLabels(logger, gr.Number)
68+
}
69+
70+
func (g GitHub) CreateLabels(logger *slog.Logger, pr int) error {
71+
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/issues/%d/labels", g.Owner, g.Repo, pr)
72+
labelRequest := GitHubPRLabelRequest{
73+
Labels: g.GitHubPR.Labels,
74+
}
75+
76+
body, err := json.Marshal(labelRequest)
77+
if err != nil {
78+
return err
79+
}
80+
logger.Debug(string(body))
81+
82+
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
83+
if err != nil {
84+
return err
85+
}
86+
req.Header.Add("Accept", "application/vnd.github+json")
87+
req.Header.Add("X-GitHub-Api-Version", "2022-11-28")
88+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", g.Token))
89+
4290
resp, err := sendRequest(req)
4391
if err != nil {
4492
return err
@@ -60,10 +108,11 @@ func NewGitHub(pr PullRequest) (PRCreator, error) {
60108
Repo: pr.Repo,
61109
Token: pr.Token,
62110
GitHubPR: GitHubPR{
63-
Title: pr.Title,
64-
Body: buf.String(),
65-
Head: pr.Head,
66-
Base: pr.Base,
111+
Title: pr.Title,
112+
Body: buf.String(),
113+
Head: pr.Head,
114+
Base: pr.Base,
115+
Labels: pr.Labels,
67116
},
68117
}, nil
69118
}

digestabotctl/platforms/gitlab.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"log/slog"
88
"net/http"
9+
"strings"
910
"text/template"
1011
)
1112

@@ -21,6 +22,7 @@ type GitLabMR struct {
2122
Description string `json:"description"`
2223
Source string `json:"source_branch"`
2324
Target string `json:"target_branch"`
25+
Labels string `json:"labels"`
2426
}
2527

2628
func (g GitLab) CreatePR(logger *slog.Logger) error {
@@ -64,6 +66,7 @@ func NewGitLab(pr PullRequest) (PRCreator, error) {
6466
Description: buf.String(),
6567
Source: pr.Head,
6668
Target: pr.Base,
69+
Labels: strings.Join(pr.Labels, ","),
6770
},
6871
}, nil
6972
}

digestabotctl/platforms/platforms.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type PullRequest struct {
5454
Diff string
5555
Base string
5656
Head string
57+
Labels []string
5758
RepoData
5859
}
5960

0 commit comments

Comments
 (0)