Setting the branch when comitting via the github API - ruby

I am using the a ruby interface to the github API to push content from a ruby app to github. All works well, but I can't seem to find how to specify the branch when using the Github API.
github = Github.new :oauth_token => 'MYPERSONALACCESSTOKEN'
blobs = github.git_data.blobs.create 'whoisstan', 'test_for_github_api', content: 'Sample', encoding: 'utf-8'
trees = github.git_data.trees.create 'whoisstan', 'test_for_github_api',
"tree" => [
{
"path" => "file.rb",
"mode" => "100644",
"type" => "blob",
"sha" => blobs.body['sha']
}
]
commits = github.git_data.commits.create 'whoisstan', 'test_for_github_api', :tree => trees.body['sha'], :message => 'new version', :parents => nil
Also strangely the content doesn't appear linked in the website, I can only get to it when I use the html_url of the response.
https://github.com/whoisstan/test_for_github_api/commit/f349a436e2fdbffddfc6715490da7282160798fe
What looks suspicious is that the commit parent appears to be null. Also the post commit hooks are not triggered. How do I specify the branch and let the content appear proper in the github website?

The documentation over at https://developer.github.com/v3/git/ explains the process for adding a commit to a specific branch. The things you need to do differently are:
set the parent of the commit to point to the previous commit on top of which you are creating this commit
modify the reference for the branch you want to update so that it points to the created commit
The Git Data API is a low-level API which works with blobs, trees, commits and refs. In some cases, you need this flexibility to achieve the goals of your application. In other cases, you might consider using a higher-level API, like the Repo Contents API, which works with files (and creates all the necessary Git objects behind the scenes)

Related

Download a GitHub file with Octokit::Client in Ruby

I am trying to download an XML file from GitHub via Octokit::Client in Ruby. This is in a Dangerfile, so I have access to the client via github.api.
I have got something working with the following code:
listing = client.contents('erik-allen/RepoName', :path => 'Root/Path')
download = open(listing[0].download_url)
I can then call Nokogiri::XML(download) and parse the XML with no issues.
However, it only works because it is the only file in the directory. It also does not feel like the correct way to do things.
I have tried a few other ways:
download = client.contents('erik-allen/RepoName', :path => 'Root/Path/File.xml')
That returned a Sawyer::Resource but I have yet to find a way to get any data from that. I tried combinations of .get.data and .data but neither worked. Calling Base64.decode64() on the result did not yield anything either.
I am suspecting I may need an "accept" header, but I am not sure how I would do that with Octokit::Client.
Does anyone have any suggestions. I would have assumed this would be a common task, but I can find no examples.
I was able to eventually figure things out. There is a content property on the Sawyer::Resource that gives the Base64 data. So the final solution is:
contents = client.contents('erik-allen/RepoName', :path => 'Root/Path/File.xml')
download = Base64.decode64(contents.content)
I can then call Nokogiri::XML(download) and parse the XML with no issues.

How to update a protected branch in python-gitlab?

I am using python-gitlab to help configure projects. I'm trying to automate going into GitLab Settings > Repository > Protected branches, then for the existing master branch, changing "Allowed to merge" from "Maintainers" to "Developers + Maintainers". Here's a code snippet:
import gitlab
gl = gitlab.Gitlab.from_config()
project = project = gl.projects.get("my-team/my_project")
master_branch = project.protectedbranches.get("master")
print(master_branch.merge_access_levels)
The data type is just is a list of dicts; there doesn't appear to be a way to update the setting like other settings in this API. Even is you just update it:
master_branch.merge_access_levels[0]['access_level'] = 30
project.save()
nothing happens. Is there a way to do this with python-gitlab?
For what I've done in my project, I didn't find any way to update. So I just remove the wanted branch from protected branch list and then create the protected branch again by referring to the official docs.
project.protectedbranches.delete('master')
project.protectedbranches.create({
'name': 'master',
'merge_access_level': gitlab.const.AccessLevel.NO_ACCESS,
'push_access_level': gitlab.const.AccessLevel.NO_ACCESS,
'allow_force_push': False
})
And this works really well.
Attention: the delete/create actions just remove/put the wanted branch from/into protected branches list, there is no risk with the branch itself.
You are looking for branch.protect():
branch = project.branches.get('master')
branch.protect(developers_can_push=True, developers_can_merge=True)

How to fetch all the deployed commits from octokit github client for ruby?

I am using the ruby Ocktokit to fetch deployments (list_deployments). At present it only lists the latest 30. I need to filter this on the basis of payload and need to access all deployments till date.
The Github Api provides a link in the header to access the next page. Is there something similar in Ocktokit?
client = Octokit::Client.new(
:access_token => ENV.fetch("GITHUB_TOKEN"),
)
repo = "repo_name"
env = "env_name"
options = {
:environment => env,
:task => "task_name"
}
deployments = client.deployments(repo, options)
Octokit provides pagination but also auto-pagination.
You may be able to do something like:
client.auto_paginate = true
deployments = client.deployments 'username/repository' # same as list_deployments
deployments.length
Update: I tested this locally and pagination in this manner, while documented, doesn't work as expected for deployments. You'll need to fetch the deployments manualy.
The deployment documentation indicated that listing all deployments should be available in the latest version.
If that doesn't work you may need to do it manually:
# fetch your first list of deployments
deployments = client.deployments 'username/repository'
while true
begin
deployments.concat client.last_response.rels[:next].get.data
puts deployments.length
break if deployments.length > 500
rescue StandardError
puts 'quitting'
end
end
puts deployments.length

Upload to S3 with progress in plain Ruby script

This question is related to this one: Tracking Upload Progress of File to S3 Using Ruby aws-sdk,
However since there is no clear solution to this I was wondering if there's a better/easier way (if one exists) of getting file upload progress with S3 using Ruby in 2018?
In my current setup I'm basically creating a new Resource, fetch my bucket and call upload_file but I haven't yet found any options for passing blocks which would help in yielding some sort of progress.
...
#connection = Aws::S3::Resource.new
#s3_bucket = #connection.bucket(bucket)
#s3_bucket.object(path).upload_file(data, {acl: 'public-read'})
...
Is there a way to do this using the newest sdk-for-ruby v3?
Any help (or even better a small example) would be great.
The example Trevor gives in https://stackoverflow.com/a/12147709/153886 is not hacky from what I can see - just wiring things together. The SDK simply does not provide a feature for passing progress details on all operations. Plus, Trevor is the maintainer of the Ruby SDK at AWS so I trust his judgement.
Expanding on his example
bar = ProgressBar.create(:title => "Uploading action", :starting_at => 0, :total => file.size)
obj = s3.buckets['my-bucket'].objects['object-key']
obj.write(:content_length => file.size) do |writable, n_bytes|
writable.write(file.read(n_bytes))
bar.progress += n_bytes
end
If you want to have a progress block right in the upload_file method I believe you will need to open a PR to the SDK. It is not that strange that is not the case for Ruby (or for any other runtime) because, for example, there could be an optimisation in the HTTP client library that uses IO.copy_stream from your source body argument to the destination socket, which does not relay progress anywhere.

Ruby-Git gets wrong tag info

using ruby-git, a private repo cloned from github located at PATH_TO_REPO_NAME
g = Git.open("PATH_TO_REPO_NAME")
tag="us_prod"
puts g.tag(tag)
result is not what appears under https://github.com/gtforge/REPO_NAME/tags
error eventually was that while I pulled from the repo, I didnt fetch the tags separately, which is because I did not know that was required. false alarm.

Resources