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:
parent
41002efbff
commit
f383a0056d
|
@ -17,7 +17,6 @@ package gitea
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
gitsource "github.com/sorintlab/agola/internal/gitsources"
|
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 {
|
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
|
// 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
|
// 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
|
// when the public key value has changed
|
||||||
keys, err := c.client.ListDeployKeys(owner, repo)
|
keys, err := c.client.ListDeployKeys(owner, repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -193,26 +192,20 @@ func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteRepoWebhook(owner, repo, u 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)
|
hooks, err := c.client.ListRepoHooks(owner, repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving repository webhooks")
|
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 {
|
for _, hook := range hooks {
|
||||||
if hurl, ok := hook.Config["url"]; ok {
|
if hook.Config["url"] == u {
|
||||||
u, err := url.Parse(hurl)
|
|
||||||
if err == nil && u.Host == curURL.Host {
|
|
||||||
if err := c.client.DeleteRepoHook(owner, repo, hook.ID); err != nil {
|
if err := c.client.DeleteRepoHook(owner, repo, hook.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error deleting existing repository webhook")
|
return errors.Wrapf(err, "error deleting existing repository webhook")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"path"
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -207,19 +206,15 @@ func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteRepoWebhook(owner, repo, u 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)
|
hooks, _, err := c.client.Projects.ListProjectHooks(path.Join(owner, repo), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving repository webhooks")
|
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 {
|
for _, hook := range hooks {
|
||||||
u, err := url.Parse(hook.URL)
|
if hook.URL == u {
|
||||||
if err == nil && u.Host == curURL.Host {
|
|
||||||
if _, err := c.client.Projects.DeleteProjectHook(path.Join(owner, repo), hook.ID); err != nil {
|
if _, err := c.client.Projects.DeleteProjectHook(path.Join(owner, repo), hook.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error deleting existing repository webhook")
|
return errors.Wrapf(err, "error deleting existing repository webhook")
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
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))
|
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")
|
return errors.Wrapf(err, "failed to create deploy key")
|
||||||
}
|
}
|
||||||
c.log.Infof("deleting existing webhooks")
|
c.log.Infof("deleting existing webhooks")
|
||||||
|
|
Loading…
Reference in New Issue