-
Notifications
You must be signed in to change notification settings - Fork 6
/
github.go
77 lines (59 loc) · 1.39 KB
/
github.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package internal
import (
"context"
"errors"
"io"
"net/http"
"strings"
"time"
"github.com/google/go-github/v57/github"
)
func ParseGithubPullRequestURL(pullRequestURL string) (string, string, string, error) {
parts := strings.Split(pullRequestURL, "/")
if len(parts) != 7 {
return "", "", "", errors.New("invalid pull request URL")
}
owner := parts[3]
repo := parts[4]
prNumber := parts[6]
return owner, repo, prNumber, nil
}
func GetPullRequest(owner string, repo string, prNumber int, callback OnGetPullRequestEvent) error {
timeout, err := GetContextTimeout()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
client := github.NewClient(nil)
pullRequest, _, err := client.PullRequests.Get(ctx, owner, repo, prNumber)
if err != nil {
return err
}
pullRequest.GetDiffURL()
ch := make(chan string)
go getDiffContents(ch, pullRequest.GetDiffURL())
contents := <-ch
callback(contents)
return nil
}
func getDiffContents(c chan<- string, diffURL string) {
diffContents, err := http.Get(diffURL)
if err != nil {
c <- err.Error()
return
}
bodyBytes, err := io.ReadAll(diffContents.Body)
if err != nil {
c <- err.Error()
return
}
c <- string(bodyBytes)
}
func GetContextTimeout() (time.Duration, error) {
cfg, err := GetConfig()
if err != nil {
return 0, err
}
return cfg.ContextTimeout, nil
}