mirror of
https://github.com/grafana/grafana.git
synced 2025-07-30 03:02:44 +08:00

* Spike: Extras * Attempt to wire it up * Hack * Fix issue with jobs * Wire more things up * Fix more wiring stuff * Remove webhook secret key from main registration * Move secret encryption also outside register * Add TODOs in code * Add more explanations * Move connectors to different package * Move pull request job into webhooks * Separate registration * Remove duplicate files * Fix missing function * Extract webhook repository logic out of the core github repository * Use status patcher in webhook connector * Fix change in go mod * Change hooks signature * Remove TODOs * Remove Webhook methos from go-git * Remove leftover * Fix mistake in OpenAPI spec * Fix some tests * Fix some issues * Fix linting
98 lines
2.7 KiB
Go
98 lines
2.7 KiB
Go
package provisioning
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
|
|
|
"github.com/grafana/grafana-app-sdk/logging"
|
|
provisioning "github.com/grafana/grafana/pkg/apis/provisioning/v0alpha1"
|
|
"github.com/grafana/grafana/pkg/registry/apis/provisioning/repository"
|
|
"github.com/grafana/grafana/pkg/registry/apis/provisioning/safepath"
|
|
)
|
|
|
|
type historySubresource struct {
|
|
repoGetter RepoGetter
|
|
}
|
|
|
|
func (h *historySubresource) New() runtime.Object {
|
|
// This is added as the "ResponseType" regardless what ProducesObject() returns
|
|
return &provisioning.HistoryList{}
|
|
}
|
|
|
|
func (h *historySubresource) Destroy() {}
|
|
|
|
func (h *historySubresource) NamespaceScoped() bool {
|
|
return true
|
|
}
|
|
|
|
func (h *historySubresource) GetSingularName() string {
|
|
return "History"
|
|
}
|
|
|
|
func (h *historySubresource) ProducesMIMETypes(verb string) []string {
|
|
return []string{"application/json"}
|
|
}
|
|
|
|
func (h *historySubresource) ProducesObject(verb string) runtime.Object {
|
|
return &provisioning.HistoryList{}
|
|
}
|
|
|
|
func (h *historySubresource) ConnectMethods() []string {
|
|
return []string{http.MethodGet}
|
|
}
|
|
|
|
func (h *historySubresource) NewConnectOptions() (runtime.Object, bool, string) {
|
|
return nil, true, "" // true adds the {path} component
|
|
}
|
|
|
|
func (h *historySubresource) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
|
|
logger := logging.FromContext(ctx).With("logger", "history-subresource")
|
|
ctx = logging.Context(ctx, logger)
|
|
repo, err := h.repoGetter.GetHealthyRepository(ctx, name)
|
|
if err != nil {
|
|
logger.Debug("failed to find repository", "error", err)
|
|
return nil, err
|
|
}
|
|
|
|
return WithTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
versioned, ok := repo.(repository.Versioned)
|
|
if !ok {
|
|
responder.Error(apierrors.NewBadRequest("this repository does not support history"))
|
|
return
|
|
}
|
|
|
|
query := r.URL.Query()
|
|
ref := query.Get("ref")
|
|
|
|
filePath, err := pathAfterPrefix(r.URL.Path, fmt.Sprintf("/%s/history/", name))
|
|
if err != nil {
|
|
responder.Error(apierrors.NewBadRequest(err.Error()))
|
|
return
|
|
}
|
|
|
|
if err := safepath.IsSafe(filePath); err != nil {
|
|
responder.Error(apierrors.NewBadRequest(err.Error()))
|
|
return
|
|
}
|
|
|
|
logger = logger.With("ref", ref, "path", filePath)
|
|
ctx = logging.Context(r.Context(), logger)
|
|
|
|
// TODO: Add history pagination
|
|
commits, err := versioned.History(ctx, filePath, ref)
|
|
if err != nil {
|
|
logger.Debug("failed to get history", "error", err)
|
|
responder.Error(err)
|
|
return
|
|
}
|
|
|
|
responder.Object(http.StatusOK, &provisioning.HistoryList{Items: commits})
|
|
}), 30*time.Second), nil
|
|
}
|