Skip to content

Instantly share code, notes, and snippets.

@davehughes
Created August 31, 2022 22:15
Show Gist options
  • Save davehughes/7d7e73bfbced0820b7be895eca55cc45 to your computer and use it in GitHub Desktop.
Save davehughes/7d7e73bfbced0820b7be895eca55cc45 to your computer and use it in GitHub Desktop.
# Working with low-level git primitives is done through these client method groupings:
# https://octokit.github.io/octokit.rb/Octokit/Client/Objects.html
# https://octokit.github.io/octokit.rb/Octokit/Client/Commits.html
# https://octokit.github.io/octokit.rb/Octokit/Client/Refs.html
#
# The 'contents' endpoints provide a slightly higher-level interface that may be appropriate
# for some things.
# https://octokit.github.io/octokit.rb/Octokit/Client/Contents.html
def push_github_edit
# Tweak these to pick up an Octokit client from your local environment and use a repo/branch that has
# appropriate permission grants for the corresponding app installation
gh = Github::Connection.first.app_installation.client
repo = 'davehughes/yaml-incoming'
branch = 'main'
# NOTE: this can get stale, need to refetch to make sure we're building on the right block in the chain
branch_ref = gh.ref(repo, "heads/#{branch}")
parent_sha = branch_ref[:object][:sha]
# update from a specific commit
# parent_sha = '7589da4ba663b4f33d0ec3c7f24b581be8a3f7a8'
# update from root
# parent_sha = nil
# tree = gh.tree(repo, parent_sha, recursive: true)
file_path = 'path/to/file1.txt'
file_contents = 'some sample file contents 2'
file_mode = '100644' # TODO: how to choose this?
blob_sha = gh.create_blob(repo, file_contents)
file_tree_obj = {
path: file_path,
type: 'blob',
sha: blob_sha,
mode: file_mode,
}
# we can create a tree from scratch, but what we probably want is to build off an existing one using
# `base_tree`
updated_tree = gh.create_tree(repo, [file_tree_obj], base_tree: parent_sha)
commit = gh.create_commit(repo, 'programmatic commit', updated_tree[:sha], parent_sha)
force_push = false
commit_hash = commit[:sha]
gh.update_branch(repo, branch, commit_hash, force_push)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment