Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to create draft pull requests #45

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,36 @@ require (
github.com/spf13/afero v1.11.0
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.12.0
golang.org/x/mod v0.13.0
golang.org/x/oauth2 v0.18.0
)

require (
// Unfortunately, there is no way, with the REST API to mark a PR as ready.
// see https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#update-a-pull-request
// Instead, we must use the graphQL client and in particular, for which there is a dedicated function in
// the official GitHub CLI client.
github.com/cli/go-gh/v2 v2.6.0
github.com/shurcooL/githubv4 v0.0.0-20230704064427-599ae7bbf278
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
github.com/PuerkitoBio/goquery v1.7.1 // indirect
github.com/andybalholm/cascadia v1.2.0 // indirect
github.com/PuerkitoBio/goquery v1.8.1 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/antchfx/htmlquery v1.2.4 // indirect
github.com/antchfx/xmlquery v1.3.7 // indirect
github.com/antchfx/xpath v1.2.0 // indirect
github.com/aymanbagabas/go-osc52 v1.0.3 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/cli/safeexec v1.0.1 // indirect
github.com/cli/shurcooL-graphql v0.0.4 // indirect
github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
github.com/emirpasic/gods v1.18.1 // indirect
Expand All @@ -45,25 +57,33 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/henvic/httpretty v0.1.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/muesli/termenv v0.13.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/saintfish/chardet v0.0.0-20120816061221-3af4cd4741ca // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/temoto/robotstxt v1.1.2 // indirect
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
75 changes: 64 additions & 11 deletions go.sum

Large diffs are not rendered by default.

85 changes: 76 additions & 9 deletions pkg/api/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,40 @@ import (

gh "github.com/adevinta/maiao/pkg/github"
"github.com/adevinta/maiao/pkg/log"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/google/go-github/v55/github"
"github.com/shurcooL/githubv4"
"github.com/sirupsen/logrus"
)

// GitHub implements the PullRequester interface allowing to create pull requests for a given repository
type GitHub struct {
GraphQLClient *api.GraphQLClient
*github.Client
Host string
Owner string
Repository string
}

// RepoName implements the ghrepo.Interface interface required to call the github graphql API from https://github.com/cli/cli
// See https://github.com/cli/cli/blob/dc804d928714120a3f4b53f78847aec7ba282c63/internal/ghrepo/repo.go#L14
func (g *GitHub) RepoName() string {
return g.Repository
}

// RepoHost implements the ghrepo.Interface interface required to call the github graphql API from https://github.com/cli/cli
// See https://github.com/cli/cli/blob/dc804d928714120a3f4b53f78847aec7ba282c63/internal/ghrepo/repo.go#L14
func (g *GitHub) RepoOwner() string {
return g.Owner
}

// RepoHost implements the ghrepo.Interface interface required to call the github graphql API from https://github.com/cli/cli
// See https://github.com/cli/cli/blob/dc804d928714120a3f4b53f78847aec7ba282c63/internal/ghrepo/repo.go#L14
func (g *GitHub) RepoHost() string {
return g.Host
}

// Ensure ensures a PR is opened for the head branch
func (g *GitHub) Ensure(ctx context.Context, options PullRequestOptions) (*PullRequest, bool, error) {
ctx = log.WithContextFields(ctx, logrus.Fields{
Expand All @@ -42,12 +63,17 @@ func (g *GitHub) Ensure(ctx context.Context, options PullRequestOptions) (*PullR
}
switch len(prs) {
case 0:
pr, _, err := g.PullRequests.Create(context.Background(), g.Owner, g.Repository, &github.NewPullRequest{
newPROptions := &github.NewPullRequest{
Title: github.String(options.Title),
Body: github.String(options.Body),
Base: github.String(options.Base),
Head: github.String(options.Head),
})
}
if options.WIP {
log.ForContext(ctx).Info("adding draft marker")
newPROptions.Draft = github.Bool(true)
}
pr, _, err := g.PullRequests.Create(context.Background(), g.Owner, g.Repository, newPROptions)
if err != nil {
log.ForContext(ctx).WithError(err).Error("failed to create new pull request")
return nil, false, err
Expand Down Expand Up @@ -83,7 +109,7 @@ func (g *GitHub) Update(ctx context.Context, pr *PullRequest, options PullReques
return nil, err
}
ctx = log.WithContextFields(ctx, logrus.Fields{"prID": id})
p, _, err := g.PullRequests.Edit(ctx, g.Owner, g.Repository, id, &github.PullRequest{
prUpdateOptions := &github.PullRequest{
Title: github.String(options.Title),
Body: github.String(options.Body),
Base: &github.PullRequestBranch{
Expand All @@ -92,12 +118,41 @@ func (g *GitHub) Update(ctx context.Context, pr *PullRequest, options PullReques
Head: &github.PullRequestBranch{
Ref: github.String(options.Head),
},
})
}
p, _, err := g.PullRequests.Edit(ctx, g.Owner, g.Repository, id, prUpdateOptions)
if err != nil {
log.ForContext(ctx).WithError(err).Error("failed to edit pull request")
return nil, err
}
log.ForContext(ctx).Info("edit pull request")
if options.Ready {
log.ForContext(ctx).Info("marking pull request as ready")

var mutation struct {
MarkPullRequestReadyForReview struct {
PullRequest struct {
ID githubv4.ID
}
} `graphql:"markPullRequestReadyForReview(input: $input)"`
}

variables := map[string]interface{}{
"input": githubv4.MarkPullRequestReadyForReviewInput{
// https://github.blog/changelog/2018-05-30-end-jean-grey-preview/
// The NodeID seems to be the pivot between the REST API and the graphQL API
PullRequestID: p.GetNodeID(),
},
}

// Unfortunately, there is no way, with the REST API to mark a PR as ready.
// see https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#update-a-pull-request
// Instead, use the graphQL client and in particular, use the github cli implementation
err = g.GraphQLClient.Mutate("PullRequestReadyForReview", &mutation, variables)
if err != nil {
log.ForContext(ctx).WithError(err).Error("failed to mark pull request as ready")
return nil, err
}
}
return &PullRequest{
ID: strconv.Itoa(*p.Number),
URL: *p.URL,
Expand Down Expand Up @@ -134,7 +189,12 @@ func NewGitHubUpserter(ctx context.Context, endpoint *transport.Endpoint) (*GitH
log.ForContext(ctx).WithField("repository", endpoint.Path).Error("invalid repository, expecting <org>/<repo>")
return nil, fmt.Errorf("invalid repository, expecting <org>/<repo>")
}
client, err := gh.NewClient(endpoint.Host)
httpClient, err := gh.NewHTTPClientForDomain(ctx, endpoint.Host)
if err != nil {
log.ForContext(ctx).WithError(err).Errorf("failed to create a new http client: %s", err.Error())
return nil, err
}
client, err := gh.NewClient(httpClient, endpoint.Host)
if err != nil {
log.ForContext(ctx).WithError(err).Errorf("failed to create a new github client: %s", err.Error())
return nil, err
Expand All @@ -144,11 +204,18 @@ func NewGitHubUpserter(ctx context.Context, endpoint *transport.Endpoint) (*GitH
return nil, err
}

graphQLClient, err := gh.NewGraphQLClient(httpClient, endpoint.Host)
if err != nil {
log.ForContext(ctx).WithError(err).Errorf("failed to create a new github graphQL client: %s", err.Error())
return nil, err
}

gh := &GitHub{
Host: endpoint.Host,
Owner: repo.GetOwner().GetLogin(),
Repository: repo.GetName(),
Client: client,
Host: endpoint.Host,
Owner: repo.GetOwner().GetLogin(),
Repository: repo.GetName(),
Client: client,
GraphQLClient: graphQLClient,
}
log.ForContext(ctx).Trace("initialized github client")
return gh, nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/api/pullRequester.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type PullRequestOptions struct {
Head string
Title string
Body string
WIP bool
Ready bool
}

// PullRequest defines the object
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ func NewCommand() *cobra.Command {
rootCmd.PersistentFlags().StringP("topic", "t", "", "Topic to submit branch to")
rootCmd.PersistentFlags().Bool("debug", false, "Run the command in debug mode")
rootCmd.PersistentFlags().String("remote", "", "Specifies the remote the review should be done on. By default the tracking remote of the target branch is used")
rootCmd.PersistentFlags().BoolP("work-in-progress", "w", false, "Mark the review as work in progress, or draft in compatible remotes. This flag is exclusively effective when creating Pull Requests")
rootCmd.PersistentFlags().BoolP("ready", "W", false, "Mark the review as ready in compatible remotes (i.e. removing the work in progress or draft flag)")
tjamet marked this conversation as resolved.
Show resolved Hide resolved
rootCmd.AddCommand(
&cobra.Command{
Use: "install",
Expand Down
10 changes: 6 additions & 4 deletions pkg/cmd/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ func review(cmd *cobra.Command, args []string) error {
}
}
return maiao.Review(context.Background(), repo, maiao.ReviewOptions{
Remote: cmd.Flag("remote").Value.String(),
SkipRebase: cmd.Flag("no-rebase").Value.String() != "false",
Topic: cmd.Flag("topic").Value.String(),
Branch: branch,
Remote: cmd.Flag("remote").Value.String(),
SkipRebase: cmd.Flag("no-rebase").Value.String() != "false",
Topic: cmd.Flag("topic").Value.String(),
Branch: branch,
WorkInProgress: cmd.Flag("work-in-progress").Value.String() != "false",
Ready: cmd.Flag("ready").Value.String() != "false",
})
}
6 changes: 5 additions & 1 deletion pkg/github/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import (
)

func ExampleNewGithubClient() {
client, err := NewClient("github.com")
httpClient, err := NewHTTPClientForDomain(context.Background(), "github.com")
if err != nil {
Logger.Errorf("failed to create http client: %s", err.Error())
}
client, err := NewClient(httpClient, "github.com")
if err != nil {
Logger.Errorf("failed to create github client: %s", err.Error())
}
Expand Down
57 changes: 40 additions & 17 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,66 @@ package gh
import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/adevinta/maiao/pkg/log"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/google/go-github/v55/github"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)

// NewClient instanciates a new github client depending on the domain name
//
// When requesting a client for a different host than github.com,
// a client for github enterprise is considered
//
// Credentials are even taken from GITHUB_TOKEN environment variable or
// from your ~/.netrc file
func NewClient(domain string) (*github.Client, error) {
logger := Logger.WithFields(logrus.Fields{
"context": "initializing GitHub client",
})
func GitHubAPIDomain(domain string) string {
if domain == "github.com" {
domain = "api.github.com"
return "api.github.com"
}
return domain
}

func NewHTTPClientForDomain(ctx context.Context, domain string) (*http.Client, error) {
domain = GitHubAPIDomain(domain)
// TODO: move this to handle unauthorized calls.
token, err := getGithubToken(domain)
if err != nil {
logger.Errorf("unable to find token for %s: %s", domain, err.Error())
log.ForContext(ctx).WithError(err).WithField("domain", domain).Errorf("unable to find token")
return nil, fmt.Errorf("unable to find token for %s: %s", domain, err.Error())
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
c := github.NewClient(tc)
if domain != "api.github.com" {
return tc, nil
}

func NewGraphQLClient(httpClient *http.Client, domain string) (*api.GraphQLClient, error) {
opts := api.ClientOptions{
AuthToken: "overridden by Transport",
Host: GitHubAPIDomain(domain),
Transport: httpClient.Transport,
}
client, err := api.NewGraphQLClient(opts)
if err != nil {
return nil, err
}
return client, nil
}

// NewClient instanciates a new github client depending on the domain name
//
// When requesting a client for a different host than github.com,
// a client for github enterprise is considered
//
// Credentials are even taken from GITHUB_TOKEN environment variable or
// from your ~/.netrc file
func NewClient(httpClient *http.Client, domain string) (*github.Client, error) {
c := github.NewClient(httpClient)
switch domain {
case "github.com", "api.github.com":
default:
GitHubURL := url.URL{
Scheme: "https",
Host: domain,
Host: GitHubAPIDomain(domain),
Path: "/api/v3/",
}
GitHubUploadURL := GitHubURL
Expand Down
16 changes: 15 additions & 1 deletion pkg/github/github_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package gh

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/adevinta/maiao/pkg/credentials"
"github.com/adevinta/maiao/pkg/system"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func setCredentialStore(c credentials.CredentialGetter) {
Expand All @@ -19,6 +21,18 @@ type TestCredentialGetter struct {
Check func()
}

func TestNewGraphQLClient(t *testing.T) {
t.Setenv("GITHUB_TOKEN", "test")
ctx := context.Background()
httpClient, err := NewHTTPClientForDomain(ctx, "github.com")
require.NoError(t, err)
graphQLClient, err := NewGraphQLClient(httpClient, "github.com")

assert.NoError(t, err)
assert.NotNil(t, graphQLClient)

}

func (c *TestCredentialGetter) CredentialForHost(string) (*credentials.Credentials, error) {
if c.Check != nil {
c.Check()
Expand Down
2 changes: 2 additions & 0 deletions pkg/maiao/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ func prOptions(repo lgit.Repository, prAPI api.PullRequester, options ReviewOpti
Head: change.branch,
Title: title,
Body: strings.Join(append([]string{change.message.Body}, additions...), "\n"),
Ready: options.Ready,
WIP: options.WorkInProgress,
}
}
Loading