Skip to content

Commit 35faae7

Browse files
committed
Add unit tests for getting a size of a pull request
1 parent 98e744f commit 35faae7

File tree

5 files changed

+169
-7
lines changed

5 files changed

+169
-7
lines changed

cmd/gh-actions-pr-size/github.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@ func getAllPullRequestFiles(
4242
return res, nil
4343
}
4444

45-
// getPullRequestSize determines a size of a pull request by calculating a total number of changed lines.
46-
func getPullRequestSize(
45+
// getPullRequestSize returns the total number of changed lines of the specified pull request.
46+
func getPullRequestChangedLines(
4747
ctx context.Context,
4848
client *github.Client,
4949
owner, repo string,
5050
number int,
51-
) (size, error) {
51+
) (int, error) {
5252
files, err := getAllPullRequestFiles(ctx, client, owner, repo, number)
5353
if err != nil {
54-
return sizeUnknown, fmt.Errorf("get all commit files: %w", err)
54+
return 0, fmt.Errorf("get all commit files: %w", err)
5555
}
5656

5757
// TODO(kkohtaka): Filter out linguist-generated files
@@ -60,7 +60,7 @@ func getPullRequestSize(
6060
for _, file := range files {
6161
change += *file.Additions + *file.Deletions
6262
}
63-
return newSize(change), nil
63+
return change, nil
6464
}
6565

6666
// setLabelOnPullRequest checks the current labels on the pull request. If there exists a label for pull request size,

cmd/gh-actions-pr-size/github_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
"strconv"
8+
"strings"
9+
"testing"
10+
11+
"github.com/google/go-github/v29/github"
12+
"github.com/jarcoal/httpmock"
13+
"github.com/stretchr/testify/assert"
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestGetPullRequestChangedLines(t *testing.T) {
18+
tcs := []struct {
19+
name string
20+
commitFiles [][]*github.CommitFile
21+
want int
22+
}{
23+
{
24+
name: "The target pull request changes a single file.",
25+
commitFiles: [][]*github.CommitFile{
26+
{
27+
{
28+
Additions: github.Int(100),
29+
Deletions: github.Int(200),
30+
},
31+
},
32+
},
33+
want: 300,
34+
},
35+
{
36+
name: "The target pull request changes multiple files.",
37+
commitFiles: [][]*github.CommitFile{
38+
{
39+
{
40+
Additions: github.Int(100),
41+
Deletions: github.Int(200),
42+
},
43+
{
44+
Additions: github.Int(300),
45+
Deletions: github.Int(400),
46+
},
47+
},
48+
},
49+
want: 1000,
50+
},
51+
{
52+
name: "The target pull request changes multiple files with pagenation.",
53+
commitFiles: [][]*github.CommitFile{
54+
{
55+
{
56+
Additions: github.Int(100),
57+
Deletions: github.Int(200),
58+
},
59+
},
60+
{
61+
{
62+
Additions: github.Int(300),
63+
Deletions: github.Int(400),
64+
},
65+
},
66+
{
67+
{
68+
Additions: github.Int(500),
69+
Deletions: github.Int(600),
70+
},
71+
},
72+
},
73+
want: 2100,
74+
},
75+
}
76+
for _, tt := range tcs {
77+
t.Run(tt.name, func(t *testing.T) {
78+
client := &http.Client{}
79+
httpmock.ActivateNonDefault(client)
80+
defer httpmock.DeactivateAndReset()
81+
82+
const baseURL = "https://api.github.com/repos/kkohtaka/gh-actions-pr-size/pulls/42/files"
83+
httpmock.RegisterResponder(
84+
"GET",
85+
baseURL,
86+
func(req *http.Request) (*http.Response, error) {
87+
page := 1
88+
if v, ok := req.URL.Query()["page"]; ok && len(v) > 0 {
89+
if v, err := strconv.Atoi(v[0]); err == nil {
90+
page = v
91+
}
92+
}
93+
if page-1 >= len(tt.commitFiles) {
94+
return nil, fmt.Errorf("invalid query value")
95+
}
96+
97+
var links []string
98+
links = append(links, fmt.Sprintf(`<%s?page=%d>; rel="first"`, baseURL, 1))
99+
if page-1 >= 1 {
100+
links = append(links, fmt.Sprintf(`<%s?page=%d>; rel="prev"`, baseURL, page-1))
101+
}
102+
if page+1 <= len(tt.commitFiles) {
103+
links = append(links, fmt.Sprintf(`<%s?page=%d>; rel="next"`, baseURL, page+1))
104+
}
105+
links = append(links, fmt.Sprintf(`<%s?page=%d>; rel="last"`, baseURL, len(tt.commitFiles)))
106+
107+
resp, err := httpmock.NewJsonResponse(200, tt.commitFiles[page-1])
108+
if err != nil {
109+
return nil, err
110+
}
111+
resp.Header.Set("Content-Type", "application/json")
112+
resp.Header.Set("Link", strings.Join(links, ", "))
113+
return resp, nil
114+
},
115+
)
116+
117+
got, err := getPullRequestChangedLines(
118+
context.Background(),
119+
github.NewClient(client),
120+
"kkohtaka",
121+
"gh-actions-pr-size",
122+
42,
123+
)
124+
require.NoError(t, err)
125+
assert.Equal(t, tt.want, got)
126+
})
127+
}
128+
}
129+
130+
func TestGetPullRequestChangedLinesReturnsError(t *testing.T) {
131+
client := &http.Client{}
132+
httpmock.ActivateNonDefault(client)
133+
defer httpmock.DeactivateAndReset()
134+
135+
const baseURL = "https://api.github.com/repos/kkohtaka/gh-actions-pr-size/pulls/42/files"
136+
httpmock.RegisterResponder(
137+
"GET",
138+
baseURL,
139+
httpmock.NewErrorResponder(fmt.Errorf("test for error handling")),
140+
)
141+
142+
got, err := getPullRequestChangedLines(
143+
context.Background(),
144+
github.NewClient(client),
145+
"kkohtaka",
146+
"gh-actions-pr-size",
147+
42,
148+
)
149+
assert.ErrorContains(t, err, "get all commit files: list commit files: ")
150+
assert.Zero(t, got)
151+
}

cmd/gh-actions-pr-size/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ func main() {
6464
}
6565
client := github.NewClient(tc)
6666

67-
size, err := getPullRequestSize(ctx, client, owner, repo, number)
67+
changed, err := getPullRequestChangedLines(ctx, client, owner, repo, number)
6868
if err != nil {
69-
logger.Fatal("Could not get pull request size", zap.Error(err))
69+
logger.Fatal("Could not get the total number of changed lines in the pull request", zap.Error(err))
7070
}
71+
72+
size := newSize(changed)
7173
logger.Info("Got a size of a pull request", zap.String("size", size.String()))
7274

7375
err = setLabelOnPullRequest(ctx, client, owner, repo, number, size)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.14
44

55
require (
66
github.com/google/go-github/v29 v29.0.3
7+
github.com/jarcoal/httpmock v1.2.0
8+
github.com/stretchr/testify v1.7.1
79
go.uber.org/zap v1.21.0
810
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
911
)

go.sum

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,26 @@ github.com/google/go-github/v29 v29.0.3 h1:IktKCTwU//aFHnpA+2SLIi7Oo9uhAzgsdZNbc
1111
github.com/google/go-github/v29 v29.0.3/go.mod h1:CHKiKKPHJ0REzfwc14QMklvtHwCveD0PxlMjLlzAM5E=
1212
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
1313
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
14+
github.com/jarcoal/httpmock v1.2.0 h1:gSvTxxFR/MEMfsGrvRbdfpRUMBStovlSRLw0Ep1bwwc=
15+
github.com/jarcoal/httpmock v1.2.0/go.mod h1:oCoTsnAz4+UoOUIf5lJOWV2QQIW5UoeUI6aM2YnWAZk=
1416
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
1517
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
1618
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1719
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1820
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
21+
github.com/maxatome/go-testdeep v1.11.0 h1:Tgh5efyCYyJFGUYiT0qxBSIDeXw0F5zSoatlou685kk=
22+
github.com/maxatome/go-testdeep v1.11.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
1923
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
2024
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
2125
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2226
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
27+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
2328
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2429
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
2530
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
2631
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
32+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
33+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2734
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
2835
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
2936
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=

0 commit comments

Comments
 (0)