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
17 changes: 17 additions & 0 deletions githubtasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
}
29 changes: 29 additions & 0 deletions githubtasksutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"golang.org/x/net/context"

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

Expand Down Expand Up @@ -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)

Expand Down
40 changes: 40 additions & 0 deletions githubtasksutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/brotherlogic/keystore/client"

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

Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}