Exemplo n.º 1
0
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)))
}
Exemplo n.º 2
0
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)))
}
Exemplo n.º 3
0
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
}
Exemplo n.º 4
0
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
}
Exemplo n.º 5
0
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
}
Exemplo n.º 6
0
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)))
}
Exemplo n.º 7
0
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")
	}
}
Exemplo n.º 8
0
// 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
	})
}
Exemplo n.º 9
0
// 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
}
Exemplo n.º 10
0
// 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))
	})
}
Exemplo n.º 11
0
// 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))
}