This repository has been archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds back the examples we previously had, adopted to the new campaign specs. Where and how the examples are referenced follows what was proposed in https://github.com/sourcegraph/sourcegraph/pull/10921 but temporarily removed.
- Loading branch information
Showing
4 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Example Campaigns | ||
|
||
The following is a list of examples that show how to use [Sourcegraph campaigns](../index.md) to make useful, real-world changes: | ||
|
||
- [Refactoring Go code using Comby](refactor_go_comby.md) | ||
- [Updating Go import statements using Comby](updating_go_import_statements.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Refactor Go code using Comby | ||
|
||
This campaign rewrites Go statements from | ||
|
||
```go | ||
fmt.Sprintf("%d", number) | ||
``` | ||
|
||
to | ||
|
||
```go | ||
strconv.Itoa(number) | ||
``` | ||
|
||
since they are equivalent. | ||
|
||
Since the replacements could change the formatting of the code, it also runs `gofmt` over the repository. | ||
|
||
## Campaign spec | ||
|
||
```yaml | ||
name: sprintf-to-itoa | ||
description: Run `comby` to replace `fmt.Sprintf("%d", integer)` calls with `strconv.Iota` | ||
|
||
# Find all repositories that contain the `fmt.Sprintf` statement using structural search | ||
on: | ||
- repositoriesMatchingQuery: lang:go fmt.Sprintf("%d", :[v]) patterntype:structural | ||
|
||
steps: | ||
- run: comby -in-place 'fmt.Sprintf("%d", :[v])' 'strconv.Itoa(:[v])' .go -matcher .go -exclude-dir .,vendor | ||
container: comby/comby | ||
- run: gofmt -w ./ | ||
container: golang:1.15-alpine | ||
|
||
# Describe the changeset (e.g., GitHub pull request) you want for each repository. | ||
changesetTemplate: | ||
title: Replace equivalent fmt.Sprintf calls with strconv.Itoa | ||
body: This campaign replaces `fmt.Sprintf("%d", integer)` calls with semantically equivalent `strconv.Itoa` calls | ||
branch: campaigns/sprintf-to-itoa # Push the commit to this branch. | ||
commit: | ||
message: Replacing fmt.Sprintf with strconv.Iota | ||
published: false | ||
``` | ||
## Instructions | ||
1. Save the campaign spec above as `YOUR_CAMPAIGN_SPEC.campaign.yaml`. | ||
1. Create a campaign from the campaign spec by running the following [Sourcegraph CLI (`src`)](https://github.com/sourcegraph/src-cli) command: | ||
|
||
<pre><code>src campaign preview -f <em>YOUR_CAMPAIGN_SPEC.campaign.yaml</em></code></pre> | ||
|
||
1. Open the preview URL that the command printed out. | ||
1. Examine the preview. Confirm that the changesets are the ones you intended to track. If not, edit the campaign spec and then rerun the command above. | ||
1. Click the **Create campaign** button. |
60 changes: 60 additions & 0 deletions
60
doc/user/campaigns/examples/updating_go_import_statements.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Updating Go import statements using Comby | ||
|
||
This campaign rewrites Go import paths for the `log15` package from `gopkg.in/inconshreveable/log15.v2` to `github.com/inconshreveable/log15` using [Comby](https://comby.dev/). | ||
|
||
It can handle single-package import statements like these | ||
|
||
```go | ||
import "gopkg.in/inconshreveable/log15.v2" | ||
``` | ||
|
||
and multi-package import statements like these: | ||
|
||
```go | ||
import ( | ||
"io" | ||
|
||
"github.com/pkg/errors" | ||
"gopkg.in/inconshreveable/log15.v2" | ||
) | ||
``` | ||
|
||
## Campaign spec | ||
|
||
```yaml | ||
name: update-log15-import | ||
description: This campaign updates Go import paths for the `log15` package from `gopkg.in/inconshreveable/log15.v2` to `github.com/inconshreveable/log15` using [Comby](https://comby.dev/) | ||
|
||
# Find all repositories that contain the import we want to change. | ||
on: | ||
- repositoriesMatchingQuery: lang:go gopkg.in/inconshreveable/log15.v2 | ||
|
||
# In each repository | ||
steps: | ||
# we first replace the import when it's part of a multi-package import statement | ||
- run: comby -in-place 'import (:[before]"gopkg.in/inconshreveable/log15.v2":[after])' 'import (:[before]"github.com/inconshreveable/log15":[after])' .go -matcher .go -exclude-dir .,vendor | ||
container: comby/comby | ||
# ... and when it's a single import line. | ||
- run: comby -in-place 'import "gopkg.in/inconshreveable/log15.v2"' 'import "github.com/inconshreveable/log15"' .go -matcher .go -exclude-dir .,vendor | ||
container: comby/comby | ||
|
||
# Describe the changeset (e.g., GitHub pull request) you want for each repository. | ||
changesetTemplate: | ||
title: Update import path for log15 package to use GitHub | ||
body: Updates Go import paths for the `log15` package from `gopkg.in/inconshreveable/log15.v2` to `github.com/inconshreveable/log15` using [Comby](https://comby.dev/) | ||
branch: campaigns/update-log15-import # Push the commit to this branch. | ||
commit: | ||
message: Fix import path for log15 package | ||
published: false | ||
``` | ||
## Instructions | ||
1. Save the campaign spec above as `YOUR_CAMPAIGN_SPEC.campaign.yaml`. | ||
1. Create a campaign from the campaign spec by running the following [Sourcegraph CLI (`src`)](https://github.com/sourcegraph/src-cli) command: | ||
|
||
<pre><code>src campaign preview -f <em>YOUR_CAMPAIGN_SPEC.campaign.yaml</em></code></pre> | ||
|
||
1. Open the preview URL that the command printed out. | ||
1. Examine the preview. Confirm that the changesets are the ones you intended to track. If not, edit the campaign spec and then rerun the command above. | ||
1. Click the **Create campaign** button. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters