# Git Sync

> Git repository connections, package browsing, preview, apply, link and export.

Source: https://docs.sommatic.ai/en/api/reference/git-sync/

## Create a git repository connection.

`POST /platform-infrastructure/git-repository/`

Authentication: Bearer token

### Request body

- `organization_id` (string, required): Organization that owns this connection.
- `name` (string, required): Human-readable name shown in the UI (e.g. "Acme · Support solutions").
- `description` (string): Optional description of what the repository contains.
- `provider` (PlatformInfrastructureGitRepositoryProvider, required): Git provider object { id, name, title } (github, gitlab, bitbucket, azure-devops).
  - `id` (integer)
  - `name` (string):  Allowed values: `github`, `gitlab`, `bitbucket`, `azure-devops`.
  - `title` (string)
- `base_url` (string): API base URL for self-hosted or enterprise servers (e.g. https://github.example.com/api/v3). Empty means the provider's public API (https://api.github.com for GitHub).
- `repository` (string, required): Repository full name (owner/name).
- `branch` (string, required): Branch this installation reads (e.g. development for dev, main for prod).
- `credential_id` (string, required): Platform credential (api-key) holding the read token. Resolved only inside the backend.
- `environment` (string, required): Name of the environments/<environment>.json file used to resolve tokens (e.g. dev, prod).
- `root_path` (string): Optional folder inside the repository that contains the packages. Empty means the repository root.
- `tags` (array<string>): Optional free-form tags.

### Responses

- `200`: OK.

### Example request

```bash
curl -X POST "https://api.sommatic.ai/platform-infrastructure/git-repository/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "organization_id": "<organization_id>",
       "name": "<name>",
       "provider": {},
       "repository": "<repository>",
       "branch": "<branch>",
       "credential_id": "<credential_id>",
       "environment": "<environment>"
     }'
```

## Update a git repository connection.

`PATCH /platform-infrastructure/git-repository/`

Authentication: Bearer token

### Request body

- `organization_id` (string, required): Organization that owns this connection.
- `name` (string, required): Human-readable name shown in the UI (e.g. "Acme · Support solutions").
- `description` (string): Optional description of what the repository contains.
- `provider` (PlatformInfrastructureGitRepositoryProvider, required): Git provider object { id, name, title } (github, gitlab, bitbucket, azure-devops).
  - `id` (integer)
  - `name` (string):  Allowed values: `github`, `gitlab`, `bitbucket`, `azure-devops`.
  - `title` (string)
- `base_url` (string): API base URL for self-hosted or enterprise servers (e.g. https://github.example.com/api/v3). Empty means the provider's public API (https://api.github.com for GitHub).
- `repository` (string, required): Repository full name (owner/name).
- `branch` (string, required): Branch this installation reads (e.g. development for dev, main for prod).
- `credential_id` (string, required): Platform credential (api-key) holding the read token. Resolved only inside the backend.
- `environment` (string, required): Name of the environments/<environment>.json file used to resolve tokens (e.g. dev, prod).
- `root_path` (string): Optional folder inside the repository that contains the packages. Empty means the repository root.
- `tags` (array<string>): Optional free-form tags.

### Responses

- `200`: OK.

### Example request

```bash
curl -X PATCH "https://api.sommatic.ai/platform-infrastructure/git-repository/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "organization_id": "<organization_id>",
       "name": "<name>",
       "provider": {},
       "repository": "<repository>",
       "branch": "<branch>",
       "credential_id": "<credential_id>",
       "environment": "<environment>"
     }'
```

## Soft-delete a git repository connection.

`DELETE /platform-infrastructure/git-repository/`

Authentication: Bearer token

### Request body

- `id` (string)

### Responses

- `200`: OK.

### Example request

```bash
curl -X DELETE "https://api.sommatic.ai/platform-infrastructure/git-repository/" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "id": "<id>"
     }'
```

## Read the sommatic.json manifest of a package and list its resources.

`GET /platform-infrastructure/git-repository/{id}/package`

Authentication: Bearer token

### Parameters

- `id` (path, string, required)
- `path` (query, string, required): Package folder (e.g. juridica).
- `ref` (query, string)

### Responses

- `200`: { head, path, manifest, resources: [{ kind, key, path }] }

### Example request

```bash
curl -X GET "https://api.sommatic.ai/platform-infrastructure/git-repository/<id>/package?path=<path>" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```

## Test the connection (credential, branch, environment file) and list its packages.

`GET /platform-infrastructure/git-repository/{id}/test`

Authentication: Bearer token

### Parameters

- `id` (path, string, required)
- `ref` (query, string): Optional branch, tag or commit. Defaults to the configured branch.

### Responses

- `200`: { repository, branch, environment, head: { sha, message, date }, packages: [path] }

### Example request

```bash
curl -X GET "https://api.sommatic.ai/platform-infrastructure/git-repository/<id>/test" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```

## List the folders and files under a path of the repository, flagging Sommatic AI packages.

`GET /platform-infrastructure/git-repository/{id}/tree`

Authentication: Bearer token

### Parameters

- `id` (path, string, required)
- `path` (query, string): Folder relative to the connection root. Empty for the root.
- `ref` (query, string)

### Responses

- `200`: { head, path, is_package, children: [{ name, path, type, is_package }], packages }

### Example request

```bash
curl -X GET "https://api.sommatic.ai/platform-infrastructure/git-repository/<id>/tree" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```

## Get git repository connections by query selector.

`GET /platform-infrastructure/git-repository/{queryselector}`

Authentication: Bearer token

### Parameters

- `queryselector` (path, enum, required):  Allowed values: `id`, `organization-id`, `all`, `catalogs`, `status`.
- `search` (query, string): Id (queryselector id) or organization id (queryselector organization-id).

### Responses

- `200`: OK.

### Example request

```bash
curl -X GET "https://api.sommatic.ai/platform-infrastructure/git-repository/<queryselector>" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN"
```

## Apply a reviewed commit to Sommatic AI and optionally publish the version.

`POST /platform-infrastructure/git-sync/apply`

Writes the resolved content of `expected_commit` into the workflow draft / knowledge resource content and records git_binding. With `publish: true` it also creates the version (workflow) or snapshot (knowledge resource). Unchanged resources are skipped, and published only when their deployed version does not come from this commit yet. Only the head of the connection branch can be applied. In installations whose webhooks run in test mode the workflow draft is live: applying is deploying.

Authentication: Bearer token

### Request body

- `repository_id` (string, required)
- `expected_commit` (string, required): Commit returned by preview.
- `resource_id` (string)
- `key` (string)
- `path` (string)
- `keys` (array<string>): With path, apply only these keys of the package.
- `publish` (boolean)
- `description` (string): Note stored in the published version.

### Responses

- `200`: { commit, published, applied, unchanged, failed, items: [{ key, result, reason?, version? }] }

### Example request

```bash
curl -X POST "https://api.sommatic.ai/platform-infrastructure/git-sync/apply" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "repository_id": "<repository_id>",
       "expected_commit": "<expected_commit>"
     }'
```

## Convert a Sommatic AI resource into a repository file (template for a new or updated resource).

`POST /platform-infrastructure/git-sync/export`

Returns the file text in the repository format (header `sommatic`, stable key order), with environment values replaced by @@env.X@@, declared resource ids by @@ref.key@@ and detected secrets by @@secret.REPLACE_ME@@, plus the manifest entry and the environment entry to add. Writes nothing.

Authentication: Bearer token

### Request body

- `repository_id` (string, required)
- `resource_id` (string, required)
- `kind` (string):  Allowed values: `workflows`, `knowledge`.
- `key` (string): Logical key; defaults to the declared key or <package>.<slug>.
- `package_path` (string): Package folder where the file will live.

### Responses

- `200`: { key, file_path, manifest_entry, environment_entry, text, tokens, warnings }

### Example request

```bash
curl -X POST "https://api.sommatic.ai/platform-infrastructure/git-sync/export" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "repository_id": "<repository_id>",
       "resource_id": "<resource_id>"
     }'
```

## Unlink or relink a resource from Git in this installation.

`POST /platform-infrastructure/git-sync/link`

The link is defined in the repository (environments/<env>.json → resources). linked = false stops syncing the resource here (preview marks it, apply skips it) without touching the repository; linked = true resumes it.

Authentication: Bearer token

### Request body

- `resource_id` (string, required)
- `kind` (string):  Allowed values: `workflows`, `knowledge`.
- `linked` (boolean, required)

### Responses

- `200`: { resource_id, kind, git_binding }

### Example request

```bash
curl -X POST "https://api.sommatic.ai/platform-infrastructure/git-sync/link" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "resource_id": "<resource_id>",
       "linked": false
     }'
```

## Preview what a git sync would change. Writes nothing.

`POST /platform-infrastructure/git-sync/preview`

Resolves the selected resources at the connection branch (or `ref`) with the environment of the connection and compares them with Sommatic AI. Select one resource with `resource_id` or `key`, or a whole package with `path`. Returns the commit to pass as `expected_commit` to apply.

Authentication: Bearer token

### Request body

- `repository_id` (string, required)
- `resource_id` (string): Physical id of a workflow or knowledge resource.
- `key` (string): Logical key (e.g. support.triage).
- `path` (string): Package folder; previews every resource of the package.
- `ref` (string): Optional branch, tag or commit.
- `include_content` (boolean): Include the resolved content of each resource (default true).

### Responses

- `200`: { repository, branch, environment, commit, commit_message, items: [{ key, kind, path, resource_id,
  exists, unresolved, changes: { has_changes, nodes, edges, fields, ... }, content }] }

### Example request

```bash
curl -X POST "https://api.sommatic.ai/platform-infrastructure/git-sync/preview" \
  -H "Authorization: Bearer $SOMMATIC_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
       "repository_id": "<repository_id>"
     }'
```
