From 3b515389c821016eecde2325c5fdb380d74bf89c Mon Sep 17 00:00:00 2001 From: Simon Tucker Date: Sat, 11 Jan 2020 14:48:59 -0800 Subject: [PATCH] Adds milestones to task. --- githubtasks.go | 27 ++++++++++++++++++++++ githubtasksutils.go | 31 ++++++++++++++++++++++++++ githubtasksutils_test.go | 48 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/githubtasks.go b/githubtasks.go index 78793385..3c6673d6 100644 --- a/githubtasks.go +++ b/githubtasks.go @@ -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" @@ -24,6 +48,7 @@ const ( type Server struct { *goserver.GoServer config *pb.Config + github github } // Init builds the server @@ -32,6 +57,7 @@ func Init() *Server { GoServer: &goserver.GoServer{}, config: &pb.Config{}, } + s.github = &prodGithub{dial: s.DialMaster} return s } @@ -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()) } diff --git a/githubtasksutils.go b/githubtasksutils.go index a88a2bf6..ca370aa2 100644 --- a/githubtasksutils.go +++ b/githubtasksutils.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "time" "golang.org/x/net/context" @@ -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 +} diff --git a/githubtasksutils_test.go b/githubtasksutils_test.go index ba5329aa..6da27f47 100644 --- a/githubtasksutils_test.go +++ b/githubtasksutils_test.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "testing" "time" @@ -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 } @@ -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) { @@ -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) + } + }