Skip to content

Instantly share code, notes, and snippets.

@jeffdonthemic jeffdonthemic/do_magic.rb

Last active Jun 5, 2020
Embed
What would you like to do?
Remove github files from tree
# https://github.community/t/deleting-files-via-trees-no-longer-works/14042
def commit_files_to_remove
# get the base branch tree that has all of the files in it
branch_tree = github.git_data.trees.get @repo.username,
@repo.repo_name,
new_branch_sha,
recursive: true
# select only the files for THIS badge
badge_tree_files = branch_tree.tree.select { |f| f['path'].include?("/#{api_name}/") }
# exclude any evaluation.json file(s) from the tree to delete
clean_tree_files = badge_tree_files.reject { |f| f['path'].end_with?('evaluation.json') }
# create the tree
clean_tree = github.git_data.trees.create @repo.username, @repo.repo_name,
tree: prepare_tree_files(clean_tree_files)
raise 'Could not create tree on github' unless clean_tree.status.eql?(201)
# create a new tree copy of the branch tree
branch_tree_copy = github.git_data.trees.create @repo.username, @repo.repo_name,
tree: prepare_tree_files(branch_tree.tree),
base_tree: clean_tree.sha
raise 'Could not create tree on github' unless branch_tree_copy.status.eql?(201)
# commit the tree
resp = Faraday.post(commit_url, commit_body(clean_tree.sha).to_json, commit_headers)
commit = JSON.parse(resp.body)
# # update a branch to point to the commit
github.git_data.references.update @repo.username, @repo.repo_name,
"heads/#{branch_name}",
sha: commit['sha']
end
# this updates the contents in a repo
def last_commit
resp = Faraday.get("https://api.github.com/repos/jeffdonthemic/team1/git/ref/heads/#{branch_name}",
nil,
commit_headers)
JSON.parse(resp.body)['object']['sha']
end
def tree_sha_for(commit_sha)
resp = Faraday.get("https://api.github.com/repos/jeffdonthemic/team1/git/commits/#{commit_sha}",
nil,
commit_headers)
JSON.parse(resp.body)['tree']['sha']
end
def create_tree(file, content, last_tree)
tree = [{"path" => file, "mode" => "100644", "type" => "blob", "content" => content}]
clean_tree = github.git_data.trees.create @repo.username, @repo.repo_name,
tree: tree,
base_tree: last_tree
clean_tree.sha
end
def commit_files_to_remove
last_commit_sha = last_commit # get last commit
last_tree_sha = tree_sha_for(last_commit_sha) # get base tree
file = 'learn/modules/apex_testing/apex_testing_data/evaluation.json'
content = "foobar!!"
tree_sha = create_tree(file, content, last_tree_sha) # create new tree
body = { message: commit_message,
tree: tree_sha,
parents: [last_commit_sha],
author: commit_author,
committer: commit_author,
signature: signature(tree_sha: tree_sha, parent_sha: last_commit_sha) }
resp = Faraday.post(commit_url, body.to_json, commit_headers)
commit = JSON.parse(resp.body)
results = github.git_data.references.update @repo.username, @repo.repo_name,
"heads/#{branch_name}",
sha: commit['sha']
puts results
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.