-
Notifications
You must be signed in to change notification settings - Fork 81
/
event.go
95 lines (83 loc) · 3.04 KB
/
event.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
package reconciler
import (
"context"
"fmt"
"strconv"
"github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/keys"
"github.com/openshift-pipelines/pipelines-as-code/pkg/formatting"
"github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketcloud"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/bitbucketserver"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitea"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/github"
"github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitlab"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"go.uber.org/zap"
)
func (r *Reconciler) detectProvider(ctx context.Context, logger *zap.SugaredLogger, pr *tektonv1.PipelineRun) (provider.Interface, *info.Event, error) {
gitProvider, ok := pr.GetLabels()[keys.GitProvider]
if !ok {
return nil, nil, fmt.Errorf("failed to detect git provider for pipleinerun %s : git-provider label not found", pr.GetName())
}
event := buildEventFromPipelineRun(pr)
var provider provider.Interface
switch gitProvider {
case "github", "github-enterprise":
gh := github.New()
if event.InstallationID != 0 {
if err := gh.InitAppClient(ctx, r.run.Clients.Kube, event); err != nil {
return nil, nil, err
}
}
provider = gh
case "gitlab":
provider = &gitlab.Provider{}
case "bitbucket-cloud":
provider = &bitbucketcloud.Provider{}
case "bitbucket-server":
provider = &bitbucketserver.Provider{}
case "gitea":
provider = &gitea.Provider{}
default:
return nil, nil, fmt.Errorf("failed to detect provider for pipelinerun: %s : unknown provider", pr.GetName())
}
provider.SetLogger(logger)
return provider, event, nil
}
func buildEventFromPipelineRun(pr *tektonv1.PipelineRun) *info.Event {
event := info.NewEvent()
prLabels := pr.GetLabels()
prAnno := pr.GetAnnotations()
event.URL = prAnno[keys.RepoURL]
// it's safer to get repo, org from repo.url since we have to remove the / and other chars in labels which drops
// the SubPath that gitlab is using.
repo, org, _ := formatting.GetRepoOwnerSplitted(event.URL)
event.Organization = repo
event.Repository = org
event.EventType = prLabels[keys.EventType]
event.BaseBranch = prLabels[keys.Branch]
event.SHA = prLabels[keys.SHA]
event.SHATitle = prAnno[keys.ShaTitle]
event.SHAURL = prAnno[keys.ShaURL]
prNumber := prLabels[keys.PullRequest]
if prNumber != "" {
event.PullRequestNumber, _ = strconv.Atoi(prNumber)
}
// GitHub
if installationID, ok := prAnno[keys.InstallationID]; ok {
id, _ := strconv.Atoi(installationID)
event.InstallationID = int64(id)
}
if gheURL, ok := prAnno[keys.GHEURL]; ok {
event.GHEURL = gheURL
}
// Gitlab
if projectID, ok := prAnno[keys.SourceProjectID]; ok {
event.SourceProjectID, _ = strconv.Atoi(projectID)
}
if projectID, ok := prAnno[keys.TargetProjectID]; ok {
event.TargetProjectID, _ = strconv.Atoi(projectID)
}
return event
}