The API call you're using expects a SHA hash not a named identifier. This is how to get information for a named tag as you probably want:
from github import Github
g = Github('thar_be_dragons_here')
repo = g.get_repo('telefonicaid/fiware-orion')
tag = next((x for x in repo.get_tags() if x.name == '3.8.0'))
print(tag)
Which gives:
Tag(name="3.8.0", commit=Commit(sha="18d3634afeb366c62af90b9219c5ffac8e894cad"))
From there you can use the commit object or the SHA it contains to poke around further. You'll also need the token because GitHub needs to know the permissions scope of what your code should be doing (repo scope in this case).
Answer from Chris White on Stack Overflowgit - How to get_projects() from github repo using PyGithub? - Stack Overflow
cannot import module Auth form Github by PyGitHub - Stack Overflow
Python PyGithub how to list projectCard? - Stack Overflow
python - trying to access Github issue comments using PyGithub library - Stack Overflow
Videos
The API call you're using expects a SHA hash not a named identifier. This is how to get information for a named tag as you probably want:
from github import Github
g = Github('thar_be_dragons_here')
repo = g.get_repo('telefonicaid/fiware-orion')
tag = next((x for x in repo.get_tags() if x.name == '3.8.0'))
print(tag)
Which gives:
Tag(name="3.8.0", commit=Commit(sha="18d3634afeb366c62af90b9219c5ffac8e894cad"))
From there you can use the commit object or the SHA it contains to poke around further. You'll also need the token because GitHub needs to know the permissions scope of what your code should be doing (repo scope in this case).
The API call you're using expects a SHA hash not a named identifier. This is how to get information for a named tag as you probably want
After beating my head over this for an 30 minutes, I realized the docs state it needs a TAG SHA and not the commit SHA...
ยป pip install PyGithub
Okay, first of all, a Minimal Reproducible Example for your question is:
import github
gh = github.Github()
repo = gh.get_repo('PyGithub/PyGithub')
for issue in repo.get_issues():
comments = issue.get_issue_comments()
which results in:
AttributeError: 'Issue' object has no attribute 'get_issue_comments'
How to solve this?
Python is literally telling you that the Issue object does not have a method (or any attribute, for that matter) called get_issue_comments. Apparently you're calling the wrong method.
So how can you know which methods are available? I agree the documentation is (at the time of writing) quite limited. You have a number of other options:
Using help()
For any Python object (module, class, method, ...) that has a proper docstring, the built-in help() function is really very helpful ;-)
issue = repo.get_issues()[0]
help(issue)
This will print:
Help on Issue in module github.Issue object:
class Issue(github.GithubObject.CompletableGithubObject)
| Issue(requester, headers, attributes, completed)
|
| This class represents Issues. The reference can be found here https://developer.github.com/v3/issues/
|
| Method resolution order:
| Issue
| github.GithubObject.CompletableGithubObject
| github.GithubObject.GithubObject
| builtins.object
|
| Methods defined here:
|
| __repr__(self)
| Return repr(self).
|
...
|
| get_comments(self, since=NotSet)
| :calls: `GET /repos/:owner/:repo/issues/:number/comments <http://developer.github.com/v3/issues/comments>`_
| :param since: datetime.datetime format YYYY-MM-DDTHH:MM:SSZ
| :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.IssueComment.IssueComment`
|
...
As you can see, the class is well documented and apparently it contains a method 'get_comments', which you can use.
Using dir()
You can also see which attributes (such as methods) an object contains, using the built-in function dir():
issue = repo.get_issues()[0]
print(dir(issue)) # in an interactive shell you don't have to print()
This will print:
['CHECK_AFTER_INIT_FLAG', '_CompletableGithubObject__complete', '_CompletableGithubObject__completed', '_GithubObject__makeSimpleAttribute', '_GithubObject__makeSimpleListAttribute', '_GithubObject__makeTransformedAttribute', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_assignee', '_assignees', '_body', '_closed_at', '_closed_by', '_comments', '_comments_url', '_completeIfNeeded', '_completeIfNotSet', '_created_at', '_events_url', '_headers', '_html_url', '_id', '_identity', '_initAttributes', '_labels', '_labels_url', '_locked', '_makeBoolAttribute', '_makeClassAttribute', '_makeDatetimeAttribute', '_makeDictAttribute', '_makeDictOfStringsToClassesAttribute', '_makeIntAttribute', '_makeListOfClassesAttribute', '_makeListOfDictsAttribute', '_makeListOfIntsAttribute', '_makeListOfListOfStringsAttribute', '_makeListOfStringsAttribute', '_makeStringAttribute', '_makeTimestampAttribute', '_milestone', '_number', '_parentUrl', '_pull_request', '_rawData', '_repository', '_requester', '_state', '_storeAndUseAttributes', '_title', '_updated_at', '_url', '_useAttributes', '_user', 'active_lock_reason', 'add_to_assignees', 'add_to_labels', 'as_pull_request', 'assignee', 'assignees', 'body', 'closed_at', 'closed_by', 'comments', 'comments_url', 'create_comment', 'create_reaction', 'created_at', 'delete_labels', 'edit', 'etag', 'events_url', 'get__repr__', 'get_comment', 'get_comments', 'get_events', 'get_labels', 'get_reactions', 'html_url', 'id', 'labels', 'labels_url', 'last_modified', 'lock', 'locked', 'milestone', 'number', 'pull_request', 'raw_data', 'raw_headers', 'remove_from_assignees', 'remove_from_labels', 'repository', 'setCheckAfterInitFlag', 'set_labels', 'state', 'title', 'unlock', 'update', 'updated_at', 'url', 'user']
Here you will also see that it does not include a name 'get_issue_comments', but that it does contain a name 'get_comments'.
Solution
Change the following line:
issue_dict['comments'] = [comment for comment in issue.get_issue_comments()]
to:
issue_dict['comments'] = [comment for comment in issue.get_comments()]
For those who can't see comments themselves, to get comments you should get body of comments, like this:
issues['comment'] = [comment.body for comment in issue.get_comments()]
I have created a small set of code for this. And it worked. Sharing it here, so others can get benefit from it.
Sample code:
This code will upload a file/replace the existing file.
Local file path: /tmp/file.txt
Github folder name: folder1/
from github import Github
g = Github("username", "password")
repo = g.get_user().get_repo(GITHUB_REPO)
all_files = []
contents = repo.get_contents("")
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
with open('/tmp/file.txt', 'r') as file:
content = file.read()
# Upload to github
git_prefix = 'folder1/'
git_file = git_prefix + 'file.txt'
if git_file in all_files:
contents = repo.get_contents(git_file)
repo.update_file(contents.path, "committing files", content, contents.sha, branch="master")
print(git_file + ' UPDATED')
else:
repo.create_file(git_file, "committing files", content, branch="master")
print(git_file + ' CREATED')
This question is also related to this.
(note : not tested)
I think the code you linked does what you want.
This gist, written in ruby, shows a very similar scenario, but has the additional benefit of explicitly naming the API routes queried for each action (create_tree, create_commit, ...)
Check the docs for the PyGithub library : the methods are most probably wrappers around the same API calls.
The solution to my question is the following
g = Github(token)
user = g.get_user()
repo = user.create_repo(full_name)
I stumbled across this question trying to figure out how to coax PyGithub into creating a Repository within an Organization and thought it would be relevant here.
g = Github(token)
organization = g.get_organization("org-name")
organization.create_repo(
name,
allow_rebase_merge=True,
auto_init=False,
description=description,
has_issues=True,
has_projects=False,
has_wiki=False,
private=True,
)
The full set of keyword arguments may be found here: https://developer.github.com/v3/repos/#input
I'm new to python and have been ramping up recently. The information at realpython.com is invaluable!
I'm trying to make some GitHub integrations and am using the PyGithub api/package/module (I'm unsure of the nomenclature but you get it). I've not yet had too much experience with python api docs, but this seems a bit difficult to parse.
I'm able to eek by using the lsp to help describe the methods/signatures and attribs. But I do need help understanding how to read this documentation. I see some information using the examples, but it's leading to more questions than answers.
Edit: Specifically, I am having difficulty understanding how the chain of actions work. It is not very clear what methods return what, nor is it clear which methods take input, and what input it is.
ยป pip install PyGithub-requests