Skip to content

Commit dfa6d64

Browse files
Main merge
2 parents f59df75 + b6c3ea0 commit dfa6d64

File tree

11 files changed

+1169
-64
lines changed

11 files changed

+1169
-64
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ RUN apk add --no-cache ca-certificates
1414
RUN apk add git --no-cache
1515
RUN apk add openssh --no-cache
1616
COPY --from=build-env /go/src/github.com/devtron-labs/git-sensor/git-sensor .
17+
COPY --from=build-env /go/src/github.com/devtron-labs/git-sensor/scripts/ .
1718

1819
RUN adduser -D devtron
1920
RUN chown -R devtron:devtron ./git-sensor
2021
USER devtron
2122

22-
CMD ["./git-sensor"]
23+
CMD ["./git-sensor"]

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ wire:
2222
wire
2323

2424
clean:
25-
rm -f git-sensor
25+
rm -rf git-sensor
26+
export GOFLAGS=-buildvcs=false
2627

2728
run: build
2829
./git-sensor

api/GrpcHandler.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ func (impl *GrpcHandlerImpl) FetchChanges(ctx context.Context, req *pb.FetchScmC
198198
if res.Commits != nil {
199199
pbGitCommits = make([]*pb.GitCommit, 0, len(res.Commits))
200200
for _, item := range res.Commits {
201-
201+
item.TruncateMessageIfExceedsMaxLength()
202+
if !item.IsMessageValidUTF8() {
203+
item.FixInvalidUTF8Message()
204+
}
202205
mappedCommit, err := impl.mapGitCommit(item)
203206
if err != nil {
204207
impl.logger.Debugw("failed to map git commit from bean to proto specified type",
@@ -515,8 +518,9 @@ func (impl *GrpcHandlerImpl) GetAllWebhookEventConfigForHost(ctx context.Context
515518

516519
return nil, err
517520
}
521+
webhookConfig := &pb.WebhookEventConfigResponse{}
518522
if res == nil {
519-
return nil, nil
523+
return webhookConfig, nil
520524
}
521525

522526
// Mapping response

internal/Configuration.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package internal
33
import "github.com/caarlos0/env"
44

55
type Configuration struct {
6-
CommitStatsTimeoutInSec int `env:"COMMIT_STATS_TIMEOUT_IN_SEC" envDefault:"2"`
6+
CommitStatsTimeoutInSec int `env:"COMMIT_STATS_TIMEOUT_IN_SEC" envDefault:"2"`
7+
EnableFileStats bool `env:"ENABLE_FILE_STATS" envDefault:"false"`
8+
GitHistoryCount int `env:"GIT_HISTORY_COUNT" envDefault:"15"`
9+
MinLimit int `env:"MIN_LIMIT_FOR_PVC" envDefault:"1"` // in MB
710
}
811

912
func ParseConfiguration() (*Configuration, error) {

pkg/RepoManages.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type RepoManagerImpl struct {
6565
webhookEventDataMappingRepository sql.WebhookEventDataMappingRepository
6666
webhookEventDataMappingFilterResultRepository sql.WebhookEventDataMappingFilterResultRepository
6767
webhookEventBeanConverter git.WebhookEventBeanConverter
68+
configuration *internal.Configuration
6869
}
6970

7071
func NewRepoManagerImpl(
@@ -79,6 +80,7 @@ func NewRepoManagerImpl(
7980
webhookEventDataMappingRepository sql.WebhookEventDataMappingRepository,
8081
webhookEventDataMappingFilterResultRepository sql.WebhookEventDataMappingFilterResultRepository,
8182
webhookEventBeanConverter git.WebhookEventBeanConverter,
83+
configuration *internal.Configuration,
8284
) *RepoManagerImpl {
8385
return &RepoManagerImpl{
8486
logger: logger,
@@ -93,6 +95,7 @@ func NewRepoManagerImpl(
9395
webhookEventDataMappingRepository: webhookEventDataMappingRepository,
9496
webhookEventDataMappingFilterResultRepository: webhookEventDataMappingFilterResultRepository,
9597
webhookEventBeanConverter: webhookEventBeanConverter,
98+
configuration: configuration,
9699
}
97100
}
98101

@@ -187,7 +190,7 @@ func (impl RepoManagerImpl) updatePipelineMaterialCommit(materials []*sql.CiPipe
187190
continue
188191
}
189192

190-
commits, err := impl.repositoryManager.ChangesSince(material.CheckoutLocation, pipelineMaterial.Value, "", "", 0)
193+
commits, err := impl.repositoryManager.ChangesSince(material.CheckoutLocation, pipelineMaterial.Value, "", "", impl.configuration.GitHistoryCount)
191194
//commits, err := impl.FetchChanges(pipelineMaterial.Id, "", "", 0)
192195
if err == nil {
193196
impl.logger.Infow("commits found", "commit", commits)
@@ -341,7 +344,11 @@ func (impl RepoManagerImpl) checkoutMaterial(material *sql.GitMaterial) (*sql.Gi
341344
if err != nil {
342345
return material, err
343346
}
344-
err = impl.repositoryManager.Add(material.GitProviderId, checkoutPath, material.Url, userName, password, gitProvider.AuthMode, gitProvider.SshPrivateKey)
347+
gitContext := &git.GitContext{
348+
Username: userName,
349+
Password: password,
350+
}
351+
err = impl.repositoryManager.Add(material.GitProviderId, checkoutPath, material.Url, gitContext, gitProvider.AuthMode, gitProvider.SshPrivateKey)
345352
if err == nil {
346353
material.CheckoutLocation = checkoutPath
347354
material.CheckoutStatus = true
@@ -617,17 +624,38 @@ func (impl RepoManagerImpl) GetLatestCommitForBranch(pipelineMaterialId int, bra
617624
}()
618625

619626
userName, password, err := git.GetUserNamePassword(gitMaterial.GitProvider)
620-
updated, repo, err := impl.repositoryManager.Fetch(userName, password, gitMaterial.Url, gitMaterial.CheckoutLocation)
627+
gitContext := &git.GitContext{
628+
Username: userName,
629+
Password: password,
630+
}
631+
updated, repo, err := impl.repositoryManager.Fetch(gitContext, gitMaterial.Url, gitMaterial.CheckoutLocation, gitMaterial)
632+
if !updated {
633+
impl.logger.Warn("repository is up to date")
634+
}
635+
if err == nil {
636+
gitMaterial.CheckoutStatus = true
637+
} else {
638+
gitMaterial.CheckoutStatus = false
639+
gitMaterial.CheckoutMsgAny = err.Error()
640+
gitMaterial.FetchErrorMessage = err.Error()
641+
642+
impl.logger.Errorw("error in fetching the repository ", "gitMaterial", gitMaterial, "err", err)
643+
return nil, err
644+
}
621645

646+
err = impl.materialRepository.Update(gitMaterial)
622647
if err != nil {
623-
impl.logger.Errorw("error in fetching the repository ", "err", err)
648+
impl.logger.Errorw("error in updating material repo", "err", err, "material", gitMaterial)
624649
return nil, err
625650
}
626-
if !updated {
627-
impl.logger.Warn("repository is up to date")
651+
ciPipelineMaterial, err := impl.ciPipelineMaterialRepository.FindByGitMaterialId(gitMaterial.Id)
652+
if err != nil {
653+
impl.logger.Errorw("unable to load material", "err", err, "ciPipelineMaterial", ciPipelineMaterial)
654+
return nil, err
628655
}
656+
err = impl.updatePipelineMaterialCommit(ciPipelineMaterial)
629657
if err != nil {
630-
impl.logger.Errorw("error in fetching the repository ", "err", err)
658+
impl.logger.Errorw("error in updating pipeline material", "err", err)
631659
return nil, err
632660
}
633661

pkg/git/Bean.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/devtron-labs/git-sensor/internal/sql"
2121
"gopkg.in/src-d/go-git.v4/plumbing/object"
2222
"time"
23+
"unicode/utf8"
2324
)
2425

2526
type FetchScmChangesRequest struct {
@@ -64,6 +65,29 @@ type GitCommit struct {
6465
Excluded bool `json:",omitempty"`
6566
}
6667

68+
func (gitCommit *GitCommit) TruncateMessageIfExceedsMaxLength() {
69+
maxLength := 1024
70+
if len(gitCommit.Message) > maxLength {
71+
gitCommit.Message = gitCommit.Message[:maxLength-3] + "..."
72+
}
73+
}
74+
75+
// IsMessageValidUTF8 checks if a string is valid UTF-8.
76+
func (gitCommit *GitCommit) IsMessageValidUTF8() bool {
77+
return utf8.ValidString(gitCommit.Message)
78+
}
79+
80+
// FixInvalidUTF8Message replaces invalid UTF-8 sequences with the replacement character (U+FFFD).
81+
func (gitCommit *GitCommit) FixInvalidUTF8Message() {
82+
invalidUTF8 := []rune(gitCommit.Message)
83+
for i, r := range invalidUTF8 {
84+
if !utf8.ValidRune(r) {
85+
invalidUTF8[i] = utf8.RuneError
86+
}
87+
}
88+
gitCommit.Message = string(invalidUTF8)
89+
}
90+
6791
type WebhookAndCiData struct {
6892
ExtraEnvironmentVariables map[string]string `json:"extraEnvironmentVariables"` // extra env variables which will be used for CI
6993
WebhookData *WebhookData `json:"webhookData"`

pkg/git/GitCliUtil.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package git
22

33
import (
4+
"context"
45
"fmt"
56
"go.uber.org/zap"
67
"gopkg.in/src-d/go-git.v4"
@@ -10,6 +11,13 @@ import (
1011
"strings"
1112
)
1213

14+
type GitContext struct {
15+
context.Context // Embedding original Go context
16+
Username string
17+
Password string
18+
CloningMode string
19+
}
20+
1321
type GitUtil struct {
1422
logger *zap.SugaredLogger
1523
}
@@ -22,10 +30,10 @@ func NewGitUtil(logger *zap.SugaredLogger) *GitUtil {
2230

2331
const GIT_ASK_PASS = "/git-ask-pass.sh"
2432

25-
func (impl *GitUtil) Fetch(rootDir string, username string, password string) (response, errMsg string, err error) {
33+
func (impl *GitUtil) Fetch(gitContext *GitContext, rootDir string) (response, errMsg string, err error) {
2634
impl.logger.Debugw("git fetch ", "location", rootDir)
2735
cmd := exec.Command("git", "-C", rootDir, "fetch", "origin", "--tags", "--force")
28-
output, errMsg, err := impl.runCommandWithCred(cmd, username, password)
36+
output, errMsg, err := impl.runCommandWithCred(cmd, gitContext.Username, gitContext.Password)
2937
impl.logger.Debugw("fetch output", "root", rootDir, "opt", output, "errMsg", errMsg, "error", err)
3038
return output, errMsg, err
3139
}

0 commit comments

Comments
 (0)