-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathgithub.go
More file actions
125 lines (108 loc) · 3.3 KB
/
github.go
File metadata and controls
125 lines (108 loc) · 3.3 KB
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
package github
import (
"context"
"errors"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
)
var (
repoOwner = flag.String("github_repo_owner", "", "the owner user/organization to use for github api requests")
repo = flag.String("github_repo", "", "the repo to use for github api requests")
pat = flag.String("github_access_token", os.Getenv("GITHUB_TOKEN"), "the access token to authenticate requests")
githubEnterpriseHost = flag.String("github_enterprise_host", "", "The host name of the private enterprise github, e.g. git.corp.adobe.com")
)
func updateExistingPR(ctx context.Context, gh *github.Client, owner, repo, from, to, title, body string) error {
log.Println("PR already exists, updating title and body...")
// List PRs to find the existing one
listOpts := &github.PullRequestListOptions{
State: "open",
Head: owner + ":" + from,
Base: to,
}
prs, _, err := gh.PullRequests.List(ctx, owner, repo, listOpts)
if err != nil {
log.Println("Error listing PRs:", err)
return err
}
if len(prs) == 0 {
return errors.New("could not find open PR matching the head and base branches")
}
// Update the first matching PR
existingPR := prs[0]
updatePR := &github.PullRequest{
Title: &title,
Body: &body,
}
updatedPR, _, err := gh.PullRequests.Edit(ctx, owner, repo, *existingPR.Number, updatePR)
if err != nil {
log.Println("Error updating PR:", err)
return err
}
if updatedPR.URL != nil {
log.Println("Updated existing PR:", *updatedPR.URL)
} else {
log.Println("Updated existing PR #", *existingPR.Number)
}
return nil
}
func CreatePR(from, to, title, body string) error {
if *repoOwner == "" {
return errors.New("github_repo_owner must be set")
}
if *repo == "" {
return errors.New("github_repo must be set")
}
if *pat == "" {
return errors.New("github_access_token must be set")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: *pat},
)
tc := oauth2.NewClient(ctx, ts)
var gh *github.Client
if *githubEnterpriseHost != "" {
baseUrl := "https://" + *githubEnterpriseHost + "/api/v3/"
uploadUrl := "https://" + *githubEnterpriseHost + "/api/uploads/"
var err error
gh, err = github.NewEnterpriseClient(baseUrl, uploadUrl, tc)
if err != nil {
log.Println("Error in creating github client", err)
return nil
}
} else {
gh = github.NewClient(tc)
}
pr := &github.NewPullRequest{
Title: &title,
Head: &from,
Base: &to,
Body: &body,
Issue: nil,
MaintainerCanModify: new(bool),
Draft: new(bool),
}
createdPr, resp, err := gh.PullRequests.Create(ctx, *repoOwner, *repo, pr)
if err == nil {
log.Println("Created PR: ", *createdPr.URL)
return err
}
if resp.StatusCode == http.StatusUnprocessableEntity {
// Handle the case: "Create PR" request fails because it already exists
return updateExistingPR(ctx, gh, *repoOwner, *repo, from, to, title, body)
}
// All other github responses
defer resp.Body.Close()
responseBody, readingErr := ioutil.ReadAll(resp.Body)
if readingErr != nil {
log.Println("cannot read response body")
} else {
log.Println("github response: ", string(responseBody))
}
return err
}