gitsources: update api
* Generalize to use repopath instead of (owner, reponame)
This commit is contained in:
parent
a2cd92c70b
commit
4cf72748b4
|
@ -127,12 +127,12 @@ func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetRepoInfo(owner, reponame string) (*gitsource.RepoInfo, error) {
|
func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetFile(owner, repo, commit, file string) ([]byte, error) {
|
func (c *Client) GetFile(repopath, commit, file string) ([]byte, error) {
|
||||||
resp, err := c.getResponse("GET", fmt.Sprintf("%s/%s/raw/%s/%s", owner, repo, commit, file), nil, nil, nil)
|
resp, err := c.getResponse("GET", fmt.Sprintf("%s/raw/%s/%s", repopath, commit, file), nil, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -142,27 +142,27 @@ func (c *Client) GetFile(owner, repo, commit, file string) ([]byte, error) {
|
||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateDeployKey(owner, repo, title, pubKey string, readonly bool) error {
|
func (c *Client) CreateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteDeployKey(owner, repo, title string) error {
|
func (c *Client) DeleteDeployKey(repopath, title string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly bool) error {
|
func (c *Client) UpdateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error {
|
func (c *Client) CreateRepoWebhook(repopath, url, secret string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteRepoWebhook(owner, repo, u string) error {
|
func (c *Client) DeleteRepoWebhook(repopath, u string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateStatus(owner, repo, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error {
|
func (c *Client) CreateStatus(repopath, commitSHA string, status gitsource.CommitStatus, targetURL, description, context string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -107,10 +108,8 @@ func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) {
|
||||||
Sender: sender,
|
Sender: sender,
|
||||||
|
|
||||||
Repo: types.WebhookDataRepo{
|
Repo: types.WebhookDataRepo{
|
||||||
Name: hook.Repo.Name,
|
Path: path.Join(hook.Repo.Owner.Username, hook.Repo.Name),
|
||||||
Owner: hook.Repo.Owner.Username,
|
WebURL: hook.Repo.URL,
|
||||||
FullName: hook.Repo.FullName,
|
|
||||||
RepoURL: hook.Repo.URL,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,10 +142,8 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData {
|
||||||
PullRequestLink: hook.PullRequest.URL,
|
PullRequestLink: hook.PullRequest.URL,
|
||||||
|
|
||||||
Repo: types.WebhookDataRepo{
|
Repo: types.WebhookDataRepo{
|
||||||
Name: hook.Repo.Name,
|
Path: path.Join(hook.Repo.Owner.Username, hook.Repo.Name),
|
||||||
Owner: hook.Repo.Owner.Username,
|
WebURL: hook.Repo.URL,
|
||||||
FullName: hook.Repo.FullName,
|
|
||||||
RepoURL: hook.Repo.URL,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return build
|
return build
|
||||||
|
|
|
@ -18,6 +18,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
gitsource "github.com/sorintlab/agola/internal/gitsources"
|
gitsource "github.com/sorintlab/agola/internal/gitsources"
|
||||||
|
|
||||||
|
@ -56,6 +57,14 @@ func fromCommitStatus(status gitsource.CommitStatus) gitea.StatusState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func parseRepoPath(repopath string) (string, string, error) {
|
||||||
|
parts := strings.Split(repopath, "/")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return "", "", errors.Errorf("wrong gitea repo path: %q", repopath)
|
||||||
|
}
|
||||||
|
return parts[0], parts[1], nil
|
||||||
|
}
|
||||||
|
|
||||||
func New(opts Opts) (*Client, error) {
|
func New(opts Opts) (*Client, error) {
|
||||||
client := gitea.NewClient(opts.URL, opts.Token)
|
client := gitea.NewClient(opts.URL, opts.Token)
|
||||||
if opts.SkipVerify {
|
if opts.SkipVerify {
|
||||||
|
@ -111,7 +120,11 @@ func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetRepoInfo(owner, reponame string) (*gitsource.RepoInfo, error) {
|
func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
|
||||||
|
owner, reponame, err := parseRepoPath(repopath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
repo, err := c.client.GetRepo(owner, reponame)
|
repo, err := c.client.GetRepo(owner, reponame)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -123,13 +136,21 @@ func (c *Client) GetRepoInfo(owner, reponame string) (*gitsource.RepoInfo, error
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetFile(owner, repo, commit, file string) ([]byte, error) {
|
func (c *Client) GetFile(repopath, commit, file string) ([]byte, error) {
|
||||||
data, err := c.client.GetFile(owner, repo, commit, file)
|
owner, reponame, err := parseRepoPath(repopath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
data, err := c.client.GetFile(owner, reponame, commit, file)
|
||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateDeployKey(owner, repo, title, pubKey string, readonly bool) error {
|
func (c *Client) CreateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
||||||
_, err := c.client.CreateDeployKey(owner, repo, gitea.CreateKeyOption{
|
owner, reponame, err := parseRepoPath(repopath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = c.client.CreateDeployKey(owner, reponame, gitea.CreateKeyOption{
|
||||||
Title: title,
|
Title: title,
|
||||||
Key: pubKey,
|
Key: pubKey,
|
||||||
ReadOnly: readonly,
|
ReadOnly: readonly,
|
||||||
|
@ -138,12 +159,16 @@ func (c *Client) CreateDeployKey(owner, repo, title, pubKey string, readonly boo
|
||||||
return errors.Wrapf(err, "error creating deploy key")
|
return errors.Wrapf(err, "error creating deploy key")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly bool) error {
|
func (c *Client) UpdateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
||||||
|
owner, reponame, err := parseRepoPath(repopath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
// 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
|
||||||
// authorized_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, reponame)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
||||||
}
|
}
|
||||||
|
@ -153,13 +178,13 @@ func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly boo
|
||||||
if key.Key == pubKey {
|
if key.Key == pubKey {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if err := c.client.DeleteDeployKey(owner, repo, key.ID); err != nil {
|
if err := c.client.DeleteDeployKey(owner, reponame, key.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error removing existing deploy key")
|
return errors.Wrapf(err, "error removing existing deploy key")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := c.client.CreateDeployKey(owner, repo, gitea.CreateKeyOption{
|
if _, err := c.client.CreateDeployKey(owner, reponame, gitea.CreateKeyOption{
|
||||||
Title: title,
|
Title: title,
|
||||||
Key: pubKey,
|
Key: pubKey,
|
||||||
ReadOnly: readonly,
|
ReadOnly: readonly,
|
||||||
|
@ -170,15 +195,19 @@ func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly boo
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteDeployKey(owner, repo, title string) error {
|
func (c *Client) DeleteDeployKey(repopath, title string) error {
|
||||||
keys, err := c.client.ListDeployKeys(owner, repo)
|
owner, reponame, err := parseRepoPath(repopath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
keys, err := c.client.ListDeployKeys(owner, reponame)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
if key.Title == title {
|
if key.Title == title {
|
||||||
if err := c.client.DeleteDeployKey(owner, repo, key.ID); err != nil {
|
if err := c.client.DeleteDeployKey(owner, reponame, key.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error removing existing deploy key")
|
return errors.Wrapf(err, "error removing existing deploy key")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -187,7 +216,12 @@ func (c *Client) DeleteDeployKey(owner, repo, title string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error {
|
func (c *Client) CreateRepoWebhook(repopath, url, secret string) error {
|
||||||
|
owner, reponame, err := parseRepoPath(repopath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
opts := gitea.CreateHookOption{
|
opts := gitea.CreateHookOption{
|
||||||
Type: "gitea",
|
Type: "gitea",
|
||||||
Config: map[string]string{
|
Config: map[string]string{
|
||||||
|
@ -198,13 +232,18 @@ func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error {
|
||||||
Events: []string{"push", "pull_request"},
|
Events: []string{"push", "pull_request"},
|
||||||
Active: true,
|
Active: true,
|
||||||
}
|
}
|
||||||
_, err := c.client.CreateRepoHook(owner, repo, opts)
|
|
||||||
|
_, err = c.client.CreateRepoHook(owner, reponame, opts)
|
||||||
|
|
||||||
return errors.Wrapf(err, "error creating repository webhook")
|
return errors.Wrapf(err, "error creating repository webhook")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteRepoWebhook(owner, repo, u string) error {
|
func (c *Client) DeleteRepoWebhook(repopath, u string) error {
|
||||||
hooks, err := c.client.ListRepoHooks(owner, repo)
|
owner, reponame, err := parseRepoPath(repopath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
hooks, err := c.client.ListRepoHooks(owner, reponame)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving repository webhooks")
|
return errors.Wrapf(err, "error retrieving repository webhooks")
|
||||||
}
|
}
|
||||||
|
@ -213,7 +252,7 @@ func (c *Client) DeleteRepoWebhook(owner, repo, u string) error {
|
||||||
// projects
|
// projects
|
||||||
for _, hook := range hooks {
|
for _, hook := range hooks {
|
||||||
if hook.Config["url"] == u {
|
if hook.Config["url"] == u {
|
||||||
if err := c.client.DeleteRepoHook(owner, repo, hook.ID); err != nil {
|
if err := c.client.DeleteRepoHook(owner, reponame, hook.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error deleting existing repository webhook")
|
return errors.Wrapf(err, "error deleting existing repository webhook")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -108,10 +109,8 @@ func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) {
|
||||||
Sender: sender,
|
Sender: sender,
|
||||||
|
|
||||||
Repo: types.WebhookDataRepo{
|
Repo: types.WebhookDataRepo{
|
||||||
Name: hook.Repo.Name,
|
Path: path.Join(hook.Repo.Owner.Username, hook.Repo.Name),
|
||||||
Owner: hook.Repo.Owner.Username,
|
WebURL: hook.Repo.URL,
|
||||||
FullName: hook.Repo.FullName,
|
|
||||||
RepoURL: hook.Repo.URL,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +129,7 @@ func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) {
|
||||||
whd.Message = fmt.Sprintf("Tag %s", whd.Tag)
|
whd.Message = fmt.Sprintf("Tag %s", whd.Tag)
|
||||||
default:
|
default:
|
||||||
// ignore received webhook since it doesn't have a ref we're interested in
|
// ignore received webhook since it doesn't have a ref we're interested in
|
||||||
return nil, fmt.Errorf("Unsupported webhook ref %q", hook.Ref)
|
return nil, fmt.Errorf("unsupported webhook ref %q", hook.Ref)
|
||||||
}
|
}
|
||||||
|
|
||||||
return whd, nil
|
return whd, nil
|
||||||
|
@ -154,10 +153,8 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData {
|
||||||
PullRequestLink: hook.PullRequest.URL,
|
PullRequestLink: hook.PullRequest.URL,
|
||||||
|
|
||||||
Repo: types.WebhookDataRepo{
|
Repo: types.WebhookDataRepo{
|
||||||
Name: hook.Repo.Name,
|
Path: path.Join(hook.Repo.Owner.Username, hook.Repo.Name),
|
||||||
Owner: hook.Repo.Owner.Username,
|
WebURL: hook.Repo.URL,
|
||||||
FullName: hook.Repo.FullName,
|
|
||||||
RepoURL: hook.Repo.URL,
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return build
|
return build
|
||||||
|
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -121,8 +120,8 @@ func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, er
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetRepoInfo(owner, reponame string) (*gitsource.RepoInfo, error) {
|
func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
|
||||||
repo, _, err := c.client.Projects.GetProject(path.Join(owner, reponame))
|
repo, _, err := c.client.Projects.GetProject(repopath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -145,8 +144,8 @@ func (c *Client) GetUserInfo() (*gitsource.UserInfo, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) GetFile(owner, repo, commit, file string) ([]byte, error) {
|
func (c *Client) GetFile(repopath, commit, file string) ([]byte, error) {
|
||||||
f, _, err := c.client.RepositoryFiles.GetFile(path.Join(owner, repo), file, &gitlab.GetFileOptions{Ref: gitlab.String(commit)})
|
f, _, err := c.client.RepositoryFiles.GetFile(repopath, file, &gitlab.GetFileOptions{Ref: gitlab.String(commit)})
|
||||||
data, err := base64.StdEncoding.DecodeString(f.Content)
|
data, err := base64.StdEncoding.DecodeString(f.Content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -154,8 +153,8 @@ func (c *Client) GetFile(owner, repo, commit, file string) ([]byte, error) {
|
||||||
return data, err
|
return data, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateDeployKey(owner, repo, title, pubKey string, readonly bool) error {
|
func (c *Client) CreateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
||||||
_, _, err := c.client.DeployKeys.AddDeployKey(path.Join(owner, repo), &gitlab.AddDeployKeyOptions{
|
_, _, err := c.client.DeployKeys.AddDeployKey(repopath, &gitlab.AddDeployKeyOptions{
|
||||||
Title: gitlab.String(title),
|
Title: gitlab.String(title),
|
||||||
Key: gitlab.String(pubKey),
|
Key: gitlab.String(pubKey),
|
||||||
})
|
})
|
||||||
|
@ -163,8 +162,8 @@ func (c *Client) CreateDeployKey(owner, repo, title, pubKey string, readonly boo
|
||||||
return errors.Wrapf(err, "error creating deploy key")
|
return errors.Wrapf(err, "error creating deploy key")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly bool) error {
|
func (c *Client) UpdateDeployKey(repopath, title, pubKey string, readonly bool) error {
|
||||||
keys, _, err := c.client.DeployKeys.ListProjectDeployKeys(path.Join(owner, repo), nil)
|
keys, _, err := c.client.DeployKeys.ListProjectDeployKeys(repopath, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
||||||
}
|
}
|
||||||
|
@ -174,13 +173,13 @@ func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly boo
|
||||||
if key.Key == pubKey {
|
if key.Key == pubKey {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if _, err := c.client.DeployKeys.DeleteDeployKey(path.Join(owner, repo), key.ID); err != nil {
|
if _, err := c.client.DeployKeys.DeleteDeployKey(repopath, key.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error removing existing deploy key")
|
return errors.Wrapf(err, "error removing existing deploy key")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, _, err := c.client.DeployKeys.AddDeployKey(path.Join(owner, repo), &gitlab.AddDeployKeyOptions{
|
if _, _, err := c.client.DeployKeys.AddDeployKey(repopath, &gitlab.AddDeployKeyOptions{
|
||||||
Title: &title,
|
Title: &title,
|
||||||
Key: &pubKey,
|
Key: &pubKey,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
@ -190,15 +189,15 @@ func (c *Client) UpdateDeployKey(owner, repo, title, pubKey string, readonly boo
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteDeployKey(owner, repo, title string) error {
|
func (c *Client) DeleteDeployKey(repopath, title string) error {
|
||||||
keys, _, err := c.client.DeployKeys.ListProjectDeployKeys(path.Join(owner, repo), nil)
|
keys, _, err := c.client.DeployKeys.ListProjectDeployKeys(repopath, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
return errors.Wrapf(err, "error retrieving existing deploy keys")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
if key.Title == title {
|
if key.Title == title {
|
||||||
if _, err := c.client.DeployKeys.DeleteDeployKey(path.Join(owner, repo), key.ID); err != nil {
|
if _, err := c.client.DeployKeys.DeleteDeployKey(repopath, key.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error removing existing deploy key")
|
return errors.Wrapf(err, "error removing existing deploy key")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -207,18 +206,19 @@ func (c *Client) DeleteDeployKey(owner, repo, title string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) CreateRepoWebhook(owner, repo, url, secret string) error {
|
func (c *Client) CreateRepoWebhook(repopath, url, secret string) error {
|
||||||
opts := &gitlab.AddProjectHookOptions{
|
opts := &gitlab.AddProjectHookOptions{
|
||||||
URL: gitlab.String(url),
|
URL: gitlab.String(url),
|
||||||
PushEvents: gitlab.Bool(true),
|
PushEvents: gitlab.Bool(true),
|
||||||
|
MergeRequestsEvents: gitlab.Bool(true),
|
||||||
}
|
}
|
||||||
_, _, err := c.client.Projects.AddProjectHook(path.Join(owner, repo), opts)
|
_, _, err := c.client.Projects.AddProjectHook(repopath, opts)
|
||||||
|
|
||||||
return errors.Wrapf(err, "error creating repository webhook")
|
return errors.Wrapf(err, "error creating repository webhook")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteRepoWebhook(owner, repo, u string) error {
|
func (c *Client) DeleteRepoWebhook(repopath, u string) error {
|
||||||
hooks, _, err := c.client.Projects.ListProjectHooks(path.Join(owner, repo), nil)
|
hooks, _, err := c.client.Projects.ListProjectHooks(repopath, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "error retrieving repository webhooks")
|
return errors.Wrapf(err, "error retrieving repository webhooks")
|
||||||
}
|
}
|
||||||
|
@ -227,7 +227,7 @@ func (c *Client) DeleteRepoWebhook(owner, repo, u string) error {
|
||||||
// projects
|
// projects
|
||||||
for _, hook := range hooks {
|
for _, hook := range hooks {
|
||||||
if hook.URL == u {
|
if hook.URL == u {
|
||||||
if _, err := c.client.Projects.DeleteProjectHook(path.Join(owner, repo), hook.ID); err != nil {
|
if _, err := c.client.Projects.DeleteProjectHook(repopath, hook.ID); err != nil {
|
||||||
return errors.Wrapf(err, "error deleting existing repository webhook")
|
return errors.Wrapf(err, "error deleting existing repository webhook")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -237,6 +237,5 @@ func (c *Client) DeleteRepoWebhook(owner, repo, u string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) {
|
func (c *Client) ParseWebhook(r *http.Request) (*types.WebhookData, error) {
|
||||||
hookEvent := "X-Gitea-Event"
|
return nil, errors.Errorf("unimplemented")
|
||||||
return nil, errors.Errorf("unknown webhook event type: %q", r.Header.Get(hookEvent))
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,13 +30,13 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitSource interface {
|
type GitSource interface {
|
||||||
GetRepoInfo(owner, repo string) (*RepoInfo, error)
|
GetRepoInfo(repopath string) (*RepoInfo, error)
|
||||||
GetFile(owner, repo, commit, file string) ([]byte, error)
|
GetFile(repopath, commit, file string) ([]byte, error)
|
||||||
DeleteDeployKey(owner, repo, title string) error
|
DeleteDeployKey(repopath, title string) error
|
||||||
CreateDeployKey(owner, repo, title, pubKey string, readonly bool) error
|
CreateDeployKey(repopath, title, pubKey string, readonly bool) error
|
||||||
UpdateDeployKey(owner, repo, title, pubKey string, readonly bool) error
|
UpdateDeployKey(repopath, title, pubKey string, readonly bool) error
|
||||||
DeleteRepoWebhook(owner, repo, url string) error
|
DeleteRepoWebhook(repopath, url string) error
|
||||||
CreateRepoWebhook(owner, repo, url, secret string) error
|
CreateRepoWebhook(repopath, url, secret string) error
|
||||||
ParseWebhook(r *http.Request) (*types.WebhookData, error)
|
ParseWebhook(r *http.Request) (*types.WebhookData, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/sorintlab/agola/internal/services/gateway/common"
|
"github.com/sorintlab/agola/internal/services/gateway/common"
|
||||||
"github.com/sorintlab/agola/internal/services/types"
|
"github.com/sorintlab/agola/internal/services/types"
|
||||||
|
@ -67,11 +66,7 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
|
||||||
return nil, errors.Wrapf(err, "failed to create gitsource client")
|
return nil, errors.Wrapf(err, "failed to create gitsource client")
|
||||||
}
|
}
|
||||||
|
|
||||||
repoOwner := strings.TrimPrefix(path.Dir(req.RepoPath), "/")
|
repo, err := gitsource.GetRepoInfo(req.RepoPath)
|
||||||
repoName := path.Base(req.RepoPath)
|
|
||||||
c.log.Infof("repoOwner: %s, repoName: %s", repoOwner, repoName)
|
|
||||||
|
|
||||||
repo, err := gitsource.GetRepoInfo(repoOwner, repoName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "failed to get repository info from gitsource")
|
return nil, errors.Wrapf(err, "failed to get repository info from gitsource")
|
||||||
}
|
}
|
||||||
|
@ -114,15 +109,13 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
|
||||||
|
|
||||||
return p, c.SetupProject(ctx, rs, la, &SetupProjectRequest{
|
return p, c.SetupProject(ctx, rs, la, &SetupProjectRequest{
|
||||||
Project: p,
|
Project: p,
|
||||||
RepoOwner: repoOwner,
|
RepoPath: req.RepoPath,
|
||||||
RepoName: repoName,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetupProjectRequest struct {
|
type SetupProjectRequest struct {
|
||||||
Project *types.Project
|
Project *types.Project
|
||||||
RepoOwner string
|
RepoPath string
|
||||||
RepoName string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, la *types.LinkedAccount, conf *SetupProjectRequest) error {
|
func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, la *types.LinkedAccount, conf *SetupProjectRequest) error {
|
||||||
|
@ -143,15 +136,15 @@ func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSourc
|
||||||
// will trigger multiple different runs
|
// will trigger multiple different runs
|
||||||
deployKeyName := fmt.Sprintf("agola deploy key - %s", conf.Project.ID)
|
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, deployKeyName, string(pubKey), true); err != nil {
|
if err := gitsource.UpdateDeployKey(conf.RepoPath, 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")
|
||||||
if err := gitsource.DeleteRepoWebhook(conf.RepoOwner, conf.RepoName, webhookURL); err != nil {
|
if err := gitsource.DeleteRepoWebhook(conf.RepoPath, webhookURL); err != nil {
|
||||||
return errors.Wrapf(err, "failed to delete repository webhook")
|
return errors.Wrapf(err, "failed to delete repository webhook")
|
||||||
}
|
}
|
||||||
c.log.Infof("creating webhook to url: %s", webhookURL)
|
c.log.Infof("creating webhook to url: %s", webhookURL)
|
||||||
if err := gitsource.CreateRepoWebhook(conf.RepoOwner, conf.RepoName, webhookURL, ""); err != nil {
|
if err := gitsource.CreateRepoWebhook(conf.RepoPath, webhookURL, ""); err != nil {
|
||||||
return errors.Wrapf(err, "failed to create repository webhook")
|
return errors.Wrapf(err, "failed to create repository webhook")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,12 +173,8 @@ func (c *CommandHandler) ReconfigProject(ctx context.Context, projectID string)
|
||||||
return errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID)
|
return errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID)
|
||||||
}
|
}
|
||||||
|
|
||||||
repoOwner := strings.TrimPrefix(path.Dir(p.RepoPath), "/")
|
|
||||||
repoName := path.Base(p.RepoPath)
|
|
||||||
|
|
||||||
return c.SetupProject(ctx, rs, la, &SetupProjectRequest{
|
return c.SetupProject(ctx, rs, la, &SetupProjectRequest{
|
||||||
Project: p,
|
Project: p,
|
||||||
RepoOwner: repoOwner,
|
RepoPath: p.RepoPath,
|
||||||
RepoName: repoName,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -231,7 +231,7 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
|
||||||
h.log.Debugf("user: %s", util.Dump(user))
|
h.log.Debugf("user: %s", util.Dump(user))
|
||||||
userID = user.ID
|
userID = user.ID
|
||||||
|
|
||||||
cloneURL = fmt.Sprintf("%s/%s/%s", h.apiExposedURL+"/repos", webhookData.Repo.Owner, webhookData.Repo.Name)
|
cloneURL = fmt.Sprintf("%s/%s", h.apiExposedURL+"/repos", webhookData.Repo.Path)
|
||||||
runType = types.RunTypeUser
|
runType = types.RunTypeUser
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -240,7 +240,7 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
|
||||||
var data []byte
|
var data []byte
|
||||||
err := util.ExponentialBackoff(util.FetchFileBackoff, func() (bool, error) {
|
err := util.ExponentialBackoff(util.FetchFileBackoff, func() (bool, error) {
|
||||||
var err error
|
var err error
|
||||||
data, err = gitSource.GetFile(webhookData.Repo.Owner, webhookData.Repo.Name, webhookData.CommitSHA, agolaDefaultConfigPath)
|
data, err = gitSource.GetFile(webhookData.Repo.Path, webhookData.CommitSHA, agolaDefaultConfigPath)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@ type WebhookData struct {
|
||||||
CompareLink string `json:"compare_link,omitempty"` // Pimray link to source. It can be the commit
|
CompareLink string `json:"compare_link,omitempty"` // Pimray link to source. It can be the commit
|
||||||
CommitLink string `json:"commit_link,omitempty"` // Pimray link to source. It can be the commit
|
CommitLink string `json:"commit_link,omitempty"` // Pimray link to source. It can be the commit
|
||||||
CommitSHA string `json:"commit_sha,omitempty"` // commit SHA (SHA1 but also future SHA like SHA256)
|
CommitSHA string `json:"commit_sha,omitempty"` // commit SHA (SHA1 but also future SHA like SHA256)
|
||||||
OldCommitSHA string `json:"old_commit_sha,omitempty"` // commit SHA of the head before this push
|
|
||||||
Ref string `json:"ref,omitempty"` // Ref containing the commit SHA
|
Ref string `json:"ref,omitempty"` // Ref containing the commit SHA
|
||||||
Message string `json:"message,omitempty"` // Message to use (Push last commit message summary, PR title, Tag message etc...)
|
Message string `json:"message,omitempty"` // Message to use (Push last commit message summary, PR title, Tag message etc...)
|
||||||
Sender string `json:"sender,omitempty"`
|
Sender string `json:"sender,omitempty"`
|
||||||
|
@ -56,8 +55,6 @@ type WebhookData struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type WebhookDataRepo struct {
|
type WebhookDataRepo struct {
|
||||||
Name string `json:"name,omitempty"`
|
WebURL string `json:"web_url,omitempty"`
|
||||||
Owner string `json:"owner,omitempty"`
|
Path string `json:"path,omitempty"`
|
||||||
FullName string `json:"full_name,omitempty"`
|
|
||||||
RepoURL string `json:"repo_url,omitempty"`
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue