Skip to content

Commit

Permalink
This fixes #639
Browse files Browse the repository at this point in the history
Although when user specified last flag script always override GenerateName.
So using GenerateName if last PipelineRun has a GenerateName,
If not, setting a GenerateName based on last PipelineRun Name.
  • Loading branch information
16yuki0702 authored and tekton-robot committed Jan 31, 2020
1 parent a8867c5 commit 2e5e541
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/cmd/pipeline/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,11 @@ func (opt *startOptions) startPipeline(pName string) error {
if err != nil {
return err
}
if len(prLast.ObjectMeta.GenerateName) > 0 {
pr.ObjectMeta.GenerateName = prLast.ObjectMeta.GenerateName
} else {
pr.ObjectMeta.GenerateName = prLast.ObjectMeta.Name + "-"
}
pr.Spec.Resources = prLast.Spec.Resources
pr.Spec.Params = prLast.Spec.Params
// If the prLast is a "new" PR, let's populate those fields too
Expand Down
77 changes: 77 additions & 0 deletions pkg/cmd/pipeline/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2410,3 +2410,80 @@ func Test_lastPipelineRun(t *testing.T) {
})
}
}

func Test_start_pipeline_last_generate_name(t *testing.T) {

pipelineName := "test-pipeline"

ps := []*v1alpha1.Pipeline{
tb.Pipeline(pipelineName, "ns",
tb.PipelineSpec(
tb.PipelineDeclaredResource("git-repo", "git"),
tb.PipelineDeclaredResource("build-image", "image"),
tb.PipelineParamSpec("pipeline-param-1", v1alpha1.ParamTypeString, tb.ParamSpecDefault("somethingdifferent-1")),
tb.PipelineParamSpec("rev-param", v1alpha1.ParamTypeString, tb.ParamSpecDefault("revision")),
tb.PipelineTask("unit-test-1", "unit-test-task",
tb.PipelineTaskInputResource("workspace", "git-repo"),
tb.PipelineTaskOutputResource("image-to-use", "best-image"),
tb.PipelineTaskOutputResource("workspace", "git-repo"),
),
),
),
}

prs := []*v1alpha1.PipelineRun{
tb.PipelineRun("test-pipeline-run-123", "ns",
tb.PipelineRunLabel("tekton.dev/pipeline", pipelineName),
tb.PipelineRunSpec(pipelineName,
tb.PipelineRunServiceAccountName("test-sa"),
tb.PipelineRunResourceBinding("git-repo", tb.PipelineResourceBindingRef("some-repo")),
tb.PipelineRunResourceBinding("build-image", tb.PipelineResourceBindingRef("some-image")),
tb.PipelineRunParam("pipeline-param-1", "somethingmorefun"),
tb.PipelineRunParam("rev-param", "revision1"),
),
),
}

// Setting GenerateName for test
prs[0].ObjectMeta.GenerateName = "test-generatename-pipeline-run-"

ns := []*corev1.Namespace{
{
ObjectMeta: metav1.ObjectMeta{
Name: "ns",
},
},
}

seedData, _ := test.SeedTestData(t, pipelinetest.Data{Namespaces: ns})

objs := []runtime.Object{ps[0], prs[0]}
pClient := newPipelineClient(objs...)

cs := pipelinetest.Clients{
Pipeline: pClient,
Kube: seedData.Kube,
}

p := &test.Params{Tekton: cs.Pipeline, Kube: cs.Kube}

pipeline := Command(p)
got, _ := test.ExecuteCommand(pipeline, "start", pipelineName,
"--last",
"-r=git-repo=scaffold-git",
"-p=rev-param=revision2",
"-s=svc1",
"--task-serviceaccount=task3=task3svc3",
"--task-serviceaccount=task5=task3svc5",
"-n", "ns")

expected := "Pipelinerun started: random\n\nIn order to track the pipelinerun progress run:\ntkn pipelinerun logs random -f -n ns\n"
test.AssertOutput(t, expected, got)

pr, err := cs.Pipeline.TektonV1alpha1().PipelineRuns(p.Namespace()).Get("random", v1.GetOptions{})
if err != nil {
t.Errorf("Error getting pipelineruns %s", err.Error())
}

test.AssertOutput(t, "test-generatename-pipeline-run-", pr.ObjectMeta.GenerateName)
}

0 comments on commit 2e5e541

Please sign in to comment.