From bbec65a07b6216ff55a6516a1ec9e33a70c7baf4 Mon Sep 17 00:00:00 2001 From: "Craig J. Bass" <1889973+craigjbass@users.noreply.github.com> Date: Tue, 15 Nov 2022 10:27:31 +0000 Subject: [PATCH] Refactor: Extract github specifics --- cmd/action/main.go | 76 ++-------------------------------- internal/github/actionEvent.go | 47 +++++++++++++++++++++ internal/github/presenter.go | 43 +++++++++++++++++++ 3 files changed, 94 insertions(+), 72 deletions(-) create mode 100644 internal/github/actionEvent.go create mode 100644 internal/github/presenter.go diff --git a/cmd/action/main.go b/cmd/action/main.go index c73d87b..41e73c0 100644 --- a/cmd/action/main.go +++ b/cmd/action/main.go @@ -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)) } diff --git a/internal/github/actionEvent.go b/internal/github/actionEvent.go new file mode 100644 index 0000000..5309b14 --- /dev/null +++ b/internal/github/actionEvent.go @@ -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 +} diff --git a/internal/github/presenter.go b/internal/github/presenter.go new file mode 100644 index 0000000..0fa834e --- /dev/null +++ b/internal/github/presenter.go @@ -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 +}