Merge pull request #8933 from hlreyes/team-scoped-clear-task-cache

Add `--team` flag to `clear-task-cache` command
This commit is contained in:
Taylor Silva 2024-04-04 10:39:33 -04:00 committed by GitHub
commit 86386ebbc0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 5 deletions

View File

@ -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 {
@ -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 {
@ -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())

View File

@ -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(

View File

@ -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",
@ -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)),
)
})
})