Skip to content

Commit c232a0b

Browse files
committed
support custom labels
1 parent 3f3fba8 commit c232a0b

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ jobs:
5050
5151
- `--from`: The base branch name. Required.
5252
- `--to`: The target branch name. Required.
53+
- `--labels`: Specify the labels to add to the pull request as a comma-separated list of strings. Optional.
5354

5455
## Environment Variables
5556

@@ -72,7 +73,6 @@ While inspired by git-pr-release, this tool pays homage to its predecessor yet i
7273

7374
# TODO
7475
- [ ] Support a custom template
75-
- [ ] Support custom labels
7676
- [ ] Add more testing
7777

7878
# Release flow

github_client.go

+8
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,11 @@ func (c *GithubClient) UpdatePullRequest(ctx context.Context, prNumber int, titl
125125

126126
return pr, nil
127127
}
128+
129+
func (c *GithubClient) AddLabelsToPullRequest(ctx context.Context, prNumber int, labels []string) error {
130+
if len(labels) == 0 {
131+
return nil
132+
}
133+
_, _, err := c.client.Issues.AddLabelsToIssue(ctx, c.owner, c.repo, prNumber, labels)
134+
return err
135+
}

github_client_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,28 @@ func TestCreatePullRequest_alreadyExists(t *testing.T) {
178178
t.Errorf("PullRequests.List returned %+v, want %+v", created, false)
179179
}
180180
}
181+
182+
func TestAddLabelsToPullRequest(t *testing.T) {
183+
ctx := context.Background()
184+
185+
mux := http.NewServeMux()
186+
187+
mux.HandleFunc(
188+
"/repos/owner/repo/issues/1/labels",
189+
func(w http.ResponseWriter, r *http.Request) {
190+
fmt.Fprint(w, `[]`)
191+
},
192+
)
193+
194+
ts := httptest.NewServer(mux)
195+
defer ts.Close()
196+
197+
apiUrl, _ := url.Parse(ts.URL)
198+
client := NewClient(GithubClientOptions{owner: "owner", repo: "repo", githubToken: "githubToken", apiUrl: apiUrl})
199+
200+
err := client.AddLabelsToPullRequest(ctx, 1, []string{"label1", "label2"})
201+
202+
if err != nil {
203+
t.Errorf("PullRequests.Get returned error: %v", err)
204+
}
205+
}

main.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import (
1212

1313
type Options struct {
1414
// from flag
15-
from string
16-
to string
15+
from string
16+
to string
17+
labels []string
1718

1819
// from env
1920
owner string
@@ -23,8 +24,9 @@ type Options struct {
2324
}
2425

2526
func getOptions() (Options, error) {
26-
from := flag.String("from", "", "source branch")
27-
to := flag.String("to", "", "target branch")
27+
from := flag.String("from", "", "The base branch name.")
28+
to := flag.String("to", "", "The target branch name.")
29+
labelsFlag := flag.String("labels", "", "Specify the labels to add to the pull request as a comma-separated list of strings.")
2830
flag.Parse()
2931

3032
githubToken := os.Getenv("GITHUB_TOKEN")
@@ -35,9 +37,15 @@ func getOptions() (Options, error) {
3537

3638
apiUrl, _ := url.Parse(rawApiUrl)
3739

40+
var labels []string
41+
if labelsFlag != nil {
42+
labels = strings.Split(*labelsFlag, ",")
43+
}
44+
3845
return Options{
3946
from: *from,
4047
to: *to,
48+
labels: labels,
4149
owner: owner,
4250
repo: repo,
4351
gitHubToken: githubToken,
@@ -101,6 +109,14 @@ func run(options Options) error {
101109
logger.Println("The pull request already exists. The body was updated.", pr.GetNumber())
102110
}
103111

112+
if len(options.labels) > 0 {
113+
err := client.AddLabelsToPullRequest(ctx, pr.GetNumber(), options.labels)
114+
if err != nil {
115+
return err
116+
}
117+
logger.Println("Added labels to the pull request.", pr.GetNumber())
118+
}
119+
104120
return nil
105121
}
106122

0 commit comments

Comments
 (0)