func (b *Build) build(ctx context.Context, r slash.Responder, owner, repo, branch string) error { sha, err := b.resolveBranch(owner, repo, branch) if err != nil { return r.Respond(slash.Reply(err.Error())) } fullRepo := fmt.Sprintf("%s/%s", owner, repo) id := newID() opts := builder.BuildOptions{ ID: id, Repository: fullRepo, Branch: branch, Sha: sha, } if err := b.Queue.Push(ctx, opts); err != nil { return r.Respond(slash.Reply(err.Error())) } url, err := b.url(opts) if err != nil { return r.Respond(slash.Reply(err.Error())) } return r.Respond(slash.Reply(fmt.Sprintf("Building %s@%s: %s", fullRepo, branch, url))) }
func (s *Slack) Enable(ctx context.Context, r slash.Responder, command slash.Command) error { params := slash.Params(ctx) owner, repo := params["owner"], params["repo"] if err := s.client.EnableRepo(ctx, fmt.Sprintf("%s/%s", owner, repo)); err != nil { return r.Respond(slash.Reply(fmt.Sprintf("error: %v", err))) } return r.Respond(slash.Reply(fmt.Sprintf("Installed webhook on %s/%s", owner, repo))) }
func (s *Slack) Build(ctx context.Context, r slash.Responder, command slash.Command) error { params := slash.Params(ctx) owner, repo, branch := params["owner"], params["repo"], params["branch"] r.Respond(slash.Reply("One moment...")) if err := s.build(ctx, r, owner, repo, branch); err != nil { return r.Respond(slash.Reply(fmt.Sprintf("error: %s", err))) } return nil }
func Handle(ctx context.Context, r slash.Responder, command slash.Command) error { if err := r.Respond(slash.Reply("Cool beans")); err != nil { return err } for i := 0; i < 4; i++ { <-time.After(time.Second) if err := r.Respond(slash.Reply(fmt.Sprintf("Async response %d", i))); err != nil { return err } } return nil }
func (b *Build) ServeCommand(ctx context.Context, r slash.Responder, c slash.Command) (slash.Response, error) { params := slash.Params(ctx) owner, repo, branch := params["owner"], params["repo"], params["branch"] go b.build(ctx, r, owner, repo, branch) return slash.Reply("One moment..."), nil }
func (s *Slack) build(ctx context.Context, r slash.Responder, owner, repo, branch string) error { fullRepo := fmt.Sprintf("%s/%s", owner, repo) build, err := s.client.Build(ctx, conveyor.BuildRequest{ Repository: fullRepo, Branch: branch, }) if err != nil { return err } url, err := s.url(build) if err != nil { return err } return r.Respond(slash.Reply(fmt.Sprintf("Building %s@%s: %s", fullRepo, branch, url))) }
func ExampleServer() { // A slash.Handler that will handle our slash commands. h := slash.NewServer(slash.HandlerFunc(func(ctx context.Context, r slash.Responder, c slash.Command) error { return r.Respond(slash.Reply("Hey")) })) // Responses from the above handler will be posted here. responses := slashtest.NewServer() defer responses.Close() req, _ := slashtest.NewRequest("POST", "/", responses.NewCommand()) resp := httptest.NewRecorder() h.ServeHTTP(resp, req) select { case resp := <-responses.Responses: fmt.Println(resp.Text) // Output: Hey case <-time.After(time.Second): panic("timeout") } }
// replyHandler returns a slash.Handler that just replies to the user with the // text. func replyHandler(text string) slash.Handler { return slash.HandlerFunc(func(ctx context.Context, r slash.Responder, c slash.Command) (slash.Response, error) { return slash.Reply(text), nil }) }
// Zipcode is a slash handler that returns the weather for a zip code. func Zipcode(ctx context.Context, r slash.Responder, command slash.Command) (slash.Response, error) { params := slash.Params(ctx) zip := params["zip"] return slash.Reply(zip), nil }
// replyHandler returns a slash.Handler that just replies to the user with the // text. func replyHandler(text string) slash.Handler { return slash.HandlerFunc(func(ctx context.Context, r slash.Responder, c slash.Command) error { return r.Respond(slash.Reply(text)) }) }
// Zipcode is a slash handler that returns the weather for a zip code. func Zipcode(ctx context.Context, r slash.Responder, command slash.Command) error { params := slash.Params(ctx) zip := params["zip"] return r.Respond(slash.Reply(zip)) }