drone/cli/operations/hooks/hooks.go
Johannes Batzill 3b120dd2b3 Add Zerolog Support to GITRPC (#126)
This change adds the following:
- Inject APP server zerolog RequestID as metadata into all gitrpc calls from client side.
- Inject Zerolog logger into context with common fields set (like service, method, requestID, ...). This allows for better request tracking within gitrpc, but also request tracking across services we extract the requestID send by the grpc client
- Modify http logs to use http. prefix for common http related logging annotations.
2022-12-16 08:39:10 -08:00

69 lines
1.8 KiB
Go

// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package hooks
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/harness/gitness/client"
"github.com/rs/zerolog/log"
"gopkg.in/alecthomas/kingpin.v2"
)
type updatedRefData struct {
branch string
oldCommit string
newCommit string
}
func Register(app *kingpin.Application, client client.Client) {
cmd := app.Command("hooks", "manage git server hooks")
registerUpdate(cmd, client)
registerPostReceive(cmd, client)
registerPreReceive(cmd, client)
}
// getUpdatedReferencesFromStdIn reads the updated references provided by git from stdin.
// The expected format is "<old-value> SP <new-value> SP <ref-name> LF"
// For more details see https://git-scm.com/docs/githooks#pre-receive
func getUpdatedReferencesFromStdIn() ([]updatedRefData, error) {
reader := bufio.NewReader(os.Stdin)
var lines []string
for {
line, err := reader.ReadString('\n')
// if end of file is reached, break the loop
if err == io.EOF {
break
}
if err != nil {
log.Error().Msgf("Error when reading from standard input - %v", err)
return nil, err
}
lines = append(lines, line)
}
var updatedRefs []updatedRefData
for _, data := range lines {
// splitting line of expected form "<old-value> SP <new-value> SP <ref-name> LF"
splitGitHookData := strings.Split(data, " ")
if len(splitGitHookData) != 3 {
return nil, fmt.Errorf("received invalid data format or didn't receive enough parameters - %v",
splitGitHookData)
}
updatedRefs = append(updatedRefs, updatedRefData{
oldCommit: splitGitHookData[0],
newCommit: splitGitHookData[1],
branch: splitGitHookData[2],
})
}
return updatedRefs, nil
}