-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to use 3rd party GitHub and GitLab packages (#25)
- Loading branch information
Showing
10 changed files
with
128 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,55 @@ | ||
package github | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"context" | ||
"log" | ||
"net/http" | ||
"runtime/debug" | ||
|
||
"github.com/emmahsax/go-git-helper/internal/configfile" | ||
"github.com/google/go-github/v56/github" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
type GitHub struct { | ||
Debug bool | ||
Debug bool | ||
Client *github.Client | ||
} | ||
|
||
type Response struct { | ||
HtmlURL string `json:"html_url"` | ||
Message string `json:"message"` | ||
Errors []struct { | ||
Resource string `json:"resource"` | ||
Code string `json:"code"` | ||
Message string `json:"message"` | ||
} `json:"errors"` | ||
} | ||
func NewGitHub(debugB bool) *GitHub { | ||
cf := configfile.NewConfigFile(debugB) | ||
c := newGitHubClient(cf.GitHubToken()) | ||
|
||
func NewGitHub(debug bool) *GitHub { | ||
return &GitHub{ | ||
Debug: debug, | ||
Debug: debugB, | ||
Client: c, | ||
} | ||
} | ||
|
||
func (c *GitHub) CreatePullRequest(repoName string, options map[string]interface{}) interface{} { | ||
return c.run(repoName, "POST", "/repos/"+repoName+"/pulls", options) | ||
} | ||
|
||
func (c *GitHub) run(username, requestType, curlURL string, payload map[string]interface{}) interface{} { | ||
var result Response | ||
jsonBytes, err := json.Marshal(payload) | ||
if err != nil { | ||
if c.Debug { | ||
debug.PrintStack() | ||
} | ||
log.Fatal(err) | ||
return result | ||
func (c *GitHub) CreatePullRequest(owner, repo string, options map[string]string) (*github.PullRequest, error) { | ||
createOpts := &github.NewPullRequest{ | ||
Base: github.String(options["base"]), | ||
Body: github.String(options["body"]), | ||
Head: github.String(options["head"]), | ||
MaintainerCanModify: github.Bool(true), | ||
Title: github.String(options["title"]), | ||
} | ||
req, err := http.NewRequest(requestType, "https://api.github.com"+curlURL, bytes.NewBuffer(jsonBytes)) | ||
if err != nil { | ||
if c.Debug { | ||
debug.PrintStack() | ||
} | ||
log.Fatal(err) | ||
return result | ||
} | ||
cf := configfile.NewConfigFile(c.Debug) | ||
req.Header.Set("Authorization", "token "+cf.GitHubToken()) | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
pr, _, err := c.Client.PullRequests.Create(context.Background(), owner, repo, createOpts) | ||
if err != nil { | ||
if c.Debug { | ||
debug.PrintStack() | ||
} | ||
log.Fatal(err) | ||
return result | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, _ := io.ReadAll(resp.Body) | ||
if err := json.Unmarshal(body, &result); err != nil { | ||
if c.Debug { | ||
debug.PrintStack() | ||
} | ||
log.Fatal("Cannot unmarshal JSON") | ||
return err | ||
} | ||
return pr, nil | ||
} | ||
|
||
return result | ||
func newGitHubClient(token string) *github.Client { | ||
ctx := context.Background() | ||
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) | ||
tc := oauth2.NewClient(ctx, ts) | ||
git := github.NewClient(tc) | ||
return git | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.