Creating a Batch Change
Learn in detail about how to create, view, and filter your Batch Changes.
Batch changes are created by writing a batch spec and executing that batch spec with the Sourcegraph CLI src
.
Batch changes can also be used on multiple projects within a monorepo by using the workspaces
key in your batch spec.
There are two ways of creating a Batch Change:
- On your local machine, with the Sourcegraph CLI
- Remotely, with server-side execution
Create a batch change with the Sourcegraph CLI
This part of the guide will walk you through creating a batch change on your local machine with the Sourcegraph CLI.
Requirements
- Sourcegraph instance with repositories in it. See the Quickstart guide on how to set up a Sourcegraph instance
- Installed and configured Sourcegraph CLI. Read about the detailed process in the quickstart guide
- Configure the credentials for the code host(s) on which you'll create changesets. Read more about Configuring user credentials docs on how to add and manage credentials
Writing a batch spec
To create a Batch Change, you need a batch spec describing the change. Here is an example batch spec that describes a batch change to add Hello World to all README
files:
YAMLversion: 2 name: hello-world description: Add Hello World to READMEs # Find all repositories that contain a README file. on: - repositoriesMatchingQuery: file:README # In each repository, run this command. Each repository's resulting diff is captured. steps: - run: IFS=$'\n'; echo Hello World | tee -a $(find -name README) container: alpine:3 # Describe the changeset (e.g., GitHub pull request) you want for each repository. changesetTemplate: title: Hello World body: My first batch change! branch: hello-world # Push the commit to this branch. commit: message: Append Hello World to all README files published: false # Do not publish any changes to the code hosts yet
The commits created from your spec will use the git config
values for user.name
and user.email
from your local environment or batch-changes@sourcegraph.com
if no user is set. Alternatively, you can also specify an author
in this spec.
Creating a batch change after previewing
After writing a batch spec, you use the Sourcegraph CLI (src
) to execute the batch spec and upload it to Sourcegraph, where you can preview the changes and apply the batch spec to create a batch change:
- Run the following command in your terminal:
SHELLsrc batch preview -f YOUR_BATCH_SPEC.yaml
- Before any branches are pushed or changesets (e.g., GitHub pull requests) are created, you will see a preview of all changes and can confirm each one before proceeding.
skip-errors
flag : src batch preview -f spec.batch.yml -skip-errors
.- Wait for it to run and compute the changes for each repository (using the repositories and commands in the batch spec).
- Open the preview URL where the command is printed out.
- Examine the preview. This is the result of executing the batch spec. Confirm that the changes are what you intended. If not, edit the batch spec and then rerun the command above.
- Click the Apply button to create the batch change.
After you've applied a batch spec, you can publish changesets to the code host when you're ready. This will turn the patches into commits, branches, and changesets (such as GitHub pull requests) for others to review and merge.
You can share the link to your batch change with other users if you want their help. Any user on your Sourcegraph instance can view it in the batch changes list.
If a user viewing the batch change lacks read access to a repository in the batch change, they can only see limited information about the changes to that repository (and not the repository name, file paths, or diff).
You can update a batch change's changes at any time, even after you've published changesets. For more information, see Updating a batch change.
Applying a batch spec without a preview
You can use Sourcegraph CLI (src
) to directly apply a batch spec to create or update a batch change without having to use the UI.
Instead of running src batch preview
you run the following:
BASHsrc batch apply -f YOUR_BATCH_SPEC.yaml
This command won't print a link to a preview. It will create or update the batch change it describes directly.
That can be useful if you want to update a single field in the batch spec, i.e., the description
or the changesetTemplate.body
, or if you're going to continuously update a batch change by running src
in a CI workflow.
Creating a batch change in a different namespace
Batch changes are uniquely identified by their name and namespace. The namespace can be any Sourcegraph username or the name of a Sourcegraph organization.
By default, batch changes will use your username on Sourcegraph as your namespace. To create batch changes in a different namespace, use the -namespace
flag when previewing or applying a batch spec:
BASHsrc batch preview -f your_batch_spec.yaml -namespace <SOURCEGRAPH_USERNAME_OR_ORG>
When creating a batch change server-side using the UI, you can select the namespace to which the batch change belongs when you create it.
Administration
Once a batch change is open, any Sourcegraph user can view it. However, the namespace determines who can administer it, such as editing or deleting it. When a batch change is created in a user namespace, only that user (and site admins) can administer it. When a batch change is created in an organization namespace, all members of that organization (and site admins) can administer it.
Create a batch change with server-side execution
Here, you'll learn how to create and run a batch change via server-side execution.
Click the Create batch change button on the Batch Changes page, or go to /batch-changes/create
.
You will be prompted to choose a name for your namespace and optionally define a custom namespace to put your batch change in.
Once done, click Create.
Editing the spec file
You should see the editor view now. This view consists of three main areas:
- The library sidebar panel on the left
- The editor in the middle
- The workspaces preview panel on the right
You can pick from the examples in the library pane to get started quickly or begin working on your batch spec in the editor right away. The editor will provide documentation as you hover over tokens in the YAML spec and supports auto-completion.
Previewing workspaces
Once satisfied or to test your batch change's scope, you can run a new preview from the right-hand side panel at any time. After resolution, it will show all the workspaces in repositories that match the given on
statements. You can search through them and determine if your query is satisfying before starting execution. You can also exclude single workspaces from this list.
Executing your batch spec
When the spec is ready to run, ensure the preview is up to date and then click Run batch spec. This takes you to the execution screen. On this page, you see:
- Run statistics at the top
- All the workspaces, including status and diff stat, in the left panel
- Details on a particular workspace on the right-hand side panel where you can see steps with:
- Logs
- Results
- Command
- Per-step diffs
- Output variables
- Execution timelines for debugging
Once finished, you can proceed to the batch spec preview, as you know it from before.
Previewing and applying the batch spec
On this page, you can review the proposed changes and the operations taken by Sourcegraph on each changeset. Once satisfied, click Apply.
Congratulations, you ran your first batch change server-side 🎊
Viewing batch changes
You can view a list of all batch changes by clicking the Batch Changes icon in the top navigation bar:
Filtering batch changes
You can also use the filters to switch between showing all open or closed batch changes.
Filtering changesets
When looking at a batch change, you can search and filter the list of changesets with the controls at the top of the list: