drone/app/api/controller/githook/pre_receive_process.go
Darko Draskovic 07224c37de feat: [CODE-3352]: Add git pre-receive preprocessor (#3535)
* Replace map[string]string with []CommitInfo in FindCommitterMismatchOutput
* Merge remote-tracking branch 'origin/main' into dd/pre-preceive-preprocessor
* Merge remote-tracking branch 'origin/main' into dd/pre-preceive-preprocessor
* Add total to FindCommitterMismatchOutput
* Reuse changedRefs for only deleted branches on push check
* Merge remote-tracking branch 'origin/main' into dd/pre-preceive-preprocessor
* Fix typo commiter -> committer
* Rename listAllObjects in listGitObjDir and loop thru dirs outside listGitObjDir
* Use line with commiter prefix to find commiter email
* Merge remote-tracking branch 'origin/main' into dd/pre-preceive-preprocessor
* Merge remote-tracking branch 'origin/main' into dd/pre-preceive-preprocessor
* Rename vars and add total to findOversizeFiles
* Use CatFileBatch instead of git show info to find commiter emails and remove unused CatFileBatchCheck
* Use WithAlternateObjectDirs in findCommiterEmailsMismatch
* Merge remote-tracking branch 'origin/mai
2025-03-18 20:21:28 +00:00

120 lines
3.2 KiB
Go

// Copyright 2023 Harness, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package githook
import (
"context"
"fmt"
"github.com/harness/gitness/app/services/settings"
"github.com/harness/gitness/git"
"github.com/harness/gitness/git/hook"
"github.com/harness/gitness/types"
"github.com/gotidy/ptr"
)
func (c *Controller) processObjects(
ctx context.Context,
repo *types.RepositoryCore,
principal *types.Principal,
refUpdates changedRefs,
in types.GithookPreReceiveInput,
output *hook.Output,
) error {
if refUpdates.hasOnlyDeletedBranches() {
return nil
}
var sizeLimit int64
var err error
sizeLimit, err = settings.RepoGet(
ctx,
c.settings,
repo.ID,
settings.KeyFileSizeLimit,
settings.DefaultFileSizeLimit,
)
if err != nil {
return fmt.Errorf("failed to check settings for file size limit: %w", err)
}
principalCommitterMatch, err := settings.RepoGet(
ctx,
c.settings,
repo.ID,
settings.KeyPrincipalCommitterMatch,
settings.DefaultPrincipalCommitterMatch,
)
if err != nil {
return fmt.Errorf("failed to check settings for principal committer match: %w", err)
}
if sizeLimit == 0 && !principalCommitterMatch {
return nil
}
preReceiveObjsIn := git.ProcessPreReceiveObjectsParams{
ReadParams: git.ReadParams{
RepoUID: repo.GitUID,
AlternateObjectDirs: in.Environment.AlternateObjectDirs,
},
}
if sizeLimit > 0 {
preReceiveObjsIn.FindOversizeFilesParams = &git.FindOversizeFilesParams{
SizeLimit: sizeLimit,
}
}
if principalCommitterMatch && principal != nil {
preReceiveObjsIn.FindCommitterMismatchParams = &git.FindCommitterMismatchParams{
PrincipalEmail: principal.Email,
}
}
preReceiveObjsOut, err := c.git.ProcessPreReceiveObjects(
ctx,
preReceiveObjsIn,
)
if err != nil {
return fmt.Errorf("failed to process pre-receive objects: %w", err)
}
if preReceiveObjsOut.FindOversizeFilesOutput != nil &&
len(preReceiveObjsOut.FindOversizeFilesOutput.FileInfos) > 0 {
output.Error = ptr.String("Changes blocked by files exceeding the file size limit")
printOversizeFiles(
output,
preReceiveObjsOut.FindOversizeFilesOutput.FileInfos,
preReceiveObjsOut.FindOversizeFilesOutput.Total,
sizeLimit,
)
}
if preReceiveObjsOut.FindCommitterMismatchOutput != nil &&
len(preReceiveObjsOut.FindCommitterMismatchOutput.CommitInfos) > 0 {
output.Error = ptr.String("Committer verification failed: authenticated user and committer must match")
printCommitterMismatch(
output,
preReceiveObjsOut.FindCommitterMismatchOutput.CommitInfos,
preReceiveObjsIn.FindCommitterMismatchParams.PrincipalEmail,
preReceiveObjsOut.FindCommitterMismatchOutput.Total,
)
}
return nil
}