Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api,repositories): clean not pushed branch when checkout #5251

Merged
merged 2 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 57 additions & 19 deletions engine/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,30 @@ func (r *Router) compress(fn http.HandlerFunc) http.HandlerFunc {
}
}

const requestIDHeader = "Request-ID"

func (r *Router) setRequestID(h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var requestID string
if existingRequestID := r.Header.Get(requestIDHeader); existingRequestID != "" {
if _, err := uuid.FromString(existingRequestID); err == nil {
requestID = existingRequestID
}
}
if requestID == "" {
requestID = sdk.UUID()
}

ctx := r.Context()
ctx = context.WithValue(ctx, log.ContextLoggingRequestIDKey, requestID)
r = r.WithContext(ctx)

w.Header().Set(requestIDHeader, requestID)

h(w, r)
}
}

func (r *Router) recoverWrap(h http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
var err error
Expand All @@ -132,10 +156,10 @@ func (r *Router) recoverWrap(h http.HandlerFunc) http.HandlerFunc {
}
}

log.Error(context.TODO(), "[PANIC_RECOVERY] Panic occurred on %s:%s, recover %s", req.Method, req.URL.String(), err)
log.Error(req.Context(), "[PANIC_RECOVERY] Panic occurred on %s:%s", req.Method, req.URL.String())
trace := make([]byte, 4096)
count := runtime.Stack(trace, true)
log.Error(context.TODO(), "[PANIC_RECOVERY] Stacktrace of %d bytes\n%s\n", count, trace)
log.Error(req.Context(), "[PANIC_RECOVERY] Stacktrace of %d bytes\n%s\n", count, trace)

//Checking if there are two much panics in two minutes
//If last panic was more than 2 minutes ago, reinit the panic counter
Expand All @@ -144,7 +168,7 @@ func (r *Router) recoverWrap(h http.HandlerFunc) http.HandlerFunc {
} else {
dur := time.Since(*r.lastPanic)
if dur.Minutes() > float64(2) {
log.Info(context.Background(), "[PANIC_RECOVERY] Last panic was %d seconds ago", int(dur.Seconds()))
log.Info(req.Context(), "[PANIC_RECOVERY] Last panic was %d seconds ago", int(dur.Seconds()))
r.nbPanic = 0
}
}
Expand All @@ -155,10 +179,10 @@ func (r *Router) recoverWrap(h http.HandlerFunc) http.HandlerFunc {
//If two much panic, change the status of /mon/status with panicked = true
if r.nbPanic > nbPanicsBeforeFail {
r.panicked = true
log.Error(context.TODO(), "[PANIC_RECOVERY] RESTART NEEDED")
log.Error(req.Context(), "[PANIC_RECOVERY] RESTART NEEDED")
}

service.WriteError(context.TODO(), w, req, err)
service.WriteError(req.Context(), w, req, err)
}
}()
h.ServeHTTP(w, req)
Expand Down Expand Up @@ -253,13 +277,13 @@ func (r *Router) computeScopeDetails() {
func (r *Router) Handle(uri string, scope HandlerScope, handlers ...*service.HandlerConfig) {
uri = r.Prefix + uri
config, f := r.handle(uri, scope, handlers...)
r.Mux.Handle(uri, r.pprofLabel(config, r.compress(r.recoverWrap(f))))
r.Mux.Handle(uri, r.pprofLabel(config, r.compress(r.setRequestID(r.recoverWrap(f)))))
}

func (r *Router) HandlePrefix(uri string, scope HandlerScope, handlers ...*service.HandlerConfig) {
uri = r.Prefix + uri
config, f := r.handle(uri, scope, handlers...)
r.Mux.PathPrefix(uri).HandlerFunc(r.pprofLabel(config, r.compress(r.recoverWrap(f))))
r.Mux.PathPrefix(uri).HandlerFunc(r.pprofLabel(config, r.compress(r.setRequestID(r.recoverWrap(f)))))
}

// Handle adds all handler for their specific verb in gorilla router for given uri
Expand Down Expand Up @@ -289,15 +313,12 @@ func (r *Router) handle(uri string, scope HandlerScope, handlers ...*service.Han
defer cancel()

var requestID string
if existingRequestID := req.Header.Get("Request-ID"); existingRequestID != "" {
if _, err := uuid.FromString(existingRequestID); err == nil {
requestID = existingRequestID
iRequestID := ctx.Value(log.ContextLoggingRequestIDKey)
if iRequestID != nil {
if id, ok := iRequestID.(string); ok {
requestID = id
}
}
if requestID == "" {
requestID = uuid.NewV4().String()
}
ctx = context.WithValue(ctx, log.ContextLoggingRequestIDKey, requestID)

dateRFC5322 := req.Header.Get("Date")
dateReq, err := sdk.ParseDateRFC5322(dateRFC5322)
Expand Down Expand Up @@ -477,16 +498,26 @@ func processAsyncRequests(ctx context.Context, chanRequest chan asynchronousRequ
for {
select {
case req := <-chanRequest:
if iRequestID, ok := req.contextValues[log.ContextLoggingRequestIDKey]; ok {
if requestID, ok := iRequestID.(string); ok {
ctx = context.WithValue(ctx, log.ContextLoggingRequestIDKey, requestID)
}
}
if err := req.do(ctx, handler); err != nil {
isErrWithStack := sdk.IsErrorWithStack(err)
fields := logrus.Fields{}
if isErrWithStack {
fields["stack_trace"] = fmt.Sprintf("%+v", err)
}
myError, ok := err.(sdk.Error)
if ok && myError.Status >= 500 {
if req.nbErrors > retry {
log.Error(ctx, "Asynchronous Request on Error: %v with status:%d", err, myError.Status)
log.ErrorWithFields(ctx, fields, "Asynchronous Request on Error: %v with status: %d", err, myError.Status)
} else {
chanRequest <- req
}
} else {
log.Error(ctx, "Asynchronous Request on Error: %v", err)
log.ErrorWithFields(ctx, fields, "Asynchronous Request on Error: %v", err)
}
}
case <-ctx.Done():
Expand All @@ -504,9 +535,16 @@ func (r *Router) Asynchronous(handler service.AsynchronousHandlerFunc, retry int
return func() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
async := asynchronousRequest{
contextValues: ContextValues(ctx),
request: *r,
vars: mux.Vars(r),
contextValues: map[interface{}]interface{}{
log.ContextLoggingRequestIDKey: ctx.Value(log.ContextLoggingRequestIDKey),
contextSession: ctx.Value(contextSession),
contextAPIConsumer: ctx.Value(contextAPIConsumer),
contextJWT: ctx.Value(contextJWT),
contextJWTRaw: ctx.Value(contextJWTRaw),
contextJWTFromCookie: ctx.Value(contextJWTFromCookie),
},
request: *r,
vars: mux.Vars(r),
}
if btes, err := ioutil.ReadAll(r.Body); err == nil {
async.body = bytes.NewBuffer(btes)
Expand Down
15 changes: 0 additions & 15 deletions engine/api/router_context.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package api

import (
"context"
)

type contextKey int

const (
Expand All @@ -14,14 +10,3 @@ const (
contextDate
contextJWTFromCookie
)

// ContextValues retuns auth values of a context
func ContextValues(ctx context.Context) map[interface{}]interface{} {
return map[interface{}]interface{}{
contextSession: ctx.Value(contextSession),
contextAPIConsumer: ctx.Value(contextAPIConsumer),
contextJWT: ctx.Value(contextJWT),
contextJWTRaw: ctx.Value(contextJWTRaw),
contextJWTFromCookie: ctx.Value(contextJWTFromCookie),
}
}
5 changes: 3 additions & 2 deletions engine/repositories/processor_checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ func (s *Service) processCheckout(ctx context.Context, op *sdk.Operation) error
}
log.Debug("processCheckout> repo cloned with current branch: %s", currentBranch)

if err := gitRepo.ResetHard("origin/" + currentBranch); err != nil {
// Clean no commited changes if exists
if err := gitRepo.ResetHard("HEAD"); err != nil {
return sdk.WithStack(err)
}
log.Debug("processCheckout> repo reset to origin/%s", currentBranch)
log.Debug("processCheckout> repo reset to HEAD")

if op.Setup.Checkout.Tag != "" {
log.Debug("processCheckout> fetching tag %s from %s", op.Setup.Checkout.Tag, op.URL)
Expand Down