mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 06:31:50 +08:00
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package repository
|
|
|
|
import (
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
|
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
|
)
|
|
|
|
func IsWriteAllowed(repo *provisioning.Repository, ref string) error {
|
|
if len(repo.Spec.Workflows) == 0 {
|
|
return apierrors.NewBadRequest("this repository is read only")
|
|
}
|
|
|
|
var supportsWrite, supportsBranch bool
|
|
for _, v := range repo.Spec.Workflows {
|
|
switch v {
|
|
case provisioning.WriteWorkflow:
|
|
supportsWrite = true
|
|
case provisioning.BranchWorkflow:
|
|
supportsBranch = repo.Spec.Type.IsGit()
|
|
}
|
|
}
|
|
|
|
// Ref may be the configured branch for github repositories
|
|
if ref != "" && repo.Spec.GitHub != nil && repo.Spec.GitHub.Branch == ref {
|
|
ref = ""
|
|
}
|
|
|
|
// Ref may be the configured branch for git repositories
|
|
if ref != "" && repo.Spec.Git != nil && repo.Spec.Git.Branch == ref {
|
|
ref = ""
|
|
}
|
|
|
|
switch {
|
|
case ref == "" && !supportsWrite:
|
|
return apierrors.NewBadRequest("this repository does not support the write workflow")
|
|
case ref != "" && !supportsBranch:
|
|
return apierrors.NewBadRequest("this repository does not support the branch workflow")
|
|
default:
|
|
return nil
|
|
}
|
|
}
|