Skip to content

Commit

Permalink
Merge pull request #6 from lowply/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
lowply authored May 23, 2019
2 parents 44bbe63 + 6d5947d commit 271b46e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 39 deletions.
69 changes: 30 additions & 39 deletions src/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ type issue struct {
*request
endpoint string
template string
payload payload
}

type payload struct {
Title string `json:"title"`
Body string `json:"body"`
Assignees []string `json:"assignees"`
Labels []string `json:"labels"`
}

func NewIssue() *issue {
Expand All @@ -38,7 +30,7 @@ func NewIssue() *issue {
return i
}

func (i issue) parseTemplate() (string, error) {
func (i *issue) parseTemplate() (string, error) {
d := &struct {
Year string
WeekStartDate string
Expand Down Expand Up @@ -76,55 +68,54 @@ func (i issue) parseTemplate() (string, error) {
return b.String(), nil
}

func (i issue) splitAndTrimSpace(s string) []string {
arr := strings.Split(s, ",")
for i := range arr {
arr[i] = strings.TrimSpace(arr[i])
}
return arr
}

func (i *issue) generatePayload() error {
t := &struct {
Name string `yaml:"name"`
About string `yaml:"about"`
Title string `yaml:"title"`
Labels string `yaml:"labels"`
Assignees string `yaml:"assignees"`
}{}

func (i *issue) generatePayload() ([]byte, error) {
templateBody, err := i.parseTemplate()
if err != nil {
return err
return nil, err
}

s := strings.Split(templateBody, "---\n")

t := &struct {
Name string `yaml:"name"`
About string `yaml:"about"`
Title string `yaml:"title"`
Labels StringSlice `yaml:"labels"`
Assignees StringSlice `yaml:"assignees"`
}{}

err = yaml.UnmarshalStrict([]byte(s[1]), t)
if err != nil {
return err
return nil, err
}

i.payload.Title = t.Title
i.payload.Body = strings.Replace(s[2], "\n", "", 1)
i.payload.Assignees = i.splitAndTrimSpace(t.Assignees)
i.payload.Labels = i.splitAndTrimSpace(t.Labels)
payload := &struct {
Title string `json:"title"`
Body string `json:"body"`
Assignees []string `json:"assignees"`
Labels []string `json:"labels"`
}{}

return nil
}
payload.Title = t.Title
payload.Body = strings.Replace(s[2], "\n", "", 1)
payload.Assignees = t.Assignees.splitAndTrimSpace()
payload.Labels = t.Labels.splitAndTrimSpace()

func (i *issue) post() (string, error) {
err := i.generatePayload()
d, err := json.Marshal(payload)
if err != nil {
return "", err
return nil, err
}

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

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

response, err := i.request.post(d, i.endpoint)
response, err := i.request.post(data, i.endpoint)
if err != nil {
return "", err
}
Expand Down
13 changes: 13 additions & 0 deletions src/string_slice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import "strings"

type StringSlice string

func (s StringSlice) splitAndTrimSpace() []string {
arr := strings.Split(string(s), ",")
for i, v := range arr {
arr[i] = strings.TrimSpace(v)
}
return arr
}

0 comments on commit 271b46e

Please sign in to comment.