Example #1
0
func (r *receiver) lastStatus(c *gobot.Context) {
	log.WithField("provider", "gocd").Debugf("#lastStatus")

	projects, err := r.api.BuildStatus()
	if err != nil {
		c.Fail(err)
	}

	pipeline := c.Match(1)
	filtered := []goapi.Project{}
	for _, p := range projects {
		if parts := strings.Split(p.Name, " :: "); len(parts) != 2 {
			continue
		} else if parts[0] != pipeline {
			continue
		}
		filtered = append(filtered, p)
	}

	if len(filtered) == 0 {
		c.Respond(fmt.Sprintf("Unable to find a pipeline with name, %s", pipeline))
		return
	}

	response := c.Respond(fmt.Sprintf("%s:", pipeline))
	for i, p := range filtered {
		text := fmt.Sprintf("%d. %s => %s", i+1, p.Name, p.LastBuildStatus)
		response.Append(text)
	}
}
Example #2
0
func (r *receiver) scheduledPipeline(c *gobot.Context) {
	log.WithField("provider", "gocd").Debugf("#allBuilds")

	pipeline := c.Match(1)
	if err := r.api.PipelineSchedule(pipeline); err != nil {
		c.Fail(err)
		return
	}

	c.Respond(fmt.Sprintf("Scheduled pipeline, %s", pipeline))
}
Example #3
0
func (r *receiver) listPipelines(c *gobot.Context) {
	log.WithField("provider", "gocd").Debugf("#listPipelines")

	groups, err := r.api.PipelineGroups()
	if err != nil {
		c.Fail(err)
		return
	}
	response := c.Respond("Piplines:")

	for _, g := range groups {
		for i, p := range g.Pipelines {
			response.Append(fmt.Sprintf(" %d. %s", i+1, p.Name))
		}
	}
}
Example #4
0
func verify(c *gobot.Context) {
	log.Debugf("verifying mfa code")

	secret, err := loadOtp(c.User)
	if err != nil {
		c.Fail(err)
		return
	}

	totp := &otp.TOTP{Secret: secret}
	if code := c.Match(1); totp.Now().Verify(code) {
		c.Respond("MFA code valid")
	} else {
		c.Respond(fmt.Sprintf("invalid MFA code, expected %s", totp.Get()))
	}
}
Example #5
0
func (r *receiver) failedBuilds(c *gobot.Context) {
	log.WithField("provider", "gocd").Debugf("#failedBuilds")

	projects, err := r.api.BuildStatus()
	if err != nil {
		c.Fail(err)
	}

	failed := goapi.OnlyFailedBuilds(projects)

	if len(failed) == 0 {
		c.Respond("All builds running green!")
		return
	}

	response := c.Respond("Failed builds:")
	for i, p := range failed {
		text := fmt.Sprintf("%d. %s => %s", i+1, p.Name, p.LastBuildStatus)
		response.Append(text)
	}
}
Example #6
0
func registerMFA(c *gobot.Context) {
	log.Debugf("registering mfa")
	c.Respond(fmt.Sprintf("registering a %s mfa", c.Match(1)))

	data := make([]byte, 10)
	if n, err := rand.Read(data); err != nil {
		c.Fail(err)
		return
	} else if n != len(data) {
		c.Fail(fmt.Errorf("read %d random bytes, wanted %d", n, len(data)))
		return
	}
	secret := base32.StdEncoding.EncodeToString(data)

	saveOtp(c.User, secret)

	code, err := qr.Encode("otpauth://totp/Gobot?secret="+secret, qr.Q)
	if err != nil {
		c.Fail(err)
		return
	}

	c.Upload(gobot.Attachment{
		Title:       "QR Code",
		Filename:    "QR.png",
		Content:     bytes.NewReader(code.PNG()),
		ContentType: "image/png",
	})
}