Fix cloning down new repositories (fix #12)
This commit is contained in:
		
							
								
								
									
										27
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								main.go
									
									
									
									
									
								
							@ -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))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						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!"})
 | 
						sendMessage(MatrixMessage{text: "🟢 Deployed successfully!"})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										32
									
								
								site.go
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								site.go
									
									
									
									
									
								
							@ -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)
 | 
					 | 
				
			||||||
	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 {
 | 
					 | 
				
			||||||
	log.Printf("Pulling down repository %s...", name)
 | 
						log.Printf("Pulling down repository %s...", name)
 | 
				
			||||||
	repo,err := git.Open(configuration.sites_dir+"/"+name)
 | 
						repo,err := git.Open(configuration.sites_dir+"/"+name)
 | 
				
			||||||
	if err != nil {
 | 
						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{}
 | 
						return DeployError{}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user