Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: Gitops commit state #451

Merged
merged 6 commits into from Mar 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 87 additions & 2 deletions pkg/dashboard/server/flux.go
Expand Up @@ -13,11 +13,15 @@ import (
"github.com/gimlet-io/gimlet-cli/pkg/dashboard/notifications"
"github.com/gimlet-io/gimlet-cli/pkg/dashboard/server/streaming"
"github.com/gimlet-io/gimlet-cli/pkg/dashboard/store"
"github.com/gimlet-io/gimlet-cli/pkg/dx"
"github.com/gimlet-io/gimlet-cli/pkg/git/nativeGit"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
log "github.com/sirupsen/logrus"
)

func fluxEvent(w http.ResponseWriter, r *http.Request) {

buf, _ := ioutil.ReadAll(r.Body)
rdr1 := ioutil.NopCloser(bytes.NewBuffer(buf))
rdr2 := ioutil.NopCloser(bytes.NewBuffer(buf))
Expand Down Expand Up @@ -54,6 +58,15 @@ func fluxEvent(w http.ResponseWriter, r *http.Request) {
log.Errorf("could not save or update gitops commit: %s", err)
}

gitopsRepoCache := ctx.Value("gitRepoCache").(*nativeGit.RepoCache)
clientHub, _ := ctx.Value("clientHub").(*streaming.ClientHub)
err = updateGitopsCommitStatuses(clientHub, gitopsRepoCache, store, event, repoName, env)
if err != nil {
log.Errorf("cannot update releases: %s", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}

dzsak marked this conversation as resolved.
Show resolved Hide resolved
if !stateUpdated {
w.WriteHeader(http.StatusOK)
w.Write([]byte(""))
Expand All @@ -63,7 +76,6 @@ func fluxEvent(w http.ResponseWriter, r *http.Request) {
notificationsManager := ctx.Value("notificationsManager").(notifications.Manager)
notificationsManager.Broadcast(notifications.NewMessage(repoName, gitopsCommit, env))

clientHub, _ := ctx.Value("clientHub").(*streaming.ClientHub)
streaming.BroadcastGitopsCommitEvent(clientHub, *gitopsCommit)

w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -101,3 +113,76 @@ func parseRev(rev string) (string, error) {

return parts[1], nil
}

func updateGitopsCommitStatuses(
clientHub *streaming.ClientHub,
gitopsRepoCache *nativeGit.RepoCache,
store *store.Store,
event fluxEvents.Event,
repoName, env string,
) error {
if _, ok := event.Metadata["revision"]; !ok {
return fmt.Errorf("could not extract gitops sha from Flux message: %s", event)
}
eventHash, err := parseRev(event.Metadata["revision"])
if err != nil {
return err
}

repo, err := gitopsRepoCache.InstanceForRead(repoName)
if err != nil {
return err
}

hash := plumbing.NewHash(eventHash)
commitWalker, err := repo.Log(&git.LogOptions{
From: hash,
})
if err != nil {
return err
}

err = commitWalker.ForEach(func(c *object.Commit) error {
hashString := c.Hash.String()
if hashString == eventHash {
return nil
}

gitopsCommitFromDb, err := store.GitopsCommit(hashString)
if err != nil {
log.Warnf("cannot get gitops commit: %s", err)
return nil
}

if gitopsCommitFromDb != nil &&
(gitopsCommitFromDb.Status == dx.ValidationFailed ||
gitopsCommitFromDb.Status == dx.ReconciliationFailed ||
gitopsCommitFromDb.Status == dx.HealthCheckFailed ||
gitopsCommitFromDb.Status == dx.ReconciliationSucceeded) {
return fmt.Errorf("%s", "EOF")
}

gitopsCommitToSave := model.GitopsCommit{
Sha: hashString,
Status: event.Reason,
StatusDesc: event.Message,
Created: event.Timestamp.Unix(),
Env: env,
}

_, err = store.SaveOrUpdateGitopsCommit(&gitopsCommitToSave)
if err != nil {
log.Warnf("could not save or update gitops commit: %s", err)
return nil
}
streaming.BroadcastGitopsCommitEvent(clientHub, gitopsCommitToSave)

return nil
})
if err != nil &&
err.Error() != "EOF" {
return err
}

return nil
}