Skip to content

Commit

Permalink
feat: Improve lang detection and update templates
Browse files Browse the repository at this point in the history
This commit enhances the language detection mechanism by allowing
multiple detection files per language. This is particularly useful
for languages like Python that can have different project structures.

Part of this PR we have updated the Tekton templates to use the v1 API
version instead of v1beta1.

A new test case has been added for a Python project using Poetry.

Issue: https://issues.redhat.com/browse/SRVKP-5779
  • Loading branch information
chmouel authored and savitaashture committed Jul 26, 2024
1 parent 8b75702 commit 9ca0a1a
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 45 deletions.
28 changes: 14 additions & 14 deletions pkg/cmd/tknpac/create/testdata/TestGenerateTemplate.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
apiVersion: tekton.dev/v1beta1
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: tartanpion
Expand Down Expand Up @@ -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 }}"
21 changes: 21 additions & 0 deletions pkg/cmd/tknpac/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 24 additions & 13 deletions pkg/cmd/tknpac/generate/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,39 @@ 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": {},
}

//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 {
Expand All @@ -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
Expand Down
28 changes: 14 additions & 14 deletions pkg/cmd/tknpac/generate/templates/generic.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
apiVersion: tekton.dev/v1beta1
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: pipelinerun-generic
Expand Down Expand Up @@ -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 }}"
2 changes: 1 addition & 1 deletion pkg/cmd/tknpac/generate/templates/go.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
apiVersion: tekton.dev/v1beta1
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: pipelinerun-go
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/tknpac/generate/templates/java.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
apiVersion: tekton.dev/v1beta1
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: pipelinerun-java
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/tknpac/generate/templates/nodejs.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
apiVersion: tekton.dev/v1beta1
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: pipelinerun-nodejs
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/tknpac/generate/templates/python.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
apiVersion: tekton.dev/v1beta1
apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
name: pipelinerun-python
Expand Down

0 comments on commit 9ca0a1a

Please sign in to comment.