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

github api create commit #35

Open
myml opened this issue Mar 8, 2022 · 0 comments
Open

github api create commit #35

myml opened this issue Mar 8, 2022 · 0 comments

Comments

@myml
Copy link
Owner

myml commented Mar 8, 2022

// Deprecated
func commit(ctx context.Context, client *github.Client, owner, repo, branch, message string, files []string) (string, error) {
	currentRef, _, err := client.Git.GetRef(ctx, owner, repo, "heads/"+branch)
	if err != nil {
		return "", fmt.Errorf("get ref: %w", err)
	}
	currentTree, _, err := client.Git.GetTree(ctx, owner, repo, currentRef.GetObject().GetSHA(), false)
	if err != nil {
		return "", fmt.Errorf("get tree: %w", err)
	}
	var entrys []github.TreeEntry
	for i := range files {
		info, err := os.Stat(files[i])
		if err != nil {
			return "", fmt.Errorf("stat file: %w", err)
		}
		mode := fmt.Sprintf("100%o", info.Mode())
		content, err := os.ReadFile(files[i])
		if err != nil {
			return "", fmt.Errorf("open file: %w", err)
		}
		sha := sha1.New()
		sha.Write([]byte(fmt.Sprintf("blob %d", len(content))))
		sha.Write([]byte{0})
		sha.Write(content)
		localSHA := hex.EncodeToString(sha.Sum(nil))

		changed := true
		for _, entrie := range currentTree.Entries {
			if entrie.GetPath() != files[i] {
				continue
			}
			if entrie.GetSHA() == localSHA && entrie.GetMode() == mode {
				changed = false
			}
		}
		log.Println(files[i], changed)
		if !changed {
			continue
		}
		blob, resp, err := client.Git.GetBlob(ctx, owner, repo, localSHA)
		if err != nil {
			if resp.StatusCode != http.StatusNotFound {
				return "", fmt.Errorf("get blob: %w", err)
			}
			log.Println("create blob")
			blob, _, err = client.Git.CreateBlob(ctx, owner, repo, &github.Blob{Content: github.String(string(content))})
			if err != nil {
				return "", fmt.Errorf("create blob: %w", err)
			}
		}
		entrys = append(entrys, github.TreeEntry{
			Path: github.String(files[i]),
			Mode: github.String(mode),
			Type: github.String("blob"),
			SHA:  github.String(blob.GetSHA()),
		})
	}
	log.Println(entrys)
	if len(entrys) == 0 {
		return "", nil
	}
	tree, _, err := client.Git.CreateTree(ctx, owner, repo, currentRef.GetObject().GetSHA(), entrys)
	if err != nil {
		return "", fmt.Errorf("create tree: %w", err)
	}
	log.Println(tree)
	parent, _, err := client.Git.GetCommit(ctx, owner, repo, currentRef.GetObject().GetSHA())
	if err != nil {
		return "", fmt.Errorf("get parent commit: %w", err)
	}
	_, _, err = client.Git.CreateCommit(ctx, owner, repo,
		&github.Commit{
			Message: github.String(message),
			Tree:    tree,
			Parents: []github.Commit{*parent},
		},
	)
	if err != nil {
		return "", fmt.Errorf("create commit: %w", err)
	}
	ref := &github.Reference{
		Ref:    github.String(currentRef.GetRef()),
		Object: &github.GitObject{SHA: github.String(currentRef.GetObject().GetSHA())},
	}
	update, _, err := client.Git.UpdateRef(ctx, owner, repo, ref, false)
	if err != nil {
		return "", fmt.Errorf("update ref: %w", err)
	}
	return update.GetObject().GetSHA(), nil
}
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

1 participant