A campaign can be created from a set of patches, one per repository. For each patch, a changeset (what code hosts call pull request or merge request) will be created on the code host on which the repository is hosted.
Here is the short version for how to create a patch set and turn that into changesets by creating a campaign:
action.json
).src actions scope-query -f action.json
src actions exec -f action.json -create-patchset
Read on for more detailed steps and documentation and see "Actions" for more information about how to define and execute actions.
If you have not done so already, first install, set up and configure the src
CLI to point to your Sourcegraph instance.
The first thing we need is a definition of an "action". An action contains a list of steps to run in each repository returned by the results of the scopeQuery
search string. There are two types of steps: docker
and command
. See "Actions" for more information.
Here is an example of a multi-step action definition using the docker
and command
types:
{ "scopeQuery": "repo:go-* -repohasfile:INSTALL.md", "steps": [ { "type": "command", "args": ["sh", "-c", "echo '# Installation' > INSTALL.md"] }, { "type": "command", "args": ["sed", "-i", "", "s/No install instructions/See INSTALL.md/", "README.md"] }, { "type": "docker", "image": "golang:1.13-alpine", "args": ["go", "fix", "/work/..."] } ] }
Save that definition in a file called action.json
(or any other name of your choosing).
This action will be executed for each repository that has go-
in its name and doesn't have an INSTALL.md
file.
The first step (a command
step) creates an INSTALL.md
file in the root directory of each repository by running sh
in a temporary copy of each repository. This is executed on the machine on which src
is being run.
The second step, again a "command"
step, runs the sed
command to replace text in the README.md
file in the root of each repository (the -i ''
argument is only necessary for BSD versions of sed
that usually come with macOS).
The third step starts a Docker container based on the golang:1.13-alpine
image and runs go fix /work/...
in it.
As you can see from these examples, the "output" of an action is the modified, local copy of a repository.
With our action file defined, we can now execute it:
src actions exec -f action.json
This command is going to:
"scopeQuery"
and extract it to a local temporary directory in /tmp
.(See "Actions" for more information about how actions are executed.)
The output, a set of patches, can be saved into a file by redirecting it:
src actions exec -f action.json > patches.json
The next step is to save the set of patches on the Sourcegraph instance so they can be turned into a campaign.
To do that, run:
src campaign patchset create-from-patches < patches.json
Or leverage the cache of src action exec
and re-run the command to pipe the patches directly into src campaign patchset create-from-patches
:
src actions exec -f action.json | src campaign patchset create-from-patches
You can also use src action exec
with the -create-patchset
flag, which is equivalent to the last command:
src actions exec -f action.json -create-patchset
Once completed, the output will contain:
src
SLI to create a campaign from the patch set.If you're happy with the preview of the campaign, it's time to trigger the creation of changesets (pull requests) on the code host(s).
That is done by creating and publishing a campaign with the given patchset.
You can either do that in the Sourcegraph UI or on the CLI:
src campaigns create -name='My campaign name' \ -desc='My first CLI-created campaign' \ -patchset=Q2FtcGFpZ25QbGFuOjg= \ -branch=my-first-campaign
Creating this campaign will asynchronously create a changeset (pull request) for each repository that has a patch in the patch set. You can check the progress of campaign completion by viewing the campaign on your Sourcegraph instance.
The -branch
flag specifies the branch name that will be used for each pull request. If a branch with that name already exists for a repository, a fallback will be generated by appending a counter at the end of the name, e.g.: my-first-campaign-1
.
If you have defined the $EDITOR
environment variable, the configured editor will be used to edit the name and Markdown description of the campaign:
src campaigns create -patchset=Q2FtcGFpZ25QbGFuOjg= -branch=my-first-campaign