Example #1
0
func (r bot) rename(p *robots.Payload, cmd utils.Command) error {
	old := cmd.Arg(0)
	new := cmd.Arg(1)
	if new == "" || old == "" {
		r.handler.Send(p, "You need to provide the old and new name. Usage: `!project rename <old-name> <new-name>`")
		return nil
	}

	pr, err := db.GetProjectByName(old)
	if err != nil {
		return err
	}

	if pr == nil {
		r.handler.Send(p, "Project *"+old+"* not found.")
		return nil
	}

	pr.Name = new
	err = db.UpdateProject(*pr)
	if err != nil {
		return err
	}

	r.handler.Send(p, "Project *"+old+"* renamed to *"+new+"*")
	return nil
}
Example #2
0
func (r bot) myStories(p *robots.Payload, cmd utils.Command) error {
	name := cmd.Arg(0)
	var pr *db.Project
	var err error

	if name == "" {
		pr, err = db.GetProjectByChannel(p.ChannelName)
	} else {
		pr, err = db.GetProjectByName(name)
	}

	if err != nil {
		return err
	}

	if pr == nil {
		r.handler.Send(p, "Missing project name.")
		return nil
	}

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

	user, err := db.GetUserByName(p.UserName)
	if err != nil {
		return err
	}

	filter := map[string]string{
		"owned_by": user.StrPivotalId(),
		"state":    "started,finished",
	}
	stories, err := pvt.FilteredStories(pr.StrPivotalId(), filter)
	if err != nil {
		return err
	}

	if len(stories) < 1 {
		r.handler.Send(p, "No open stories in project *"+pr.Name+"* for *"+p.UserName+"*")
		return nil
	}

	str := "Current stories in project *" + pr.Name + "* for *" + p.UserName + "*:\n"
	atts := []robots.Attachment{}
	for _, s := range stories {
		fallback := fmt.Sprintf("%d - %s - %s\n", s.Id, s.Name, s.State)
		title := fmt.Sprintf("%d - %s\n", s.Id, s.Name)
		a := utils.FmtAttachment(fallback, title, s.Url, s.State)
		atts = append(atts, a)
	}

	r.handler.SendWithAttachments(p, str, atts)
	return nil
}
Example #3
0
func getProject(name string) (*db.Project, error) {
	pr, err := db.GetProjectByName(name)
	if err != nil {
		return nil, err
	}
	if pr == nil {
		err := errors.New("Project *" + name + "* not found.")
		return nil, err
	}

	return pr, nil
}
Example #4
0
func (r bot) makeLink(p *robots.Payload, name string, mvnId string, pvtId string) error {
	prj, err := db.GetProjectByName(name)
	if err != nil {
		return err
	}

	if prj != nil {
		r.handler.Send(p, fmt.Sprintf("Project with name %s already exists.", name))
		return nil
	}

	mvnProject, err := r.getMvnProject(p, mvnId)
	if err != nil {
		msg := fmt.Sprintf("Error loading mavenlink project %s: %s", mvnId, err.Error())
		return errors.New(msg)
	}

	pvtProject, err := r.getPvtProject(p, pvtId)
	if err != nil {
		msg := fmt.Sprintf("Error loading pivotal project %s: %s", pvtId, err.Error())
		return errors.New(msg)
	}

	mvnInt, err := strconv.ParseInt(mvnProject.Id, 10, 64)
	if err != nil {
		return err
	}
	pvtInt := pvtProject.Id

	project := db.Project{
		Name:        name,
		MavenlinkId: mvnInt,
		PivotalId:   pvtInt,
		CreatedBy:   p.UserName,
	}
	err = db.CreateProject(project)
	if err != nil {
		return err
	}

	r.handler.Send(p, fmt.Sprintf("Project %s linked %s - %s and %d - %s", name,
		mvnProject.Id, mvnProject.Title,
		pvtProject.Id, pvtProject.Name))

	return err
}
Example #5
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
}
Example #6
0
func (r bot) setChannel(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
	}

	ps.Channel = p.ChannelName
	if err := db.UpdateProject(*ps); err != nil {
		return err
	}

	r.handler.Send(p, "Project *"+name+"* assigned to *"+ps.Channel+"* channel.")
	return nil
}
Example #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
}
Example #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
}