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

gh: Move away from using curl for patches and to GitHub API #601

Merged
merged 6 commits into from
Sep 22, 2014
Merged

Conversation

mislav
Copy link
Owner

@mislav mislav commented Jul 28, 2014

⚠️ This initially failing PR is an TODO item for am/apply commands. We need to switch them over to the GitHub API so that they won't have any external dependencies.

/cc @jingweno

@mislav mislav added this to the v2.2 - The switch to Go milestone Jul 28, 2014
@owenthereal
Copy link
Contributor

I could help with this since it involves changes to octokit. I could start as early as this evening.

@mislav
Copy link
Owner Author

mislav commented Jul 28, 2014

Thanks!

@owenthereal
Copy link
Contributor

i just rebased this branch on top of the latest gh to continue the work

@mislav
Copy link
Owner Author

mislav commented Sep 10, 2014

@jingweno Hey do you need any help finalizing the patches API so we can download them from hub?

@owenthereal
Copy link
Contributor

@mislav Yes, please help a bit. I've added the commit patch API. The pull request patch API should be similar. The gist api is half way there since I haven't added the method to return raw. I'll try to come back to this project this week.

@mislav
Copy link
Owner Author

mislav commented Sep 16, 2014

@jingweno Now that go-octokit is ready for this, do you want me to try fixing this today/tomorrow or are you already on this?

@owenthereal
Copy link
Contributor

@mislav Please go ahead. I'll move on to disabling autoupdate for homebrew build

@mislav mislav force-pushed the no-curl branch 2 times, most recently from 99020f4 to 740a5d3 Compare September 21, 2014 07:22
In testing, we want to redirect all requests to a local test server
pretending to be GitHub API, but we want them to preserve their original
"Host" header. A good place to do that transparently is the `RoundTrip`
method that handles every request for this http.Client instance.
This enables fetching patches from private repos that you have access to.
@mislav
Copy link
Owner Author

mislav commented Sep 21, 2014

@jingweno Ready for 👀, thanks

if gist {
prefix = "gist-"
}
bytes, err := ioutil.ReadAll(patch)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.Copy is a better way do copy from a reader to writer:

diff --git a/commands/apply.go b/commands/apply.go
index 8521886..8b5d06e 100644
--- a/commands/apply.go
+++ b/commands/apply.go
@@ -3,7 +3,6 @@ package commands
 import (
        "io"
        "io/ioutil"
-       "os"
        "regexp"

        "github.com/github/hub/github"
@@ -94,13 +93,12 @@ func transformApplyArgs(args *Args) {
                }

                idx := args.IndexOfParam(arg)
-               patchFile, err := ioutil.TempFile(os.TempDir(), "hub")
+               patchFile, err := ioutil.TempFile("", "hub")
                utils.Check(err)

-               bytes, err := ioutil.ReadAll(patch)
+               _, err = io.Copy(patchFile, patch)
                utils.Check(err)

-               patchFile.Write(bytes)
                patchFile.Close()
                patch.Close()

Also ioutil.TempFile uses os.TempDir() by default already.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call; now addressed.

@owenthereal
Copy link
Contributor

Looking 👍

})

c := newHttpClient(s.URL.String(), false)
c.Get(fmt.Sprintf("%s/test", s.URL))
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole point of OverrideURL is that it rewrites Host and Scheme. This test doesn't verify that, as it makes a request to the same host:port that the test server is running at.

Instead, try to make a request to "https://example.com" and verify that the X-Original-Scheme is "https" and that the request host was "example.com".

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we need a test that verifies a very important condition: that the http client won't rewrite URLs if OverrideURL is nil. So, we should initiate newHttpClient("", false), make a request to the test server, and verify that the hostname (probably just "127.0.0.1") is unchanged and that X-Original-Scheme was not set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5603206 (--amend)

mislav added a commit that referenced this pull request Sep 22, 2014
gh: Move away from using curl for patches and to GitHub API
@mislav mislav merged commit 3294fe5 into gh Sep 22, 2014
@mislav mislav deleted the no-curl branch September 22, 2014 15:22
@mislav
Copy link
Owner Author

mislav commented Sep 22, 2014

@jingweno Thanks! Shipped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants