From 5b1903641e69a1f476d65384d8233e905e34b9aa Mon Sep 17 00:00:00 2001 From: Simon Tucker Date: Sun, 19 Jan 2020 10:15:57 -0800 Subject: [PATCH] Adds step to close tasks. --- githubtasks.go | 17 +++++++++++++++++ githubtasksutils.go | 29 +++++++++++++++++++++++++++++ githubtasksutils_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/githubtasks.go b/githubtasks.go index d40459f6..6bb300e1 100644 --- a/githubtasks.go +++ b/githubtasks.go @@ -17,6 +17,7 @@ import ( ) type github interface { + getIssue(ctx context.Context, service string, number int32) (*ghcpb.Issue, error) createMilestone(ctx context.Context, m *pb.Milestone) (int32, error) createTask(ctx context.Context, m *pb.Task, service string, milestoneNumber int32) (int32, error) } @@ -40,6 +41,21 @@ func (p *prodGithub) createMilestone(ctx context.Context, m *pb.Milestone) (int3 return resp.GetNumber(), err } +func (p *prodGithub) getIssue(ctx context.Context, service string, number int32) (*ghcpb.Issue, error) { + conn, err := p.dial("githubcard") + if err != nil { + return nil, err + } + defer conn.Close() + + client := ghcpb.NewGithubClient(conn) + resp, err := client.Get(ctx, &ghcpb.Issue{Service: service, Number: number}) + if err != nil { + return nil, err + } + return resp, err +} + func (p *prodGithub) createTask(ctx context.Context, t *pb.Task, service string, mn int32) (int32, error) { conn, err := p.dial("githubcard") if err != nil { @@ -149,6 +165,7 @@ func main() { server.RegisterRepeatingTask(server.validateIntegrity, "validate_integrity", time.Minute*5) server.RegisterLockingTask(server.processProjects, "process_projects") + server.RegisterLockingTask(server.updateProjects, "update_projects") fmt.Printf("%v", server.Serve()) } diff --git a/githubtasksutils.go b/githubtasksutils.go index 6a0d3e1d..e95ce1c1 100644 --- a/githubtasksutils.go +++ b/githubtasksutils.go @@ -7,6 +7,7 @@ import ( "golang.org/x/net/context" + ghcpb "github.com/brotherlogic/githubcard/proto" pb "github.com/brotherlogic/githubtasks/proto" ) @@ -50,6 +51,34 @@ func (s *Server) validateIntegrity(ctx context.Context) error { return err } +func (s *Server) updateProjects(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 { + for _, task := range milestone.GetTasks() { + if task.GetState() == pb.Task_ACTIVE { + issue, err := s.github.getIssue(ctx, milestone.GetGithubProject(), task.GetNumber()) + if err != nil { + return time.Now().Add(time.Minute * 5), err + } + + if issue.GetState() == ghcpb.Issue_CLOSED { + task.State = pb.Task_COMPLETE + } + err = s.save(ctx) + } + } + } + } + } + } + + return time.Now().Add(time.Minute * 5), err +} + func (s *Server) processProjects(ctx context.Context) (time.Time, error) { err := s.load(ctx) diff --git a/githubtasksutils_test.go b/githubtasksutils_test.go index 083e467a..6613fd21 100644 --- a/githubtasksutils_test.go +++ b/githubtasksutils_test.go @@ -8,6 +8,7 @@ import ( "github.com/brotherlogic/keystore/client" + ghcpb "github.com/brotherlogic/githubcard/proto" pb "github.com/brotherlogic/githubtasks/proto" ) @@ -29,6 +30,13 @@ func (t *testGithub) createTask(ctx context.Context, m *pb.Task, service string, return 10, nil } +func (t *testGithub) getIssue(ctx context.Context, service string, number int32) (*ghcpb.Issue, error) { + if t.fail { + return nil, fmt.Errorf("Built to fail") + } + return &ghcpb.Issue{State: ghcpb.Issue_CLOSED}, nil +} + func InitTestServer() *Server { s := Init() s.SkipLog = true @@ -229,3 +237,35 @@ func TestAddTasks(t *testing.T) { } } + +func TestUpdateTasks(t *testing.T) { + s := InitTestServer() + s.AddProject(context.Background(), &pb.AddProjectRequest{Add: &pb.Project{Name: "Hello", Milestones: []*pb.Milestone{&pb.Milestone{Name: "Testing", State: pb.Milestone_ACTIVE, Number: 1, Tasks: []*pb.Task{}}}}}) + _, err := s.processProjects(context.Background()) + + _, err = s.AddTask(context.Background(), + &pb.AddTaskRequest{MilestoneName: "Testing", MilestoneNumber: 1, Title: "Add stuff", Body: "Do Stuff"}) + _, err = s.processProjects(context.Background()) + _, err = s.updateProjects(context.Background()) + + if err != nil { + t.Errorf("Bad update: %v", err) + } +} +func TestUpdateTasksWithFail(t *testing.T) { + s := InitTestServer() + + s.AddProject(context.Background(), &pb.AddProjectRequest{Add: &pb.Project{Name: "Hello", Milestones: []*pb.Milestone{&pb.Milestone{Name: "Testing", State: pb.Milestone_ACTIVE, Number: 1, Tasks: []*pb.Task{}}}}}) + _, err := s.processProjects(context.Background()) + + _, err = s.AddTask(context.Background(), + &pb.AddTaskRequest{MilestoneName: "Testing", MilestoneNumber: 1, Title: "Add stuff", Body: "Do Stuff"}) + _, err = s.processProjects(context.Background()) + + s.github = &testGithub{fail: true} + _, err = s.updateProjects(context.Background()) + + if err == nil { + t.Errorf("Bad update: %v", err) + } +}