-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubpr.go
127 lines (105 loc) · 3.27 KB
/
githubpr.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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-github/v57/github"
)
func getPRDetails(client *github.Client, owner string, repo string, number int) string {
pr, _, err := client.PullRequests.Get(context.Background(), owner, repo, number)
if err != nil {
panic(err)
}
files, _, err := client.PullRequests.ListFiles(context.Background(), owner, repo, number, nil)
if err != nil {
panic(err)
}
fileDetails := make([]map[string]interface{}, len(files))
for i, file := range files {
fileDetail := map[string]interface{}{
"filename": *file.Filename,
"additions": *file.Additions,
"deletions": *file.Deletions,
}
fileDetails[i] = fileDetail
}
compareCommitsRaw, _, err := client.Repositories.CompareCommitsRaw(context.Background(), owner, repo, *pr.Base.Ref, *pr.Head.Ref, github.RawOptions{Type: github.Diff})
if err != nil {
panic(err)
}
commitsDiff := string(compareCommitsRaw)
prDescription := *pr.Body
prTitle := *pr.Title
prState := *pr.State
prURL := *pr.HTMLURL
prUser := *pr.User.Login
prBranch := *pr.Head.Ref
prBaseBranch := *pr.Base.Ref
prDiffURL := *pr.DiffURL
if prState == "closed" {
return "skip"
}
if prDescription == "" {
prDescription = "No description"
}
if prTitle == "" {
prTitle = "No title"
}
prJSON := map[string]interface{}{
"description": prDescription,
"title": prTitle,
"state": prState,
"url": prURL,
"user": prUser,
"branch": prBranch,
"baseBranch": prBaseBranch,
"diffURL": prDiffURL,
"files": fileDetails,
"commitsDiff": commitsDiff,
}
jsonData, err := json.Marshal(prJSON)
if err != nil {
fmt.Println("Error:", err)
}
fmt.Println(string(jsonData))
return string(jsonData)
}
func AddLabelsToIssue(client *github.Client, owner string, repo string, number int, labels []string) error {
_, _, err := client.Issues.AddLabelsToIssue(context.Background(), owner, repo, number, labels)
if err != nil {
return err
}
fmt.Println("Added labels to issue:", owner, repo, number)
return nil
}
func RemoveLabelFromIssue(client *github.Client, owner string, repo string, number int, label string) error {
_, err := client.Issues.RemoveLabelForIssue(context.Background(), owner, repo, number, label)
if err != nil {
return err
}
fmt.Println("Removed labels from issue:", owner, repo, number)
return nil
}
func AddReviewersToPR(client *github.Client, owner string, repo string, number int, reviewers []string) error {
_, _, err := client.PullRequests.RequestReviewers(context.Background(), owner, repo, number, github.ReviewersRequest{Reviewers: reviewers})
if err != nil {
return err
}
fmt.Println("Added reviewers to PR:", owner, repo, number)
return nil
}
func AddCommntToPR(client *github.Client, owner string, repo string, number int, comment string) error {
_, _, err := client.Issues.CreateComment(context.Background(), owner, repo, number, &github.IssueComment{
Body: &comment,
})
if err != nil {
return err
}
fmt.Println("Added comment to PR:", owner, repo, number)
return nil
}
func handlePR(user string, repo string, prNumber int) (string, string, int, error) {
fmt.Println("Webhook received for PR")
fmt.Printf("Repo: %s, Owner: %s, PR Number: %d\n", repo, user, prNumber)
return user, repo, prNumber, nil
}