Skip to content

Commit

Permalink
Merge fb36d47 into 051fc22
Browse files Browse the repository at this point in the history
  • Loading branch information
while-loop committed Aug 23, 2018
2 parents 051fc22 + fb36d47 commit 48bb47f
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# service specific vars
SERVICE := todo
VERSION := 0.0.2
VERSION := 0.1.0

ORG := toyotasupra
TARGET := ${SERVICE}d
Expand Down
9 changes: 3 additions & 6 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ type App struct {
}

func New(config *config.Config, router *mux.Router) *App {
rp := vcs.NewManager(config.VcsConfig, router)
tm := tracker.NewManager(config.TrackerConfig)
rp := vcs.NewManager(config.VcsConfig, router, tm)
return &App{
RepoMan: rp,
TrackerMan: tracker.NewManager(config.TrackerConfig, rp.IssueChan()),
TrackerMan: tm,
Router: router,
Config: config,
}
Expand All @@ -29,7 +30,3 @@ func New(config *config.Config, router *mux.Router) *App {
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.Router.ServeHTTP(w, r)
}

func (a *App) OnPush() error {
return nil
}
4 changes: 4 additions & 0 deletions pkg/issue/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"encoding/json"
)

type Creator interface {
Create([]*Issue) error
}

type Issue struct {
ID string
Title string
Expand Down
2 changes: 2 additions & 0 deletions pkg/parser/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"testing"

"fmt"
"github.com/stretchr/testify/require"
"github.com/while-loop/todo/pkg/issue"
)
Expand Down Expand Up @@ -71,6 +72,7 @@ func TestTODOs(t *testing.T) {
{` Extras context.Context // todo change Issue.Extras from Context to map`, slashRegex, nil},
}

fmt.Println(slashRegex)
for idx, tc := range tcs {
t.Run(strconv.Itoa(idx), func(inner *testing.T) {
is, _ := parseLine(tc.rexp, tc.comment)
Expand Down
61 changes: 30 additions & 31 deletions pkg/tracker/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,53 +21,52 @@ type Tracker interface {
type Manager struct {
trackers map[string]Tracker
config *config.TrackerConfig
issueCh <-chan []*issue.Issue
}

func NewManager(config *config.TrackerConfig, issueCh <-chan []*issue.Issue) *Manager {
func NewManager(config *config.TrackerConfig) *Manager {
m := &Manager{
trackers: map[string]Tracker{},
config: config,
issueCh: issueCh,
}

m.initTrackers()
go m.loop()
return m
}

func (m *Manager) loop() {
for iss := range m.issueCh {
if len(iss) <= 0 {
func (m *Manager) Create(issues []*issue.Issue) error {
if len(issues) < 0 {
return nil
}

for _, tr := range m.trackers {
tIss, err := tr.GetIssues(context.Background(), issues[0])
if err != nil {
log.Error(err)
continue
}

for _, tr := range m.trackers {
tIss, err := tr.GetIssues(context.Background(), iss[0])
if err != nil {
log.Error(err)
continue
}

toCreate := xorIssues(tIss, iss)
log.Info(iss)
log.Info(toCreate)
log.Infof("need to create %d issues", len(toCreate))
var wg sync.WaitGroup
for _, cr := range toCreate {
wg.Add(1)
go func(i *issue.Issue) {
log.Info("tocreat: ", i)
if is, err := tr.CreateIssue(context.Background(), i); err != nil {
log.Error(err)
} else {
log.Infof("Created issue: %s/%s/%s", is.Owner, is.Repo, is.ID)
}
wg.Done()
}(cr)
}
toCreate := xorIssues(tIss, issues)
log.Info(issues)
log.Info(toCreate)
log.Infof("need to create %d issues", len(toCreate))
var wg sync.WaitGroup
for _, cr := range toCreate {
wg.Add(1)
go func(i *issue.Issue) {
log.Info("tocreat: ", i)
if is, err := tr.CreateIssue(context.Background(), i); err != nil {
log.Error(err)
} else {
log.Infof("Created issue: %s/%s/%s", is.Owner, is.Repo, is.ID)
}
wg.Done()
}(cr)

wg.Wait()
}
}

return nil
}

func xorIssues(repoIssues []*issue.Issue, pushIssues []*issue.Issue) []*issue.Issue {
Expand Down
14 changes: 7 additions & 7 deletions pkg/vcs/github/event_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ func (s *Service) handlePush(w http.ResponseWriter, r *http.Request) {

// send todos to issuechan (tracker will handle reducing and filtering)
log.Infof("found %d todos in github push %s", len(todos), event.HeadCommit.URL)
go func() {
s.issueCh <- todos
}()

// todo(while-loop): change issueChan to an IssueCreator interface to wait for completion
// when AWS Lambda exits the http func, all other goroutines are shutdown
// this causes the issue creation to be abruptly stopped.. tested with a 2sec timeout
w.WriteHeader(http.StatusOK)
if err := s.issueCreator.Create(todos); err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

w.WriteHeader(http.StatusNoContent)
}

func contentUrl(owner, repo, sha, path string) string {
Expand Down
6 changes: 3 additions & 3 deletions pkg/vcs/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ const (

type Service struct {
router *mux.Router
issueCh chan<- []*issue.Issue
eventHandlers map[string]http.HandlerFunc
config *config.GithubConfig
parser parser.TodoParser
issueCreator issue.Creator
}

func NewService(config *config.GithubConfig, issueChan chan<- []*issue.Issue) *Service {
func NewService(config *config.GithubConfig, creator issue.Creator) *Service {
s := &Service{
issueCh: issueChan,
issueCreator: creator,
router: mux.NewRouter(),
eventHandlers: map[string]http.HandlerFunc{},
config: config,
Expand Down
14 changes: 7 additions & 7 deletions pkg/vcs/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ const (
)

type Service struct {
router *mux.Router
glClient interface{}
issueCh <-chan []*issue.Issue
router *mux.Router
glClient interface{}
issueCreator issue.Creator
}

func NewService(config *config.GitlabConfig, issueChan <-chan []*issue.Issue) *Service {
func NewService(config *config.GitlabConfig, issueCreator issue.Creator) *Service {
//ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: app.Config.Gitlab.AccessToken})
//oauthClient := oauth2.NewClient(context.Background(), ts)
s := &Service{
issueCh: issueChan,
router: mux.NewRouter(),
glClient: nil,
issueCreator: issueCreator,
router: mux.NewRouter(),
glClient: nil,
}

return s
Expand Down
8 changes: 7 additions & 1 deletion pkg/vcs/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/while-loop/todo/pkg/issue"
"github.com/while-loop/todo/pkg/vcs/config"
"io/ioutil"
"net/http"
Expand All @@ -13,6 +14,10 @@ import (

type testVCS struct{}

func (t *testVCS) Create([]*issue.Issue) error {
panic("implement me")
}

func (t *testVCS) Name() string {
return "testvcs"
}
Expand All @@ -27,7 +32,8 @@ func TestWebHookSubRouter(t *testing.T) {
router := mux.NewRouter()
man := NewManager(&config.VcsConfig{
Github: &config.GithubConfig{},
}, router)
}, router, &testVCS{})

man.services["testvcs"] = &testVCS{}
man.initRouter(router)

Expand Down
24 changes: 9 additions & 15 deletions pkg/vcs/manger.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ type RepositoryService interface {
}

type Manager struct {
config *config.VcsConfig
services map[string]RepositoryService
issueChan chan []*issue.Issue
config *config.VcsConfig
services map[string]RepositoryService
}

func NewManager(config *config.VcsConfig, router *mux.Router) *Manager {
func NewManager(config *config.VcsConfig, router *mux.Router, creator issue.Creator) *Manager {
m := &Manager{
config: config,
services: map[string]RepositoryService{},
issueChan: make(chan []*issue.Issue),
config: config,
services: map[string]RepositoryService{},
}

m.initServices()
m.initServices(creator)
m.initRouter(router)
return m
}
Expand All @@ -35,20 +33,16 @@ func (m *Manager) Services() map[string]RepositoryService {
return m.services
}

func (m *Manager) IssueChan() <-chan []*issue.Issue {
return m.issueChan
}

func (m *Manager) initServices() {
func (m *Manager) initServices(creator issue.Creator) {
conf := m.config

if conf.Github != nil {
srv := github.NewService(conf.Github, m.issueChan)
srv := github.NewService(conf.Github, creator)
m.services[srv.Name()] = srv
}

if conf.Gitlab != nil {
srv := gitlab.NewService(conf.Gitlab, m.issueChan)
srv := gitlab.NewService(conf.Gitlab, creator)
m.services[srv.Name()] = srv
}
}
Expand Down

0 comments on commit 48bb47f

Please sign in to comment.