Skip to content

Commit 2c52fe3

Browse files
authored
Implement GraphQLApplier (#15)
GitHub recently added the createCommitOnBranch mutation, which simplifies commit creation and adds free signing support, with some limitations that don't make it obviously better than the old approach in all cases. Due to these downsides, the different contract, and to support GitHub Enterprise, implement this as a new type instead of modifying the existing applier. Expand testing to cover the new applier, including patches that it does not support. Add the new IsUnsupported function to test if apply errors are due to GraphQL limitations. There's some possibly inefficient code that gets file modes to check for these limitations, but I don't think it's worth optimizing at this point.
1 parent d17492d commit 2c52fe3

File tree

15 files changed

+549
-10
lines changed

15 files changed

+549
-10
lines changed

applier_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/bluekeyes/go-gitdiff/gitdiff"
1717
"github.com/google/go-github/v39/github"
18+
"github.com/shurcooL/githubv4"
1819
"golang.org/x/oauth2"
1920
)
2021

@@ -61,7 +62,8 @@ type TestContext struct {
6162
BaseCommit *github.Commit
6263
BaseTree *github.Tree
6364

64-
Client *github.Client
65+
Client *github.Client
66+
V4Client *githubv4.Client
6567
}
6668

6769
func (tctx *TestContext) Branch(name string) string {
@@ -234,15 +236,16 @@ func prepareTestContext(t *testing.T) *TestContext {
234236
}
235237

236238
ctx := context.Background()
237-
client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
239+
httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
238240
&oauth2.Token{AccessToken: token},
239-
)))
241+
))
240242

241243
tctx := TestContext{
242-
Context: ctx,
243-
ID: id,
244-
Repo: repo,
245-
Client: client,
244+
Context: ctx,
245+
ID: id,
246+
Repo: repo,
247+
Client: github.NewClient(httpClient),
248+
V4Client: githubv4.NewClient(httpClient),
246249
}
247250
return &tctx
248251
}

error.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package patch2pr
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
func unsupported(msg string, args ...interface{}) error {
9+
return unsupportedErr{reason: fmt.Sprintf(msg, args...)}
10+
}
11+
12+
type unsupportedErr struct {
13+
reason string
14+
}
15+
16+
func (err unsupportedErr) Error() string {
17+
return fmt.Sprintf("unsupported: %s", err.reason)
18+
}
19+
20+
func (err unsupportedErr) Unsupported() bool {
21+
return true
22+
}
23+
24+
// IsUnsupported returns true if err is the result of trying an unsupported
25+
// operation. It is equivalent to finding the first error in err's chain that
26+
// implements
27+
//
28+
// type unsupported interface {
29+
// Unsupported() bool
30+
// }
31+
//
32+
// and then calling the Unsupported() method.
33+
func IsUnsupported(err error) bool {
34+
var u interface {
35+
Unsupported() bool
36+
}
37+
return errors.As(err, &u) && u.Unsupported()
38+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ go 1.14
55
require (
66
github.com/bluekeyes/go-gitdiff v0.6.0
77
github.com/google/go-github/v39 v39.0.0
8+
github.com/shurcooL/githubv4 v0.0.0-20210922025249-6831e00d857f
9+
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a // indirect
810
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
911
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ github.com/google/go-github/v39 v39.0.0 h1:pygGA5ySwxEez1N39GnDauD0PaWWuGgayudyZ
1212
github.com/google/go-github/v39 v39.0.0/go.mod h1:C1s8C5aCC9L+JXIYpJM5GYytdX52vC1bLvHEF1IhBrE=
1313
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
1414
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
15+
github.com/shurcooL/githubv4 v0.0.0-20210922025249-6831e00d857f h1:q4b8/GCL8Ksl+okhFKSd8DVBQNc0XExjxTO68nK0c3Y=
16+
github.com/shurcooL/githubv4 v0.0.0-20210922025249-6831e00d857f/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
17+
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a h1:KikTa6HtAK8cS1qjvUvvq4QO21QnwC+EfvB+OAuZ/ZU=
18+
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
1519
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1620
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ=
1721
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

0 commit comments

Comments
 (0)