Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions githubtasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,34 @@ import (
"golang.org/x/net/context"
"google.golang.org/grpc"

ghcpb "github.com/brotherlogic/githubcard/proto"
pb "github.com/brotherlogic/githubtasks/proto"
pbg "github.com/brotherlogic/goserver/proto"
)

type github interface {
createMilestone(ctx context.Context, m *pb.Milestone) (int32, error)
}

type prodGithub struct {
dial func(server string) (*grpc.ClientConn, error)
}

func (p *prodGithub) createMilestone(ctx context.Context, m *pb.Milestone) (int32, error) {
conn, err := p.dial("githubcard")
if err != nil {
return -1, err
}
defer conn.Close()

client := ghcpb.NewGithubClient(conn)
resp, err := client.AddMilestone(ctx, &ghcpb.AddMilestoneRequest{Title: m.GetName(), Repo: m.GetGithubProject()})
if err != nil {
return -1, err
}
return resp.GetNumber(), err
}

const (
// KEY where the config is stored
KEY = "/github.com/brotherlogic/githubtasks/config"
Expand All @@ -24,6 +48,7 @@ const (
type Server struct {
*goserver.GoServer
config *pb.Config
github github
}

// Init builds the server
Expand All @@ -32,6 +57,7 @@ func Init() *Server {
GoServer: &goserver.GoServer{},
config: &pb.Config{},
}
s.github = &prodGithub{dial: s.DialMaster}
return s
}

Expand Down Expand Up @@ -94,6 +120,7 @@ func main() {
}

server.RegisterRepeatingTask(server.validateIntegrity, "validate_integrity", time.Hour)
server.RegisterLockingTask(server.processProjects, "process_projects")

fmt.Printf("%v", server.Serve())
}
31 changes: 31 additions & 0 deletions githubtasksutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"time"

"golang.org/x/net/context"

Expand Down Expand Up @@ -32,3 +33,33 @@ func (s *Server) validateIntegrity(ctx context.Context) error {

return err
}

func (s *Server) processProjects(ctx context.Context) (time.Time, error) {
err := s.load(ctx)

if err == nil {
for _, project := range s.config.GetProjects() {
for _, milestone := range project.GetMilestones() {
if milestone.GetState() == pb.Milestone_ACTIVE {
break
}

if milestone.GetState() == pb.Milestone_CREATED {
num, err := s.github.createMilestone(ctx, milestone)

if err != nil {
return time.Now().Add(time.Minute * 5), err
}

milestone.Number = num
milestone.State = pb.Milestone_ACTIVE
break
}
}
}

err = s.save(ctx)
}

return time.Now().Add(time.Minute * 5), err
}
48 changes: 48 additions & 0 deletions githubtasksutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"fmt"
"testing"
"time"

Expand All @@ -10,12 +11,24 @@ import (
pb "github.com/brotherlogic/githubtasks/proto"
)

type testGithub struct {
fail bool
}

func (t *testGithub) createMilestone(ctx context.Context, m *pb.Milestone) (int32, error) {
if t.fail {
return -1, fmt.Errorf("Built to fail")
}
return 10, nil
}

func InitTestServer() *Server {
s := Init()
s.SkipLog = true
s.SkipIssue = true
s.GoServer.KSclient = *keystoreclient.GetTestClient(".test")
s.GoServer.KSclient.Save(context.Background(), KEY, &pb.Config{LastUpdate: time.Now().Unix()})
s.github = &testGithub{}
return s
}

Expand All @@ -41,6 +54,34 @@ func TestEmptyMilestones(t *testing.T) {
if err != nil {
t.Errorf("Error in validation: %v", err)
}

_, err = s.processProjects(context.Background())

if err != nil {
t.Errorf("Error when processing: %v", err)
}
}

func TestEmptyMilestonesWithAddFail(t *testing.T) {
s := InitTestServer()
s.github = &testGithub{fail: true}

_, err := s.AddProject(context.Background(), &pb.AddProjectRequest{Add: &pb.Project{Name: "Hello", Milestones: []*pb.Milestone{&pb.Milestone{Name: "teting"}}}})
if err != nil {
t.Errorf("Error adding project: %v", err)
}

err = s.validateIntegrity(context.Background())

if err != nil {
t.Errorf("Error in validation: %v", err)
}

_, err = s.processProjects(context.Background())

if err == nil {
t.Errorf("Processing did not fail")
}
}

func TestActiveMilestone(t *testing.T) {
Expand All @@ -56,4 +97,11 @@ func TestActiveMilestone(t *testing.T) {
if err != nil {
t.Errorf("Error in validation: %v", err)
}

_, err = s.processProjects(context.Background())

if err != nil {
t.Errorf("Error when processing: %v", err)
}

}