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
This commit is contained in:
Simone Gotti 2019-02-28 17:19:53 +01:00
parent 41002efbff
commit f383a0056d
3 changed files with 14 additions and 22 deletions

View File

@ -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")
}
}
}

View File

@ -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")
}

View File

@ -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")