Configuring credentials

Interacting with a code host (such as creating, updating, or syncing changesets) is made possible by configuring an access token for that code host. Sourcegraph uses these tokens to manage changesets on your behalf, and with your specific permissions.

Requirements

  • Sourcegraph instance with repositories in it. See the “Quickstart” guide on how to setup a Sourcegraph instance.
  • Account on the code host with access to the repositories you wish to target with your batch changes.

Types of access tokens used by Batch Changes

There are three types of access token that can be configured for use with Batch Changes:

  1. Personal access token - A token set by an individual Batch Changes user for their personal code host user account.
  2. Global service account token (Admins only) - A token that can be used by any Batch Changes user who does not have a personal access token configured.
  3. Sourcegraph code host connection token (Admins only) - A token that is typically set when connecting Sourcegraph to a code host for the first time and is used across the entire Sourcegraph instance. Using the code host connection token with Batch Changes will be deprecated in the future.

Different tokens are used for different types of operations, as is illustrated in the hierarchy table below.

🟢 Preferred - Sourcegraph will prefer to use this token for this operation, if it is configured.

🟡 Fallback - Sourcegraph will fall back to use this token for this operation, if it is configured.

🔴 Unsupported - Sourcegraph cannot use this token for this operation.

Operation Personal Access Token Global Service Account Token Code Host Connection Token
Pushing a branch with the changes 🟢 🟡 🔴
Publishing a changeset 🟢 🟡 🔴
Updating a changeset 🟢 🟡 🔴
Closing a changeset 🟢 🟡 🔴
Importing a changeset 🔴 🟢 🟡
Syncing a changeset 🔴 🟢 🟡

When writing a commit or a changeset to the code host, the author will reflect the token used (e.g., on GitHub, the commit or pull request author will be you). It is for this reason that a personal access token is preferred for most operations.

Personal access tokens

Do I need to add a personal access token?

Personal access tokens are not required if a global access token has also been configured, but users should add one if they want Sourcegraph to create commits and changesets under their name.

Adding a token

Adding a personal access token is done through the Batch Changes section of your user settings:

  1. From any Sourcegraph page, click on your avatar at the top right of the page.
  2. Select Settings from the dropdown menu.
  3. Click Batch Changes on the sidebar menu.
  4. Click Add credentials and follow the steps to create a new token for the code host

Code hosts with tokens configured are indicated by a green tick to the left of the code host name, while code hosts without tokens have an empty red circle next to them.

Removing a token

To remove a token, navigate back to the same section of your user settings, then click Remove:

The code host’s indicator should revert to the empty red circle once the token is removed.

Global service account tokens

Do I need to add a global service account token?

Global credentials are usable by all users of the Sourcegraph instance who have not added their own personal access tokens for Batch Changes. This makes them a handy fallback, but not strictly required if users are adding their own tokens.

Sourcegraph also uses the global service account to track existing changesets and keep changesets up to date. If no global service account token is set, we can currently fall back to the token configured for the code host connection. However, this fallback will be deprecated in the future, so for this reason we highly recommend setting up a global service account.

Adding a token

Adding a global service account token is done through the Batch Changes section of the site admin area:

  1. From any Sourcegraph page, click on your avatar at the top right of the page.
  2. Select Site admin from the dropdown menu.
  3. Click Batch Changes on the sidebar menu.
  4. Click Add credentials and follow the steps to create a new token for the code host

Code hosts with tokens configured are indicated by a green tick to the left of the code host name, while code hosts without tokens have an empty red circle next to them.

Removing a token

To remove a token, navigate back to the same section of the site admin area, then click Remove. The code host’s indicator should revert to the empty red circle once the token is removed.

Code host connection tokens

Code host connection tokens are typically configured the first time a site administrator is connecting Sourcegraph to the external code host. Within Batch Changes, they are only used for syncing and importing changesets when a global service account token is not configured.

Creating a code host token

To finish configuring the new credentials, you will need to create a new personal access token on your code host and paste it into the input field on the Add credentials modal:

An input dialog, titled "Batch Changes credentials: GitHub", with an input box to type or paste a token and a list of scopes that must be enabled for this type of code host token

GitHub

Follow the steps to create a personal access token on GitHub. Batch Changes requires the following scopes:

  • repo
  • read:org
  • user:email
  • read:discussion

This is done by selecting the relevant checkboxes when creating the token:

The GitHub token creation page, with the repo scope selected

GitLab

Follow the steps to create a personal access token on GitLab. Batch Changes requires the following scopes:

  • api
  • read_repository
  • write_repository

This is done by selecting the relevant checkboxes when creating the token:

The GitLab token creation page, with the api, read_repository, and write_repository scopes selected

Bitbucket Server

Follow the steps to create a personal access token on Bitbucket.

Batch Changes requires the access token to have the write permission on both projects and repositories. This is done by selecting the Write level in the Projects dropdown, and letting it be inherited by repositories:

The Bitbucket Server token creation page, with Write permissions selected on both the Project and Repository dropdowns

SSH access to code host

When Sourcegraph is configured to clone repositories using SSH via the gitURLType setting, an SSH keypair will be generated for you and the public key needs to be added to the code host to allow push access. In the process of adding your personal access token you will be given that public key. You can also come back later and copy it to paste it in your code hosts SSH access settings page.

Credentials setup process, showing the SSH public key to be copied