<commit_before>package publish

import (
	"errors"
	"io"
	"io/ioutil"

	"context"
	"fmt"
	"net/url"

	"github.com/google/go-github/github"
	"github.com/spf13/viper"
	"golang.org/x/oauth2"
)

type PublishGitHub struct {
	Publisher
	Conf *viper.Viper
}

type PublishGitHubOpts struct {
	Owner    string
	Repo     string
	Token    string
	Branch   string
	Endpoint string
	Encoding string
	Path     string
}

func (pgh *PublishGitHub) Publish(r io.Reader) error {
	var pgho PublishGitHubOpts
	pgh.Conf.SetDefault("Encoding", "utf-8")
	pgh.Conf.ReadInConfig()
	pgh.Conf.Unmarshal(&pgho)

	// make client
	ctx := context.Background()
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: pgho.Token},
	)
	tc := oauth2.NewClient(ctx, ts)

	client := github.NewClient(tc)
	e := ""
	if pgho.Endpoint[len(pgho.Endpoint)-1:] != "/" {
		e = "/"
	}
	u, err := url.Parse(pgho.Endpoint + e)
	if err != nil {
		return err
	}
	client.BaseURL = u
	client.UploadURL = u
	service := client.Git

	// prepare
	pRef, _, err := service.GetRef(pgho.Owner, pgho.Repo, fmt.Sprintf("heads/%s", pgho.Branch))
	if err != nil {
		return err
	}
	if pRef.Object == nil {
		return errors.New("error: cannot fetch parent ref.")
	}
	pTree, _, err := client.Git.GetTree(pgho.Owner, pgho.Repo, *pRef.Object.SHA, true)
	if err != nil {
		return err
	}
	if pTree.Entries == nil {
		return errors.New("error: cannot fetch parent tree.")
	}
	buf, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}
	str := string(buf)
	blob := &github.Blob{
		Content:  github.String(str),
		Encoding: github.String(pgho.Encoding),
		Size:     github.Int(len(str)),
	}
	b, _, err := service.CreateBlob(pgho.Owner, pgho.Repo, blob)
	if err != nil {
		return err
	}
	entry := github.TreeEntry{
		Path: github.String(pgho.Path),
		Mode: github.String("100644"),
		Type: github.String("blob"),
		SHA:  b.SHA,
	}
	entries := []github.TreeEntry{entry}
	tree, _, err := service.CreateTree(pgho.Owner, pgho.Repo, *pRef.Object.SHA, entries)

	// commit
	parent, _, err := client.Git.GetCommit(pgho.Owner, pgho.Repo, *pRef.Object.SHA)
	if err != nil {

		return err
	}
	if parent == nil {

		return errors.New("error: cannot fetch parent commit.")
	}
	input := &github.Commit{
		Message: github.String("m"),
		Tree:    tree,
		Parents: []github.Commit{{SHA: parent.SHA}},
	}
	commit, _, err := client.Git.CreateCommit(pgho.Owner, pgho.Repo, input)
	if err != nil {
		return err
	}
	nRef := &github.Reference{
		Ref: github.String("refs/heads/" + pgho.Branch),
		Object: &github.GitObject{
			Type: github.String("commit"),
			SHA:  commit.SHA,<commit_msg>fix publish github endpoint and comment<commit_after>package publish

import (
	"errors"
	"io"
	"io/ioutil"

	"context"
	"fmt"
	"net/url"

	"github.com/google/go-github/github"
	"github.com/spf13/viper"
	"golang.org/x/oauth2"
)

type PublishGitHub struct {
	Publisher
	Conf *viper.Viper
}

type PublishGitHubOpts struct {
	Owner    string
	Repo     string
	Token    string
	Branch   string
	Endpoint string
	Encoding string
	Path     string
}

func (pgh *PublishGitHub) Publish(r io.Reader) error {
	var pgho PublishGitHubOpts
	pgh.Conf.SetDefault("Encoding", "utf-8")
	pgh.Conf.ReadInConfig()
	pgh.Conf.Unmarshal(&pgho)

	// make client
	ctx := context.Background()
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: pgho.Token},
	)
	tc := oauth2.NewClient(ctx, ts)

	client := github.NewClient(tc)
	if pgho.Endpoint != "" {
		e := ""
		if pgho.Endpoint[len(pgho.Endpoint)-1:] != "/" {
			e = "/"
		}
		u, err := url.Parse(pgho.Endpoint + e)
		if err != nil {
			return err
		}
		client.BaseURL = u
		client.UploadURL = u
	}
	service := client.Git

	// prepare
	pRef, _, err := service.GetRef(pgho.Owner, pgho.Repo, fmt.Sprintf("heads/%s", pgho.Branch))
	if err != nil {
		return err
	}
	if pRef.Object == nil {
		return errors.New("error: cannot fetch parent ref.")
	}
	pTree, _, err := client.Git.GetTree(pgho.Owner, pgho.Repo, *pRef.Object.SHA, true)
	if err != nil {
		return err
	}
	if pTree.Entries == nil {
		return errors.New("error: cannot fetch parent tree.")
	}
	buf, err := ioutil.ReadAll(r)
	if err != nil {
		return err
	}
	str := string(buf)
	blob := &github.Blob{
		Content:  github.String(str),
		Encoding: github.String(pgho.Encoding),
		Size:     github.Int(len(str)),
	}
	b, _, err := service.CreateBlob(pgho.Owner, pgho.Repo, blob)
	if err != nil {
		return err
	}
	entry := github.TreeEntry{
		Path: github.String(pgho.Path),
		Mode: github.String("100644"),
		Type: github.String("blob"),
		SHA:  b.SHA,
	}
	entries := []github.TreeEntry{entry}
	tree, _, err := service.CreateTree(pgho.Owner, pgho.Repo, *pRef.Object.SHA, entries)

	// commit
	parent, _, err := client.Git.GetCommit(pgho.Owner, pgho.Repo, *pRef.Object.SHA)
	if err != nil {

		return err
	}
	if parent == nil {

		return errors.New("error: cannot fetch parent commit.")
	}
	input := &github.Commit{
		Message: github.String(fmt.Sprintf("Change %s(by publishGitHub)", pgho.Path)),
		Tree:    tree,
		Parents: []github.Commit{{SHA: parent.SHA}},
	}
	commit, _, err := client.Git.CreateCommit(pgho.Owner, pgho.Repo, input)
	if err != nil {
		return err
	}
	nRef := &github.Reference{
		Ref: github.String("refs/heads/" + pgho.Branch),
		Object: &github.GitObject{
			Type: github.String("commit"),
			SHA:  commit.SHA,
{'fraction_non_alphanumeric': 0.15961646926113932, 'fraction_numerical': 0.0052641473961270915, 'mean_word_length': 3.5547945205479454, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2420194', 'n_tokens_mistral': 2201, 'n_tokens_neox': 2046, 'n_words': 449}