diff --git a/pkg/cmd/tknpac/create/testdata/TestGenerateTemplate.golden b/pkg/cmd/tknpac/create/testdata/TestGenerateTemplate.golden index f36f0ab79..f123d6891 100644 --- a/pkg/cmd/tknpac/create/testdata/TestGenerateTemplate.golden +++ b/pkg/cmd/tknpac/create/testdata/TestGenerateTemplate.golden @@ -1,5 +1,5 @@ --- -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: PipelineRun metadata: name: tartanpion @@ -74,16 +74,16 @@ spec: script: | exit 0 workspaces: - - name: source - volumeClaimTemplate: - spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 1Gi - # This workspace will inject secret to help the git-clone task to be able to - # checkout the private repositories - - name: basic-auth - secret: - secretName: "{{ git_auth_secret }}" + - name: source + volumeClaimTemplate: + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + # This workspace will inject secret to help the git-clone task to be able to + # checkout the private repositories + - name: basic-auth + secret: + secretName: "{{ git_auth_secret }}" diff --git a/pkg/cmd/tknpac/generate/generate_test.go b/pkg/cmd/tknpac/generate/generate_test.go index 6983ecbea..f76abf9e3 100644 --- a/pkg/cmd/tknpac/generate/generate_test.go +++ b/pkg/cmd/tknpac/generate/generate_test.go @@ -85,6 +85,27 @@ func TestGenerateTemplate(t *testing.T) { }, regenerateTemplate: true, }, + { + name: "pull request python poetry", + askStubs: func(as *prompt.AskStubber) { + as.StubOneDefault() // pull_request + as.StubOne("") // default as main + }, + addExtraFilesInRepo: map[string]string{ + "poetry.lock": "hello", + }, + checkGeneratedFile: ".tekton/pull-request.yaml", + checkRegInGeneratedFile: []*regexp.Regexp{ + regexp.MustCompile("name: python-pull-request"), + regexp.MustCompile(".*on-event.*pull_request"), + regexp.MustCompile(".*test our Python project"), + regexp.MustCompile("- name: pylint"), + }, + gitinfo: git.Info{ + URL: "https://hello/python", + }, + regenerateTemplate: true, + }, { name: "pull request golang", askStubs: func(as *prompt.AskStubber) { diff --git a/pkg/cmd/tknpac/generate/template.go b/pkg/cmd/tknpac/generate/template.go index 4ba026666..95a988908 100644 --- a/pkg/cmd/tknpac/generate/template.go +++ b/pkg/cmd/tknpac/generate/template.go @@ -15,23 +15,23 @@ import ( ) type langOpts struct { - detectionFile string + detectionFiles []string } // I hate this part of the code so much.. but we are waiting for UBI images // having >1.6 golang for integrated templates. var languageDetection = map[string]langOpts{ "go": { - detectionFile: "go.mod", + detectionFiles: []string{"go.mod"}, }, "python": { - detectionFile: "setup.py", + detectionFiles: []string{"setup.py", "pyproject.toml", "poetry.lock"}, }, "nodejs": { - detectionFile: "package.json", + detectionFiles: []string{"package.json"}, }, "java": { - detectionFile: "pom.xml", + detectionFiles: []string{"pom.xml"}, }, "generic": {}, } @@ -39,6 +39,15 @@ var languageDetection = map[string]langOpts{ //go:embed templates var resource embed.FS +// detectLanguage determines the programming language used in the repository. +// It first checks if a language has been explicitly set in the options. If so, +// it verifies that a template is available for that language. If not, it returns an error. +// If no language is set, it iterates over the known languages and checks if a +// characteristic file for each language exists in the repository (go.mod > +// go). If it finds a match, it outputs a success message and returns the +// detected language. If no language can be detected, it defaults to "generic". +// Returns the detected language as a string, or an error if a problem +// occurred. func (o *Opts) detectLanguage() (string, error) { if o.language != "" { if _, ok := languageDetection[o.language]; !ok { @@ -49,16 +58,18 @@ func (o *Opts) detectLanguage() (string, error) { cs := o.IOStreams.ColorScheme() for t, v := range languageDetection { - if v.detectionFile == "" { + if v.detectionFiles == nil { continue } - fpath := filepath.Join(o.GitInfo.TopLevelPath, v.detectionFile) - if _, err := os.Stat(fpath); !os.IsNotExist(err) { - fmt.Fprintf(o.IOStreams.Out, "%s We have detected your repository using the programming language %s.\n", - cs.SuccessIcon(), - cs.Bold(cases.Title(language.Und, cases.NoLower).String(t)), - ) - return t, nil + for _, f := range v.detectionFiles { + fpath := filepath.Join(o.GitInfo.TopLevelPath, f) + if _, err := os.Stat(fpath); !os.IsNotExist(err) { + fmt.Fprintf(o.IOStreams.Out, "%s We have detected your repository using the programming language %s.\n", + cs.SuccessIcon(), + cs.Bold(cases.Title(language.Und, cases.NoLower).String(t)), + ) + return t, nil + } } } return "generic", nil diff --git a/pkg/cmd/tknpac/generate/templates/generic.yaml b/pkg/cmd/tknpac/generate/templates/generic.yaml index 50f2d4093..2216dea44 100644 --- a/pkg/cmd/tknpac/generate/templates/generic.yaml +++ b/pkg/cmd/tknpac/generate/templates/generic.yaml @@ -1,5 +1,5 @@ --- -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: PipelineRun metadata: name: pipelinerun-generic @@ -74,16 +74,16 @@ spec: script: | exit 0 workspaces: - - name: source - volumeClaimTemplate: - spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 1Gi - # This workspace will inject secret to help the git-clone task to be able to - # checkout the private repositories - - name: basic-auth - secret: - secretName: "{{ git_auth_secret }}" + - name: source + volumeClaimTemplate: + spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi + # This workspace will inject secret to help the git-clone task to be able to + # checkout the private repositories + - name: basic-auth + secret: + secretName: "{{ git_auth_secret }}" diff --git a/pkg/cmd/tknpac/generate/templates/go.yaml b/pkg/cmd/tknpac/generate/templates/go.yaml index 582ded160..ef28943ea 100644 --- a/pkg/cmd/tknpac/generate/templates/go.yaml +++ b/pkg/cmd/tknpac/generate/templates/go.yaml @@ -1,5 +1,5 @@ --- -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: PipelineRun metadata: name: pipelinerun-go diff --git a/pkg/cmd/tknpac/generate/templates/java.yaml b/pkg/cmd/tknpac/generate/templates/java.yaml index e12c3023f..b8cf87266 100644 --- a/pkg/cmd/tknpac/generate/templates/java.yaml +++ b/pkg/cmd/tknpac/generate/templates/java.yaml @@ -1,5 +1,5 @@ --- -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: PipelineRun metadata: name: pipelinerun-java diff --git a/pkg/cmd/tknpac/generate/templates/nodejs.yaml b/pkg/cmd/tknpac/generate/templates/nodejs.yaml index d0b2dd782..79204477f 100644 --- a/pkg/cmd/tknpac/generate/templates/nodejs.yaml +++ b/pkg/cmd/tknpac/generate/templates/nodejs.yaml @@ -1,5 +1,5 @@ --- -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: PipelineRun metadata: name: pipelinerun-nodejs diff --git a/pkg/cmd/tknpac/generate/templates/python.yaml b/pkg/cmd/tknpac/generate/templates/python.yaml index 7d36669c7..2d0083518 100644 --- a/pkg/cmd/tknpac/generate/templates/python.yaml +++ b/pkg/cmd/tknpac/generate/templates/python.yaml @@ -1,5 +1,5 @@ --- -apiVersion: tekton.dev/v1beta1 +apiVersion: tekton.dev/v1 kind: PipelineRun metadata: name: pipelinerun-python