project: save remote repository id

This commit is contained in:
Simone Gotti 2019-04-11 17:11:17 +02:00
parent 4e07550f30
commit 782750e51e
3 changed files with 27 additions and 30 deletions

View File

@ -73,7 +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")
} }
//cloneURL := fmt.Sprintf("git@%s:%s/%s.git", host, repoOwner, repoName)
sshCloneURL := repo.SSHCloneURL sshCloneURL := repo.SSHCloneURL
c.log.Infof("sshCloneURL: %s", sshCloneURL) c.log.Infof("sshCloneURL: %s", sshCloneURL)
@ -96,8 +95,9 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
ID: parentID, ID: parentID,
}, },
LinkedAccountID: la.ID, LinkedAccountID: la.ID,
RepoPath: req.RepoPath, RepositoryID: repo.ID,
CloneURL: sshCloneURL, RepositoryPath: req.RepoPath,
RepositoryCloneURL: sshCloneURL,
SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck, SkipSSHHostKeyCheck: req.SkipSSHHostKeyCheck,
SSHPrivateKey: string(privateKey), SSHPrivateKey: string(privateKey),
} }
@ -109,44 +109,36 @@ func (c *CommandHandler) CreateProject(ctx context.Context, req *CreateProjectRe
} }
c.log.Infof("project %s created, ID: %s", p.Name, p.ID) c.log.Infof("project %s created, ID: %s", p.Name, p.ID)
return p, c.SetupProject(ctx, rs, la, &SetupProjectRequest{ return p, c.SetupProject(ctx, rs, la, p)
Project: p,
RepoPath: req.RepoPath,
})
} }
type SetupProjectRequest struct { func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, la *types.LinkedAccount, project *types.Project) error {
Project *types.Project
RepoPath string
}
func (c *CommandHandler) SetupProject(ctx context.Context, rs *types.RemoteSource, la *types.LinkedAccount, conf *SetupProjectRequest) error {
gitsource, err := common.GetGitSource(rs, la) gitsource, err := common.GetGitSource(rs, la)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to create gitsource client") return errors.Wrapf(err, "failed to create gitsource client")
} }
pubKey, err := util.ExtractPublicKey([]byte(conf.Project.SSHPrivateKey)) pubKey, err := util.ExtractPublicKey([]byte(project.SSHPrivateKey))
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to extract public key") return errors.Wrapf(err, "failed to extract public key")
} }
webhookURL := fmt.Sprintf("%s/webhooks?projectid=%s", c.apiExposedURL, conf.Project.ID) webhookURL := fmt.Sprintf("%s/webhooks?projectid=%s", c.apiExposedURL, project.ID)
// generate deploy keys and webhooks containing the agola project id so we // generate deploy keys and webhooks containing the agola project id so we
// can have multiple projects referencing the same remote repository and this // can have multiple projects referencing the same remote repository and this
// 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", 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.RepoPath, deployKeyName, string(pubKey), true); err != nil { if err := gitsource.UpdateDeployKey(project.RepositoryPath, 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.RepoPath, webhookURL); err != nil { if err := gitsource.DeleteRepoWebhook(project.RepositoryPath, 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.RepoPath, webhookURL, ""); err != nil { if err := gitsource.CreateRepoWebhook(project.RepositoryPath, webhookURL, ""); err != nil {
return errors.Wrapf(err, "failed to create repository webhook") return errors.Wrapf(err, "failed to create repository webhook")
} }
@ -175,8 +167,7 @@ func (c *CommandHandler) ReconfigProject(ctx context.Context, projectRef string)
return ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID)) return ErrFromRemote(resp, errors.Wrapf(err, "failed to get remote source %q", la.RemoteSourceID))
} }
return c.SetupProject(ctx, rs, la, &SetupProjectRequest{ // TODO(sgotti) update project repo path if the remote let us query by repository id
Project: p,
RepoPath: p.RepoPath, return c.SetupProject(ctx, rs, la, p)
})
} }

View File

@ -168,7 +168,7 @@ func (h *webhooksHandler) handleWebhook(r *http.Request) (int, string, error) {
} }
sshPrivKey = project.SSHPrivateKey sshPrivKey = project.SSHPrivateKey
cloneURL = project.CloneURL 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)

View File

@ -142,15 +142,21 @@ type Project struct {
Parent Parent `json:"parent,omitempty"` Parent Parent `json:"parent,omitempty"`
// Project repository path. It may be different for every kind of git source. // The remote repository id
// It's needed to get git source needed information like the repo owner and RepositoryID string `json:"repository_id,omitempty"`
// repo user
// Examples: sgotti/agola (for github, gitea etc... sources) // The remote repository path. It may be different for every kind of git source.
RepoPath string `json:"repo_path,omitempty"` // NOTE: it may be changed remotely but won't be updated here. Every git source
// works differently so we must find a way to update it:
// * let the user update it manually
// * auto update it if the remote let us query by repository id (gitea cannot
// 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"` LinkedAccountID string `json:"linked_account_id,omitempty"`
CloneURL string `json:"clone_url,omitempty"`
SSHPrivateKey string `json:"ssh_private_key,omitempty"` // PEM Encoded private key SSHPrivateKey string `json:"ssh_private_key,omitempty"` // PEM Encoded private key
SkipSSHHostKeyCheck bool `json:"skip_ssh_host_key_check,omitempty"` SkipSSHHostKeyCheck bool `json:"skip_ssh_host_key_check,omitempty"`