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"]) log.Default().Printf("Repo: %s", repository["full_name"])
sendMessage(MatrixMessage{text: fmt.Sprintf("⚪️ Handling webhook for `%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)) repo_name := repository["name"].(string)
if deploy_error.code != 0 {
deploy_error.SendOverMatrix() exists, err := fileExists(configuration.sites_dir+"/"+repo_name)
return if err != nil {
newDeployError(1, "handler", "Failed to check if site '%s' exists", fmt.Sprint(err))
} }
deploy_error = restartSite(repository["name"].(string)) if exists {
if deploy_error.code != 0 { if deploy_error := updateSite(repository["ssh_url"].(string), repo_name); deploy_error.code != 0 {
deploy_error.SendOverMatrix() deploy_error.SendOverMatrix(); return
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
}
} }
sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"}) sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"})
} }

36
site.go
View File

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