Skip to content

Commit

Permalink
Merge pull request #23 from ScaCap/gh-21/remove-dequeued-comment
Browse files Browse the repository at this point in the history
Revert the changes related to dequeue lock status message in original PR
  • Loading branch information
ghaiszaher authored Sep 10, 2021
2 parents 89bbfa0 + d288eae commit 5599b2c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
30 changes: 0 additions & 30 deletions server/events/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package models

import (
"bytes"
"fmt"
"net/url"
paths "path"
Expand Down Expand Up @@ -706,32 +705,3 @@ type DequeueStatus struct {
ProjectLocks []ProjectLock
}

func (dequeueStatus DequeueStatus) String() string {
b := new(bytes.Buffer)
for _, value := range dequeueStatus.ProjectLocks {
// TODO monikma sometimes the PR gets a number = 0 and empty values, and then this fails. Find out when this happens and maybe find a proper solution.
if value.Pull.Num > 0 {
fmt.Fprintf(b,
"%s: PR %s (by @%s)\n",
value.Project.Path,
value.Pull.URL,
value.Pull.Author)
}
}
return "\n\nTriggered plans for the queued PRs:\n" + b.String()
}

func (dequeueStatus DequeueStatus) StringFilterProject(project string) string { // TODO monikma remove code duplication with the method above
b := new(bytes.Buffer)
for _, value := range dequeueStatus.ProjectLocks {
// TODO monikma sometimes the PR gets a number = 0 and empty values, and then this fails. Find out when this happens and maybe find a proper solution.
if value.Pull.Num > 0 && value.Project.Path == project {
fmt.Fprintf(b,
"%s: PR %s (by @%s)\n",
value.Project.Path,
value.Pull.URL,
value.Pull.Author)
}
}
return "\n\nTriggered plans for the queued PRs:\n" + b.String()
}
10 changes: 3 additions & 7 deletions server/events/pull_closed_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ type PullClosedExecutor struct {
type templatedProject struct {
RepoRelDir string
Workspaces string
DequeueStatus string
}

var pullClosedTemplate = template.Must(template.New("").Parse(
"Locks and plans deleted for the projects and workspaces modified in this pull request:\n" +
"{{ range . }}\n" +
"- dir: `{{ .RepoRelDir }}` {{ .Workspaces }}\n" +
"{{ .DequeueStatus }}{{ end }}"))
"- dir: `{{ .RepoRelDir }}` {{ .Workspaces }}{{ end }}"))

// CleanUpPull cleans up after a closed pull request.
func (p *PullClosedExecutor) CleanUpPull(repo models.Repo, pull models.PullRequest) error {
Expand All @@ -86,7 +84,7 @@ func (p *PullClosedExecutor) CleanUpPull(repo models.Repo, pull models.PullReque
return nil
}

templateData := p.buildTemplateData(locks, dequeueStatus)
templateData := p.buildTemplateData(locks)
var buf bytes.Buffer
if err = pullClosedTemplate.Execute(&buf, templateData); err != nil {
return errors.Wrap(err, "rendering template for comment")
Expand Down Expand Up @@ -118,7 +116,7 @@ func (p *PullClosedExecutor) triggerPlansForDequeuedPRs(repo models.Repo, dequeu
// templated for the VCS comment. We organize all the workspaces by their
// respective project paths so the comment can look like:
// dir: {dir}, workspaces: {all-workspaces}
func (p *PullClosedExecutor) buildTemplateData(locks []models.ProjectLock, dequeueStatus models.DequeueStatus) []templatedProject {
func (p *PullClosedExecutor) buildTemplateData(locks []models.ProjectLock) []templatedProject {
workspacesByPath := make(map[string][]string)
for _, l := range locks {
path := l.Project.Path
Expand All @@ -140,13 +138,11 @@ func (p *PullClosedExecutor) buildTemplateData(locks []models.ProjectLock, deque
projects = append(projects, templatedProject{
RepoRelDir: p,
Workspaces: "workspace: " + workspacesStr,
DequeueStatus: dequeueStatus.StringFilterProject(p),
})
} else {
projects = append(projects, templatedProject{
RepoRelDir: p,
Workspaces: "workspaces: " + workspacesStr,
DequeueStatus: dequeueStatus.StringFilterProject(p),
})

}
Expand Down
18 changes: 5 additions & 13 deletions server/events/unlock_command_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ func (u *UnlockCommandRunner) Run(
baseRepo := ctx.Pull.BaseRepo
pullNum := ctx.Pull.Num

vcsMessage := "All Atlantis locks for this PR have been unlocked and plans discarded"
numLocks, dequeueStatus, err := u.deleteLockCommand.DeleteLocksByPull(baseRepo.FullName, pullNum)
vcsMessage := prepareUnlockedVcsMessage(dequeueStatus, err, ctx)
if err != nil {
vcsMessage = "Failed to delete PR locks"
ctx.Log.Err("failed to delete locks by pull %s", err.Error())
}

// if there are no locks to delete, no errors, and SilenceNoProjects is enabled, don't comment
if err == nil && numLocks == 0 && u.SilenceNoProjects {
Expand All @@ -57,15 +61,3 @@ func (u *UnlockCommandRunner) triggerPlansForDequeuedPRs(ctx *CommandContext, de
}
}
}

func prepareUnlockedVcsMessage(dequeueStatus models.DequeueStatus, err error, ctx *CommandContext) string {
vcsMessage := "All Atlantis locks for this PR have been unlocked and plans discarded"
if len(dequeueStatus.ProjectLocks) > 0 {
vcsMessage = vcsMessage + dequeueStatus.String()
}
if err != nil {
vcsMessage = "Failed to delete PR locks"
ctx.Log.Err("failed to delete locks by pull %s", err.Error())
}
return vcsMessage
}

0 comments on commit 5599b2c

Please sign in to comment.