Skip to content

Commit

Permalink
Detect if we need to update the PipelineRun
Browse files Browse the repository at this point in the history
When the Pipelinerun needs to be updated, detect and report it.
We parse regexp on the "raw pipelinerun", we may have to do more in the
future for other upgrades.

Signed-off-by: Chmouel Boudjnah <chmouel@redhat.com>
  • Loading branch information
chmouel committed Apr 27, 2022
1 parent 04b764c commit 42ff292
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
52 changes: 35 additions & 17 deletions pkg/pipelineascode/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package pipelineascode
import (
"context"
"fmt"
"regexp"

"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
"github.com/openshift-pipelines/pipelines-as-code/pkg/matcher"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"github.com/openshift-pipelines/pipelines-as-code/pkg/resolve"
"github.com/openshift-pipelines/pipelines-as-code/pkg/templates"
tektonv1beta1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)

// matchRepoPR matches the repo and the PRs from the event
Expand Down Expand Up @@ -98,13 +98,25 @@ func (p *PacRun) matchRepoPR(ctx context.Context) ([]matcher.Match, *v1alpha1.Re
return nil, nil, nil
}

pipelineRuns, err := p.getAllPipelineRuns(ctx)
if err != nil {
return nil, nil, err
rawTemplates := p.getAllPipelineRuns(ctx)
// check for condition if need update the pipelinerun with regexp from the
// "raw" pipelinerun string
if msg, needUpdate := p.checkNeedUpdate(rawTemplates); needUpdate {
p.logger.Info(msg)
return nil, nil, fmt.Errorf(msg)
}

if pipelineRuns == nil {
// Replace those {{var}} placeholders user has in her template to the run.Info variable
allTemplates := templates.Process(p.event, rawTemplates)
pipelineRuns, err := resolve.Resolve(ctx, p.run, p.logger, p.vcx, p.event, allTemplates, &resolve.Opts{
GenerateName: true,
RemoteTasks: p.run.Info.Pac.RemoteTasks,
})
if pipelineRuns == nil || err != nil {
msg := fmt.Sprintf("cannot locate templates in %s/ directory for this repository in %s", tektonDir, p.event.HeadBranch)
if err != nil {
msg += fmt.Sprintf("err: %s", err.Error())
}
p.logger.Info(msg)
return nil, nil, nil
}
Expand All @@ -121,20 +133,26 @@ func (p *PacRun) matchRepoPR(ctx context.Context) ([]matcher.Match, *v1alpha1.Re
return matchedPRs, repo, nil
}

func (p *PacRun) getAllPipelineRuns(ctx context.Context) ([]*tektonv1beta1.PipelineRun, error) {
// checkNeedUpdate using regexp, try to match some pattern for some issue in PR
// to let the user know they need to update. or otherwise we will fail.
// checks are depreacted/removed to n+1 release of OSP.
// each check should give a good error message on how to update.
func (p *PacRun) checkNeedUpdate(tmpl string) (string, bool) {
// nolint: gosec
oldBasicAuthSecretName := `\W*secretName: "pac-git-basic-auth-{{repo_owner}}-{{repo_name}}"`
if matched, _ := regexp.MatchString(oldBasicAuthSecretName, tmpl); matched {
return `!Update needed! you have a old basic auth secret name, you need to modify your pipelinerun and change the string "secret: pac-git-basic-auth-{{repo_owner}}-{{repo_name}}" to "secret: {{ git_auth_secret }}"`, true
}
return "", false
}

func (p *PacRun) getAllPipelineRuns(ctx context.Context) string {
// Get everything in tekton directory
allTemplates, err := p.vcx.GetTektonDir(ctx, p.event, tektonDir)
if allTemplates == "" || err != nil {
// nolint: nilerr
return nil, nil
if err != nil {
p.logger.Errorw("there was an error getting all files in tektondir: %w", err)
}
}

// Replace those {{var}} placeholders user has in her template to the run.Info variable
allTemplates = templates.Process(p.event, allTemplates)

// Merge everything (i.e: tasks/pipeline etc..) as a single pipelinerun
return resolve.Resolve(ctx, p.run, p.logger, p.vcx, p.event, allTemplates, &resolve.Opts{
GenerateName: true,
RemoteTasks: p.run.Info.Pac.RemoteTasks,
})
return allTemplates
}
39 changes: 39 additions & 0 deletions pkg/pipelineascode/match_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pipelineascode

import (
"strings"
"testing"

"gotest.tools/v3/assert"
)

func TestPacRun_checkNeedUpdate(t *testing.T) {
tests := []struct {
name string
tmpl string
upgradeMessageSubstr string
needupdate bool
}{
{
name: "old secrets",
tmpl: ` secretName: "pac-git-basic-auth-{{repo_owner}}-{{repo_name}}"`,
upgradeMessageSubstr: "old basic auth secret name",
needupdate: true,
},
{
name: "no need",
tmpl: ` secretName: "foo-bar-foo"`,
needupdate: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := NewPacs(nil, nil, nil, nil, nil)
got, needupdate := p.checkNeedUpdate(tt.tmpl)
if tt.upgradeMessageSubstr != "" {
assert.Assert(t, strings.Contains(got, tt.upgradeMessageSubstr))
}
assert.Assert(t, needupdate == tt.needupdate)
})
}
}
2 changes: 1 addition & 1 deletion pkg/pipelineascode/pipelinesascode_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func testSetupCommonGhReplies(t *testing.T, mux *http.ServeMux, runevent info.Ev
// We created multiple status but the last one should be completed.
// TODO: we could maybe refine this test
if created.GetStatus() == "completed" {
assert.Equal(t, created.GetConclusion(), finalStatus)
assert.Equal(t, created.GetConclusion(), finalStatus, "we got the status `%s` but we should have get the status `%s`", created.GetConclusion(), finalStatus)
assert.Assert(t, strings.Contains(created.GetOutput().GetText(), finalStatusText),
"GetStatus/CheckRun %s != %s", created.GetOutput().GetText(), finalStatusText)
}
Expand Down

0 comments on commit 42ff292

Please sign in to comment.