> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploys e integração Git

> Inspecione o histórico de deploys, vincule um repositório pelo GitHub App da Square Cloud ou use o fluxo legado de webhook.

```go theme={null}
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`.

```go theme={null}
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)
		}
	}
}
```

<Note>
  `Branch` só é preenchido no evento `"clone"`, e `Files` (`Added`, `Removed`, `Modified`) apenas no evento `"commit"`.
</Note>

## Inspecionando a configuração atual

`api.GetApplicationCurrentDeployment(appID)` retorna um `squarecloud.DeploymentCurrent` com o vínculo do GitHub App configurado no momento.

```go theme={null}
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.

```go theme={null}
err := api.LinkApplicationGithubApp("abc123def456abc123def456", "octocat/hello-world", "main")
if err != nil {
	panic(err)
}
```

Para remover o vínculo:

```go theme={null}
err := api.UnlinkApplicationGithubApp("abc123def456abc123def456")
```

<Warning>
  `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.
</Warning>

## 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.

```go theme={null}
webhookURL, err := api.PostApplicationDeployWebhook("abc123def456abc123def456", "ghp_xxx...")
if err != nil {
	panic(err)
}

fmt.Println("Configure o GitHub para fazer POST em:", webhookURL)
```

<Note>
  Passe o token `"@"` para remover o webhook configurado.
</Note>

```go theme={null}
_, err := api.PostApplicationDeployWebhook("abc123def456abc123def456", "@")
```
