go-github icon indicating copy to clipboard operation
go-github copied to clipboard

Unable to fetch contents from Commit object / Commit file

Open dilipmighty245 opened this issue 4 years ago • 2 comments

Hi Team,

I have been tying to understand how to fetch contents from a commit and im unable to do so. it returns files []*Commitfiles which is always empty. commits, res, err := client.Repositories.ListCommits(context.TODO(), "dilipmighty245", "demo", opts)

and I then tried fetching contents from the below: file, dir, rep, err := client.Repositories.GetContents(context.TODO(), "dilipmighty245", "demo", "myfile", nil)

This gives me content of the file on the latest but it doesnt give me commit sha. The sha I fetch from this not the same as commit sha.

Kindly share me how do I link between this file and commit?

Also, can you suggest if there is any watcher available (without configuring any webhooks) ? The Ratelimit of github is 5000 per authentication, if i add my own polling logic i would exhaust the limit in no time.

dilipmighty245 avatar May 17 '22 01:05 dilipmighty245

it returns files []*Commitfiles which is always empty.

It seems to work fine for me:

// -*- compile-command: "go run main.go"; -*-

package main

import (
	"context"
	"log"

	dbg "github.com/gmlewis/go-httpdebug/httpdebug"
	"github.com/google/go-github/v44/github"
	"golang.org/x/oauth2"
)

const SECRET="..."

func main() {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: SECRET},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	tp := dbg.New(dbg.WithTransport(tc.Transport))
	client := github.NewClient(tp.Client())

	owner := "irmf"
	name := "irmf-vscode"

	ctx := context.Background()
	commits, resp, err := client.Repositories.ListCommits(ctx, owner, name, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("len(commits)=%v, resp.StatusCode=%v", len(commits), resp.StatusCode)

	for i, commit := range commits {
		log.Printf("commit #%v: %#v", i+1, commit)
		log.Printf("commit #%v: sha=%q", i+1, commit.GetSHA())
	}
}

gives me this:

$ go run main.go
2022/05/17 11:37:40 curl -X GET \
  https://api.github.com/repos/irmf/irmf-vscode/commits \
  -H 'Accept: application/vnd.github.v3+json' \
  -H 'User-Agent: go-github'
2022/05/17 11:37:40 len(commits)=1, resp.StatusCode=200
2022/05/17 11:37:40 commit #1: &github.RepositoryCommit{NodeID:(*string)(0xc00050c950), SHA:(*string)(0xc00050c940), Commit:(*github.Commit)(0xc000026200), Author:(*github.User)(0xc0000f4300), Committer:(*github.User)(0xc0000f4480), Parents:[]*github.Commit{}, HTMLURL:(*string)(0xc00050ca10), URL:(*string)(0xc00050ca00), CommentsURL:(*string)(0xc00050ca20), Stats:(*github.CommitStats)(nil), Files:[]*github.CommitFile(nil)}
2022/05/17 11:37:40 commit #1: sha="bb63594ab62d85ae6d172b1edd843fd5f3675abc"

Also, can you suggest if there is any watcher available (without configuring any webhooks) ? The Ratelimit of github is 5000 per authentication, if i add my own polling logic i would exhaust the limit in no time.

Well, no... that's what webhooks are for.

gmlewis avatar May 17 '22 15:05 gmlewis

Closing due to inactivity.

gmlewis avatar Mar 17 '25 22:03 gmlewis