Fix cloning down new repositories (fix #12)

This commit is contained in:
Noah 2024-03-10 23:13:43 -05:00
parent 5928a0a1d6
commit 541fb1ca99
2 changed files with 35 additions and 28 deletions

27
main.go
View File

@ -27,15 +27,26 @@ func handler(data map[string]interface{}) {
log.Default().Printf("Repo: %s", repository["full_name"])
sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%s`...", repository["full_name"])})
deploy_error := pullRepo(repository["ssh_url"].(string), repository["name"].(string))
if deploy_error.code != 0 {
deploy_error.SendOverMatrix()
return
repo_name := repository["name"].(string)
exists, err := fileExists(configuration.sites_dir+"/"+repo_name)
if err != nil {
newDeployError(1, "handler", "Failed to check if site '%s' exists", fmt.Sprint(err))
}
if exists {
if deploy_error := updateSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
if deploy_error := restartSite(repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
} else {
if deploy_error := cloneSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
if deploy_error := startSite(repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix(); return
}
deploy_error = restartSite(repository["name"].(string))
if deploy_error.code != 0 {
deploy_error.SendOverMatrix()
return
}
sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"})
}

32
site.go
View File

@ -82,28 +82,24 @@ func restartSite(name string) DeployError {
return DeployError{}
}
// pullRepo attempts to pull or clone a repository using the given name and url
func pullRepo(url string, name string) DeployError {
exists, err := fileExists(configuration.sites_dir+"/"+name)
if err != nil {
return newDeployError(1, "fileExists",
fmt.Sprintf("Failed to check whether folder '%s' exists while pulling down repository '%s'!", name, name), "")
}
if !exists {
log.Printf("Cloning repository %s...", name)
if err = git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to pull down repository '%s'", name), "")
}
if err := startSite(name); err.code != 0 {
return err
}
} else {
// updateSite attempts to pull or clone a repository using the given name and url
func updateSite(url string, name string) DeployError {
log.Printf("Pulling down repository %s...", name)
repo,err := git.Open(configuration.sites_dir+"/"+name)
if err != nil {
return newDeployError(1, "pullRepo", fmt.Sprintf("Failed to open git repository '%s'", name), "")
return newDeployError(1, "updateSite", fmt.Sprintf("Failed to open git repository '%s'", name), fmt.Sprint(err))
}
repo.Pull()
if err = repo.Pull(); err != nil {
return newDeployError(1, "updateSite", fmt.Sprintf("Failed to pull down git repository '%s'", name), fmt.Sprint(err))
}
return DeployError{}
}
// cloneSite
func cloneSite(url string, name string) DeployError {
log.Printf("Cloning repository %s...", name)
if err := git.Clone(url, configuration.sites_dir+"/"+name); err != nil {
return newDeployError(1, "cloneSite", fmt.Sprintf("Failed to pull down repository '%s'", name), fmt.Sprint(err))
}
return DeployError{}
}