> ## 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 integración con Git

> Inspecciona el historial de deploys, vincula un repositorio a través de la GitHub App de Square Cloud o usa el flujo legacy de webhook.

```go theme={null}
client := rest.NewClient(os.Getenv("SQUARECLOUD_API_KEY"))
defer client.Close()

api := rest.New(client)
```

## Listado del historial de deploys

`api.GetApplicationDeployments(appID)` devuelve `[][]squarecloud.Deployment` — **un slice interno por deploy**. Cada evento de un grupo comparte el mismo `ID` (el SHA-1 del commit, 40 caracteres hex) y recorre los estados del 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 ended as %s\n", last.ID, last.State)

	for _, event := range timeline {
		if event.State == "clone" {
			fmt.Println("  cloned branch", event.Branch)
		}
		if event.State == "commit" {
			fmt.Println("  files", event.Files.Added, event.Files.Removed, event.Files.Modified)
		}
	}
}
```

<Note>
  `Branch` solo se rellena en el evento `"clone"`, y `Files` (`Added`, `Removed`, `Modified`) solo en el evento `"commit"`.
</Note>

## Inspección de la configuración actual

`api.GetApplicationCurrentDeployment(appID)` devuelve un `squarecloud.DeploymentCurrent` con la vinculación de la GitHub App configurada actualmente.

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

if current.App == nil {
	fmt.Println("no GitHub App linked")
} else {
	fmt.Println("linked to", current.App.FullName)
}
```

## Vinculación mediante la GitHub App de Square Cloud (recomendado)

`api.LinkApplicationGithubApp(appID, repoName, branch)` vincula un repositorio de GitHub a la aplicación mediante la GitHub App oficial.

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

Para eliminar la vinculación:

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

<Warning>
  `LinkApplicationGithubApp` y `UnlinkApplicationGithubApp` requieren un **token de sesión (JWT)** como credencial — las claves de API normales no son aceptadas por estos endpoints. Pásalo con `rest.WithToken(sessionToken)` o construye el cliente con él.
</Warning>

## Vinculación mediante el flujo legacy de webhook

`api.PostApplicationDeployWebhook(appID, accessToken)` registra un GitHub Personal Access Token (`ghp_*` o `github_pat_*`) y devuelve la URL del webhook a configurar en GitHub.

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

fmt.Println("Configure GitHub to POST to:", webhookURL)
```

<Note>
  Pasa el token `"@"` para eliminar el webhook configurado.
</Note>

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