Skip to content

Commit fd1ed5d

Browse files
mrnuggeteseliger
andauthored
Add BaseRef field to CampaignPlanPatch and use commitID in BaseRevision (#158)
* Add BaseRef field to CampaignPlanPlatch and use commitID in BaseRevision This implements the src CLI tasks of https://github.com/sourcegraph/sourcegraph/issues/8846 With this change src CLI uses the commit ID (instead of refspec) as the `CampaignPlanPatch.BaseRevision` when creating a `CampaignPlan`. It also adds the additional `baseRef` field to `CampaignPlanPatch` and sends that to the GraphQL. Since Sourcegraph <3.14 expects the `baseRevision` of a `CampaignPlanPatch` to contain a ref (e.g. "refs/heads/master") we check the Sourcegraph version and fall back to that behaviour so that customers can use a newer src CLI version with their <3.14 instance. With the addition of these fields to ActionRepo and CampaignPlanPatch the cache of src CLI is now also busted when the revision of the default branch of a repo changes. * Update cmd/src/campaign_plans_create_from_patches.go Co-Authored-By: Erik Seliger <[email protected]> Co-authored-by: Erik Seliger <[email protected]>
1 parent 8e39fbe commit fd1ed5d

File tree

3 files changed

+59
-11
lines changed

3 files changed

+59
-11
lines changed

cmd/src/actions_exec.go

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type ActionStep struct {
4040
type CampaignPlanPatch struct {
4141
Repository string `json:"repository"`
4242
BaseRevision string `json:"baseRevision"`
43+
BaseRef string `json:"baseRef"`
4344
Patch string `json:"patch"`
4445
}
4546

@@ -349,9 +350,10 @@ func getDockerImageContentDigest(ctx context.Context, image string) (string, err
349350
}
350351

351352
type ActionRepo struct {
352-
ID string
353-
Name string
354-
Rev string
353+
ID string
354+
Name string
355+
Rev string
356+
BaseRef string
355357
}
356358

357359
func actionRepos(ctx context.Context, scopeQuery string) ([]ActionRepo, []string, error) {
@@ -373,13 +375,19 @@ query ActionRepos($query: String!) {
373375
... on Repository {
374376
id
375377
name
376-
defaultBranch { name }
378+
defaultBranch {
379+
name
380+
target { oid }
381+
}
377382
}
378383
... on FileMatch {
379384
repository {
380385
id
381386
name
382-
defaultBranch { name }
387+
defaultBranch {
388+
name
389+
target { oid }
390+
}
383391
}
384392
}
385393
}
@@ -389,16 +397,22 @@ query ActionRepos($query: String!) {
389397
`
390398
type Repository struct {
391399
ID, Name string
392-
DefaultBranch struct{ Name string }
400+
DefaultBranch struct {
401+
Name string
402+
Target struct{ OID string }
403+
}
393404
}
394405
var result struct {
395406
Search struct {
396407
Results struct {
397408
Results []struct {
398409
Typename string `json:"__typename"`
399410
ID, Name string
400-
DefaultBranch struct{ Name string }
401-
Repository Repository `json:"repository"`
411+
DefaultBranch struct {
412+
Name string
413+
Target struct{ OID string }
414+
}
415+
Repository Repository `json:"repository"`
402416
}
403417
}
404418
}
@@ -433,11 +447,17 @@ query ActionRepos($query: String!) {
433447
continue
434448
}
435449

450+
if repo.DefaultBranch.Target.OID == "" {
451+
skipped = append(skipped, repo.Name)
452+
continue
453+
}
454+
436455
if _, ok := reposByID[repo.ID]; !ok {
437456
reposByID[repo.ID] = ActionRepo{
438-
ID: repo.ID,
439-
Name: repo.Name,
440-
Rev: repo.DefaultBranch.Name,
457+
ID: repo.ID,
458+
Name: repo.Name,
459+
Rev: repo.DefaultBranch.Target.OID,
460+
BaseRef: repo.DefaultBranch.Name,
441461
}
442462
}
443463
}

cmd/src/actions_exec_backend_runner.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (x *actionExecutor) do(ctx context.Context, repo ActionRepo) (err error) {
6868
status.Patch = CampaignPlanPatch{
6969
Repository: repo.ID,
7070
BaseRevision: repo.Rev,
71+
BaseRef: repo.BaseRef,
7172
Patch: string(patch),
7273
}
7374
}

cmd/src/campaign_plans_create_from_patches.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,33 @@ func createCampaignPlanFromPatches(
9797
var result struct {
9898
CreateCampaignPlanFromPatches CampaignPlan
9999
}
100+
101+
version, err := getSourcegraphVersion()
102+
if err != nil {
103+
return err
104+
}
105+
supportsBaseRef, err := sourcegraphVersionCheck(version, ">= 3.14.0", "2020-03-11")
106+
if err != nil {
107+
return err
108+
}
109+
110+
// If we're on Sourcegraph >=3.14 the GraphQL API is "fixed" and accepts
111+
// patches with `BaseRevision` and `BaseRef` fields. <3.14 expects a ref
112+
// (e.g. "refs/heads/master") in `BaseRevision`, so we need to copy the
113+
// value over.
114+
if !supportsBaseRef {
115+
patchesWithoutBaseRef := make([]CampaignPlanPatch, len(patches))
116+
for i, p := range patches {
117+
patchesWithoutBaseRef[i] = CampaignPlanPatch{
118+
Repository: p.Repository,
119+
BaseRevision: p.BaseRef,
120+
BaseRef: "IGNORE-THIS",
121+
Patch: p.Patch,
122+
}
123+
}
124+
patches = patchesWithoutBaseRef
125+
}
126+
100127
return (&apiRequest{
101128
query: query,
102129
vars: map[string]interface{}{"patches": patches},

0 commit comments

Comments
 (0)