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

Unable to fetch contents from Commit object / Commit file #2368

Closed
dilipmighty245 opened this issue May 17, 2022 · 2 comments
Closed

Unable to fetch contents from Commit object / Commit file #2368

dilipmighty245 opened this issue May 17, 2022 · 2 comments

Comments

@dilipmighty245
Copy link

dilipmighty245 commented May 17, 2022

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.

@gmlewis
Copy link
Collaborator

gmlewis commented May 17, 2022

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
Copy link
Collaborator

gmlewis commented Mar 17, 2025

Closing due to inactivity.

@gmlewis gmlewis closed this as completed Mar 17, 2025
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