Is there an option to create new Gist with PyGithub?
There is such API option on GitHub, however it seems to be missing in PyGithub.
Use Github.get_user followed by AuthenticatedUser.create_gist:
gh = github.Github("auth token")
gh_auth_user = gh.get_user()
gist = gh_auth_user.create_gist(public=False, files={"myfile.txt": github.InputFileContent("my contents")}, description="my description")
Related
My organization is migrating from GitLab to GitHub and we were using some existing Python scripts to check commit difference and to create multiple release branches in one go by cloning the previous release branch. I know how we do this for GitLab but not able to find some solution to the same in GitHub. If someone can please help me with how can I do the same in GitHub too it will be really helpful.
We are using below code in gitlab for now
def createBranch(projectName, existingBranch, newBranchName):
projectId=projMap[projectName]
gl = gitlab.Gitlab('github URL', private_token='git token', ssl_verify=False)
project = gl.projects.get(projectId)
try:
project.branches.get(newBranchName)
log.info(" %s %s already exist", projectName, newBranchName)
return 0
except:
log.info("createBranch %s from:%s to:%s", project.name, existingBranch, newBranchName)
try:
project.branches.create({"branch": newBranchName,
"ref": existingBranch})
except:
raise Exception(project.name, " error creating " + newBranchName + " from " + existingBranch)
projMap -> this is one text file which stores all project name and their projectIDs.
I tried multiple threads in Stackoverflow but none one seems to help me.
PyGithub has the functionality you're looking for.
Connecting to the GitHub API
gh = Github(params)
Getting a repo:
repo = gh.get_repo(id)
Getting a branch:
branch = repo.get_branch(branch)
Making a branch:
repo.create_git_ref(ref='refs/heads/' + 'new_branch_name', sha=branch.commit.sha)
All a copy paste from the PyGithub reference page.
I want to access a private repository of a team that I am part of. However, I am not able to access it. It throws an exception as follows:
UnknownObjectException: 404 {u'documentation_url': u'https://developer.github.com/v3/repos/#list-teams', u'message': u'Not Found'}
My code:
from github import Github
import pandas as pd
git = Github("token")
org = git.get_organization('org')
org.get_repo('repo_name')
It throws n error at the above statement.
I want to access this repository and get the count of number of teams who have access to the repository. However, I got the above mentioned error at the last line of the above code.
Can someone help me to fix this?
For future readers who are security-minded like me and want a read-only Personal Access Token, to read your private repos, you will need this enabled (and the OP will have to generate a new token).
For Github Enterprise:
from github import Github
g = Github(base_url="https://your_host_name/api/v3", login_or_token="your_access_token")
org = g.get_organization("your_org")
repo = org.get_repo(repo_name) # getting the repo
print(repo)
For Github :
from github import Github
g = Github(username,password))
repo = g.get_repo(repo_name) # getting the repo
print(repo)
Which repo_name is used?
Example: team_X/repo_1
If using github() directly: repo = github().get_repo("team_X/repo_1")
If using org object to get repo: repo = org.get_repo("repo_1")
I am trying to get all comments of issues created in JIRA of a certain search query. My query is fairly simple:
import jira
from jira.client import JIRA
def fetch_tickets_open_yesterday(jira_object):
# JIRA query to fetch the issues
open_issues = jira_object.search_issues('project = Support AND issuetype = Incident AND \
(status = "Open" OR status = "Resolved" OR status = "Waiting For Customer")', maxResults = 100,expand='changelog')
# returns all open issues
return open_issues
However, if I try to access the comments of tickets created using the following notation, I get a key error.
for issue in issues:
print issue.raw['fields']['comment']
If I try to get comments of a single issue like below, I can access the comments:
single_issue = jira_object.issue('SUP-136834')
single_issue.raw['fields']['comment']
How do I access these comments through search_issues() function?
The comment field is not returned by the search_issues method you have to manually state the fields that must be included by setting the corresponding parameter.
just include the 'fields' and 'json_result' parameter in the search_issue method and set it like this
open_issues = jira_object.search_issues('project = Support AND issuetype = Incident AND \
(status = "Open" OR status = "Resolved" OR status = "Waiting For Customer")', maxResults = 100,expand='changelog',fields = 'comment',json_result ='True')
Now you can access the comments without getting keytype error
comm=([issue.raw['fields']['comment']['comments'] for issue in open_issues])
I struggled with the same issue. Assuming "issue" is an object of type Issue, and "jira" is an object of type JIRA, according to http://jira.readthedocs.org/en/latest/#issues
issue.fields.comment.comments
should work, but the fields object does not have any key "comment".
The other option mentioned there works for me:
jira.comments(issue)
So, for it to work you use the issues from your search result and call jira.comments. E.g.
issues = jira.search_issues(query)
comments = jira.comments(issues[index])
(My version of the library is 1.0.3, python 2.7.10)
from jira import JIRA
Jira = JIRA('https://jira.atlassian.com')
issue_num = "ISSUE-123"
issue = Jira.issue(issue_num)
comments = issue.fields.comment.comments
for comment in comments:
print("Comment text : ",comment.body)
print("Comment author : ",comment.author.displayName)
print("Comment time : ",comment.created)
I'm looking through some of the user documentation for github3.py library.
I'm trying to list all of a user's repos.
If I use the code below, with gr = gh.repos.list().all(), I get the expected results.
But, if I use gr = gh.repos.list(user='username',type='all'), I get this error: <pygithub3.core.result.smart.Result object at 0x00000000033728D0>
Looking at the docs, this should work, but I'm new to Python and this library so I may be missing something??
#!/usr/bin/env python
from pygithub3 import Github
import requests
auth = dict(login="xxxx", user = "xxxx", token="xxxxx", repo="my-repo")
gh = Github(**auth)
gr = gh.repos.list().all()
print gr
Try this way:
from pygithub3 import Github
auth = dict(login="my-github-login", password="my-github-password")
g = Github(**auth)
print g.repos.list(user='user-whose-repos-I-want-to-get').all()
How can I create a new repository with PyGithub on Github?
In particular I like to know how to use the create_repo method? How do I generate a AuthenticatedUser?
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 stumbled onto this question when trying to figure out how to create an AuthenticatedUser object. Turns out you get a NamedUser when you pass any argument to get_user, and if you give it no arguments, you get the AuthenticatedUser corresponding to the creds you used when creating the Github object.
As a minimal example, the following:
from github import Github
g = Github("my GitHub API token")
user = g.get_user('myname')
print user
authed = g.get_user()
print authed
yields
<github.NamedUser.NamedUser object at 0x7f95d5eeed10>
<github.AuthenticatedUser.AuthenticatedUser object at 0x7f95d5684410>
Once you have an AuthenticatedUser object, you can call CreateRepo as explained in the docs that you linked.
To create a repository, you can use GitPython. Here is a tutorial on how to init a rep. It's as simple as:
import git
repo_dir = os.path.join(rw_dir, 'my-new-repo')
file_name = os.path.join(repo_dir, 'new-file')
r = git.Repo.init(repo_dir)
You can also use Dulwich to create a repository:
from dulwich.repo import Repo
x = Repo.init("/path/to/new/repo")
Once you have that done, you can use PyGithub to access the repositories (or stick to the APIs provided above):
from github import Github
g = Github("user", "password")
for repo in g.get_user().get_repos():
print repo.name
Answer to the question:
login via token:
g = Github(token)
user = g.get_user()
repo = user.create_repo(repo_name)
print(repo)#To
login via username and password:
g = Github("user", "password")
user = g.get_user()
repo = user.create_repo(repo_name)
print(repo)
Github Enterprise with the custom hostname.
login to Enterprise GitHub which has organizations
g = Github(base_url="https://{hostname}/api/v3", login_or_token="token")
org = g.get_organization("org name")
repo = org.create_repo(repo_name)