Skip to content

Commit

Permalink
Merge 2f6956f into 95b4c4d
Browse files Browse the repository at this point in the history
  • Loading branch information
knight42 committed Jan 14, 2021
2 parents 95b4c4d + 2f6956f commit 3a8be89
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion manifests/cyclone/templates/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ data:
"notifications": [
{
"name": "devops",
"url": "http://{{ .Release.Name }}-pipeline.{{ .Release.Namespace }}.svc.cluster.local:7088/api/v2/pipeline/notifications"
"url": "http://pipeline.{{ .Release.Namespace }}.svc.cluster.local:7088/?Action=ReceiveNotifications&Version=2020-10-10"
}
],
"client_set": {
Expand Down
40 changes: 33 additions & 7 deletions pkg/server/biz/hook/scm.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package hook

import (
"bytes"
"context"
"fmt"
"strings"
"sync"
"text/template"

"github.com/caicloud/nirvana/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -88,8 +89,12 @@ func createSCMWebhook(scmSource *api.SCMSource, tenant, secret, repo string) err
return err
}

webhookURL, err := generateWebhookURL(tenant, secret)
if err != nil {
return cerr.ErrorUnknownInternal.Error(err)
}
webhook := &scm.Webhook{
URL: generateWebhookURL(tenant, secret),
URL: webhookURL,
Events: []scm.EventType{
scm.PushEventType,
scm.TagReleaseEventType,
Expand All @@ -108,10 +113,26 @@ func createSCMWebhook(scmSource *api.SCMSource, tenant, secret, repo string) err
return err
}

func generateWebhookURL(tenant, secret string) string {
urlPrefix := strings.TrimPrefix(config.GetWebhookURLPrefix(), "/")
// Construct webhook URL, refer to cyclone/pkg/server/apis/v1alpha1/descriptors/webhook.go
return fmt.Sprintf("%s/tenants/%s/webhook?sourceType=SCM&integration=%s", urlPrefix, tenant, secret)
func generateWebhookURL(tenant, secret string) (string, error) {
type urlData struct {
Tenant string
SourceType string
Integration string
}
tmpl, err := template.New("webhookURL").Parse(config.Config.WebhookURLTemplate)
if err != nil {
return "", fmt.Errorf("parse template: %w", err)
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, urlData{
Tenant: tenant,
SourceType: "SCM",
Integration: secret,
})
if err != nil {
return "", fmt.Errorf("execute template: %w", err)
}
return buf.String(), nil
}

// Unregister unregisters SCM webhook if if has no other wft using.
Expand Down Expand Up @@ -162,7 +183,12 @@ func deleteSCMWebhook(scmSource *api.SCMSource, tenant, secret, repo string) err
return err
}

return sp.DeleteWebhook(repo, generateWebhookURL(tenant, secret))
webhookURL, err := generateWebhookURL(tenant, secret)
if err != nil {
log.Error("Error generating webhook URL: %v", err)
return fmt.Errorf("generate webhook URL: %w", err)
}
return sp.DeleteWebhook(repo, webhookURL)
}

// LabelSCMTrigger add labels about scm trigger
Expand Down
16 changes: 2 additions & 14 deletions pkg/server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ type CycloneServerConfig struct {
// eg map[core_v1.ResourceName]string{"cpu": "2", "memory": "4Gi"}
WorkerNamespaceQuota map[core_v1.ResourceName]string `json:"worker_namespace_quota"`

// WebhookURLPrefix represents the Cyclone server path to receive webhook requests.
// If Cyclone server can be accessed by external systems, it would like be `https://{cyclone-server}/apis/v1alpha1`.
WebhookURLPrefix string `json:"webhook_url_prefix"`
// WebhookURLTemplate represents the template of webhook url.
WebhookURLTemplate string `json:"webhook_url_template"`

// StorageUsageWatcher configures PVC storage usage watchers.
StorageUsageWatcher StorageUsageWatcher `json:"storage_usage_watcher"`
Expand Down Expand Up @@ -229,17 +228,6 @@ func modifier(config *CycloneServerConfig) {
}
}

// GetWebhookURLPrefix returns webhook callback url prefix. It tries to get the url from "WEBHOOK_URL_PREFIX"
// environment variable, if the value is empty, then get it from configmap.
func GetWebhookURLPrefix() string {
urlPrefix := os.Getenv(EnvWebhookURLPrefix)
if urlPrefix != "" {
return urlPrefix
}

return Config.WebhookURLPrefix
}

// GetRecordWebURLTemplate returns record web URL template. It tries to get the url from "RECORD_WEB_URL_TEMPLATE"
// environment variable, if the value is empty, then get it from configmap.
func GetRecordWebURLTemplate() string {
Expand Down

0 comments on commit 3a8be89

Please sign in to comment.