Exemple #1
0
func (r bot) getMvnProject(p *robots.Payload, id string) (*mavenlink.Project, error) {
	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return nil, err
	}
	return mvn.GetProject(id)
}
Exemple #2
0
func (r bot) addTime(p *robots.Payload, cmd utils.Command) error {
	args, err := cmd.ParseArgs("mavenlink-id", "time-in-hours")
	if err != nil {
		return err
	}

	mvnID, timeStr := args[0], args[1]
	hours, err := strconv.ParseFloat(timeStr, 64)
	if err != nil {
		return err
	}

	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return err
	}

	story, err := mvn.GetStory(mvnID)
	if err != nil {
		return err
	}

	minutes := int(hours * 60)
	_, err = mvn.AddTimeEntry(story, minutes)
	if err != nil {
		return err
	}

	r.handler.Send(p, fmt.Sprintf("Added *%.1f* hours to story *%s - %s*",
		hours, story.Id, story.Title))
	return nil
}
Exemple #3
0
func (r bot) startTask(p *robots.Payload, cmd utils.Command) error {
	// pr, err := getProject(cmd.Arg(0))
	// if err != nil {
	// 	return err
	// }
	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return err
	}
	pvt, err := pivotal.NewFor(p.UserName)
	if err != nil {
		return err
	}

	if storyId := cmd.Arg(0); storyId != "" {
		pvtStory, err := pvt.GetStory(storyId)
		if err != nil {
			return err
		}

		pvtStory, err = pvt.SetStoryState(pvtStory.GetStringId(), "started")
		if err != nil {
			return err
		}

		if mvnId := pvtStory.GetMavenlinkId(); mvnId != "" {
			mvnStory, err := mvn.GetStory(mvnId)
			if err != nil {
				return err
			}

			fmt.Printf(" ** Got story: %+v\n", mvnStory)

			mvnStory.State = "started"
			mvnStory, err = mvn.SetStoryState(mvnStory.Id, "started")
			if err != nil {
				return err
			}
		}

		r.handler.Send(p, "Story *"+pvtStory.Name+"* started")
		return nil
	}

	// stories, err := mvn.ChildStories(pr.MvnSprintStoryId)
	// if err != nil {
	// 	return err
	// }

	// r.handler.Send(p, "Click the story you want to start on *"+pr.Name+"*:")
	// url := os.Getenv("APP_URL")
	// atts := mavenlink.CustomFormatStories(stories, url+"selection/startTask/")
	// for _, a := range atts {
	// 	r.handler.SendWithAttachments(p, "", []robots.Attachment{a})
	// }
	return nil
}
Exemple #4
0
func (r bot) list(p *robots.Payload, cmd utils.Command) error {
	ps, err := db.GetProjects()
	if err != nil {
		return err
	}

	if ps == nil || len(ps) < 1 {
		r.handler.Send(p, "There are no linked projects currently. Use `link` command to add one.")
		return nil
	}

	s := ""

	for _, pr := range ps {
		fmt.Println(" *** Project", pr)
		fmt.Println("     Starting PVT", strconv.FormatInt(pr.PivotalId, 10))
		pvtPr, err := r.getPvtProject(p, strconv.FormatInt(pr.PivotalId, 10))
		if err != nil {
			return err
		}
		fmt.Println("     Starting MVN", strconv.FormatInt(pr.MavenlinkId, 10))
		mvnPr, err := r.getMvnProject(p, strconv.FormatInt(pr.MavenlinkId, 10))
		if err != nil {
			return err
		}

		sprintInfo := ""
		if pr.MvnSprintStoryId != "" {
			mvn, err := mavenlink.NewFor(p.UserName)
			if err != nil {
				return err
			}

			fmt.Println("     Starting Story", pr.MvnSprintStoryId)
			sprintStory, err := mvn.GetStory(pr.MvnSprintStoryId)
			fmt.Println("     Sotry result", err)
			if err != nil {
				fmt.Println("     Returning error:", err)
				return err
			}

			sprintInfo = "Current sprint: " + sprintStory.Title + "\n"
			fmt.Println("     Finished Story")
		}

		s += fmt.Sprintf(
			"*%s*\nPivotal %d - %s to Mavenlink %s - %s\n%s",
			pr.Name, pvtPr.Id, pvtPr.Name, mvnPr.Id, mvnPr.Title, sprintInfo)
		fmt.Println("     Finished Project")
	}

	fmt.Println("     Sending response:", s)
	r.handler.Send(p, s)
	return nil
}
Exemple #5
0
func (r bot) create(p *robots.Payload, cmd utils.Command) error {
	alias := cmd.Arg(0)
	if alias == "" {
		r.handler.Send(p, "Missing project alias. Usage: `!project createproject <alias> <long-name>`")
		return nil
	}
	name := cmd.StrFrom(1)
	if name == "" {
		r.handler.Send(p, "Missing project name. Usage: `!project createproject <alias> <long-name>`")
		return nil
	}
	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return err
	}
	pvt, err := pivotal.NewFor(p.UserName)
	if err != nil {
		return err
	}
	pvtProject := pivotal.Project{
		Name: name,
		// PointScale: "1,2,3,4,5,6,7,8,9,10,16,20",
	}
	pvtNewProject, err := pvt.CreateProject(pvtProject)
	if err != nil {
		return err
	}
	mvnProject := mavenlink.Project{
		Title:       name,
		Description: fmt.Sprintf("[pvt:%s]", pvtNewProject.Id),
		CreatorRole: "maven",
	}
	mvnNewProject, err := mvn.CreateProject(mvnProject)
	if err != nil {
		return err
	}
	if mvnNewProject == nil {
		return errors.New("Mavenlink returned a nil project")
	}
	pvtNewProject.Description = "[mvn:" + mvnNewProject.Id + "]"
	pvtNewProject, err = pvt.UpdateProject(*pvtNewProject)
	if err != nil {
		return err
	}

	err = r.makeLink(p, alias, mvnNewProject.Id, strconv.FormatInt(pvtNewProject.Id, 10))
	if err != nil {
		return err
	}

	r.handler.Send(p, "Project *"+name+"* created on Pivotal and Mavenlink.")
	return nil
}
Exemple #6
0
func (r bot) setSprint(p *robots.Payload, cmd utils.Command) error {
	name := cmd.Arg(0)
	if name == "" {
		r.handler.Send(p, "Missing project name")
		return nil
	}

	id := cmd.Arg(1)
	if id == "" {
		r.handler.Send(p, "Missing mavenlink story id to assign as current sprint")
		return nil
	}

	ps, err := db.GetProjectByName(name)
	if err != nil {
		return err
	}

	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return err
	}

	mvnStory, err := mvn.GetStory(id)
	if err != nil {
		return err
	}

	if mvnStory == nil {
		r.handler.Send(p, "Story with id "+id+" wasn't found")
		return nil
	}

	fmt.Println("Got story", mvnStory.Id)
	ps.MvnSprintStoryId = mvnStory.Id
	if err := db.UpdateProject(*ps); err != nil {
		return err
	}

	r.handler.Send(p, "Project *"+name+"* updated.")
	return nil
}
Exemple #7
0
func (r bot) stories(p *robots.Payload, cmd utils.Command) error {
	name := cmd.Arg(0)

	var ps *db.Project
	var err error
	if name != "" {
		ps, err = db.GetProjectByName(name)
		if err != nil {
			return err
		}
		if ps == nil {
			r.handler.Send(p, "Project *"+name+"* not found")
			return nil
		}
	}

	if ps == nil {
		ps, err = db.GetProjectByChannel(p.ChannelName)
		if err != nil {
			return err
		}
		if ps == nil {
			r.handler.Send(p, "Missing project name")
			return nil
		}
	}

	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return err
	}

	sprint, err := mvn.GetStory(ps.MvnSprintStoryId)
	if err != nil {
		return err
	}

	stories, err := mvn.GetChildStories(ps.MvnSprintStoryId)
	if err != nil {
		return err
	}

	r.handler.Send(p, "Mavenlink stories for *"+ps.Name+"*, sprint *"+sprint.Title+"*:")
	atts := mavenlink.FormatStories(stories)
	for _, a := range atts {
		r.handler.SendWithAttachments(p, "", []robots.Attachment{a})
	}

	var totalEstimated int64
	var totalLogged int64
	for _, s := range stories {
		totalEstimated += s.TimeEstimateInMinutes
		totalLogged += s.LoggedBillableTimeInMinutes
	}

	s := ""
	if totalEstimated > 0 {
		s += fmt.Sprintf("Total estimated: %s", utils.FormatHour(totalEstimated))
	}
	if totalLogged > 0 {
		if totalEstimated > 0 {
			s += " - "
		}
		s += fmt.Sprintf("Total logged: %s", utils.FormatHour(totalLogged))
	}
	if s != "" {
		r.handler.Send(p, s)
	}

	return nil
}
Exemple #8
0
func (r bot) addSprint(p *robots.Payload, cmd utils.Command) error {
	name := cmd.Arg(0)
	if name == "" {
		r.handler.Send(p, "Missing project name")
		return nil
	}

	ps, err := db.GetProjectByName(name)
	if err != nil {
		return err
	}

	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return err
	}

	sprintName := cmd.StrFrom(1)
	if sprintName == "" {
		sprintName = "Sprint 1"

		if ps.MvnSprintStoryId != "" {
			s, err := mvn.GetStory(ps.MvnSprintStoryId)
			if err != nil {
				return err
			}

			matched, err := regexp.MatchString(`Sprint [\d]+`, s.Title)
			if err != nil {
				return err
			}
			if matched {
				num, err := strconv.ParseInt(strings.Split(s.Title, " ")[1], 10, 64)
				if err != nil {
					return err
				}
				sprintName = fmt.Sprintf("Sprint %d", (num + 1))
			}
		}
	}

	s := mavenlink.Story{
		Title:       sprintName,
		WorkspaceId: strconv.FormatInt(ps.MavenlinkId, 10),
		StoryType:   "milestone",
	}

	ns, err := mvn.CreateStory(s)
	if err != nil {
		return err
	}

	fmt.Printf("%+v\n", ns)

	ps.MvnSprintStoryId = ns.Id
	err = db.UpdateProject(*ps)
	if err != nil {
		return err
	}

	s = *ns
	r.handler.Send(p, "Added new sprint to *"+ps.Name+"*: "+s.Id+" - "+s.Title)
	return nil
}
Exemple #9
0
func (r bot) addStory(p *robots.Payload, cmd utils.Command) error {
	name := cmd.Arg(0)
	if name == "" {
		err := errors.New(
			"Missing project name. Use `!project addtask <project> [type:<story-type>] <task-name>`")
		return err
	}

	storyType := cmd.Param("type")
	if storyType == "" {
		storyType = "feature"
	}

	storyName := strings.Join(cmd.ArgsFrom(1), " ")
	if storyName == "" {
		err := errors.New(
			"Missing story name. Use `!project addtask <project> <task-name>`")
		return err
	}

	pr, err := getProject(name)
	if err != nil {
		return err
	}
	mvn, err := mavenlink.NewFor(p.UserName)
	if err != nil {
		return err
	}
	pvt, err := pivotal.NewFor(p.UserName)
	if err != nil {
		return err
	}

	pvtStory := pivotal.Story{
		Name:      storyName,
		ProjectId: pr.PivotalId,
		Type:      storyType,
	}
	pvtNewStory, err := pvt.CreateStory(pvtStory)
	if err != nil {
		return err
	}

	desc := fmt.Sprintf("[pvt:%d]", pvtNewStory.Id)
	mvnStory := mavenlink.Story{
		Title:       storyName,
		Description: desc,
		ParentId:    pr.MvnSprintStoryId,
		WorkspaceId: strconv.FormatInt(pr.MavenlinkId, 10),
		StoryType:   "task",
	}
	mvnNewStory, err := mvn.CreateStory(mvnStory)
	if err != nil {
		return err
	}

	tmpStory := pivotal.Story{
		Id:          pvtNewStory.Id,
		Description: "[mvn:" + mvnNewStory.Id + "]",
	}
	pvtNewStory, err = pvt.UpdateStory(tmpStory)
	if err != nil {
		return err
	}

	pvtAtt := utils.FmtAttachment(
		fmt.Sprintf("- Pivotal %d - %s\n", pvtNewStory.Id, pvtNewStory.Name),
		fmt.Sprintf("Pivotal %d - %s\n", pvtNewStory.Id, pvtNewStory.Name),
		pvtNewStory.Url, "")
	mvnAtt := utils.FmtAttachment(
		fmt.Sprintf("- Mavenlink %s - %s\n", mvnNewStory.Id, mvnNewStory.Title),
		fmt.Sprintf("Mavenlink %s - %s\n", mvnNewStory.Id, mvnNewStory.Title),
		mvnNewStory.URL(), "")

	s := "Stories successfully added:\n"
	r.handler.SendWithAttachments(p, s, []robots.Attachment{pvtAtt, mvnAtt})
	return nil
}
Exemple #10
0
func claimTimer(bot *UserBot, cmd utils.Command) error {
	timerName := cmd.Arg(0)
	if timerName == "" {
		return errors.New("Missing timer. Usage: `claim <timer-name> <pivotal-task-id>`")
	}

	taskID := cmd.Arg(1)
	if taskID == "" {
		return errors.New("Missing task id. Usage: `claim <timer-name> <pivotal-task-id>`")
	}

	username := bot.lastMessage.User.Name
	timer, err := db.GetTimerByName(username, timerName)
	if err != nil {
		return err
	}
	if timer == nil {
		return errors.New("You have no timer with name *" + timerName + "*")
	}

	err = timer.Stop()
	if err != nil {
		return err
	}
	bot.reply("Timer *" + timer.Name + "* stopped.")

	pvt, err := pivotal.NewFor(username)
	if err != nil {
		return err
	}

	mvn, err := mavenlink.NewFor(username)
	if err != nil {
		return err
	}

	task, err := pvt.GetStory(taskID)
	if err != nil {
		return err
	}

	mvnID := task.GetMavenlinkId()
	if mvnID == "" {
		return errors.New("Can't claim because the Pivotal task doesn't have a mavenlink tag like `[mvn:<id>]`")
	}

	story, err := mvn.GetStory(mvnID)
	if err != nil {
		return err
	}

	_, err = mvn.AddTimeEntry(story, timer.Minutes())
	if err != nil {
		return err
	}

	bot.reply(fmt.Sprintf("Added *%d* minutes to story *%s - %s*",
		timer.Minutes(), story.Id, story.Title))

	return nil
}