Skip to content

Commit

Permalink
Merge pull request #116 from MH4GF/refactor-settings-and-format-2
Browse files Browse the repository at this point in the history
refactor(lib): Remove environment variable dependencies from `Settings.Init()` and `Format.All()`
  • Loading branch information
masutaka authored Oct 28, 2023
2 parents cfde59f + 490504f commit 70105e7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (e *Events) filter(events []*github.Event) []*github.Event {

for _, event := range events {
if e.debug {
format := NewFormat(e.ctx, e.client, false)
format := NewFormat(e.ctx, e.client, Settings{}, false)
fmt.Printf("[Debug] %s: %v\n", *event.Type, format.Line(event, 999))
}

Expand Down
20 changes: 8 additions & 12 deletions lib/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import (

// Format is Formatter
type Format struct {
ctx context.Context
client *github.Client
debug bool
ctx context.Context
client *github.Client
settings Settings
debug bool
}

// NewFormat is an initializer
func NewFormat(ctx context.Context, client *github.Client, debug bool) *Format {
return &Format{ctx: ctx, client: client, debug: debug}
func NewFormat(ctx context.Context, client *github.Client, settings Settings, debug bool) *Format {
return &Format{ctx: ctx, client: client, settings: settings, debug: debug}
}

// Line is line infomation
Expand Down Expand Up @@ -162,11 +163,6 @@ func getOwnerRepo(repoFullName string) (string, string) {
// All returns all lines which are formatted and sorted
func (f *Format) All(lines Lines) (string, error) {
var result, prevRepoName, currentRepoName string
var settings Settings

if err := settings.Init(); err != nil {
return "", err
}

sort.Sort(lines)

Expand All @@ -175,10 +171,10 @@ func (f *Format) All(lines Lines) (string, error) {

if currentRepoName != prevRepoName {
prevRepoName = currentRepoName
result += fmt.Sprintf("\n%s\n\n", formatSubject(settings, currentRepoName))
result += fmt.Sprintf("\n%s\n\n", formatSubject(f.settings, currentRepoName))
}

result += fmt.Sprintf("%s\n", formatLine(settings, line))
result += fmt.Sprintf("%s\n", formatLine(f.settings, line))
}

return result, nil
Expand Down
50 changes: 50 additions & 0 deletions lib/format_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package lib_test

import (
"context"
"testing"

"github.com/google/go-github/github"
"github.com/masutaka/github-nippou/lib"
)

func TestFormatAll(t *testing.T) {
issue := github.Issue{
State: github.String("closed"),
Title: github.String("イベントを取得できないことがある"),
User: &github.User{Login: github.String("masutaka")},
HTMLURL: github.String("https://github.com/masutaka/github-nippou/issues/1"),
}
pr := github.PullRequest{
State: github.String("closed"),
Title: github.String("Bundle Update on 2015-10-04"),
User: &github.User{Login: github.String("deppbot")},
HTMLURL: github.String("https://github.com/masutaka/github-nippou/pull/31"),
Merged: github.Bool(true),
}
lines := lib.Lines{
lib.NewLineByIssue("masutaka/github-nippou", issue),
lib.NewLineByPullRequest("masutaka/github-nippou", pr),
}
settings := lib.Settings{}
settings.Init("", "")

ctx := context.Background()
f := lib.NewFormat(ctx, nil, settings, false)

result, err := f.All(lines)
if err != nil {
t.Errorf("unexpected error: %v", err)
}

expected := `
### masutaka/github-nippou
* [イベントを取得できないことがある](https://github.com/masutaka/github-nippou/issues/1) by @[masutaka](https://github.com/masutaka) **closed!**
* [Bundle Update on 2015-10-04](https://github.com/masutaka/github-nippou/pull/31) by @[deppbot](https://github.com/deppbot) **merged!**
`

if result != expected {
t.Errorf("unexpected result: got %q, want %q", result, expected)
}
}
6 changes: 5 additions & 1 deletion lib/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func List(sinceDate, untilDate string, debug bool) error {
if err != nil {
return err
}
format := NewFormat(ctx, client, debug)
var settings Settings
if err = settings.Init(getGistID(), accessToken); err != nil {
return err
}
format := NewFormat(ctx, client, settings, debug)

parallelNum, err := getParallelNum()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lib/open_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func OpenSettings() error {
var settings Settings

if err := settings.Init(); err != nil {
if err := settings.Init(getGistID(), ""); err != nil {
return nil
}

Expand Down
11 changes: 1 addition & 10 deletions lib/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,13 @@ type Settings struct {
}

// Init initializes Settings
func (s *Settings) Init() error {
func (s *Settings) Init(gistID string, accessToken string) error {
var content string
var err error

gistID := getGistID()

if gistID != "" {
ctx := context.Background()

accessToken, err := getAccessToken()
if err != nil {
return err
}

client := getClient(ctx, accessToken)

gist, _, err := client.Gists.Get(ctx, gistID)
if err != nil {
return err
Expand Down

0 comments on commit 70105e7

Please sign in to comment.