-
Notifications
You must be signed in to change notification settings - Fork 41
/
build_pipeline.go
124 lines (101 loc) · 4.45 KB
/
build_pipeline.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright 2024 Red Hat Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package tekton
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"strings"
h "github.com/konflux-ci/integration-service/helpers"
"github.com/konflux-ci/operator-toolkit/metadata"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
// master branch in github/gitlab
MasterBranch = "master"
// main branch in github/gitlab
MainBranch = "main"
// PipelineAsCodeSourceBranchAnnotation is the branch name of the the pull request is created from
PipelineAsCodeSourceBranchAnnotation = "pipelinesascode.tekton.dev/source-branch"
// PipelineAsCodeSourceRepoOrg is the repo org build PLR is triggered by
PipelineAsCodeSourceRepoOrg = "pipelinesascode.tekton.dev/url-org"
// PipelineAsCodePullRequestLabel is the type of event which triggered the pipelinerun in build service
PipelineAsCodePullRequestLabel = "pipelinesascode.tekton.dev/pull-request"
)
// AnnotateBuildPipelineRun sets annotation for a build pipelineRun in defined context and returns that pipeline
func AnnotateBuildPipelineRun(ctx context.Context, pipelineRun *tektonv1.PipelineRun, key, value string, cl client.Client) error {
patch := client.MergeFrom(pipelineRun.DeepCopy())
_ = metadata.SetAnnotation(&pipelineRun.ObjectMeta, key, value)
err := cl.Patch(ctx, pipelineRun, patch)
if err != nil {
return err
}
return nil
}
// LabelBuildPipelineRun sets annotation for a build pipelineRun in defined context and returns that pipeline
func LabelBuildPipelineRun(ctx context.Context, pipelineRun *tektonv1.PipelineRun, key, value string, cl client.Client) error {
patch := client.MergeFrom(pipelineRun.DeepCopy())
_ = metadata.SetLabel(&pipelineRun.ObjectMeta, key, value)
err := cl.Patch(ctx, pipelineRun, patch)
if err != nil {
return err
}
return nil
}
// AnnotateBuildPipelineRunWithCreateSnapshotAnnotation sets annotation test.appstudio.openshift.io/create-snapshot-status to build pipelineRun with
// a message that reflects either success or failure for creating a snapshot
func AnnotateBuildPipelineRunWithCreateSnapshotAnnotation(ctx context.Context, pipelineRun *tektonv1.PipelineRun, cl client.Client, ensureSnapshotExistsErr error) error {
message := ""
status := ""
if ensureSnapshotExistsErr == nil {
if !metadata.HasAnnotation(pipelineRun, SnapshotNameLabel) {
// do nothing for in progress build PLR
return nil
}
message = fmt.Sprintf("Sucessfully created snapshot. See annotation %s for name", SnapshotNameLabel)
status = "success"
} else {
message = fmt.Sprintf("Failed to create snapshot. Error: %s", ensureSnapshotExistsErr.Error())
status = "failed"
}
jsonResult, err := json.Marshal(map[string]string{
"status": status,
"message": message,
})
if err != nil {
return err
}
return AnnotateBuildPipelineRun(ctx, pipelineRun, h.CreateSnapshotAnnotationName, string(jsonResult), cl)
}
// GetPRGroupFromBuildPLR gets the PR group from the substring before @ from
// the source-branch pac annotation, for main, it generate PR group with {source-branch}-{url-org}
func GetPRGroupFromBuildPLR(pipelineRun *tektonv1.PipelineRun) string {
if prGroup, found := pipelineRun.ObjectMeta.Annotations[PipelineAsCodeSourceBranchAnnotation]; found {
if prGroup == MainBranch || prGroup == MasterBranch && metadata.HasAnnotation(pipelineRun, PipelineAsCodeSourceRepoOrg) {
prGroup = prGroup + "-" + pipelineRun.ObjectMeta.Annotations[PipelineAsCodeSourceRepoOrg]
}
return strings.Split(prGroup, "@")[0]
}
return ""
}
// GenerateSHA generate a 63 charactors sha string used by pipelineRun and snapshot label
func GenerateSHA(str string) string {
hash := sha256.Sum256([]byte(str))
return fmt.Sprintf("%x", hash)[0:62]
}
// IsPLRCreatedByPACPushEvent checks if a PLR has label PipelineAsCodeEventTypeLabel and with push or Push value
func IsPLRCreatedByPACPushEvent(plr *tektonv1.PipelineRun) bool {
return !metadata.HasLabel(plr, PipelineAsCodePullRequestLabel)
}