-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add push file size limit and report func (and use it in githook) (#1173)
- Loading branch information
1 parent
808ccda
commit e2689a3
Showing
17 changed files
with
638 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// 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) checkFileSizeLimit( | ||
ctx context.Context, | ||
rgit RestrictedGIT, | ||
repo *types.Repository, | ||
in types.GithookPreReceiveInput, | ||
output *hook.Output, | ||
) error { | ||
// return if all new refs are nil refs | ||
allNilRefs := true | ||
for _, refUpdate := range in.RefUpdates { | ||
if refUpdate.New.IsNil() { | ||
continue | ||
} | ||
allNilRefs = false | ||
break | ||
} | ||
if allNilRefs { | ||
return nil | ||
} | ||
|
||
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) | ||
} | ||
if sizeLimit <= 0 { | ||
return nil | ||
} | ||
|
||
res, err := rgit.FindOversizeFiles( | ||
ctx, | ||
&git.FindOversizeFilesParams{ | ||
RepoUID: repo.GitUID, | ||
GitObjectDirs: in.Environment.AlternateObjectDirs, | ||
SizeLimit: sizeLimit, | ||
}, | ||
) | ||
if err != nil { | ||
return fmt.Errorf("failed to get file sizes: %w", err) | ||
} | ||
|
||
if len(res.FileInfos) > 0 { | ||
output.Error = ptr.String("Changes blocked by files exceeding the file size limit") | ||
printOversizeFiles(output, res.FileInfos, sizeLimit) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// 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 reposettings | ||
|
||
import ( | ||
"github.com/harness/gitness/app/services/settings" | ||
|
||
"github.com/gotidy/ptr" | ||
) | ||
|
||
// GeneralSettings represent the general repository settings as exposed externally. | ||
type GeneralSettings struct { | ||
FileSizeLimit *int64 `json:"file_size_limit" yaml:"file_size_limit"` | ||
} | ||
|
||
func GetDefaultGeneralSettings() *GeneralSettings { | ||
return &GeneralSettings{ | ||
FileSizeLimit: ptr.Int64(settings.DefaultFileSizeLimit), | ||
} | ||
} | ||
|
||
func GetGeneralSettingsMappings(s *GeneralSettings) []settings.SettingHandler { | ||
return []settings.SettingHandler{ | ||
settings.Mapping(settings.KeyFileSizeLimit, s.FileSizeLimit), | ||
} | ||
} | ||
|
||
func GetGeneralSettingsAsKeyValues(s *GeneralSettings) []settings.KeyValue { | ||
kvs := make([]settings.KeyValue, 0, 1) | ||
|
||
if s.FileSizeLimit != nil { | ||
kvs = append(kvs, settings.KeyValue{ | ||
Key: settings.KeyFileSizeLimit, | ||
Value: s.FileSizeLimit, | ||
}) | ||
} | ||
return kvs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 reposettings | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/harness/gitness/app/auth" | ||
"github.com/harness/gitness/types/enum" | ||
) | ||
|
||
// GeneralFind returns the general settings of a repo. | ||
func (c *Controller) GeneralFind( | ||
ctx context.Context, | ||
session *auth.Session, | ||
repoRef string, | ||
) (*GeneralSettings, error) { | ||
repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoView, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out := GetDefaultGeneralSettings() | ||
mappings := GetGeneralSettingsMappings(out) | ||
err = c.settings.RepoMap(ctx, repo.ID, mappings...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to map settings: %w", err) | ||
} | ||
|
||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// 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 reposettings | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/harness/gitness/app/auth" | ||
"github.com/harness/gitness/app/paths" | ||
"github.com/harness/gitness/audit" | ||
"github.com/harness/gitness/types/enum" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// GeneralUpdate updates the general settings of the repo. | ||
func (c *Controller) GeneralUpdate( | ||
ctx context.Context, | ||
session *auth.Session, | ||
repoRef string, | ||
in *GeneralSettings, | ||
) (*GeneralSettings, error) { | ||
repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoEdit, false) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// read old settings values | ||
old := GetDefaultGeneralSettings() | ||
oldMappings := GetGeneralSettingsMappings(old) | ||
err = c.settings.RepoMap(ctx, repo.ID, oldMappings...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to map settings (old): %w", err) | ||
} | ||
|
||
err = c.settings.RepoSetMany(ctx, repo.ID, GetGeneralSettingsAsKeyValues(in)...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to set settings: %w", err) | ||
} | ||
|
||
// read all settings and return complete config | ||
out := GetDefaultGeneralSettings() | ||
mappings := GetGeneralSettingsMappings(out) | ||
err = c.settings.RepoMap(ctx, repo.ID, mappings...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to map settings: %w", err) | ||
} | ||
|
||
err = c.auditService.Log(ctx, | ||
session.Principal, | ||
audit.NewResource(audit.ResourceTypeRepositorySettings, repo.Identifier), | ||
audit.ActionUpdated, | ||
paths.Parent(repo.Path), | ||
audit.WithOldObject(old), | ||
audit.WithNewObject(out), | ||
) | ||
if err != nil { | ||
log.Ctx(ctx).Warn().Msgf("failed to insert audit log for update repository settings operation: %s", err) | ||
} | ||
|
||
return out, nil | ||
} |
Oops, something went wrong.