merge changes from main

eb/code-1016-2
enver 2024-03-21 15:25:06 +01:00
commit 6d649d041a
17 changed files with 66 additions and 45 deletions

View File

@ -100,7 +100,7 @@ delete-tools: ## Delete the tools
# Install golangci-lint
$(GOBIN)/golangci-lint:
@echo "🔘 Installing golangci-lint... (`date '+%H:%M:%S'`)"
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN)
@curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.56.2
# Install goimports to format code
$(GOBIN)/goimports:

View File

@ -17,6 +17,7 @@ package pullreq
import (
"context"
"fmt"
"strings"
"time"
apiauth "github.com/harness/gitness/app/api/auth"
@ -42,6 +43,8 @@ import (
type MergeInput struct {
Method enum.MergeMethod `json:"method"`
SourceSHA string `json:"source_sha"`
Title string `json:"title"`
Message string `json:"message"`
BypassRules bool `json:"bypass_rules"`
DryRun bool `json:"dry_run"`
}
@ -64,6 +67,14 @@ func (in *MergeInput) sanitize() error {
in.Method = method
}
// cleanup title / message (NOTE: git doesn't support white space only)
in.Title = strings.TrimSpace(in.Title)
in.Message = strings.TrimSpace(in.Message)
if in.Method == enum.MergeMethodRebase && (in.Title != "" || in.Message != "") {
return usererror.BadRequest("rebase doesn't support customizing commit title and message")
}
return nil
}
@ -306,15 +317,16 @@ func (c *Controller) Merge(
committer = identityFromPrincipalInfo(*session.Principal.ToPrincipalInfo())
}
var mergeTitle string
switch in.Method {
case enum.MergeMethodMerge:
mergeTitle = fmt.Sprintf("Merge branch '%s' of %s (#%d)", pr.SourceBranch, sourceRepo.Path, pr.Number)
case enum.MergeMethodSquash:
mergeTitle = fmt.Sprintf("%s (#%d)", pr.Title, pr.Number)
case enum.MergeMethodRebase:
mergeTitle = "" // Not used.
// backfill commit title if none provided
if in.Title == "" {
switch in.Method {
case enum.MergeMethodMerge:
in.Title = fmt.Sprintf("Merge branch '%s' of %s (#%d)", pr.SourceBranch, sourceRepo.Path, pr.Number)
case enum.MergeMethodSquash:
in.Title = fmt.Sprintf("%s (#%d)", pr.Title, pr.Number)
case enum.MergeMethodRebase:
// Not used.
}
}
// create merge commit(s)
@ -327,8 +339,8 @@ func (c *Controller) Merge(
BaseBranch: pr.TargetBranch,
HeadRepoUID: sourceRepo.GitUID,
HeadBranch: pr.SourceBranch,
Title: mergeTitle,
Message: "",
Title: in.Title,
Message: in.Message,
Committer: committer,
CommitterDate: &now,
Author: author,

View File

@ -36,6 +36,7 @@ import (
"github.com/harness/gitness/lock"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
)
@ -64,6 +65,7 @@ type Controller struct {
indexer keywordsearch.Indexer
resourceLimiter limiter.ResourceLimiter
mtxManager lock.MutexManager
identifierCheck check.RepoIdentifier
}
func NewController(
@ -85,6 +87,7 @@ func NewController(
indexer keywordsearch.Indexer,
limiter limiter.ResourceLimiter,
mtxManager lock.MutexManager,
identifierCheck check.RepoIdentifier,
) *Controller {
return &Controller{
defaultBranch: config.Git.DefaultBranch,
@ -106,6 +109,7 @@ func NewController(
indexer: indexer,
resourceLimiter: limiter,
mtxManager: mtxManager,
identifierCheck: identifierCheck,
}
}

View File

@ -164,7 +164,7 @@ func (c *Controller) sanitizeCreateInput(in *CreateInput) error {
return err
}
if err := check.RepoIdentifier(in.Identifier); err != nil {
if err := c.identifierCheck(in.Identifier); err != nil {
return err
}

View File

@ -22,7 +22,6 @@ import (
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/services/importer"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
)
type ImportInput struct {
@ -98,7 +97,7 @@ func (c *Controller) sanitizeImportInput(in *ImportInput) error {
return err
}
if err := check.RepoIdentifier(in.Identifier); err != nil {
if err := c.identifierCheck(in.Identifier); err != nil {
return err
}

View File

@ -22,7 +22,6 @@ import (
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/harness/gitness/types/enum"
)
@ -93,7 +92,7 @@ func (c *Controller) sanitizeMoveInput(in *MoveInput) error {
}
if in.Identifier != nil {
if err := check.RepoIdentifier(*in.Identifier); err != nil {
if err := c.identifierCheck(*in.Identifier); err != nil {
return err
}
}

View File

@ -28,6 +28,7 @@ import (
"github.com/harness/gitness/lock"
"github.com/harness/gitness/store/database/dbtx"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/check"
"github.com/google/wire"
)
@ -56,10 +57,11 @@ func ProvideController(
indexer keywordsearch.Indexer,
limiter limiter.ResourceLimiter,
mtxManager lock.MutexManager,
identifierCheck check.RepoIdentifier,
) *Controller {
return NewController(config, tx, urlProvider,
authorizer, repoStore,
spaceStore, pipelineStore,
principalStore, ruleStore, principalInfoCache, protectionManager,
rpcClient, importer, codeOwners, reporeporter, indexer, limiter, mtxManager)
rpcClient, importer, codeOwners, reporeporter, indexer, limiter, mtxManager, identifierCheck)
}

View File

@ -131,7 +131,7 @@ func mapFileStats(c *git.Commit) []types.CommitFileStats {
fileStats[i] = types.CommitFileStats{
Path: fStat.Path,
OldPath: fStat.OldPath,
Status: fStat.ChangeType,
Status: fStat.Status,
ChangeStats: types.ChangeStats{
Insertions: fStat.Insertions,
Deletions: fStat.Deletions,

View File

@ -192,18 +192,18 @@ func commitInfoFrom(commit git.Commit) CommitInfo {
for _, stat := range commit.FileStats {
switch {
case stat.ChangeType == gitenum.FileDiffStatusModified:
case stat.Status == gitenum.FileDiffStatusModified:
modified = append(modified, stat.Path)
case stat.ChangeType == gitenum.FileDiffStatusRenamed:
case stat.Status == gitenum.FileDiffStatusRenamed:
added = append(added, stat.Path)
removed = append(removed, stat.OldPath)
case stat.ChangeType == gitenum.FileDiffStatusDeleted:
case stat.Status == gitenum.FileDiffStatusDeleted:
removed = append(removed, stat.Path)
case stat.ChangeType == gitenum.FileDiffStatusAdded || stat.ChangeType == gitenum.FileDiffStatusCopied:
case stat.Status == gitenum.FileDiffStatusAdded || stat.Status == gitenum.FileDiffStatusCopied:
added = append(added, stat.Path)
case stat.ChangeType == gitenum.FileDiffStatusUndefined:
case stat.Status == gitenum.FileDiffStatusUndefined:
default:
log.Warn().Msgf("unknown change type %q for path %q", stat.ChangeType, stat.Path)
log.Warn().Msgf("unknown status %q for path %q", stat.Status, stat.Path)
}
}

View File

@ -187,7 +187,8 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
if err != nil {
return nil, err
}
repoController := repo.ProvideController(config, transactor, provider, authorizer, repoStore, spaceStore, pipelineStore, principalStore, ruleStore, principalInfoCache, protectionManager, gitInterface, repository, codeownersService, reporter, indexer, resourceLimiter, mutexManager)
repoIdentifier := check.ProvideRepoIdentifierCheck()
repoController := repo.ProvideController(config, transactor, provider, authorizer, repoStore, spaceStore, pipelineStore, principalStore, ruleStore, principalInfoCache, protectionManager, gitInterface, repository, codeownersService, reporter, indexer, resourceLimiter, mutexManager, repoIdentifier)
executionStore := database.ProvideExecutionStore(db)
checkStore := database.ProvideCheckStore(db, principalInfoCache)
stageStore := database.ProvideStageStore(db)

View File

@ -268,7 +268,7 @@ func getCommitFileStats(
fileStats[i] = CommitFileStats{
Path: changeInfoTypes[path].Path,
OldPath: changeInfoTypes[path].OldPath,
ChangeType: changeInfoTypes[path].ChangeType,
ChangeType: changeInfoTypes[path].Status,
Insertions: info.Insertions,
Deletions: info.Deletions,
}
@ -345,7 +345,7 @@ func gitGetRenameDetails(
}
for _, c := range changeInfos {
if c.ChangeType == enum.FileDiffStatusRenamed && (c.OldPath == path || c.Path == path) {
if c.Status == enum.FileDiffStatusRenamed && (c.OldPath == path || c.Path == path) {
return &PathRenameDetails{
OldPath: c.OldPath,
Path: c.Path,
@ -360,8 +360,8 @@ func gitLogNameStatus(ctx context.Context, repoPath string, sha sha.SHA) ([]stri
cmd := command.New("log",
command.WithFlag("--name-status"),
command.WithFlag("--format="),
command.WithArg(sha.String()),
command.WithFlag("--max-count=1"),
command.WithArg(sha.String()),
)
output := &bytes.Buffer{}
err := cmd.Run(ctx, command.WithDir(repoPath), command.WithStdout(output))
@ -419,7 +419,7 @@ func getChangeInfoTypes(
c.Path = lineParts[1]
}
c.ChangeType = convertChangeType(ctx, line)
c.Status = convertFileDiffStatus(ctx, line)
changeInfoTypes[c.Path] = c
}
@ -478,16 +478,16 @@ func getChangeInfoChanges(
}
type changeInfoType struct {
ChangeType enum.FileDiffStatus
OldPath string // populated only in case of renames
Path string
Status enum.FileDiffStatus
OldPath string // populated only in case of renames
Path string
}
type changeInfoChange struct {
Insertions int64
Deletions int64
}
func convertChangeType(ctx context.Context, c string) enum.FileDiffStatus {
func convertFileDiffStatus(ctx context.Context, c string) enum.FileDiffStatus {
switch {
case strings.HasPrefix(c, "A"):
return enum.FileDiffStatusAdded

View File

@ -124,7 +124,7 @@ type ListCommitsOutput struct {
}
type CommitFileStats struct {
ChangeType enum.FileDiffStatus
Status enum.FileDiffStatus
Path string
OldPath string // populated only in case of renames
Insertions int64

View File

@ -73,7 +73,7 @@ func mapFileStats(typeStats []api.CommitFileStats) []CommitFileStats {
for i, tStat := range typeStats {
stats[i] = CommitFileStats{
ChangeType: tStat.ChangeType,
Status: tStat.ChangeType,
Path: tStat.Path,
OldPath: tStat.OldPath,
Insertions: tStat.Insertions,

View File

@ -121,8 +121,10 @@ func Identifier(identifier string) error {
return nil
}
// RepoIdentifier performs the default Identifier check and also blocks illegal repo identifiers.
func RepoIdentifier(identifier string) error {
type RepoIdentifier func(identifier string) error
// RepoIdentifierDefault performs the default Identifier check and also blocks illegal repo identifiers.
func RepoIdentifierDefault(identifier string) error {
if err := Identifier(identifier); err != nil {
return err
}

View File

@ -22,6 +22,7 @@ import (
var WireSet = wire.NewSet(
ProvidePrincipalUIDCheck,
ProvideSpaceIdentifierCheck,
ProvideRepoIdentifierCheck,
)
func ProvideSpaceIdentifierCheck() SpaceIdentifier {
@ -31,3 +32,7 @@ func ProvideSpaceIdentifierCheck() SpaceIdentifier {
func ProvidePrincipalUIDCheck() PrincipalUID {
return PrincipalUIDDefault
}
func ProvideRepoIdentifierCheck() RepoIdentifier {
return RepoIdentifierDefault
}

View File

@ -39,7 +39,6 @@ const Editor = forwardRef<MonacoCodeEditorRef, AdvancedSourceCodeEditorProps>((p
const { getString } = useStrings()
const { onChange, onEntityAddUpdate, onEntityFieldAddUpdate } = props
const editorRef = useRef<MonacoCodeEditorRef | null>(null)
const [latestYAML, setLatestYAML] = useState<string>('')
const [entityYAMLData, setEntityYAMLData] = useState<EntityAddUpdateInterface>()
const [entityFieldData, setEntityFieldYAMLData] = useState<Partial<EntityAddUpdateInterface>>()
const entityYAMLDataRef = useRef<EntityAddUpdateInterface>()
@ -50,10 +49,6 @@ const Editor = forwardRef<MonacoCodeEditorRef, AdvancedSourceCodeEditorProps>((p
setForwardedRef(ref, editor)
}
useEffect(() => {
onChange?.(latestYAML)
}, [latestYAML]) // eslint-disable-line react-hooks/exhaustive-deps
useEffect(() => {
if (!isEmpty(entityYAMLData)) {
onEntityAddUpdate(entityYAMLData)
@ -179,7 +174,7 @@ const Editor = forwardRef<MonacoCodeEditorRef, AdvancedSourceCodeEditorProps>((p
const debouncedHandleYAMLUpdate = useMemo(
() =>
debounce((updatedYAML: string) => {
setLatestYAML(updatedYAML)
onChange?.(updatedYAML)
prepareEntityFieldUpdate()
}, 300),
[prepareEntityFieldUpdate]

View File

@ -15,7 +15,7 @@
*/
import React, { useMemo } from 'react'
import { Container, Layout, Button, FlexExpander, ButtonVariation, Text } from '@harnessio/uicore'
import { Container, Layout, Button, FlexExpander, ButtonVariation, Text, ButtonSize } from '@harnessio/uicore'
import cx from 'classnames'
import { Icon } from '@harnessio/icons'
import { Color } from '@harnessio/design-system'
@ -33,6 +33,7 @@ import { useGetSpaceParam } from 'hooks/useGetSpaceParam'
import { permissionProps } from 'utils/Utils'
import CodeSearch from 'components/CodeSearch/CodeSearch'
import { useDocumentTitle } from 'hooks/useDocumentTitle'
import { CopyButton } from 'components/CopyButton/CopyButton'
import css from './ContentHeader.module.scss'
export function ContentHeader({
@ -122,6 +123,7 @@ export function ContentHeader({
)
}}
/>
{resourcePath && <CopyButton content={resourcePath} icon={CodeIcon.Copy} size={ButtonSize.MEDIUM} />}
</Layout.Horizontal>
</Container>
<FlexExpander />