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

commit api #3024

Open
Jerry-yz opened this issue Dec 7, 2023 · 3 comments
Open

commit api #3024

Jerry-yz opened this issue Dec 7, 2023 · 3 comments

Comments

@Jerry-yz
Copy link

Jerry-yz commented Dec 7, 2023

i meet a error when i request get commit api;
this is my code:
c := github.NewClient(&http.Client{}).WithAuthToken("xxxxxxxxxxxxxx")
_, resp, err := c.Repositories.GetCommit(context.TODO(), "Jerry-yz", "go-zero-example", "013bdd7b435e9bff6e13617fa85b47c06883890c", &github.ListOptions{})
if err != nil {
panic(err)
}
defer resp.Body.Close()
commit := new(github.RepositoryCommit)
b, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
if err := json.Unmarshal(b, commit); err != nil {
panic(err)
}
panic: file already closed
so, i find this err in client.Do
this func read this resp.body and defer resp.close(), so i meet error when read resp.body from GetCommit() func

@gmlewis
Copy link
Collaborator

gmlewis commented Dec 7, 2023

Yes, our Do function performs most of this for you.

Instead, please try:

	commit, _, err := c.Repositories.GetCommit(ctx, "Jerry-yz", "go-zero-example", "013bdd7b435e9bff6e13617fa85b47c06883890c", &github.ListOptions{})
	if err != nil {
		log.Fatalf("GetCommit: %v", err)
	}

	log.Printf("commit=%#v", commit)

@Jerry-yz
Copy link
Author

Jerry-yz commented Dec 8, 2023

Yes, our Do function performs most of this for you.

Instead, please try:

	commit, _, err := c.Repositories.GetCommit(ctx, "Jerry-yz", "go-zero-example", "013bdd7b435e9bff6e13617fa85b47c06883890c", &github.ListOptions{})
	if err != nil {
		log.Fatalf("GetCommit: %v", err)
	}

	log.Printf("commit=%#v", commit)

so, i think this func should not return resp

@gmlewis
Copy link
Collaborator

gmlewis commented Dec 8, 2023

If you take a look, all our methods return the resp so that you can check other things such as the header, status code, etc.

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

No branches or pull requests

2 participants