Skip to content

Commit dfb55c9

Browse files
authored
fix: pullrequest nil in issue comment (#5)
1 parent 15fb190 commit dfb55c9

File tree

4 files changed

+53
-24
lines changed

4 files changed

+53
-24
lines changed

pkg/github/controller.go

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ func (w *Worker) AddLabels(owner string, repo string, number int, labels []strin
3939
return resp, err
4040
}
4141

42+
func (w *Worker) GetCheckRun(owner string, repo string, checkrunId int64) (*v41.CheckRun, error) {
43+
resp, _, err := w.Client.Checks.GetCheckRun(context.TODO(), owner, repo, checkrunId)
44+
if err != nil {
45+
log.Printf("error creating checkrun: %v\n", err)
46+
}
47+
return resp, err
48+
}
49+
4250
func (w *Worker) CreateCheckRun(owner string, repo string, checkrun v41.CreateCheckRunOptions) (*v41.CheckRun, error) {
4351
resp, _, err := w.Client.Checks.CreateCheckRun(context.TODO(), owner, repo, checkrun)
4452
if err != nil {

pkg/github/issue.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package github
33
import (
44
"fmt"
55
"log"
6+
"strconv"
67
"strings"
78

89
ghwebhooks "github.com/go-playground/webhooks/v6/github"
@@ -15,24 +16,31 @@ func (w *Worker) processIssueComment(owner *string, pullRequest *v41.PullRequest
1516
}
1617

1718
func (w *Worker) approve(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.IssueCommentPayload) {
18-
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.ApproveCommand) {
19-
message := fmt.Sprintf("[%s] Looks Good To Me!", p.Issue.User.Login)
20-
_, err := w.PullRequestCreateReview(*owner, p.Repository.Name, *pullRequest.Number, v41.PullRequestReviewRequest{
21-
Body: &message,
22-
Event: v41.String("APPROVE"),
23-
})
24-
if err != nil {
25-
log.Printf("error creview: %v\n", err)
19+
if pullRequest != nil {
20+
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.ApproveCommand) {
21+
message := fmt.Sprintf("[%s] Looks Good To Me!", p.Issue.User.Login)
22+
_, err := w.PullRequestCreateReview(*owner, p.Repository.Name, *pullRequest.Number, v41.PullRequestReviewRequest{
23+
Body: &message,
24+
Event: v41.String("APPROVE"),
25+
})
26+
if err != nil {
27+
log.Printf("error creview: %v\n", err)
28+
}
2629
}
2730
}
2831
}
2932

3033
func (w *Worker) reRunLaboratoryTest(owner *string, pullRequest *v41.PullRequest, p *ghwebhooks.IssueCommentPayload) {
31-
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.ReRunTestSuiteCommand) {
32-
w.CreateCheckRun(*owner, p.Repository.Name, v41.CreateCheckRunOptions{
33-
Name: "Laboratory test",
34-
Status: v41.String("in_progress"),
35-
HeadSHA: *pullRequest.Head.SHA,
36-
})
34+
if pullRequest != nil {
35+
if strings.EqualFold(p.Comment.Body, w.Config.Layout.PullRequest.ReRunTestSuiteCommand) {
36+
pullRequestNumber := strconv.Itoa(int(*pullRequest.Number))
37+
w.CreateCheckRun(*owner, p.Repository.Name, v41.CreateCheckRunOptions{
38+
Name: "Laboratory test",
39+
Status: v41.String("in_progress"),
40+
HeadSHA: *pullRequest.Head.SHA,
41+
DetailsURL: pullRequest.HTMLURL,
42+
ExternalID: &pullRequestNumber,
43+
})
44+
}
3745
}
3846
}

pkg/github/pull_request.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
package github
22

33
import (
4+
"strconv"
5+
46
ghwebhooks "github.com/go-playground/webhooks/v6/github"
57
v41 "github.com/google/go-github/v41/github"
68
)
79

810
func (w *Worker) processPullRequest(owner *string, p *ghwebhooks.PullRequestPayload) {
911
if p.Action == "opened" || p.Action == "reopened" || p.Action == "edited" {
12+
pullRequestNumber := strconv.Itoa(int(p.PullRequest.Number))
1013
w.CreateCheckRun(*owner, p.Repository.Name, v41.CreateCheckRunOptions{
11-
Name: "Laboratory test",
12-
Status: v41.String("in_progress"),
13-
HeadSHA: p.PullRequest.Head.Sha,
14+
Name: "Laboratory test",
15+
Status: v41.String("in_progress"),
16+
HeadSHA: p.PullRequest.Head.Sha,
17+
DetailsURL: &p.PullRequest.HTMLURL,
18+
ExternalID: &pullRequestNumber,
1419
})
1520
}
1621
}

pkg/github/worker.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ func (w *Worker) processIssueCommentEvent(p *ghwebhooks.IssueCommentPayload) {
1818
var pullRequest *v41.PullRequest
1919
owner, _ := getOwner(&w.Config)
2020
if len(p.Issue.PullRequest.HTMLURL) > 0 {
21-
pullRequestId, err := strconv.Atoi(strings.Split(p.Issue.PullRequest.HTMLURL, "/")[6])
22-
if err == nil {
23-
pullRequest, err = w.GetPullRequest(*owner, p.Repository.Name, pullRequestId)
21+
if pullRequestNumber, err := strconv.Atoi(strings.Split(p.Issue.PullRequest.HTMLURL, "/")[6]); err != nil {
22+
pullRequest, err = w.GetPullRequest(*owner, p.Repository.Name, pullRequestNumber)
2423
if err != nil {
2524
return
2625
}
@@ -32,11 +31,20 @@ func (w *Worker) processIssueCommentEvent(p *ghwebhooks.IssueCommentPayload) {
3231

3332
func (w *Worker) processCheckRunEvent(p *ghwebhooks.CheckRunPayload) {
3433
if p.CheckRun.App.ID == int64(w.Config.Github.AppID) {
34+
var pullRequest *v41.PullRequest
3535
owner, _ := getOwner(&w.Config)
36-
pullRequest, err := w.GetPullRequest(*owner, p.Repository.Name, int(p.CheckRun.PullRequests[0].Number))
37-
if err != nil {
38-
return
39-
}
36+
if len(p.CheckRun.PullRequests) > 0 {
37+
pullRequest, _ = w.GetPullRequest(*owner, p.Repository.Name, int(p.CheckRun.PullRequests[0].Number))
38+
} else {
39+
chekRun, err := w.GetCheckRun(*owner, p.Repository.Name, p.CheckRun.ID)
40+
if err != nil {
41+
return
42+
}
43+
if len(*chekRun.ExternalID) > 0 {
44+
pullRequestId, _ := strconv.Atoi(*chekRun.ExternalID)
45+
pullRequest, _ = w.GetPullRequest(*owner, p.Repository.Name, pullRequestId)
46+
}
47+
}
4048
w.processCheckRun(owner, pullRequest, p)
4149
}
4250
}

0 commit comments

Comments
 (0)