Skip to content

Commit

Permalink
Merge pull request #78 from cloudnativedaysjp/seaman
Browse files Browse the repository at this point in the history
Bug fix for Cosme package
  • Loading branch information
ShotaKitazawa authored May 27, 2023
2 parents 90cac1e + d77d13a commit 049dafe
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions pkg/cosme/cosme.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"
"strings"
"time"

"github.com/go-playground/webhooks/v6/github"
"golang.org/x/exp/slog"
Expand All @@ -13,6 +14,11 @@ import (
"github.com/cloudnativedaysjp/seaman/pkg/utils"
)

const (
channelLength = 8
timeoutFromEnqueuedUntilProceeded = 20 * time.Second
)

type issueCommentHandler func(ctx context.Context, payload github.IssueCommentPayload, args []string) error

type handler struct {
Expand All @@ -25,6 +31,7 @@ type handler struct {
type data struct {
payload github.IssueCommentPayload
ctx context.Context
cancel context.CancelFunc
}

func New(logger *slog.Logger, secret string) (*handler, error) {
Expand All @@ -36,7 +43,7 @@ func New(logger *slog.Logger, secret string) (*handler, error) {
return nil, err
}
h := &handler{
make(chan data),
make(chan data, channelLength),
make(map[string]issueCommentHandler),
logger.With("package", "cosme"),
hook,
Expand Down Expand Up @@ -71,13 +78,12 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

// hook handler

if len(strings.Fields(payload.Comment.Body)) == 0 {
h.log.Info("invalid command: args.length == 0")
return
}
h.c <- data{payload, r.Context()}
ctx, cancel := context.WithTimeout(context.Background(), timeoutFromEnqueuedUntilProceeded)
h.c <- data{payload, ctx, cancel}

w.WriteHeader(http.StatusOK)
}
Expand All @@ -89,6 +95,7 @@ func (h *handler) RunBackground() {
h.log.Info("context has already exceeded")
continue
default:
d.cancel()
}

ctx := context.Background()
Expand Down

0 comments on commit 049dafe

Please sign in to comment.