From f383a0056d2def6e8709a013a9158ed9bf447dc0 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 28 Feb 2019 17:19:53 +0100 Subject: [PATCH] gateway gitsources: use owner id for deploy keys and webhook urls In this way we could have multiple projects pointing to the same remote repository and every projects will have its own deploy key and webhook url --- internal/gitsources/gitea/gitea.go | 19 ++++++------------- internal/gitsources/gitlab/gitlab.go | 11 +++-------- internal/services/gateway/command/project.go | 6 +++++- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/internal/gitsources/gitea/gitea.go b/internal/gitsources/gitea/gitea.go index f55cb31..5bc11f4 100644 --- a/internal/gitsources/gitea/gitea.go +++ b/internal/gitsources/gitea/gitea.go @@ -17,7 +17,6 @@ package gitea import ( "crypto/tls" "net/http" - "net/url" "strconv" gitsource "github.com/sorintlab/agola/internal/gitsources" @@ -130,7 +129,7 @@ func (c *Client) CreateDeployKey(owner, repo, title, pubKey string, readonly boo func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly bool) error { // NOTE(sgotti) gitea has a bug where if we delete and remove the same key with // the same value it is correctly readded and the admin must force a - // authorizec_keys regeneration on the server. To avoid this we update it only + // authorized_keys regeneration on the server. To avoid this we update it only // when the public key value has changed keys, err := c.client.ListDeployKeys(owner, repo) if err != nil { @@ -193,23 +192,17 @@ func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error { } func (c *Client) DeleteRepoWebhook(owner, repo, u string) error { - curURL, err := url.Parse(u) - if err != nil { - return errors.Wrapf(err, "failed to parse url") - } - hooks, err := c.client.ListRepoHooks(owner, repo) if err != nil { return errors.Wrapf(err, "error retrieving repository webhooks") } + // match the full url so we can have multiple webhooks for different agola + // projects for _, hook := range hooks { - if hurl, ok := hook.Config["url"]; ok { - u, err := url.Parse(hurl) - if err == nil && u.Host == curURL.Host { - if err := c.client.DeleteRepoHook(owner, repo, hook.ID); err != nil { - return errors.Wrapf(err, "error deleting existing repository webhook") - } + if hook.Config["url"] == u { + if err := c.client.DeleteRepoHook(owner, repo, hook.ID); err != nil { + return errors.Wrapf(err, "error deleting existing repository webhook") } } } diff --git a/internal/gitsources/gitlab/gitlab.go b/internal/gitsources/gitlab/gitlab.go index 2a3371a..9f54153 100644 --- a/internal/gitsources/gitlab/gitlab.go +++ b/internal/gitsources/gitlab/gitlab.go @@ -21,7 +21,6 @@ import ( "fmt" "net" "net/http" - "net/url" "path" "strconv" "time" @@ -207,19 +206,15 @@ func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error { } func (c *Client) DeleteRepoWebhook(owner, repo, u string) error { - curURL, err := url.Parse(u) - if err != nil { - return errors.Wrapf(err, "failed to parse url") - } - hooks, _, err := c.client.Projects.ListProjectHooks(path.Join(owner, repo), nil) if err != nil { return errors.Wrapf(err, "error retrieving repository webhooks") } + // match the full url so we can have multiple webhooks for different agola + // projects for _, hook := range hooks { - u, err := url.Parse(hook.URL) - if err == nil && u.Host == curURL.Host { + if hook.URL == u { if _, err := c.client.Projects.DeleteProjectHook(path.Join(owner, repo), hook.ID); err != nil { return errors.Wrapf(err, "error deleting existing repository webhook") } diff --git a/internal/services/gateway/command/project.go b/internal/services/gateway/command/project.go index 1d8649d..ce3d98a 100644 --- a/internal/services/gateway/command/project.go +++ b/internal/services/gateway/command/project.go @@ -137,8 +137,12 @@ func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSourc webhookURL := fmt.Sprintf("%s/webhooks?projectid=%s", c.apiExposedURL, conf.Project.ID) + // generate deploy keys and webhooks containing the agola project id so we + // can have multiple projects referencing the same remote repository and this + // will trigger multiple different runs + deployKeyName := fmt.Sprintf("agola deploy key - %s", conf.Project.ID) c.log.Infof("creating/updating deploy key: %s", string(pubKey)) - if err := gitsource.UpdateDeployKey(conf.RepoOwner, conf.RepoName, "agola deploy key", string(pubKey), true); err != nil { + if err := gitsource.UpdateDeployKey(conf.RepoOwner, conf.RepoName, deployKeyName, string(pubKey), true); err != nil { return errors.Wrapf(err, "failed to create deploy key") } c.log.Infof("deleting existing webhooks")