Skip to content

Commit a268e32

Browse files
committed
Use new types in GraphQL applier
1 parent 5c6187d commit a268e32

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

applier.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ func NewApplier(client *github.Client, repo Repository, c *github.Commit) *Appli
5050
// tree entries, and returns the entry. If the application succeeds, Apply
5151
// creates a blob in the repository with the modified content.
5252
//
53-
// If the apply fails due to a conflict, Apply returns an error of type *Conflict.
53+
// If the apply fails due to a conflict, Apply returns an error of type
54+
// *Conflict.
5455
func (a *Applier) Apply(ctx context.Context, f *gitdiff.File) (*github.TreeEntry, error) {
5556
// TODO(bkeyes): validate file to make sure fields are consistent
5657
// maybe two modes: validate and fix, where fix tries to set

graphql_applier.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"encoding/base64"
7-
"errors"
87
"fmt"
98
"io/ioutil"
109
"os"
@@ -81,6 +80,9 @@ func (a *GraphQLApplier) SetV3Client(client *github.Client) {
8180
// When given an unsupported patch, Apply returns an error such that
8281
// IsUnsupported(err) is true. Setting a V3 client with SetV3Client allows
8382
// Apply to process some patches that are otherwise unsupported.
83+
//
84+
// If the apply fails due to a conflict, Apply returns an error of type
85+
// *Conflict.
8486
func (a *GraphQLApplier) Apply(ctx context.Context, f *gitdiff.File) error {
8587
// As of 2021-09-22, createCommitOnBranch handles file modes
8688
// inconsistently:
@@ -122,11 +124,11 @@ func (a *GraphQLApplier) applyCreate(ctx context.Context, f *gitdiff.File) error
122124
return err
123125
}
124126
if exists {
125-
return errors.New("existing entry for new file")
127+
return &Conflict{Type: ConflictNewFileExists, File: f.NewName}
126128
}
127129

128130
var b bytes.Buffer
129-
if err := gitdiff.Apply(&b, bytes.NewReader(nil), f); err != nil {
131+
if err := apply(&b, bytes.NewReader(nil), f.NewName, f); err != nil {
130132
return err
131133
}
132134

@@ -141,12 +143,12 @@ func (a *GraphQLApplier) applyDelete(ctx context.Context, f *gitdiff.File) error
141143
return err
142144
}
143145
if !exists {
144-
// because the rest of application is strict, return an error if the
146+
// Because the rest of application is strict, return an error if the
145147
// file was already deleted, since it indicates a conflict of some kind
146-
return errors.New("missing entry for deleted file")
148+
return &Conflict{Type: ConflictDeletedFileMissing, File: f.OldName}
147149
}
148150

149-
if err := gitdiff.Apply(ioutil.Discard, bytes.NewReader(data), f); err != nil {
151+
if err := apply(ioutil.Discard, bytes.NewReader(data), f.OldName, f); err != nil {
150152
return err
151153
}
152154

@@ -160,12 +162,12 @@ func (a *GraphQLApplier) applyModify(ctx context.Context, f *gitdiff.File) error
160162
return err
161163
}
162164
if !exists {
163-
return errors.New("no entry for modified file")
165+
return &Conflict{Type: ConflictModifiedFileMissing, File: f.OldName}
164166
}
165167

166168
if len(f.TextFragments) > 0 || f.BinaryFragment != nil {
167169
var b bytes.Buffer
168-
if err := gitdiff.Apply(&b, bytes.NewReader(data), f); err != nil {
170+
if err := apply(&b, bytes.NewReader(data), f.OldName, f); err != nil {
169171
return err
170172
}
171173
data = b.Bytes()

0 commit comments

Comments
 (0)