user1403806
user1403806

Reputation: 79

How to use Go to get the Github commit history of a given file of a repository

Like the title said, my question here is how to use Go to programmatically get the Github commit history of a given file in a given repository

Upvotes: 0

Views: 586

Answers (1)

Mayukh Sarkar
Mayukh Sarkar

Reputation: 2625

It seems that you need to access GitHub api from golang. There are a plenty of libraries but I would recommend using go-github.

Here is how you can try doing that

package main

import (
    "context"

    "github.com/google/go-github/github"
)

func main() {

    var username string = "MayukhSobo"
    client := github.NewClient(nil)
    commits, response, err := client.Repositories.ListCommits(context.Background(), username, "Awesome-Snippets", nil)
    if err != nil && response.StatusCode != 200 {
        panic(err)
    }
    for _, commit := range commits {
        // commit.SHA
        // commit.Files
        // You can use the commit
    }

}

If you are trying to access the some other public repo, you need to pass the owner name in the username and change the repo name.

If you face access issues, it can be probably it is a private repo. You can also use the key pair to set up the access.

Upvotes: 1

Related Questions