mirror of
https://github.com/sourcegraph/sourcegraph.git
synced 2026-02-06 16:51:55 +00:00
automation: make regex search replace the default campaign (#7844)
This commit is contained in:
parent
e0406ee92c
commit
11f2d387df
@ -267,7 +267,7 @@ func (c *comby) generateDiff(ctx context.Context, repo api.RepoName, commit api.
|
||||
|
||||
type regexSearchReplaceArgs struct {
|
||||
ScopeQuery string `json:"scopeQuery"`
|
||||
RegexMatch string `json:"regexMatch"`
|
||||
RegexpMatch string `json:"regexpMatch"`
|
||||
TextReplace string `json:"textReplace"`
|
||||
}
|
||||
|
||||
@ -281,16 +281,16 @@ func (c *regexSearchReplace) searchQuery() string {
|
||||
// We add the regexMatch because without it search may only return a
|
||||
// truncated list of file matches. We also add count:10000 to have a
|
||||
// higher chance of finding all matches.
|
||||
return fmt.Sprintf("%s %s count:10000", c.args.ScopeQuery, c.args.RegexMatch)
|
||||
return fmt.Sprintf("%s %s count:10000", c.args.ScopeQuery, c.args.RegexpMatch)
|
||||
}
|
||||
|
||||
func (c *regexSearchReplace) searchQueryForRepo(n api.RepoName) string {
|
||||
return fmt.Sprintf("repo:%s %s %s count:10000", regexp.QuoteMeta(string(n)), c.args.ScopeQuery, c.args.RegexMatch)
|
||||
return fmt.Sprintf("repo:%s %s %s count:10000", regexp.QuoteMeta(string(n)), c.args.ScopeQuery, c.args.RegexpMatch)
|
||||
}
|
||||
|
||||
func (c *regexSearchReplace) generateDiff(ctx context.Context, repo api.RepoName, commit api.CommitID) (string, string, error) {
|
||||
// check that the regex is valid before doing any work.
|
||||
re, err := regexp.Compile(c.args.RegexMatch)
|
||||
re, err := regexp.Compile(c.args.RegexpMatch)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
@ -259,7 +259,7 @@ func TestCampaignType_RegexSearchAndReplace(t *testing.T) {
|
||||
name: "simple search replace",
|
||||
args: regexSearchReplaceArgs{
|
||||
ScopeQuery: "repo:github",
|
||||
RegexMatch: "foo",
|
||||
RegexpMatch: "foo",
|
||||
TextReplace: "bar",
|
||||
},
|
||||
searchResultsContents: map[string]string{
|
||||
@ -278,7 +278,7 @@ func TestCampaignType_RegexSearchAndReplace(t *testing.T) {
|
||||
name: "search replace with match groups",
|
||||
args: regexSearchReplaceArgs{
|
||||
ScopeQuery: "repo:github",
|
||||
RegexMatch: "(foo)",
|
||||
RegexpMatch: "(foo)",
|
||||
TextReplace: "$1${1}bar",
|
||||
},
|
||||
searchResultsContents: map[string]string{
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
{
|
||||
"$id": "regex-search-replace-spec.json#",
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Schema for regex search replace",
|
||||
"description": "Schema for regexp search replace",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"scopeQuery": {
|
||||
@ -9,16 +9,16 @@
|
||||
"minLength": 1,
|
||||
"description": "Define a scope to narrow down repositories affected by this change. Only GitHub and Bitbucket Server are supported."
|
||||
},
|
||||
"regexMatch": {
|
||||
"regexpMatch": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"description": "Match this regular expression. RE2 syntax is supported: https://github.com/google/re2/wiki/Syntax"
|
||||
},
|
||||
"textReplace": {
|
||||
"type": "string",
|
||||
"description": "Replace the regexMatch text with this text. You may refer to match groups in regexMatch using $id or ${id} syntax."
|
||||
"description": "Replace the regexpMatch text with this text. You may refer to match groups in regexpMatch using $id or ${id} syntax."
|
||||
}
|
||||
},
|
||||
"required": ["scopeQuery", "regexMatch", "textReplace"],
|
||||
"required": ["scopeQuery", "regexpMatch", "textReplace"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ package schema
|
||||
const RegexSearchReplaceCampaignTypeSchemaJSON = `{
|
||||
"$id": "regex-search-replace-spec.json#",
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"description": "Schema for regex search replace",
|
||||
"description": "Schema for regexp search replace",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"scopeQuery": {
|
||||
@ -14,17 +14,17 @@ const RegexSearchReplaceCampaignTypeSchemaJSON = `{
|
||||
"minLength": 1,
|
||||
"description": "Define a scope to narrow down repositories affected by this change. Only GitHub and Bitbucket Server are supported."
|
||||
},
|
||||
"regexMatch": {
|
||||
"regexpMatch": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"description": "Match this regular expression. RE2 syntax is supported: https://github.com/google/re2/wiki/Syntax"
|
||||
},
|
||||
"textReplace": {
|
||||
"type": "string",
|
||||
"description": "Replace the regexMatch text with this text. You may refer to match groups in regexMatch using $id or ${id} syntax."
|
||||
"description": "Replace the regexpMatch text with this text. You may refer to match groups in regexpMatch using $id or ${id} syntax."
|
||||
}
|
||||
},
|
||||
"required": ["scopeQuery", "regexMatch", "textReplace"],
|
||||
"required": ["scopeQuery", "regexpMatch", "textReplace"],
|
||||
"additionalProperties": false
|
||||
}
|
||||
`
|
||||
|
||||
@ -1578,7 +1578,7 @@ describe('e2e test suite', () => {
|
||||
test('Create campaign preview for regexp campaign type', async () => {
|
||||
await createCampaignPreview({
|
||||
specification: JSON.stringify({
|
||||
regexMatch: 'this is file ([0-9]+)',
|
||||
regexpMatch: 'this is file ([0-9]+)',
|
||||
textReplace: 'file $1 this is',
|
||||
scopeQuery: 'repo:github.com/sourcegraph-testing/automation-e2e-test',
|
||||
}),
|
||||
|
||||
@ -22,7 +22,7 @@ describe('CampaignPlanSpecificationFields', () => {
|
||||
expect(component).toMatchSnapshot()
|
||||
|
||||
expect(onChange.calledOnce).toBe(true)
|
||||
expect(onChange.firstCall.args[0].type).toBe('comby')
|
||||
expect(onChange.firstCall.args[0].type).toBe('regexSearchReplace')
|
||||
expect(onChange.firstCall.args[0].arguments).toMatch(/scopeQuery/)
|
||||
})
|
||||
|
||||
|
||||
@ -45,7 +45,7 @@ const defaultInputByType: { [K in CampaignType]: string } = {
|
||||
}`,
|
||||
regexSearchReplace: `{
|
||||
"scopeQuery": "repo:github.com/foo/bar file:.*",
|
||||
"regexMatch": "foo",
|
||||
"regexpMatch": "foo",
|
||||
"textReplace": "bar"
|
||||
}`,
|
||||
}
|
||||
@ -61,7 +61,9 @@ export const CampaignPlanSpecificationFields: React.FunctionComponent<Props> = (
|
||||
isLightTheme,
|
||||
}) => {
|
||||
const value: CampaignPlanSpecificationFormData =
|
||||
rawValue !== undefined ? rawValue : { type: 'comby', arguments: defaultInputByType.comby }
|
||||
rawValue !== undefined
|
||||
? rawValue
|
||||
: { type: 'regexSearchReplace', arguments: defaultInputByType.regexSearchReplace }
|
||||
useEffect(() => {
|
||||
if (rawValue === undefined) {
|
||||
onChange(value)
|
||||
|
||||
@ -17,7 +17,7 @@ exports[`CampaignPlanSpecificationFields has initial value and calls onChange 1`
|
||||
className="form-control w-auto d-inline-block e2e-campaign-type"
|
||||
onChange={[Function]}
|
||||
placeholder="Select campaign type"
|
||||
value="comby"
|
||||
value="regexSearchReplace"
|
||||
>
|
||||
<option
|
||||
value="manual"
|
||||
@ -37,21 +37,9 @@ exports[`CampaignPlanSpecificationFields has initial value and calls onChange 1`
|
||||
<option
|
||||
value="regexSearchReplace"
|
||||
>
|
||||
Regex search and replace
|
||||
Regexp search and replace
|
||||
</option>
|
||||
</select>
|
||||
<small
|
||||
className="ml-1"
|
||||
>
|
||||
<a
|
||||
href="https://comby.dev/#match-syntax"
|
||||
rel="noopener noreferrer"
|
||||
tabIndex={-1}
|
||||
target="_blank"
|
||||
>
|
||||
Learn about comby syntax
|
||||
</a>
|
||||
</small>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
@ -68,39 +56,39 @@ exports[`CampaignPlanSpecificationFields has initial value and calls onChange 1`
|
||||
isLightTheme={true}
|
||||
jsonSchema={
|
||||
Object {
|
||||
"$id": "comby-spec.json#",
|
||||
"$id": "regex-search-replace-spec.json#",
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"additionalProperties": false,
|
||||
"description": "Schema for comby options",
|
||||
"description": "Schema for regexp search replace",
|
||||
"properties": Object {
|
||||
"matchTemplate": Object {
|
||||
"description": "See https://comby.dev/#match-syntax for syntax",
|
||||
"regexpMatch": Object {
|
||||
"description": "Match this regular expression. RE2 syntax is supported: https://github.com/google/re2/wiki/Syntax",
|
||||
"minLength": 1,
|
||||
"type": "string",
|
||||
},
|
||||
"rewriteTemplate": Object {
|
||||
"description": "See https://comby.dev/#match-syntax for syntax",
|
||||
"type": "string",
|
||||
},
|
||||
"scopeQuery": Object {
|
||||
"description": "Define a scope to narrow down repositories affected by this change. Only GitHub and Bitbucket Server are supported.",
|
||||
"minLength": 1,
|
||||
"type": "string",
|
||||
},
|
||||
"textReplace": Object {
|
||||
"description": "Replace the regexpMatch text with this text. You may refer to match groups in regexpMatch using $id or \${id} syntax.",
|
||||
"type": "string",
|
||||
},
|
||||
},
|
||||
"required": Array [
|
||||
"scopeQuery",
|
||||
"matchTemplate",
|
||||
"rewriteTemplate",
|
||||
"regexpMatch",
|
||||
"textReplace",
|
||||
],
|
||||
"type": "object",
|
||||
}
|
||||
}
|
||||
onChange={[Function]}
|
||||
value="{
|
||||
\\"scopeQuery\\": \\"repo:github.com/foo/bar\\",
|
||||
\\"matchTemplate\\": \\"\\",
|
||||
\\"rewriteTemplate\\": \\"\\"
|
||||
\\"scopeQuery\\": \\"repo:github.com/foo/bar file:.*\\",
|
||||
\\"regexpMatch\\": \\"foo\\",
|
||||
\\"textReplace\\": \\"bar\\"
|
||||
}"
|
||||
/>
|
||||
</div>
|
||||
@ -145,7 +133,7 @@ exports[`CampaignPlanSpecificationFields manual type editable 1`] = `
|
||||
<option
|
||||
value="regexSearchReplace"
|
||||
>
|
||||
Regex search and replace
|
||||
Regexp search and replace
|
||||
</option>
|
||||
</select>
|
||||
</React.Fragment>
|
||||
@ -215,7 +203,7 @@ exports[`CampaignPlanSpecificationFields non-manual type editable 1`] = `
|
||||
<option
|
||||
value="regexSearchReplace"
|
||||
>
|
||||
Regex search and replace
|
||||
Regexp search and replace
|
||||
</option>
|
||||
</select>
|
||||
<small
|
||||
|
||||
@ -9,5 +9,5 @@ export const campaignTypeLabels: Record<CampaignType | typeof MANUAL_CAMPAIGN_TY
|
||||
[MANUAL_CAMPAIGN_TYPE]: 'Manual',
|
||||
comby: 'Comby search and replace',
|
||||
credentials: 'Find leaked credentials',
|
||||
regexSearchReplace: 'Regex search and replace',
|
||||
regexSearchReplace: 'Regexp search and replace',
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user