Skip to content

Commit a4b9371

Browse files
author
Shivam Mukhade
authored
Merge pull request #738 from chmouel/issue-737-use-pipelinerun-templates-in-pac-generate-command
Use Pipelinerun templates in pac generate command
2 parents ed8daee + 04da04f commit a4b9371

File tree

6 files changed

+58
-125
lines changed

6 files changed

+58
-125
lines changed

pkg/cmd/tknpac/generate/template.go

Lines changed: 40 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,87 +9,73 @@ import (
99
"os"
1010
"path/filepath"
1111
"strings"
12-
"text/template"
12+
13+
"golang.org/x/text/cases"
14+
"golang.org/x/text/language"
1315
)
1416

1517
type langOpts struct {
16-
Language string
17-
detectionFile string
18-
Task string
19-
AnnotationTask string
18+
detectionFile string
2019
}
2120

2221
// I hate this part of the code so much.. but we are waiting for UBI images
2322
// having >1.6 golang for integrated templates.
2423
var languageDetection = map[string]langOpts{
2524
"go": {
26-
Language: "Golang",
27-
detectionFile: "go.mod",
28-
AnnotationTask: "golangci-lint",
29-
Task: `- name: golangci-lint
30-
taskRef:
31-
name: golangci-lint
32-
runAfter:
33-
- fetch-repository
34-
params:
35-
- name: package
36-
value: .
37-
workspaces:
38-
- name: source
39-
workspace: source
40-
`,
25+
detectionFile: "go.mod",
4126
},
4227
"python": {
43-
Language: "Python",
44-
detectionFile: "setup.py",
45-
AnnotationTask: "pylint",
46-
Task: `- name: pylint
47-
taskRef:
48-
name: pylint
49-
runAfter:
50-
- fetch-repository
51-
workspaces:
52-
- name: source
53-
workspace: source
54-
`,
28+
detectionFile: "setup.py",
29+
},
30+
"nodejs": {
31+
detectionFile: "package.json",
5532
},
33+
"java": {
34+
detectionFile: "pom.xml",
35+
},
36+
"generic": {},
5637
}
5738

5839
//go:embed templates
5940
var resource embed.FS
6041

61-
func (o *Opts) detectLanguage() (langOpts, error) {
42+
func (o *Opts) detectLanguage() (string, error) {
6243
if o.language != "" {
6344
if _, ok := languageDetection[o.language]; !ok {
64-
return langOpts{}, fmt.Errorf("no template available for %s", o.language)
45+
return "", fmt.Errorf("no template available for %s", o.language)
6546
}
66-
return languageDetection[o.language], nil
47+
return o.language, nil
6748
}
6849

6950
cs := o.IOStreams.ColorScheme()
70-
for _, v := range languageDetection {
51+
for t, v := range languageDetection {
52+
if v.detectionFile == "" {
53+
continue
54+
}
7155
fpath := filepath.Join(o.GitInfo.TopLevelPath, v.detectionFile)
7256
if _, err := os.Stat(fpath); !os.IsNotExist(err) {
7357
fmt.Fprintf(o.IOStreams.Out, "%s We have detected your repository using the programming language %s.\n",
7458
cs.SuccessIcon(),
75-
cs.Bold(v.Language),
59+
cs.Bold(cases.Title(language.Und, cases.NoLower).String(t)),
7660
)
77-
return v, nil
61+
return t, nil
7862
}
7963
}
80-
return langOpts{}, nil
64+
return "generic", nil
8165
}
8266

83-
func (o *Opts) genTmpl() (bytes.Buffer, error) {
84-
var outputBuffer bytes.Buffer
85-
embedfile, err := resource.Open("templates/pipelinerun.yaml.tmpl")
67+
func (o *Opts) genTmpl() (*bytes.Buffer, error) {
68+
lang, err := o.detectLanguage()
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
embedfile, err := resource.Open(fmt.Sprintf("templates/%s.yaml", lang))
8674
if err != nil {
8775
log.Fatal(err)
8876
}
8977
defer embedfile.Close()
9078
tmplB, _ := ioutil.ReadAll(embedfile)
91-
// can't figure out how ParseFS works, so doing this manually..
92-
t := template.Must(template.New("PipelineRun").Delims("<<", ">>").Parse(string(tmplB)))
9379

9480
prName := filepath.Base(o.GitInfo.URL)
9581

@@ -99,19 +85,14 @@ func (o *Opts) genTmpl() (bytes.Buffer, error) {
9985
prName = prName + "-" + strings.ReplaceAll(o.Event.EventType, "_", "-")
10086
}
10187

102-
lang, err := o.detectLanguage()
103-
if err != nil {
104-
return bytes.Buffer{}, err
105-
}
106-
data := map[string]interface{}{
107-
"prName": prName,
108-
"event": o.Event,
109-
"extra_task": lang,
110-
"use_cluster_task": o.generateWithClusterTask,
111-
"language_specific_tasks": "",
112-
}
113-
if err := t.Execute(&outputBuffer, data); err != nil {
114-
return bytes.Buffer{}, err
115-
}
116-
return outputBuffer, nil
88+
tmplB = bytes.ReplaceAll(tmplB, []byte("pipelinesascode.tekton.dev/on-event: \"pull_request\""),
89+
[]byte(fmt.Sprintf("pipelinesascode.tekton.dev/on-event: \"%s\"", o.Event.EventType)))
90+
91+
tmplB = bytes.ReplaceAll(tmplB, []byte("pipelinesascode.tekton.dev/on-target-branch: \"main\""),
92+
[]byte(fmt.Sprintf("pipelinesascode.tekton.dev/on-target-branch: \"%s\"", o.Event.BaseBranch)))
93+
94+
tmplB = bytes.ReplaceAll(tmplB, []byte(fmt.Sprintf("name: pipelinerun-%s", lang)),
95+
[]byte(fmt.Sprintf("name: %s", prName)))
96+
97+
return bytes.NewBuffer(tmplB), nil
11798
}

pkg/cmd/tknpac/generate/templates/pipelinerun.yaml.tmpl renamed to pkg/cmd/tknpac/generate/templates/generic.yaml

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,28 @@
22
apiVersion: tekton.dev/v1beta1
33
kind: PipelineRun
44
metadata:
5-
name: << .prName >>
5+
name: pipelinerun-generic
66
annotations:
77
# The event we are targeting as seen from the webhook payload
88
# this can be an array too, i.e: [pull_request, push]
9-
pipelinesascode.tekton.dev/on-event: "<< .event.EventType >>"
9+
pipelinesascode.tekton.dev/on-event: "pull_request"
1010

1111
# The branch or tag we are targeting (ie: main, refs/tags/*)
12-
pipelinesascode.tekton.dev/on-target-branch: "<< .event.BaseBranch >>"
13-
<< if .use_cluster_task >>
14-
# If we want to use the git-clone task from hub we could specify the
15-
# annotation `pipelinesascode.tekton.dev/task annotation` and reference it
16-
# in the taskRef. Pipelines as Code when it sees this will grab it from hub
17-
# and automatically use the latest version. right now by default we will
18-
# use the `git-clone` ClusterTasks as shipped on this cluster.
19-
#
20-
# pipelinesascode.tekton.dev/task: "git-clone"
12+
pipelinesascode.tekton.dev/on-target-branch: "main"
2113

22-
<<- else >>
2314
# Fetch the git-clone task from hub, we are able to reference later on it
2415
# with taskRef and it will automatically be embedded into our pipeline.
2516
pipelinesascode.tekton.dev/task: "git-clone"
26-
<< end >>
27-
<<- if .extra_task.AnnotationTask >>
2817

29-
# Task for <<.extra_task.Language >>
30-
pipelinesascode.tekton.dev/task-1: "[<< .extra_task.AnnotationTask >>]"
31-
<< end >>
18+
# Use maven task from hub
19+
pipelinesascode.tekton.dev/task-1: "[maven]"
20+
3221
# You can add more tasks in here to reuse, browse the one you like from here
3322
# https://hub.tekton.dev/
3423
# example:
3524
# pipelinesascode.tekton.dev/task-2: "[maven, buildah]"
3625

26+
3727
# How many runs we want to keep attached to this event
3828
pipelinesascode.tekton.dev/max-keep-runs: "5"
3929
spec:
@@ -55,9 +45,7 @@ spec:
5545
- name: fetch-repository
5646
taskRef:
5747
name: git-clone
58-
<<- if .use_cluster_task >>
5948
kind: ClusterTask
60-
<<- end >>
6149
workspaces:
6250
- name: output
6351
workspace: source
@@ -68,9 +56,6 @@ spec:
6856
value: $(params.repo_url)
6957
- name: revision
7058
value: $(params.revision)
71-
<<- if .extra_task.Task>>
72-
<< .extra_task.Task >>
73-
<<- end >>
7459
# Customize this task if you like, or just do a taskRef
7560
# to one of the hub task.
7661
- name: noop-task

pkg/cmd/tknpac/generate/templates/go-template.yaml renamed to pkg/cmd/tknpac/generate/templates/go.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
apiVersion: tekton.dev/v1beta1
33
kind: PipelineRun
44
metadata:
5-
name: go-template
5+
name: pipelinerun-go
66
annotations:
77
# The event we are targeting as seen from the webhook payload
88
# this can be an array too, i.e: [pull_request, push]
@@ -15,7 +15,6 @@ metadata:
1515
# with taskRef and it will automatically be embedded into our pipeline.
1616
pipelinesascode.tekton.dev/task: "git-clone"
1717

18-
1918
# Task for Golang
2019
pipelinesascode.tekton.dev/task-1: "[golangci-lint]"
2120

pkg/cmd/tknpac/generate/templates/java-template.yaml renamed to pkg/cmd/tknpac/generate/templates/java.yaml

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
apiVersion: tekton.dev/v1beta1
33
kind: PipelineRun
44
metadata:
5-
name: java-template
5+
name: pipelinerun-java
66
annotations:
77
# The event we are targeting as seen from the webhook payload
88
# this can be an array too, i.e: [pull_request, push]
@@ -30,22 +30,11 @@ spec:
3030
value: "{{ repo_url }}"
3131
- name: revision
3232
value: "{{ revision }}"
33-
- name: image_name
34-
value: ""
35-
- name: app_name
36-
value: ""
37-
- name: path_context
38-
value: ""
39-
- name: version
40-
value: ""
4133
pipelineSpec:
4234
params:
4335
- name: repo_url
4436
- name: revision
4537
- name: image_name
46-
- name: app_name
47-
- name: path_context
48-
- name: version
4938
workspaces:
5039
- name: source
5140
- name: basic-auth
@@ -63,37 +52,18 @@ spec:
6352
value: $(params.repo_url)
6453
- name: revision
6554
value: $(params.revision)
66-
- name: build
67-
params:
68-
- name: IMAGE
69-
value: $(params.image_name)
70-
- name: TLSVERIFY
71-
value: "false"
72-
- name: PATH_CONTEXT
73-
value: $(params.path_context)
74-
- name: VERSION
75-
value: $(params.version)
55+
- name: maven-test
56+
taskRef:
57+
name: maven
7658
runAfter:
7759
- fetch-repository
78-
taskRef:
79-
kind: ClusterTask
80-
name: s2i-java
81-
workspaces:
82-
- name: source
83-
workspace: source
84-
- name: deploy
8560
params:
86-
- name: SCRIPT
87-
value: oc rollout status deploy/$(params.app_name)
88-
runAfter:
89-
- build
90-
taskRef:
91-
kind: ClusterTask
92-
name: openshift-client
61+
- name: GOALS
62+
value:
63+
- test
9364
workspaces:
94-
- name: workspace
65+
- name: source
9566
workspace: source
96-
9767
workspaces:
9868
- name: source
9969
volumeClaimTemplate:

pkg/cmd/tknpac/generate/templates/nodejs-template.yaml renamed to pkg/cmd/tknpac/generate/templates/nodejs.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
apiVersion: tekton.dev/v1beta1
33
kind: PipelineRun
44
metadata:
5-
name: nodejs-template
5+
name: pipelinerun-nodejs
66
annotations:
77
# The event we are targeting as seen from the webhook payload
88
# this can be an array too, i.e: [pull_request, push]
@@ -15,7 +15,6 @@ metadata:
1515
# with taskRef and it will automatically be embedded into our pipeline.
1616
pipelinesascode.tekton.dev/task: "git-clone"
1717

18-
1918
# Task for Nodejs
2019
pipelinesascode.tekton.dev/task-1: "[npm]"
2120

pkg/cmd/tknpac/generate/templates/python-template.yaml renamed to pkg/cmd/tknpac/generate/templates/python.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
apiVersion: tekton.dev/v1beta1
33
kind: PipelineRun
44
metadata:
5-
name: python-template
5+
name: pipelinerun-python
66
annotations:
77
# The event we are targeting as seen from the webhook payload
88
# this can be an array too, i.e: [pull_request, push]
@@ -15,7 +15,6 @@ metadata:
1515
# with taskRef and it will automatically be embedded into our pipeline.
1616
pipelinesascode.tekton.dev/task: "git-clone"
1717

18-
1918
# Task for Python
2019
pipelinesascode.tekton.dev/task-1: "[pylint]"
2120

0 commit comments

Comments
 (0)