Skip to content

Commit

Permalink
Merge pull request #5 from lowply/better-code
Browse files Browse the repository at this point in the history
Better code
  • Loading branch information
lowply authored May 23, 2019
2 parents 009d83c + ce76db9 commit 39f7a87
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 108 deletions.
45 changes: 10 additions & 35 deletions src/comments.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
Expand All @@ -15,25 +11,23 @@ import (
)

type comment struct {
token string
repository string
endpoint string
template string
do bool
*request
endpoint string
template string
do bool
}

func NewComnent(url string) (*comment, error) {
c := &comment{}
c.token = os.Getenv("GITHUB_TOKEN")
c.repository = os.Getenv("GITHUB_REPOSITORY")
c.endpoint = url
func NewComment(endpoint string) *comment {
r := NewRequest(201)
c := &comment{request: r}
c.endpoint = endpoint
c.template = filepath.Join(os.Getenv("GITHUB_WORKSPACE"), ".github", "ift-comments.yaml")
c.do = true
_, err := os.Stat(c.template)
if err != nil {
c.do = false
}
return c, nil
return c
}

func (c comment) post() error {
Expand Down Expand Up @@ -62,29 +56,10 @@ func (c comment) post() error {
return err
}

client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, c.endpoint, bytes.NewReader(d))
_, err = c.request.post(d, c.endpoint)
if err != nil {
return err
}

req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "token "+c.token)

fmt.Println("Posting " + c.endpoint + " ...")

resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode != 201 {
// Successful response code is 201 Created
return errors.New("Error posting to " + c.endpoint + " : " + resp.Status)
}

fmt.Println("Done!\n" + string(d))
}
return nil
}
67 changes: 19 additions & 48 deletions src/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
Expand All @@ -19,12 +17,10 @@ import (
)

type issue struct {
token string
repository string
payload payload
commentsURL string
endpoint string
template string
*request
endpoint string
template string
payload payload
}

type payload struct {
Expand All @@ -34,13 +30,12 @@ type payload struct {
Labels []string `json:"labels"`
}

func NewIssue() (*issue, error) {
i := &issue{}
i.token = os.Getenv("GITHUB_TOKEN")
i.repository = os.Getenv("GITHUB_REPOSITORY")
i.endpoint = "https://api.github.com/repos/" + i.repository + "/issues"
func NewIssue() *issue {
r := NewRequest(201)
i := &issue{request: r}
i.endpoint = "https://api.github.com/repos/" + os.Getenv("GITHUB_REPOSITORY") + "/issues"
i.template = filepath.Join(os.Getenv("GITHUB_WORKSPACE"), ".github", "ISSUE_TEMPLATE", os.Getenv("IFT_TEMPLATE_NAME"))
return i, nil
return i
}

func (i issue) parseTemplate() (string, error) {
Expand Down Expand Up @@ -118,46 +113,20 @@ func (i *issue) generatePayload() error {
return nil
}

func (i *issue) post() error {
func (i *issue) post() (string, error) {
err := i.generatePayload()
if err != nil {
log.Fatal(err)
return "", err
}

d, err := json.Marshal(i.payload)
if err != nil {
return err
}

client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, i.endpoint, bytes.NewReader(d))
if err != nil {
return err
}

req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "token "+i.token)

fmt.Println("Posting " + i.endpoint + " ...")

resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode != 201 {
// Successful response code is 201 Created
return errors.New("Error posting to " + i.endpoint + " : " + resp.Status)
return "", err
}

fmt.Println("Done!\n" + string(d))

defer resp.Body.Close()

response, err := ioutil.ReadAll(resp.Body)
response, err := i.request.post(d, i.endpoint)
if err != nil {
return err
return "", err
}

r := &struct {
Expand All @@ -166,10 +135,12 @@ func (i *issue) post() error {

err = json.Unmarshal(response, r)
if err != nil {
return err
return "", err
}

i.commentsURL = r.CommentsURL
if r.CommentsURL == "" {
return "", errors.New("comments_url is missing in the response")
}

return nil
return r.CommentsURL, nil
}
34 changes: 9 additions & 25 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,21 @@ import (
)

func main() {
if os.Getenv("GITHUB_TOKEN") == "" {
log.Fatal("GITHUB_TOKEN is empty")
env := []string{"GITHUB_TOKEN", "GITHUB_REPOSITORY", "GITHUB_WORKSPACE", "IFT_TEMPLATE_NAME"}
for _, e := range env {
_, ok := os.LookupEnv(e)
if !ok {
log.Fatal(e + "is empty")
}
}

if os.Getenv("GITHUB_REPOSITORY") == "" {
log.Fatal("GITHUB_REPOSITORY is empty")
}

if os.Getenv("GITHUB_WORKSPACE") == "" {
log.Fatal("GITHUB_WORKSPACE is empty")
}

if os.Getenv("IFT_TEMPLATE_NAME") == "" {
log.Fatal("IFT_TEMPLATE_NAME is empty")
}

i, err := NewIssue()
if err != nil {
log.Fatal(err)
}

err = i.post()
if err != nil {
log.Fatal(err)
}

c, err := NewComnent(i.commentsURL)
i := NewIssue()
commentsURL, err := i.post()
if err != nil {
log.Fatal(err)
}

c := NewComment(commentsURL)
err = c.post()
if err != nil {
log.Fatal(err)
Expand Down
58 changes: 58 additions & 0 deletions src/request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
)

type request struct {
token string
repository string
statusCode int
}

func NewRequest(statusCode int) *request {
p := &request{}
p.token = os.Getenv("GITHUB_TOKEN")
p.repository = os.Getenv("GITHUB_REPOSITORY")
p.statusCode = statusCode
return p
}

func (p *request) post(d []byte, url string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(d))
if err != nil {
return nil, err
}

req.Header.Add("Accept", "application/vnd.github.v3+json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "token "+p.token)

fmt.Println("Posting " + url + " ...")

resp, err := client.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != p.statusCode {
return nil, errors.New("Error posting to " + url + " : " + resp.Status)
}

responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

fmt.Println("Done!\n" + string(d))

return responseBody, nil
}

0 comments on commit 39f7a87

Please sign in to comment.