Skip to content

Commit

Permalink
Merge pull request #8933 from hlreyes/team-scoped-clear-task-cache
Browse files Browse the repository at this point in the history
Add `--team` flag to `clear-task-cache` command
  • Loading branch information
taylorsilva committed Apr 4, 2024
2 parents 864bc19 + 1133aa5 commit 86386eb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
19 changes: 14 additions & 5 deletions fly/commands/clear_task_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

type ClearTaskCacheCommand struct {
Job flaghelpers.JobFlag `short:"j" long:"job" required:"true" description:"Job to clear cache from"`
StepName string `short:"s" long:"step" required:"true" description:"Step name to clear cache from"`
CachePath string `short:"c" long:"cache-path" default:"" description:"Cache directory to clear out"`
SkipInteractive bool `short:"n" long:"non-interactive" description:"Destroy the task cache(s) without confirmation"`
Job flaghelpers.JobFlag `short:"j" long:"job" required:"true" description:"Job to clear cache from"`
StepName string `short:"s" long:"step" required:"true" description:"Step name to clear cache from"`
CachePath string `short:"c" long:"cache-path" default:"" description:"Cache directory to clear out"`
SkipInteractive bool `short:"n" long:"non-interactive" description:"Destroy the task cache(s) without confirmation"`
Team flaghelpers.TeamFlag `long:"team" description:"Name of the team to which the pipeline belongs, if different from the target default"`
}

func (command *ClearTaskCacheCommand) Execute([]string) error {
Expand All @@ -26,6 +27,14 @@ func (command *ClearTaskCacheCommand) Execute([]string) error {
return err
}

team := target.Team()
if command.Team != "" {
team, err = target.FindTeam(command.Team.Name())
if err != nil {
return err
}
}

warningMsg := fmt.Sprintf("!!! this will remove the task cache(s) for `%s/%s`, task step `%s`",
command.Job.PipelineRef.String(), command.Job.JobName, command.StepName)
if len(command.CachePath) > 0 {
Expand All @@ -43,7 +52,7 @@ func (command *ClearTaskCacheCommand) Execute([]string) error {
}
}

numRemoved, err := target.Team().ClearTaskCache(command.Job.PipelineRef, command.Job.JobName, command.StepName, command.CachePath)
numRemoved, err := team.ClearTaskCache(command.Job.PipelineRef, command.Job.JobName, command.StepName, command.CachePath)

if err != nil {
fmt.Println(err.Error())
Expand Down
29 changes: 29 additions & 0 deletions fly/integration/clear_task_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,35 @@ var _ = Describe("Fly CLI", func() {
})
})

Context("and a non-default team is specified", func() {
var expectedURLOther = "/api/v1/teams/other-team/pipelines/some-pipeline/jobs/some-job/tasks/some-step-name/cache"

BeforeEach(func() {
args = append(args, "--team", "other-team")
})

JustBeforeEach(func() {
atcServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", "/api/v1/teams/other-team"),
ghttp.RespondWithJSONEncoded(http.StatusOK, atc.Team{
Name: "other-team",
}),
),
ghttp.CombineHandlers(
ghttp.VerifyRequest("DELETE", expectedURLOther, strings.Join(expectedQueryParams, "&")),
ghttp.RespondWithJSONEncoded(http.StatusOK, atc.ClearTaskCacheResponse{CachesRemoved: 1}),
),
)
})

It("succeeds if the user says yes", func() {
yes()
Eventually(sess).Should(gbytes.Say("1 caches removed"))
Eventually(sess).Should(gexec.Exit(0))
})
})

Context("and the task step does not exist", func() {
JustBeforeEach(func() {
atcServer.AppendHandlers(
Expand Down
4 changes: 4 additions & 0 deletions fly/integration/error_handling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ var _ = Describe("Fly CLI", func() {
exec.Command(flyPath, "-t", targetName, "resource-versions", "-r", "pipeline/branch:master/foo", "--team", nonExistentTeam)),
Entry("watch command returns an error",
exec.Command(flyPath, "-t", targetName, "watch", "-j", "pipeline/job", "--team", nonExistentTeam)),
Entry("clear-task-cache command returns an error",
exec.Command(flyPath, "-t", targetName, "clear-task-cache", "-j", "pipeline/job", "-s", "some-task-step", "--team", nonExistentTeam)),
)

DescribeTable("and you are NOT authorized to view the team",
Expand Down Expand Up @@ -204,6 +206,8 @@ var _ = Describe("Fly CLI", func() {
exec.Command(flyPath, "-t", targetName, "resource-versions", "-r", "pipeline/branch:master/foo", "--team", otherTeam)),
Entry("watch command returns an error",
exec.Command(flyPath, "-t", targetName, "watch", "-j", "pipeline/job", "--team", otherTeam)),
Entry("clear-task-cache command returns an error",
exec.Command(flyPath, "-t", targetName, "clear-task-cache", "-j", "pipeline/job", "-s", "some-task-step", "--team", otherTeam)),
)
})
})

0 comments on commit 86386eb

Please sign in to comment.