Skip to content

Commit 49cf18c

Browse files
authored
Merge branch 'main' into ann-on-start
2 parents 914baa9 + 1ba08cf commit 49cf18c

File tree

6 files changed

+206
-122
lines changed

6 files changed

+206
-122
lines changed

config/302-pac-configmap.yaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,18 @@ data:
139139
# namespace and repository CR, supported only with GitHub App
140140
auto-configure-new-github-repo: "false"
141141

142-
# add a template to generate name for namespace for your auto configured
143-
# github repo supported fields are repo_owner, repo_name eg. if defined as
144-
# `{{repo_owner}}-{{repo_name}}-ci`, then namespace generated for repository
145-
# https://github.com/owner/repo will be `owner-repo-ci`
142+
# Add a template to generate the name for a Repository CR for an auto-configured
143+
# GitHub repository. Supported fields are `repo_owner` and `repo_name`. e.g.,
144+
# if defined as `{{repo_owner}}-{{repo_name}}-repo-cr`, the generated CR name for
145+
# https://github.com/owner/test will be `owner-test-repo-cr`
146146
auto-configure-repo-namespace-template: ""
147147

148+
# add a template to generate name for repository for your auto configured
149+
# github repo. supported fields are repo_owner, repo_name eg. if defined as
150+
# `{{repo_owner}}-{{repo_name}}-repo-cr`, then repository CR generated for git repository
151+
# https://github.com/owner/test will be `owner-test-repo-cr`
152+
auto-configure-repo-repository-template: ""
153+
148154
# Enable or disable the feature to rerun the CI if push event happens on
149155
# a pull request
150156
#

docs/content/docs/install/settings.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ There is a few things you can configure through the config map
124124

125125
`https://github.com/owner/repo` will be `owner-repo-ci`
126126

127+
* `auto-configure-repo-repository-template`
128+
129+
If `auto-configure-new-github-repo` is enabled then you can provide a template
130+
for generating the name for your new repository custom resource. By default, the repository custom resource name will be generated using this format `{{repo_name}}-repo-cr`.
131+
132+
You can override the default using the following variables
133+
134+
* `{{repo_owner}}`: The repository owner.
135+
* `{{repo_name}}`: The repository name.
136+
For example, if the template is defined as `{{repo_owner}}-{{repo_name}}-repo-cr`,
137+
then the Repository CR name generated for the repository
138+
`https://github.com/owner/test` will be `owner-test-repo-cr`
139+
127140
* `remember-ok-to-test`
128141

129142
If `remember-ok-to-test` is true then if `ok-to-test` is done on pull request then in

pkg/params/settings/config.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,17 @@ type HubCatalog struct {
4848
// if there is a change performed on the default value,
4949
// update the same on "config/302-pac-configmap.yaml".
5050
type Settings struct {
51-
ApplicationName string `default:"Pipelines as Code CI" json:"application-name"`
52-
HubCatalogs *sync.Map
53-
RemoteTasks bool `default:"true" json:"remote-tasks"`
54-
MaxKeepRunsUpperLimit int `json:"max-keep-run-upper-limit"`
55-
DefaultMaxKeepRuns int `json:"default-max-keep-runs"`
56-
BitbucketCloudCheckSourceIP bool `default:"true" json:"bitbucket-cloud-check-source-ip"`
57-
BitbucketCloudAdditionalSourceIP string `json:"bitbucket-cloud-additional-source-ip"`
58-
TektonDashboardURL string `json:"tekton-dashboard-url"`
59-
AutoConfigureNewGitHubRepo bool `default:"false" json:"auto-configure-new-github-repo"`
60-
AutoConfigureRepoNamespaceTemplate string `json:"auto-configure-repo-namespace-template"`
51+
ApplicationName string `default:"Pipelines as Code CI" json:"application-name"`
52+
HubCatalogs *sync.Map
53+
RemoteTasks bool `default:"true" json:"remote-tasks"`
54+
MaxKeepRunsUpperLimit int `json:"max-keep-run-upper-limit"`
55+
DefaultMaxKeepRuns int `json:"default-max-keep-runs"`
56+
BitbucketCloudCheckSourceIP bool `default:"true" json:"bitbucket-cloud-check-source-ip"`
57+
BitbucketCloudAdditionalSourceIP string `json:"bitbucket-cloud-additional-source-ip"`
58+
TektonDashboardURL string `json:"tekton-dashboard-url"`
59+
AutoConfigureNewGitHubRepo bool `default:"false" json:"auto-configure-new-github-repo"`
60+
AutoConfigureRepoNamespaceTemplate string `json:"auto-configure-repo-namespace-template"`
61+
AutoConfigureRepoRepositoryTemplate string `json:"auto-configure-repo-repository-template"`
6162

6263
SecretAutoCreation bool `default:"true" json:"secret-auto-create"`
6364
SecretGHAppRepoScoped bool `default:"true" json:"secret-github-app-token-scoped"`

pkg/params/settings/config_test.go

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,55 +52,57 @@ func TestSyncConfig(t *testing.T) {
5252
{
5353
name: "override values",
5454
configMap: map[string]string{
55-
"application-name": "pac-pac",
56-
"remote-tasks": "false",
57-
"max-keep-run-upper-limit": "10",
58-
"default-max-keep-runs": "5",
59-
"bitbucket-cloud-check-source-ip": "false",
60-
"bitbucket-cloud-additional-source-ip": "some-ip",
61-
"tekton-dashboard-url": "https://tekton-dashboard",
62-
"auto-configure-new-github-repo": "true",
63-
"auto-configure-repo-namespace-template": "template",
64-
"secret-auto-create": "false",
65-
"secret-github-app-token-scoped": "false",
66-
"secret-github-app-scope-extra-repos": "extra-repos",
67-
"error-log-snippet": "false",
68-
"error-detection-from-container-logs": "false",
69-
"error-detection-max-number-of-lines": "100",
70-
"error-detection-simple-regexp": "^(?P<filename>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+)?([ ]*)?(?P<error>.*)",
71-
"custom-console-name": "custom-console",
72-
"custom-console-url": "https://custom-console",
73-
"custom-console-url-pr-details": "https://custom-console-pr-details",
74-
"custom-console-url-pr-tasklog": "https://custom-console-pr-tasklog",
75-
"custom-console-url-namespace": "https://custom-console-namespace",
76-
"remember-ok-to-test": "false",
77-
"skip-push-event-for-pr-commits": "true",
55+
"application-name": "pac-pac",
56+
"remote-tasks": "false",
57+
"max-keep-run-upper-limit": "10",
58+
"default-max-keep-runs": "5",
59+
"bitbucket-cloud-check-source-ip": "false",
60+
"bitbucket-cloud-additional-source-ip": "some-ip",
61+
"tekton-dashboard-url": "https://tekton-dashboard",
62+
"auto-configure-new-github-repo": "true",
63+
"auto-configure-repo-namespace-template": "template",
64+
"auto-configure-repo-repository-template": "template",
65+
"secret-auto-create": "false",
66+
"secret-github-app-token-scoped": "false",
67+
"secret-github-app-scope-extra-repos": "extra-repos",
68+
"error-log-snippet": "false",
69+
"error-detection-from-container-logs": "false",
70+
"error-detection-max-number-of-lines": "100",
71+
"error-detection-simple-regexp": "^(?P<filename>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+)?([ ]*)?(?P<error>.*)",
72+
"custom-console-name": "custom-console",
73+
"custom-console-url": "https://custom-console",
74+
"custom-console-url-pr-details": "https://custom-console-pr-details",
75+
"custom-console-url-pr-tasklog": "https://custom-console-pr-tasklog",
76+
"custom-console-url-namespace": "https://custom-console-namespace",
77+
"remember-ok-to-test": "false",
78+
"skip-push-event-for-pr-commits": "true",
7879
},
7980
expectedStruct: Settings{
80-
ApplicationName: "pac-pac",
81-
HubCatalogs: nil,
82-
RemoteTasks: false,
83-
MaxKeepRunsUpperLimit: 10,
84-
DefaultMaxKeepRuns: 5,
85-
BitbucketCloudCheckSourceIP: false,
86-
BitbucketCloudAdditionalSourceIP: "some-ip",
87-
TektonDashboardURL: "https://tekton-dashboard",
88-
AutoConfigureNewGitHubRepo: true,
89-
AutoConfigureRepoNamespaceTemplate: "template",
90-
SecretAutoCreation: false,
91-
SecretGHAppRepoScoped: false,
92-
SecretGhAppTokenScopedExtraRepos: "extra-repos",
93-
ErrorLogSnippet: false,
94-
ErrorDetection: false,
95-
ErrorDetectionNumberOfLines: 100,
96-
ErrorDetectionSimpleRegexp: "^(?P<filename>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+)?([ ]*)?(?P<error>.*)",
97-
CustomConsoleName: "custom-console",
98-
CustomConsoleURL: "https://custom-console",
99-
CustomConsolePRdetail: "https://custom-console-pr-details",
100-
CustomConsolePRTaskLog: "https://custom-console-pr-tasklog",
101-
CustomConsoleNamespaceURL: "https://custom-console-namespace",
102-
RememberOKToTest: false,
103-
SkipPushEventForPRCommits: true,
81+
ApplicationName: "pac-pac",
82+
HubCatalogs: nil,
83+
RemoteTasks: false,
84+
MaxKeepRunsUpperLimit: 10,
85+
DefaultMaxKeepRuns: 5,
86+
BitbucketCloudCheckSourceIP: false,
87+
BitbucketCloudAdditionalSourceIP: "some-ip",
88+
TektonDashboardURL: "https://tekton-dashboard",
89+
AutoConfigureNewGitHubRepo: true,
90+
AutoConfigureRepoNamespaceTemplate: "template",
91+
AutoConfigureRepoRepositoryTemplate: "template",
92+
SecretAutoCreation: false,
93+
SecretGHAppRepoScoped: false,
94+
SecretGhAppTokenScopedExtraRepos: "extra-repos",
95+
ErrorLogSnippet: false,
96+
ErrorDetection: false,
97+
ErrorDetectionNumberOfLines: 100,
98+
ErrorDetectionSimpleRegexp: "^(?P<filename>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+)?([ ]*)?(?P<error>.*)",
99+
CustomConsoleName: "custom-console",
100+
CustomConsoleURL: "https://custom-console",
101+
CustomConsolePRdetail: "https://custom-console-pr-details",
102+
CustomConsolePRTaskLog: "https://custom-console-pr-tasklog",
103+
CustomConsoleNamespaceURL: "https://custom-console-namespace",
104+
RememberOKToTest: false,
105+
SkipPushEventForPRCommits: true,
104106
},
105107
},
106108
{

pkg/provider/github/repository.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import (
1919
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020
)
2121

22-
const defaultNsTemplate = "%v-pipelines"
22+
const (
23+
defaultNsTemplate = "%v-pipelines"
24+
defaultRepoTemplate = "%v-repo-cr"
25+
)
2326

2427
func ConfigureRepository(ctx context.Context, run *params.Run, req *http.Request, payload string, pacInfo *info.PacOpts, logger *zap.SugaredLogger) (bool, bool, error) {
2528
// check if repo auto configuration is enabled
@@ -48,16 +51,18 @@ func ConfigureRepository(ctx context.Context, run *params.Run, req *http.Request
4851
}
4952

5053
logger.Infof("github: configuring repository cr for repo: %v", repoEvent.Repo.GetHTMLURL())
51-
if err := createRepository(ctx, pacInfo.AutoConfigureRepoNamespaceTemplate, run.Clients, repoEvent, logger); err != nil {
54+
nsTemplate := pacInfo.AutoConfigureRepoNamespaceTemplate
55+
repoTemplate := pacInfo.AutoConfigureRepoRepositoryTemplate
56+
if err := createRepository(ctx, nsTemplate, repoTemplate, run.Clients, repoEvent, logger); err != nil {
5257
logger.Errorf("failed repository creation: %v", err)
5358
return true, true, err
5459
}
5560

5661
return true, true, nil
5762
}
5863

59-
func createRepository(ctx context.Context, nsTemplate string, clients clients.Clients, gitEvent *github.RepositoryEvent, logger *zap.SugaredLogger) error {
60-
repoNsName, err := generateNamespaceName(nsTemplate, gitEvent)
64+
func createRepository(ctx context.Context, nsTemplate, repoTemplate string, clients clients.Clients, gitEvent *github.RepositoryEvent, logger *zap.SugaredLogger) error {
65+
repoNsName, repoCRName, err := generateNamespaceAndRepositoryName(nsTemplate, repoTemplate, gitEvent)
6166
if err != nil {
6267
return fmt.Errorf("failed to generate namespace for repo: %w", err)
6368
}
@@ -84,7 +89,7 @@ func createRepository(ctx context.Context, nsTemplate string, clients clients.Cl
8489
// create repository
8590
repo := &v1alpha1.Repository{
8691
ObjectMeta: metav1.ObjectMeta{
87-
Name: repoNsName,
92+
Name: repoCRName,
8893
Namespace: repoNsName,
8994
},
9095
Spec: v1alpha1.RepositorySpec{
@@ -100,19 +105,29 @@ func createRepository(ctx context.Context, nsTemplate string, clients clients.Cl
100105
return nil
101106
}
102107

103-
func generateNamespaceName(nsTemplate string, gitEvent *github.RepositoryEvent) (string, error) {
108+
func generateNamespaceAndRepositoryName(nsTemplate, repoTemplate string, gitEvent *github.RepositoryEvent) (string, string, error) {
104109
repoOwner, repoName, err := formatting.GetRepoOwnerSplitted(gitEvent.Repo.GetHTMLURL())
105110
if err != nil {
106-
return "", fmt.Errorf("failed to parse git repo url: %w", err)
111+
return "", "", fmt.Errorf("failed to parse git repo url: %w", err)
112+
}
113+
114+
nsName := ""
115+
repoCRName := ""
116+
placeholders := map[string]string{
117+
"repo_owner": repoOwner,
118+
"repo_name": repoName,
107119
}
108120

109121
if nsTemplate == "" {
110-
return fmt.Sprintf(defaultNsTemplate, repoName), nil
122+
nsName = fmt.Sprintf(defaultNsTemplate, repoName)
123+
} else {
124+
nsName = templates.ReplacePlaceHoldersVariables(nsTemplate, placeholders, nil, http.Header{}, map[string]any{})
111125
}
112126

113-
maptemplate := map[string]string{
114-
"repo_owner": repoOwner,
115-
"repo_name": repoName,
127+
if repoTemplate == "" {
128+
repoCRName = fmt.Sprintf(defaultRepoTemplate, repoName)
129+
} else {
130+
repoCRName = templates.ReplacePlaceHoldersVariables(repoTemplate, placeholders, nil, http.Header{}, map[string]any{})
116131
}
117-
return templates.ReplacePlaceHoldersVariables(nsTemplate, maptemplate, nil, http.Header{}, map[string]any{}), nil
132+
return nsName, repoCRName, nil
118133
}

0 commit comments

Comments
 (0)