-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
diff.go
36 lines (30 loc) · 910 Bytes
/
diff.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package github
import (
"fmt"
"io"
"strings"
)
const maxDiffSize = 1 * 1024 * 1024
func (c *client) GetDiff(repo string, sha string) (string, error) {
return c.getDiff("GetDiff", fmt.Sprintf("/repos/%s/commits/%s", repo, sha))
}
func (c *client) GetCompare(repo string, base, head string) (string, error) {
return c.getDiff("GetCompare", fmt.Sprintf("/repos/%s/compare/%s...%s", repo, base, head))
}
func (c *client) getDiff(name, path string) (string, error) {
req, err := c.request("GET", c.url(path), nil)
if err != nil {
return "", err
}
req.Header.Add("Accept", "application/vnd.github.v3.diff")
res, _, err := c.doReq(req)
if err != nil {
return "", fmt.Errorf("%s %s: %w", name, strings.TrimPrefix(path, "/repos/"), err)
}
defer res.Body.Close()
bs, err := io.ReadAll(&io.LimitedReader{R: res.Body, N: maxDiffSize})
if err != nil {
return "", err
}
return string(bs), nil
}