| 
 | 1 | +package executor  | 
 | 2 | + | 
 | 3 | +import (  | 
 | 4 | +	"fmt"  | 
 | 5 | +	"strings"  | 
 | 6 | + | 
 | 7 | +	"github.com/pkg/errors"  | 
 | 8 | +	"github.com/sourcegraph/go-diff/diff"  | 
 | 9 | +	"github.com/sourcegraph/src-cli/internal/batches"  | 
 | 10 | +)  | 
 | 11 | + | 
 | 12 | +func createChangesetSpecs(task *Task, result executionResult, features batches.FeatureFlags) ([]*batches.ChangesetSpec, error) {  | 
 | 13 | +	repo := task.Repository.Name  | 
 | 14 | + | 
 | 15 | +	tmplCtx := &ChangesetTemplateContext{  | 
 | 16 | +		BatchChangeAttributes: *task.BatchChangeAttributes,  | 
 | 17 | +		Steps: StepsContext{  | 
 | 18 | +			Changes: result.ChangedFiles,  | 
 | 19 | +			Path:    result.Path,  | 
 | 20 | +		},  | 
 | 21 | +		Outputs:    result.Outputs,  | 
 | 22 | +		Repository: *task.Repository,  | 
 | 23 | +	}  | 
 | 24 | + | 
 | 25 | +	var authorName string  | 
 | 26 | +	var authorEmail string  | 
 | 27 | + | 
 | 28 | +	if task.Template.Commit.Author == nil {  | 
 | 29 | +		if features.IncludeAutoAuthorDetails {  | 
 | 30 | +			// user did not provide author info, so use defaults  | 
 | 31 | +			authorName = "Sourcegraph"  | 
 | 32 | + | 
 | 33 | +		}  | 
 | 34 | +	} else {  | 
 | 35 | +		var err error  | 
 | 36 | +		authorName, err = renderChangesetTemplateField("authorName", task.Template.Commit.Author.Name, tmplCtx)  | 
 | 37 | +		if err != nil {  | 
 | 38 | +			return nil, err  | 
 | 39 | +		}  | 
 | 40 | +		authorEmail, err = renderChangesetTemplateField("authorEmail", task.Template.Commit.Author.Email, tmplCtx)  | 
 | 41 | +		if err != nil {  | 
 | 42 | +			return nil, err  | 
 | 43 | +		}  | 
 | 44 | +	}  | 
 | 45 | + | 
 | 46 | +	title, err := renderChangesetTemplateField("title", task.Template.Title, tmplCtx)  | 
 | 47 | +	if err != nil {  | 
 | 48 | +		return nil, err  | 
 | 49 | +	}  | 
 | 50 | + | 
 | 51 | +	body, err := renderChangesetTemplateField("body", task.Template.Body, tmplCtx)  | 
 | 52 | +	if err != nil {  | 
 | 53 | +		return nil, err  | 
 | 54 | +	}  | 
 | 55 | + | 
 | 56 | +	message, err := renderChangesetTemplateField("message", task.Template.Commit.Message, tmplCtx)  | 
 | 57 | +	if err != nil {  | 
 | 58 | +		return nil, err  | 
 | 59 | +	}  | 
 | 60 | + | 
 | 61 | +	// TODO: As a next step, we should extend the ChangesetTemplateContext to also include  | 
 | 62 | +	// TransformChanges.Group and then change validateGroups and groupFileDiffs to, for each group,  | 
 | 63 | +	// render the branch name *before* grouping the diffs.  | 
 | 64 | +	defaultBranch, err := renderChangesetTemplateField("branch", task.Template.Branch, tmplCtx)  | 
 | 65 | +	if err != nil {  | 
 | 66 | +		return nil, err  | 
 | 67 | +	}  | 
 | 68 | + | 
 | 69 | +	newSpec := func(branch, diff string) *batches.ChangesetSpec {  | 
 | 70 | +		return &batches.ChangesetSpec{  | 
 | 71 | +			BaseRepository: task.Repository.ID,  | 
 | 72 | +			CreatedChangeset: &batches.CreatedChangeset{  | 
 | 73 | +				BaseRef:        task.Repository.BaseRef(),  | 
 | 74 | +				BaseRev:        task.Repository.Rev(),  | 
 | 75 | +				HeadRepository: task.Repository.ID,  | 
 | 76 | +				HeadRef:        "refs/heads/" + branch,  | 
 | 77 | +				Title:          title,  | 
 | 78 | +				Body:           body,  | 
 | 79 | +				Commits: []batches.GitCommitDescription{  | 
 | 80 | +					{  | 
 | 81 | +						Message:     message,  | 
 | 82 | +						AuthorName:  authorName,  | 
 | 83 | +						AuthorEmail: authorEmail,  | 
 | 84 | +						Diff:        diff,  | 
 | 85 | +					},  | 
 | 86 | +				},  | 
 | 87 | +				Published: task.Template.Published.ValueWithSuffix(repo, branch),  | 
 | 88 | +			},  | 
 | 89 | +		}  | 
 | 90 | +	}  | 
 | 91 | + | 
 | 92 | +	var specs []*batches.ChangesetSpec  | 
 | 93 | + | 
 | 94 | +	groups := groupsForRepository(task.Repository.Name, task.TransformChanges)  | 
 | 95 | +	if len(groups) != 0 {  | 
 | 96 | +		err := validateGroups(task.Repository.Name, task.Template.Branch, groups)  | 
 | 97 | +		if err != nil {  | 
 | 98 | +			return specs, err  | 
 | 99 | +		}  | 
 | 100 | + | 
 | 101 | +		// TODO: Regarding 'defaultBranch', see comment above  | 
 | 102 | +		diffsByBranch, err := groupFileDiffs(result.Diff, defaultBranch, groups)  | 
 | 103 | +		if err != nil {  | 
 | 104 | +			return specs, errors.Wrap(err, "grouping diffs failed")  | 
 | 105 | +		}  | 
 | 106 | + | 
 | 107 | +		for branch, diff := range diffsByBranch {  | 
 | 108 | +			specs = append(specs, newSpec(branch, diff))  | 
 | 109 | +		}  | 
 | 110 | +	} else {  | 
 | 111 | +		specs = append(specs, newSpec(defaultBranch, result.Diff))  | 
 | 112 | +	}  | 
 | 113 | + | 
 | 114 | +	return specs, nil  | 
 | 115 | +}  | 
 | 116 | + | 
 | 117 | +func groupsForRepository(repo string, transform *batches.TransformChanges) []batches.Group {  | 
 | 118 | +	var groups []batches.Group  | 
 | 119 | + | 
 | 120 | +	if transform == nil {  | 
 | 121 | +		return groups  | 
 | 122 | +	}  | 
 | 123 | + | 
 | 124 | +	for _, g := range transform.Group {  | 
 | 125 | +		if g.Repository != "" {  | 
 | 126 | +			if g.Repository == repo {  | 
 | 127 | +				groups = append(groups, g)  | 
 | 128 | +			}  | 
 | 129 | +		} else {  | 
 | 130 | +			groups = append(groups, g)  | 
 | 131 | +		}  | 
 | 132 | +	}  | 
 | 133 | + | 
 | 134 | +	return groups  | 
 | 135 | +}  | 
 | 136 | + | 
 | 137 | +func validateGroups(repo, defaultBranch string, groups []batches.Group) error {  | 
 | 138 | +	uniqueBranches := make(map[string]struct{}, len(groups))  | 
 | 139 | + | 
 | 140 | +	for _, g := range groups {  | 
 | 141 | +		if _, ok := uniqueBranches[g.Branch]; ok {  | 
 | 142 | +			return fmt.Errorf("transformChanges would lead to multiple changesets in repository %s to have the same branch %q", repo, g.Branch)  | 
 | 143 | +		} else {  | 
 | 144 | +			uniqueBranches[g.Branch] = struct{}{}  | 
 | 145 | +		}  | 
 | 146 | + | 
 | 147 | +		if g.Branch == defaultBranch {  | 
 | 148 | +			return fmt.Errorf("transformChanges group branch for repository %s is the same as branch %q in changesetTemplate", repo, defaultBranch)  | 
 | 149 | +		}  | 
 | 150 | +	}  | 
 | 151 | + | 
 | 152 | +	return nil  | 
 | 153 | +}  | 
 | 154 | + | 
 | 155 | +func groupFileDiffs(completeDiff, defaultBranch string, groups []batches.Group) (map[string]string, error) {  | 
 | 156 | +	fileDiffs, err := diff.ParseMultiFileDiff([]byte(completeDiff))  | 
 | 157 | +	if err != nil {  | 
 | 158 | +		return nil, err  | 
 | 159 | +	}  | 
 | 160 | + | 
 | 161 | +	// Housekeeping: we setup these two datastructures so we can  | 
 | 162 | +	// - access the group.Branch by the directory for which they should be used  | 
 | 163 | +	// - check against the given directories, in order.  | 
 | 164 | +	branchesByDirectory := make(map[string]string, len(groups))  | 
 | 165 | +	dirs := make([]string, len(branchesByDirectory))  | 
 | 166 | +	for _, g := range groups {  | 
 | 167 | +		branchesByDirectory[g.Directory] = g.Branch  | 
 | 168 | +		dirs = append(dirs, g.Directory)  | 
 | 169 | +	}  | 
 | 170 | + | 
 | 171 | +	byBranch := make(map[string][]*diff.FileDiff, len(groups))  | 
 | 172 | +	byBranch[defaultBranch] = []*diff.FileDiff{}  | 
 | 173 | + | 
 | 174 | +	// For each file diff...  | 
 | 175 | +	for _, f := range fileDiffs {  | 
 | 176 | +		name := f.NewName  | 
 | 177 | +		if name == "/dev/null" {  | 
 | 178 | +			name = f.OrigName  | 
 | 179 | +		}  | 
 | 180 | + | 
 | 181 | +		// .. we check whether it matches one of the given directories in the  | 
 | 182 | +		// group transformations, with the last match winning:  | 
 | 183 | +		var matchingDir string  | 
 | 184 | +		for _, d := range dirs {  | 
 | 185 | +			if strings.Contains(name, d) {  | 
 | 186 | +				matchingDir = d  | 
 | 187 | +			}  | 
 | 188 | +		}  | 
 | 189 | + | 
 | 190 | +		// If the diff didn't match a rule, it goes into the default branch and  | 
 | 191 | +		// the default changeset.  | 
 | 192 | +		if matchingDir == "" {  | 
 | 193 | +			byBranch[defaultBranch] = append(byBranch[defaultBranch], f)  | 
 | 194 | +			continue  | 
 | 195 | +		}  | 
 | 196 | + | 
 | 197 | +		// If it *did* match a directory, we look up which branch we should use:  | 
 | 198 | +		branch, ok := branchesByDirectory[matchingDir]  | 
 | 199 | +		if !ok {  | 
 | 200 | +			panic("this should not happen: " + matchingDir)  | 
 | 201 | +		}  | 
 | 202 | + | 
 | 203 | +		byBranch[branch] = append(byBranch[branch], f)  | 
 | 204 | +	}  | 
 | 205 | + | 
 | 206 | +	finalDiffsByBranch := make(map[string]string, len(byBranch))  | 
 | 207 | +	for branch, diffs := range byBranch {  | 
 | 208 | +		printed, err := diff.PrintMultiFileDiff(diffs)  | 
 | 209 | +		if err != nil {  | 
 | 210 | +			return nil, errors.Wrap(err, "printing multi file diff failed")  | 
 | 211 | +		}  | 
 | 212 | +		finalDiffsByBranch[branch] = string(printed)  | 
 | 213 | +	}  | 
 | 214 | +	return finalDiffsByBranch, nil  | 
 | 215 | +}  | 
0 commit comments