client := rest.NewClient(os.Getenv("SQUARECLOUD_API_KEY"))
defer client.Close()
api := rest.New(client)
Listando o histórico de deploys
api.GetApplicationDeployments(appID) retorna [][]squarecloud.Deployment — um slice interno por deploy. Todos os eventos de um grupo compartilham o mesmo ID (o SHA-1 do commit, 40 caracteres hex) e percorrem os estados do ciclo de vida: pending → clone → commit → restarting → success | error.
history, err := api.GetApplicationDeployments("abc123def456abc123def456")
if err != nil {
panic(err)
}
for _, timeline := range history {
last := timeline[len(timeline)-1]
fmt.Printf("Deploy %s terminou como %s\n", last.ID, last.State)
for _, event := range timeline {
if event.State == "clone" {
fmt.Println(" branch clonada", event.Branch)
}
if event.State == "commit" {
fmt.Println(" arquivos", event.Files.Added, event.Files.Removed, event.Files.Modified)
}
}
}
Branch só é preenchido no evento "clone", e Files (Added, Removed, Modified) apenas no evento "commit".
Inspecionando a configuração atual
api.GetApplicationCurrentDeployment(appID) retorna um squarecloud.DeploymentCurrent com o vínculo do GitHub App configurado no momento.
current, err := api.GetApplicationCurrentDeployment("abc123def456abc123def456")
if err != nil {
panic(err)
}
if current.App == nil {
fmt.Println("nenhum GitHub App vinculado")
} else {
fmt.Println("vinculado a", current.App.FullName)
}
Vinculando pelo GitHub App da Square Cloud (recomendado)
api.LinkApplicationGithubApp(appID, repoName, branch) vincula um repositório GitHub à aplicação por meio do GitHub App oficial.
err := api.LinkApplicationGithubApp("abc123def456abc123def456", "octocat/hello-world", "main")
if err != nil {
panic(err)
}
Para remover o vínculo:
err := api.UnlinkApplicationGithubApp("abc123def456abc123def456")
LinkApplicationGithubApp e UnlinkApplicationGithubApp exigem um token de sessão (JWT) como credencial — chaves de API comuns não são aceitas por esses endpoints. Passe-o com rest.WithToken(sessionToken) ou construa o cliente com ele.
Vinculando pelo fluxo legado de webhook
api.PostApplicationDeployWebhook(appID, accessToken) registra um GitHub Personal Access Token (ghp_* ou github_pat_*) e retorna a URL do webhook a configurar no GitHub.
webhookURL, err := api.PostApplicationDeployWebhook("abc123def456abc123def456", "ghp_xxx...")
if err != nil {
panic(err)
}
fmt.Println("Configure o GitHub para fazer POST em:", webhookURL)
Passe o token "@" para remover o webhook configurado.
_, err := api.PostApplicationDeployWebhook("abc123def456abc123def456", "@")