From ca1d837ecd4c484a9860e3cae1d23ebddfc9b18d Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Thu, 11 Apr 2019 16:49:07 +0200 Subject: [PATCH] webhook: use hook provided ssh url for cloning Additionally don't save a CloneURL field inside the project type. If in future some git source doesn't provide a clone url we could just calculate it from project.RepoPath or call the remote api to retrieve it. --- internal/gitsources/gitea/parse.go | 6 ++++-- internal/gitsources/gitea/types.go | 4 ++++ internal/gitsources/gitlab/parse.go | 2 ++ internal/services/gateway/command/project.go | 4 ---- internal/services/gateway/webhook.go | 3 ++- internal/services/types/types.go | 2 -- internal/services/types/webhook.go | 5 +++-- 7 files changed, 15 insertions(+), 11 deletions(-) diff --git a/internal/gitsources/gitea/parse.go b/internal/gitsources/gitea/parse.go index f07edf3..4a97769 100644 --- a/internal/gitsources/gitea/parse.go +++ b/internal/gitsources/gitea/parse.go @@ -103,6 +103,7 @@ func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) { // common data whd := &types.WebhookData{ CommitSHA: hook.After, + SSHURL: hook.Repo.SSHURL, Ref: hook.Ref, CompareLink: hook.Compare, CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.After), @@ -141,9 +142,10 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData { if sender == "" { sender = hook.Sender.Login } - build := &types.WebhookData{ + whd := &types.WebhookData{ Event: types.WebhookEventPullRequest, CommitSHA: hook.PullRequest.Head.Sha, + SSHURL: hook.Repo.SSHURL, Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number), CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.PullRequest.Head.Sha), Branch: hook.PullRequest.Base.Ref, @@ -157,5 +159,5 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData { WebURL: hook.Repo.URL, }, } - return build + return whd } diff --git a/internal/gitsources/gitea/types.go b/internal/gitsources/gitea/types.go index 129a54a..b573989 100644 --- a/internal/gitsources/gitea/types.go +++ b/internal/gitsources/gitea/types.go @@ -35,6 +35,7 @@ type pushHook struct { FullName string `json:"full_name"` URL string `json:"html_url"` Private bool `json:"private"` + SSHURL string `json:"ssh_url"` Owner struct { Name string `json:"name"` Email string `json:"email"` @@ -86,6 +87,7 @@ type pullRequestHook struct { FullName string `json:"full_name"` URL string `json:"html_url"` Private bool `json:"private"` + SSHURL string `json:"ssh_url"` Owner struct { ID int64 `json:"id"` Username string `json:"username"` @@ -105,6 +107,7 @@ type pullRequestHook struct { FullName string `json:"full_name"` URL string `json:"html_url"` Private bool `json:"private"` + SSHURL string `json:"ssh_url"` Owner struct { ID int64 `json:"id"` Username string `json:"username"` @@ -121,6 +124,7 @@ type pullRequestHook struct { FullName string `json:"full_name"` URL string `json:"html_url"` Private bool `json:"private"` + SSHURL string `json:"ssh_url"` Owner struct { ID int64 `json:"id"` Username string `json:"username"` diff --git a/internal/gitsources/gitlab/parse.go b/internal/gitsources/gitlab/parse.go index cd89d91..f2a5ae9 100644 --- a/internal/gitsources/gitlab/parse.go +++ b/internal/gitsources/gitlab/parse.go @@ -106,6 +106,7 @@ func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) { // common data whd := &types.WebhookData{ CommitSHA: hook.After, + SSHURL: hook.Project.SSHURL, Ref: hook.Ref, CommitLink: hook.Commits[0].URL, //CompareLink: hook.Compare, @@ -153,6 +154,7 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData { build := &types.WebhookData{ Event: types.WebhookEventPullRequest, CommitSHA: hook.ObjectAttributes.LastCommit.ID, + SSHURL: hook.Project.SSHURL, Ref: fmt.Sprintf("refs/merge-requests/%d/head", hook.ObjectAttributes.Iid), //CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.PullRequest.Head.Sha), CommitLink: hook.ObjectAttributes.LastCommit.URL, diff --git a/internal/services/gateway/command/project.go b/internal/services/gateway/command/project.go index 92c23bd..337051c 100644 --- a/internal/services/gateway/command/project.go +++ b/internal/services/gateway/command/project.go @@ -73,9 +73,6 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe return nil, errors.Wrapf(err, "failed to get repository info from gitsource") } - sshCloneURL := repo.SSHCloneURL - c.log.Infof("sshCloneURL: %s", sshCloneURL) - c.log.Infof("generating ssh key pairs") privateKey, _, err := util.GenSSHKeyPair(4096) if err != nil { @@ -97,7 +94,6 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe LinkedAccountID: la.ID, RepositoryID: repo.ID, RepositoryPath: req.RepoPath, - RepositoryCloneURL: sshCloneURL, SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck, SSHPrivateKey: string(privateKey), } diff --git a/internal/services/gateway/webhook.go b/internal/services/gateway/webhook.go index ed2b97f..a25b3ec 100644 --- a/internal/services/gateway/webhook.go +++ b/internal/services/gateway/webhook.go @@ -168,7 +168,6 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) { } sshPrivKey = project.SSHPrivateKey - cloneURL = project.RepositoryCloneURL skipSSHHostKeyCheck = project.SkipSSHHostKeyCheck runType = types.RunTypeProject webhookData, err = gitSource.ParseWebhook(r) @@ -184,6 +183,8 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) { webhookData.ProjectID = projectID + cloneURL = webhookData.SSHURL + // get project variables pvars, _, err := h.configstoreClient.GetProjectVariables(ctx, project.ID, true) if err != nil { diff --git a/internal/services/types/types.go b/internal/services/types/types.go index 6f102a1..0a08ea4 100644 --- a/internal/services/types/types.go +++ b/internal/services/types/types.go @@ -153,8 +153,6 @@ type Project struct { // do this but gitlab can and github has an hidden api to do this) RepositoryPath string `json:"repository_path,omitempty"` - RepositoryCloneURL string `json:"repository_clone_url,omitempty"` - LinkedAccountID string `json:"linked_account_id,omitempty"` SSHPrivateKey string `json:"ssh_private_key,omitempty"` // PEM Encoded private key diff --git a/internal/services/types/webhook.go b/internal/services/types/webhook.go index 6c975a1..bdfb952 100644 --- a/internal/services/types/webhook.go +++ b/internal/services/types/webhook.go @@ -32,9 +32,10 @@ const ( type WebhookData struct { Event WebhookEvent `json:"event,omitempty"` ProjectID string `json:"project_id,omitempty"` + SSHURL string `json:"ssh_url"` - 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 + CompareLink string `json:"compare_link,omitempty"` // Compare link to remote git source + CommitLink string `json:"commit_link,omitempty"` // Commit link to remote git source CommitSHA string `json:"commit_sha,omitempty"` // commit SHA (SHA1 but also future SHA like SHA256) 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...)