-
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.
- Loading branch information
1 parent
4ff2ced
commit bbec65a
Showing
3 changed files
with
94 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
_ "github.com/breml/rootcerts" | ||
"github.com/google/go-github/v48/github" | ||
"github.com/madetech/sparkling-dependencies/internal/dealWithPullRequest" | ||
"golang.org/x/oauth2" | ||
"github.com/madetech/sparkling-dependencies/internal/github" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type GitHubPresenter struct { | ||
client *github.Client | ||
} | ||
|
||
func (g GitHubPresenter) Exit() { | ||
os.Exit(0) | ||
} | ||
|
||
func (g GitHubPresenter) PostComment(comment dealWithPullRequest.Comment) { | ||
splitRepository := strings.Split(comment.Repository, "/") | ||
owner := splitRepository[0] | ||
repo := splitRepository[1] | ||
context.Background() | ||
_, _, err := g.client.Issues.CreateComment(context.Background(), owner, repo, int(comment.Number), &github.IssueComment{ | ||
Body: &comment.Body, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func main() { | ||
eventPayloadPath, _ := os.LookupEnv("GITHUB_EVENT_PATH") | ||
token, _ := os.LookupEnv("INPUT_GITHUB-TOKEN") | ||
useCase := dealWithPullRequest.New(github.GetEvent()) | ||
|
||
ctx := context.Background() | ||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: token}, | ||
) | ||
tc := oauth2.NewClient(ctx, ts) | ||
|
||
client := github.NewClient(tc) | ||
|
||
file := readFile(eventPayloadPath) | ||
|
||
var data struct { | ||
Sender struct { | ||
Login string `json:"login"` | ||
} `json:"sender"` | ||
PullRequest struct { | ||
Number uint32 `json:"number"` | ||
Title string `json:"title"` | ||
} `json:"pull_request"` | ||
Repository struct { | ||
FullName string `json:"full_name"` | ||
} `json:"repository"` | ||
} | ||
err := json.Unmarshal(file, &data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
useCase := dealWithPullRequest.New(dealWithPullRequest.Event{ | ||
Name: "pull_request_target", | ||
PullRequest: &dealWithPullRequest.PullRequest{ | ||
Sender: data.Sender.Login, | ||
Number: data.PullRequest.Number, | ||
Repository: data.Repository.FullName, | ||
Title: data.PullRequest.Title, | ||
}, | ||
}) | ||
useCase.Execute(GitHubPresenter{client: client}) | ||
} | ||
|
||
func readFile(eventPayloadPath string) []byte { | ||
file, err := os.ReadFile(eventPayloadPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return file | ||
token, _ := os.LookupEnv("INPUT_GITHUB-TOKEN") | ||
useCase.Execute(github.New(token)) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package github | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/madetech/sparkling-dependencies/internal/dealWithPullRequest" | ||
"os" | ||
) | ||
|
||
func GetEvent() dealWithPullRequest.Event { | ||
eventPayloadPath, _ := os.LookupEnv("GITHUB_EVENT_PATH") | ||
file := readFile(eventPayloadPath) | ||
|
||
var data struct { | ||
Sender struct { | ||
Login string `json:"login"` | ||
} `json:"sender"` | ||
PullRequest struct { | ||
Number uint32 `json:"number"` | ||
Title string `json:"title"` | ||
} `json:"pull_request"` | ||
Repository struct { | ||
FullName string `json:"full_name"` | ||
} `json:"repository"` | ||
} | ||
err := json.Unmarshal(file, &data) | ||
if err != nil { | ||
panic(err) | ||
} | ||
event := dealWithPullRequest.Event{ | ||
Name: "pull_request_target", | ||
PullRequest: &dealWithPullRequest.PullRequest{ | ||
Sender: data.Sender.Login, | ||
Number: data.PullRequest.Number, | ||
Repository: data.Repository.FullName, | ||
Title: data.PullRequest.Title, | ||
}, | ||
} | ||
return event | ||
} | ||
|
||
func readFile(eventPayloadPath string) []byte { | ||
file, err := os.ReadFile(eventPayloadPath) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return file | ||
} |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"github.com/google/go-github/v48/github" | ||
"github.com/madetech/sparkling-dependencies/internal/dealWithPullRequest" | ||
"golang.org/x/oauth2" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type GitHubPresenter struct { | ||
Client *github.Client | ||
} | ||
|
||
func (g GitHubPresenter) Exit() { | ||
os.Exit(0) | ||
} | ||
|
||
func (g GitHubPresenter) PostComment(comment dealWithPullRequest.Comment) { | ||
splitRepository := strings.Split(comment.Repository, "/") | ||
owner := splitRepository[0] | ||
repo := splitRepository[1] | ||
context.Background() | ||
_, _, err := g.Client.Issues.CreateComment(context.Background(), owner, repo, int(comment.Number), &github.IssueComment{ | ||
Body: &comment.Body, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func New(token string) GitHubPresenter { | ||
ctx := context.Background() | ||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: token}, | ||
) | ||
tc := oauth2.NewClient(ctx, ts) | ||
|
||
client := github.NewClient(tc) | ||
presenter := GitHubPresenter{Client: client} | ||
return presenter | ||
} |