> ## 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 & Git-Integration

> Inspiziere den Deploy-Verlauf, verknüpfe ein Repository über die Square Cloud GitHub App oder nutze den Legacy-Webhook-Flow.

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

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

## Den Deploy-Verlauf auflisten

`api.GetApplicationDeployments(appID)` gibt `[][]squarecloud.Deployment` zurück — **eine innere Slice pro Deploy**. Jedes Event einer Gruppe teilt dieselbe `ID` (den Commit-SHA-1, 40 Hex-Zeichen) und durchläuft die Lebenszykluszustände: `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` ist nur beim Event `"clone"` befüllt und `Files` (`Added`, `Removed`, `Modified`) nur beim Event `"commit"`.
</Note>

## Die aktuelle Konfiguration inspizieren

`api.GetApplicationCurrentDeployment(appID)` gibt einen `squarecloud.DeploymentCurrent` mit der aktuell konfigurierten GitHub-App-Verknüpfung zurück.

```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)
}
```

## Verknüpfen über die Square Cloud GitHub App (empfohlen)

`api.LinkApplicationGithubApp(appID, repoName, branch)` verknüpft ein GitHub-Repository über die offizielle GitHub App mit der Anwendung.

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

Um die Verknüpfung zu entfernen:

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

<Warning>
  `LinkApplicationGithubApp` und `UnlinkApplicationGithubApp` erfordern ein **Session-Token (JWT)** als Zugangsdaten — einfache API-Schlüssel werden von diesen Endpoints nicht akzeptiert. Übergib es mit `rest.WithToken(sessionToken)` oder baue den Client damit.
</Warning>

## Verknüpfen über den Legacy-Webhook-Flow

`api.PostApplicationDeployWebhook(appID, accessToken)` registriert ein GitHub Personal Access Token (`ghp_*` oder `github_pat_*`) und gibt die Webhook-URL zurück, die auf GitHub zu konfigurieren ist.

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

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

<Note>
  Übergib das Token `"@"`, um den konfigurierten Webhook zu entfernen.
</Note>

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