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

Fixes #1006: Keep track of project status even if plans have been deleted #1005

Merged
merged 12 commits into from
Jun 24, 2020
2 changes: 2 additions & 0 deletions server/events/command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ func (c *DefaultCommandRunner) automerge(ctx *CommandContext, pullStatus models.

// Make the API call to perform the merge.
ctx.Log.Info("automerging pull request")

err := c.VCSClient.MergePull(ctx.Pull)

if err != nil {
Expand All @@ -337,6 +338,7 @@ func (c *DefaultCommandRunner) automerge(ctx *CommandContext, pullStatus models.
ctx.Log.Err("failed to comment about automerge failing: %s", err)
}
}

}

func (c *DefaultCommandRunner) runProjectCmds(cmds []models.ProjectCommandContext, cmdName models.CommandName) CommandResult {
Expand Down
35 changes: 35 additions & 0 deletions server/events/db/boltdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,41 @@ func (b *BoltDB) DeleteProjectStatus(pull models.PullRequest, workspace string,
return errors.Wrap(err, "DB transaction failed")
}

// UpdateProjectStatus updates all project statuses under pull that match
// workspace and repoRelDir.
func (b *BoltDB) UpdateProjectStatus(pull models.PullRequest, workspace string, repoRelDir string, targetStatus models.ProjectPlanStatus) error {
key, err := b.pullKey(pull)
if err != nil {
return err
}
err = b.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(b.pullsBucketName)
currStatusPtr, err := b.getPullFromBucket(bucket, key)
if err != nil {
return err
}
if currStatusPtr == nil {
return nil
}
currStatus := *currStatusPtr

// Create a new projectStatuses array without the ones we want to
// delete.
var newProjects []models.ProjectStatus
for _, p := range currStatus.Projects {
if p.Workspace == workspace && p.RepoRelDir == repoRelDir {
p.Status = targetStatus
}
newProjects = append(newProjects, p)
}

// Overwrite the old pull status.
currStatus.Projects = newProjects
return b.writePullToBucket(bucket, key, currStatus)
})
return errors.Wrap(err, "DB transaction failed")
}

func (b *BoltDB) pullKey(pull models.PullRequest) ([]byte, error) {
hostname := pull.BaseRepo.VCSHost.Hostname
if strings.Contains(hostname, pullKeySeparator) {
Expand Down
7 changes: 6 additions & 1 deletion server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,12 +476,15 @@ const (
// PlannedPlanStatus means that a plan has been successfully generated but
// not yet applied.
PlannedPlanStatus
// ErrorApplyStatus means that a plan has been generated but there was an
// ErroredApplyStatus means that a plan has been generated but there was an
// error while applying it.
ErroredApplyStatus
// AppliedPlanStatus means that a plan has been generated and applied
// successfully.
AppliedPlanStatus
// NotPlannedPlanStatus means that either no plan has been generated yet or
// a plan has been deleted - eg after using the UI to unlock the project
NotPlannedPlanStatus
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only set when the project is unlocked, not when no plan has been generated yet, so maybe we can call this DiscardedPlanStatus and update the description to say:

means that there was an unapplied plan that was discarded due to a project being unlocked

)

// String returns a string representation of the status.
Expand All @@ -495,6 +498,8 @@ func (p ProjectPlanStatus) String() string {
return "apply_errored"
case AppliedPlanStatus:
return "applied"
case NotPlannedPlanStatus:
return "not_planned"
default:
panic("missing String() impl for ProjectPlanStatus")
}
Expand Down
4 changes: 2 additions & 2 deletions server/locks_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func (l *LocksController) DeleteLock(w http.ResponseWriter, r *http.Request) {
l.Logger.Err("unable to delete workspace: %s", err)
}
}
if err := l.DB.DeleteProjectStatus(lock.Pull, lock.Workspace, lock.Project.Path); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should delete the DeleteProjectStatus method since it's not used anymore

l.Logger.Err("unable to delete project status: %s", err)
if err := l.DB.UpdateProjectStatus(lock.Pull, lock.Workspace, lock.Project.Path, models.NotPlannedPlanStatus); err != nil {
l.Logger.Err("unable to update project status: %s", err)
}

// Once the lock has been deleted, comment back on the pull request.
Expand Down