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.
This commit is contained in:
Simone Gotti 2019-04-11 16:49:07 +02:00
parent 782750e51e
commit ca1d837ecd
7 changed files with 15 additions and 11 deletions

View File

@ -103,6 +103,7 @@ func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) {
// common data // common data
whd := &types.WebhookData{ whd := &types.WebhookData{
CommitSHA: hook.After, CommitSHA: hook.After,
SSHURL: hook.Repo.SSHURL,
Ref: hook.Ref, Ref: hook.Ref,
CompareLink: hook.Compare, CompareLink: hook.Compare,
CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.After), CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.After),
@ -141,9 +142,10 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData {
if sender == "" { if sender == "" {
sender = hook.Sender.Login sender = hook.Sender.Login
} }
build := &types.WebhookData{ whd := &types.WebhookData{
Event: types.WebhookEventPullRequest, Event: types.WebhookEventPullRequest,
CommitSHA: hook.PullRequest.Head.Sha, CommitSHA: hook.PullRequest.Head.Sha,
SSHURL: hook.Repo.SSHURL,
Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number), Ref: fmt.Sprintf("refs/pull/%d/head", hook.Number),
CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.PullRequest.Head.Sha), CommitLink: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.PullRequest.Head.Sha),
Branch: hook.PullRequest.Base.Ref, Branch: hook.PullRequest.Base.Ref,
@ -157,5 +159,5 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData {
WebURL: hook.Repo.URL, WebURL: hook.Repo.URL,
}, },
} }
return build return whd
} }

View File

@ -35,6 +35,7 @@ type pushHook struct {
FullName string `json:"full_name"` FullName string `json:"full_name"`
URL string `json:"html_url"` URL string `json:"html_url"`
Private bool `json:"private"` Private bool `json:"private"`
SSHURL string `json:"ssh_url"`
Owner struct { Owner struct {
Name string `json:"name"` Name string `json:"name"`
Email string `json:"email"` Email string `json:"email"`
@ -86,6 +87,7 @@ type pullRequestHook struct {
FullName string `json:"full_name"` FullName string `json:"full_name"`
URL string `json:"html_url"` URL string `json:"html_url"`
Private bool `json:"private"` Private bool `json:"private"`
SSHURL string `json:"ssh_url"`
Owner struct { Owner struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Username string `json:"username"` Username string `json:"username"`
@ -105,6 +107,7 @@ type pullRequestHook struct {
FullName string `json:"full_name"` FullName string `json:"full_name"`
URL string `json:"html_url"` URL string `json:"html_url"`
Private bool `json:"private"` Private bool `json:"private"`
SSHURL string `json:"ssh_url"`
Owner struct { Owner struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Username string `json:"username"` Username string `json:"username"`
@ -121,6 +124,7 @@ type pullRequestHook struct {
FullName string `json:"full_name"` FullName string `json:"full_name"`
URL string `json:"html_url"` URL string `json:"html_url"`
Private bool `json:"private"` Private bool `json:"private"`
SSHURL string `json:"ssh_url"`
Owner struct { Owner struct {
ID int64 `json:"id"` ID int64 `json:"id"`
Username string `json:"username"` Username string `json:"username"`

View File

@ -106,6 +106,7 @@ func webhookDataFromPush(hook *pushHook) (*types.WebhookData, error) {
// common data // common data
whd := &types.WebhookData{ whd := &types.WebhookData{
CommitSHA: hook.After, CommitSHA: hook.After,
SSHURL: hook.Project.SSHURL,
Ref: hook.Ref, Ref: hook.Ref,
CommitLink: hook.Commits[0].URL, CommitLink: hook.Commits[0].URL,
//CompareLink: hook.Compare, //CompareLink: hook.Compare,
@ -153,6 +154,7 @@ func webhookDataFromPullRequest(hook *pullRequestHook) *types.WebhookData {
build := &types.WebhookData{ build := &types.WebhookData{
Event: types.WebhookEventPullRequest, Event: types.WebhookEventPullRequest,
CommitSHA: hook.ObjectAttributes.LastCommit.ID, CommitSHA: hook.ObjectAttributes.LastCommit.ID,
SSHURL: hook.Project.SSHURL,
Ref: fmt.Sprintf("refs/merge-requests/%d/head", hook.ObjectAttributes.Iid), 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: fmt.Sprintf("%s/commit/%s", hook.Repo.URL, hook.PullRequest.Head.Sha),
CommitLink: hook.ObjectAttributes.LastCommit.URL, CommitLink: hook.ObjectAttributes.LastCommit.URL,

View File

@ -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") 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") c.log.Infof("generating ssh key pairs")
privateKey, _, err := util.GenSSHKeyPair(4096) privateKey, _, err := util.GenSSHKeyPair(4096)
if err != nil { if err != nil {
@ -97,7 +94,6 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
LinkedAccountID: la.ID, LinkedAccountID: la.ID,
RepositoryID: repo.ID, RepositoryID: repo.ID,
RepositoryPath: req.RepoPath, RepositoryPath: req.RepoPath,
RepositoryCloneURL: sshCloneURL,
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck, SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
SSHPrivateKey: string(privateKey), SSHPrivateKey: string(privateKey),
} }

View File

@ -168,7 +168,6 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
} }
sshPrivKey = project.SSHPrivateKey sshPrivKey = project.SSHPrivateKey
cloneURL = project.RepositoryCloneURL
skipSSHHostKeyCheck = project.SkipSSHHostKeyCheck skipSSHHostKeyCheck = project.SkipSSHHostKeyCheck
runType = types.RunTypeProject runType = types.RunTypeProject
webhookData, err = gitSource.ParseWebhook(r) webhookData, err = gitSource.ParseWebhook(r)
@ -184,6 +183,8 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
webhookData.ProjectID = projectID webhookData.ProjectID = projectID
cloneURL = webhookData.SSHURL
// get project variables // get project variables
pvars, _, err := h.configstoreClient.GetProjectVariables(ctx, project.ID, true) pvars, _, err := h.configstoreClient.GetProjectVariables(ctx, project.ID, true)
if err != nil { if err != nil {

View File

@ -153,8 +153,6 @@ type Project struct {
// do this but gitlab can and github has an hidden api to do this) // do this but gitlab can and github has an hidden api to do this)
RepositoryPath string `json:"repository_path,omitempty"` RepositoryPath string `json:"repository_path,omitempty"`
RepositoryCloneURL string `json:"repository_clone_url,omitempty"`
LinkedAccountID string `json:"linked_account_id,omitempty"` LinkedAccountID string `json:"linked_account_id,omitempty"`
SSHPrivateKey string `json:"ssh_private_key,omitempty"` // PEM Encoded private key SSHPrivateKey string `json:"ssh_private_key,omitempty"` // PEM Encoded private key

View File

@ -32,9 +32,10 @@ const (
type WebhookData struct { type WebhookData struct {
Event WebhookEvent `json:"event,omitempty"` Event WebhookEvent `json:"event,omitempty"`
ProjectID string `json:"project_id,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 CompareLink string `json:"compare_link,omitempty"` // Compare link to remote git source
CommitLink string `json:"commit_link,omitempty"` // Pimray link to source. It can be the commit 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) 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 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...)