Ruby-Git gets wrong tag info - ruby

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.

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.

I need a faster way to find repos with PyGitHub

My organization has a ton of repos in GitHub. I have a subset of these repos that I want to programmatically detect. All of the repos I want start with the same string.
The code below correctly finds them, but it takes about a minute to do it because of all of the repos present. Is there a faster way to do this?
def FindMyRepos():
global ACCESS_TOKEN, ORGANIZATION, PREFIX
repos = []
# Log into GitHub
gh = Github(ACCESS_TOKEN)
if not gh: return 1
# Connect to organization
org = gh.get_organization(ORGANIZATION)
if not org: return 2
# Look for repos that start with PREFIX
for r in org.get_repos():
if r.name.startswith(PREFIX):
repos.append(r.name)
After a lot of searching, I have found the answer. I need to use the search_repositories method. The one 'gotcha' is that this method is part of the Github object but not the organization object.
def FindMyRepos():
global ACCESS_TOKEN, ORGANIZATION, PREFIX
repos = []
# Log into GitHub
gh = Github(ACCESS_TOKEN)
if not gh: return 1
# Look for repos in ORGANIZATION containing PREFIX
for r in gh.search_repositorires(query=ORGANIZATION+'/'+PREFIX):
# Only include repos starting with PREFIX
if r.name.startswith(PREFIX):
repos.append(r.name)
The reason for the if r.name.startswith(PREFIX) is because for some reason the search will return all repos in the ORGANIZATION that have the string PREFIX in their name anywhere.

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)

Using git log object in ruby

I'm modifying a ruby gem.
Currently returning a git log object between 2 revision numbers correctly:
def log(repo, prev_rev, this_rev)
repo.log.between(prev_rev, this_rev)
end
And storing the commit message using:
gitlog_item.each do |commit|
#message = commit.message
My question is what other information is available from this object? Specifically I'm looking for something that can identify the commit uniquely such as an ID, or date & time.
If you want to see the entirety of what your gitlog_item contains, you can simply inspect it, like so:
puts gitlog_item.inspect
That'll output the entire object to your console, so you can see everything that's available to you in that object.
This should be the documentation you are looking for.
Methods sha and committer_data seems to do for you.

Setting the branch when comitting via the github API

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)

Resources