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

# Administration API: Git repositories

> Read a workspace's git repository, connect it with the deploy key, GitHub App, or GitLab, check access, and rotate the deploy key with the Administration API.

Connect a workspace to the git repository of its dbt™ project from code: read the deploy key, connect the repository through the deploy key or an app, check that the key can push, and rotate the key.

> [!NOTE]
>
> **Prerequisites:**
>
> * These operations are part of the Enterprise plan. On other plans, they fail with `PLAN_UPGRADE_REQUIRED`.
> * An account API key with the `X-Paradime-Workspace` header, or a workspace API key.
> * The **Git repository admin** capability group.
>
> Keys, errors, jobs, and steps that need a person work the same in every area. See [Conventions](https://docs.paradime.io/developers/graphql-api/admin-api/conventions.md).

## Operations

| Operation | Type | What it does | Capability group |
| - | - | - | - |
| [`Workspace.gitRepository`](https://docs.paradime.io/graphql-reference/types/Workspace) | Field | Reads the repository, how Paradime reaches it, and its state. | Git repository admin |
| [`GitRepository.deployKey`](https://docs.paradime.io/graphql-reference/types/GitRepository) | Field | Reads the workspace's deploy key. Makes the first key when the workspace has none. | Git repository admin |
| [`connectGitRepository`](https://docs.paradime.io/graphql-reference/mutations/connectGitRepository) | Mutation | Connects the repository through the deploy key, the Paradime GitHub App, or your GitLab connection. With the deploy key, the clone runs as a job. | Git repository admin |
| [`checkGitRepository`](https://docs.paradime.io/graphql-reference/mutations/checkGitRepository) | Mutation | Checks, as a job, that the deploy key can push to a repository. Changes nothing. | Git repository admin |
| [`rotateGitDeployKey`](https://docs.paradime.io/graphql-reference/mutations/rotateGitDeployKey) | Mutation | Replaces the workspace's deploy key with a new one. | Git repository admin |

> [!TIP]
>
> For new code, use these operations rather than `gitRepository` and `setGitRepository` from the [Workspace Management API](https://docs.paradime.io/developers/graphql-api/api-reference/workspace-management-api). The new ones read the deploy key, clone a deploy-key repository, check access, and report errors with a code. The older operations keep working with the same **Git repository admin** group.

## Read the repository and its deploy key

`gitRepository` shows how the workspace reaches its repository:

* `mode` is `SSH_DEPLOY_KEY`, `GITHUB_APP`, or `GITLAB_APP`.
* `state` says where the connection is:

| `state` | What it means |
| - | - |
| `NOT_CONNECTED` | No repository is connected yet. |
| `PENDING_USER_ACTION` | No repository is connected yet, and a GitHub App install request for the workspace waits for an organization owner on GitHub. |
| `PENDING_VALIDATION` | The repository is saved but not cloned yet. Add the deploy key to it, then connect again with `validate: true`. |
| `CONNECTED` | The repository is cloned and in use. |
| `DEGRADED` | The GitHub App lost access to the repository, or its installation is suspended. |
| `DISCONNECTED` | The GitHub App installation was removed, and the workspace has no deploy key to fall back to. |

`githubAppInstallUrl` is the page where a workspace member installs the Paradime GitHub App, in a browser. It is `null` when your account can't connect through the GitHub App. See [Pending user action](https://docs.paradime.io/developers/graphql-api/admin-api/conventions.md#pending-user-action).

```graphql theme={"system"}
query GitRepository {
  workspace {
    gitRepository {
      mode
      state
      provider
      uri
      dbtProjectDir
      isVerified
      deployKey {
        publicKey
      }
      githubApp {
        repositoryFullName
      }
      gitlabApp {
        projectPath
      }
      githubAppInstallUrl
    }
  }
}
```

> [accordion] Example response
>
> ```json theme={"system"}
> {
>   "data": {
>     "workspace": {
>       "gitRepository": {
>         "mode": "SSH_DEPLOY_KEY",
>         "state": "NOT_CONNECTED",
>         "provider": null,
>         "uri": null,
>         "dbtProjectDir": null,
>         "isVerified": null,
>         "deployKey": { "publicKey": "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleKey" },
>         "githubApp": null,
>         "gitlabApp": null,
>         "githubAppInstallUrl": null
>       }
>     }
>   }
> }
> ```

When the workspace has no deploy key yet, reading `deployKey` makes the first one and adds an audit entry. Add the public key to the repository as a deploy key with write access, on your git provider, before you connect it.

## Connect a repository with the deploy key

Set exactly one field of `connection`: `deployKey`, `githubApp`, or `gitlabApp`. With `deployKey`:

* `provider` is `GITHUB`, `GITLAB`, `BITBUCKET`, or `AZURE_DEVOPS`.
* `uri` must be an SSH git URL, `user@host:path` or `ssh://user@host/path`, for example `git@github.com:acme/analytics.git`.
* `dbtProjectDir` is the folder inside the repository that holds `dbt_project.yml`. Leave it out when the file is at the repository's root.
* `validate` is `true` by default: Paradime clones the repository in a job, checks `dbt_project.yml`, syncs production, and restarts the workspace's editors. With `validate: false`, Paradime only saves the repository as `PENDING_VALIDATION`, for example before someone adds the deploy key to it.
* `force` replaces a cloned repository with another one. Without it, connecting a different repository to a workspace that has a clone is a `CONFLICT`.

```graphql theme={"system"}
mutation ConnectRepository($input: ConnectGitRepositoryInput!) {
  connectGitRepository(input: $input) {
    gitRepository {
      state
      uri
    }
    job {
      id
      status
    }
  }
}
```

```json theme={"system"}
{
  "input": {
    "connection": {
      "deployKey": {
        "provider": "GITHUB",
        "uri": "git@github.com:acme/analytics.git",
        "dbtProjectDir": "dbt",
        "validate": true
      }
    }
  }
}
```

The payload holds the repository as it is now, and the `job` that clones it. Poll the job with [`adminJob`](https://docs.paradime.io/developers/graphql-api/admin-api/conventions#async-jobs) until its `status` is `SUCCEEDED` or `FAILED`. The job has a time limit of 5 minutes, and its `result` is a `GitRepositoryConnectResult`. If the job fails, `failure.code` tells you why:

| `failure.code` | Why |
| - | - |
| `CONNECTION_TEST_FAILED` | Paradime could not clone the repository with the deploy key. Check that the key is on the repository, with write access. |
| `VALIDATION_FAILED` | The `dbtProjectDir` is wrong, or there is no `dbt_project.yml` in it. |
| `CONFLICT` | Another `connectGitRepository` job is cloning into the workspace, or the workspace now has a clone of another repository. |

`job` is `null` when nothing needed a clone: with `validate: false`, or when you send the same input again for a repository that is already cloned. Sending the same input again changes nothing and starts no job.

## Connect a repository through the GitHub App or GitLab

The app connections finish before the mutation returns, so they start no job.

* `githubApp` connects through the Paradime GitHub App. Send the `installationId` of an installation your account holds and the numeric `repositoryId` of the repository. Send neither to move the workspace's current repository to the app. Install the app first: see [Connect with the GitHub App](https://docs.paradime.io/products/settings/git-repositories/connect-with-github-app.md).
* `gitlabApp` connects through your account's GitLab connection. Send the connection's `baseUrl` and the numeric `projectId`, or neither to move the workspace's current repository to the connection.

```graphql theme={"system"}
mutation ConnectThroughGitHubApp($input: ConnectGitRepositoryInput!) {
  connectGitRepository(input: $input) {
    gitRepository {
      mode
      state
      githubApp {
        installationId
        repositoryId
        repositoryFullName
      }
    }
  }
}
```

```json theme={"system"}
{
  "input": {
    "connection": {
      "githubApp": {
        "installationId": "51234567",
        "repositoryId": "712345678"
      }
    }
  }
}
```

To move a workspace from an app back to its deploy key, send `deployKey` with the same `uri` and `dbtProjectDir` that the workspace has now.

## Check that the deploy key can push

`checkGitRepository` checks, in a job, that the workspace's deploy key can push to a repository. It changes nothing, so you can run it before you connect. The job has a time limit of 2 minutes. It fails with `CONNECTION_TEST_FAILED` when the key can't push, and its `result` is a `GitRepositoryCheckResult`.

```graphql theme={"system"}
mutation CheckRepository($input: CheckGitRepositoryInput!) {
  checkGitRepository(input: $input) {
    job {
      id
      kind
      status
    }
  }
}
```

```json theme={"system"}
{ "input": { "uri": "git@github.com:acme/analytics.git" } }
```

## Rotate the deploy key

`rotateGitDeployKey` replaces the workspace's deploy key and returns the new public key. Every call makes a new key.

> [!WARNING]
>
> If the connected repository uses the deploy key, clone and push break until you add the new key to the repository. In that case the call needs `force: true`. Without it, the call fails with `CONFLICT` and changes nothing.

```graphql theme={"system"}
mutation RotateDeployKey($input: RotateGitDeployKeyInput!) {
  rotateGitDeployKey(input: $input) {
    deployKey {
      publicKey
    }
  }
}
```

```json theme={"system"}
{ "input": { "force": true } }
```

## Errors

These are the cases specific to git repositories. For the error format and the codes every area shares, see [Errors](https://docs.paradime.io/developers/graphql-api/admin-api/conventions.md#errors).

| Code | When |
| - | - |
| `CONFLICT` | The workspace has no deploy key yet: read `gitRepository { deployKey { publicKey } }` first. The workspace has a clone of another repository and `force` is not `true`. The workspace connects through an app, and the `deployKey` input names another repository. The connected repository uses the deploy key, on `rotateGitDeployKey` without `force: true`. |
| `PLAN_UPGRADE_REQUIRED` | Your account can't connect repositories through the GitHub App. |
| `VALIDATION_FAILED` | `connection` sets no field, or more than one. The `uri` is not an SSH git URL. The `dbtProjectDir` is not a folder inside the repository. `validate: false` for a workspace that has a clone. Only one of `installationId` and `repositoryId`, or of `baseUrl` and `projectId`, is set, or an id is not numeric. The app can't reach the repository. |

The jobs report their own failures in `failure.code`, as described in [Connect a repository with the deploy key](#connect-a-repository-with-the-deploy-key).

## Related

* [Python SDK: Git](https://docs.paradime.io/developers/python-sdk/modules/admin/git.md): the same operations in `paradime.admin.git`.
* [Paradime CLI: `paradime admin`](https://docs.paradime.io/developers/paradime-cli/admin-cli): `paradime admin git` shows the repository, connects it, checks it, and rotates the key.
* [Git repositories](https://docs.paradime.io/products/settings/git-repositories/index.md): the same settings in the app.
* [Connect with the GitHub App](https://docs.paradime.io/products/settings/git-repositories/connect-with-github-app.md): install the Paradime GitHub App.
* [Administration API: Setup status](https://docs.paradime.io/developers/graphql-api/admin-api/setup-status.md): the `GIT_REPOSITORY` and `GITHUB_APP` steps.


## Related topics

- [Administration API: Setup status](https://docs.paradime.io/developers/graphql-api/admin-api/setup-status.md)
- [Administration API](https://docs.paradime.io/developers/graphql-api/admin-api/index.md)
- [Administration API: Conventions](https://docs.paradime.io/developers/graphql-api/admin-api/conventions.md)
- [Administration](https://docs.paradime.io/products/settings/index.md)
- [Administration API: Workspaces](https://docs.paradime.io/developers/graphql-api/admin-api/workspaces.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.