forked from tiborvass/gh-patch-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
131 lines (109 loc) · 3.36 KB
/
utils.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"os/exec"
"strings"
"github.com/Sirupsen/logrus"
"github.com/crosbymichael/octokat"
)
func getRepo(repo *octokat.Repository) octokat.Repo {
return getRepoWithOwner(repo.Name, repo.Owner.Login)
}
func getRepoWithOwner(name, owner string) octokat.Repo {
return octokat.Repo{
Name: name,
UserName: owner,
}
}
func addLabel(gh *octokat.Client, repo octokat.Repo, issueNum int, labels ...string) error {
issue := octokat.Issue{
Number: issueNum,
}
return gh.ApplyLabel(repo, &issue, labels)
}
func removeLabel(gh *octokat.Client, repo octokat.Repo, issueNum int, labels ...string) error {
issue := octokat.Issue{
Number: issueNum,
}
for _, label := range labels {
return gh.RemoveLabel(repo, &issue, label)
}
return nil
}
// add the comment if it does not exist already
func addComment(gh *octokat.Client, repo octokat.Repo, prNum, comment, commentType string) error {
// get the comments
comments, err := gh.Comments(repo, prNum, &octokat.Options{})
if err != nil {
return err
}
// check if we already made the comment
for _, c := range comments {
// if we already made the comment return nil
if strings.ToLower(c.User.Login) == "gordontheturtle" && strings.Contains(c.Body, commentType) {
logrus.Debugf("Already made comment about %q on PR %s", commentType, prNum)
return nil
}
}
// add the comment because we must not have already made it
if _, err := gh.AddComment(repo, prNum, comment); err != nil {
return err
}
logrus.Infof("Would have added comment about %q PR %s", commentType, prNum)
return nil
}
func removeComment(gh *octokat.Client, repo octokat.Repo, prNum, commentType string) error {
// get the comments
comments, err := gh.Comments(repo, prNum, &octokat.Options{})
if err != nil {
return err
}
// check if we already made the comment
for _, c := range comments {
// if we already made the comment return nil
if strings.ToLower(c.User.Login) == "gordontheturtle" && strings.Contains(c.Body, commentType) {
return gh.RemoveComment(repo, c.Id)
}
}
return nil
}
func fetchPullRequest(temp, repo string, prNum int) error {
// don't clone the whole repo
// it's too slow
cmd := exec.Command("git", "init", "--bare")
cmd.Dir = temp
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Running command failed: %s, %v", string(output), err)
}
cmd = exec.Command("git", "remote", "add", "origin", repo)
cmd.Dir = temp
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Running command failed: %s, %v", string(output), err)
}
// fetch the PR
cmd = exec.Command("git", "fetch", "origin", fmt.Sprintf("+refs/pull/%d/head:refs/remotes/origin/pr/%d", prNum, prNum))
output, err = cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("Running command failed: %s, %v", string(output), err)
}
return nil
}
func successStatus(gh *octokat.Client, repo octokat.Repo, sha, context, description string) error {
_, err := gh.SetStatus(repo, sha, &octokat.StatusOptions{
State: "success",
Context: context,
Description: description,
})
return err
}
func failureStatus(gh *octokat.Client, repo octokat.Repo, sha, context, description, targetURL string) error {
_, err := gh.SetStatus(repo, sha, &octokat.StatusOptions{
State: "failure",
Context: context,
Description: description,
URL: targetURL,
})
return err
}