Skip to content

Commit

Permalink
Refactor: Extract github specifics
Browse files Browse the repository at this point in the history
  • Loading branch information
craigjbass committed Nov 15, 2022
1 parent 4ff2ced commit bbec65a
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 72 deletions.
76 changes: 4 additions & 72 deletions cmd/action/main.go
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))
}
47 changes: 47 additions & 0 deletions internal/github/actionEvent.go
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
}
43 changes: 43 additions & 0 deletions internal/github/presenter.go
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
}

0 comments on commit bbec65a

Please sign in to comment.