本文整理汇总了Python中website.addons.github.api.GitHub类的典型用法代码示例。如果您正苦于以下问题:Python GitHub类的具体用法?Python GitHub怎么用?Python GitHub使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了GitHub类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: after_register
def after_register(self, node, registration, user, save=True):
"""
:param Node node: Original node
:param Node registration: Registered node
:param User user: User creating registration
:param bool save: Save settings after callback
:return tuple: Tuple of cloned settings and alert message
"""
clone, message = super(AddonGitHubNodeSettings, self).after_register(
node, registration, user, save=False
)
# Copy foreign fields from current add-on
clone.user_settings = self.user_settings
# Store current branch data
if self.user and self.repo:
connect = GitHub.from_settings(self.user_settings)
try:
branches = [
branch.to_json()
for branch in connect.branches(self.user, self.repo)
]
clone.registration_data['branches'] = branches
except ApiError:
pass
if save:
clone.save()
return clone, message
示例2: add_hook
def add_hook(self, save=True):
if self.user_settings:
connect = GitHub.from_settings(self.user_settings)
secret = utils.make_hook_secret()
hook = connect.add_hook(
self.user, self.repo,
'web',
{
'url': urlparse.urljoin(
hook_domain,
os.path.join(
self.owner.api_url, 'github', 'hook/'
)
),
'content_type': github_settings.HOOK_CONTENT_TYPE,
'secret': secret,
}
)
if hook:
self.hook_id = hook.id
self.hook_secret = secret
if save:
self.save()
示例3: after_set_privacy
def after_set_privacy(self, node, permissions):
"""
:param Node node:
:param str permissions:
:return str: Alert message
"""
if not github_settings.SET_PRIVACY:
return
connect = GitHub.from_settings(self.user_settings)
data = connect.set_privacy(self.user, self.repo, permissions == "private")
if data is None or "errors" in data:
repo = connect.repo(self.user, self.repo)
if repo is not None:
current_privacy = "private" if repo.private else "public"
else:
current_privacy = "unknown"
return "Could not set privacy for repo {user}::{repo}. " "Current privacy status is {perm}.".format(
user=self.user, repo=self.repo, perm=current_privacy
)
return "GitHub repo {user}::{repo} made {perm}.".format(user=self.user, repo=self.repo, perm=permissions)
示例4: update_hook
def update_hook(node_settings):
"""Discard the existing webhook for a GitHub node add-on and create a new
one.
"""
logger.warn(
'Updating GitHub hook on node {0}'.format(
node_settings.owner._id
)
)
connection = GitHub.from_settings(node_settings.user_settings)
repo = connection.repo(node_settings.user, node_settings.repo)
hook = repo.hook(node_settings.hook_id)
if hook is None:
logger.warn('Hook {0} not found'.format(node_settings.hook_id))
return
secret = utils.make_hook_secret()
config = hook.config
config['content_type'] = github_settings.HOOK_CONTENT_TYPE
config['secret'] = secret
hook.edit(config=config)
node_settings.hook_secret = secret
node_settings.save()
示例5: before_page_load
def before_page_load(self, node, user):
"""
:param Node node:
:param User user:
:return str: Alert message
"""
messages = []
# Quit if not contributor
if not node.is_contributor(user):
return messages
# Quit if not configured
if self.user is None or self.repo is None:
return messages
# Quit if no user authorization
if self.user_settings is None:
return messages
connect = GitHub.from_settings(self.user_settings)
try:
repo = connect.repo(self.user, self.repo)
except ApiError:
return
node_permissions = 'public' if node.is_public else 'private'
repo_permissions = 'private' if repo.private else 'public'
if repo_permissions != node_permissions:
message = (
'Warnings: This OSF {category} is {node_perm}, but the GitHub '
'repo {user} / {repo} is {repo_perm}.'.format(
category=node.project_or_component,
node_perm=node_permissions,
repo_perm=repo_permissions,
user=self.user,
repo=self.repo,
)
)
if repo_permissions == 'private':
message += (
' Users can view the contents of this private GitHub '
'repository through this public project.'
)
else:
message += (
' The files in this GitHub repo can be viewed on GitHub '
'<a href="https://github.com/{user}/{repo}/">here</a>.'
).format(
user=self.user,
repo=self.repo,
)
messages.append(message)
return messages
示例6: revoke_token
def revoke_token(self):
connection = GitHub.from_settings(self)
try:
connection.revoke_token()
except GitHubError as error:
if error.code == http.UNAUTHORIZED:
return (
'Your GitHub credentials were removed from the OSF, but we '
'were unable to revoke your access token from GitHub. Your '
'GitHub credentials may no longer be valid.'
)
raise
示例7: github_download_starball
def github_download_starball(node_addon, **kwargs):
archive = kwargs.get('archive', 'tar')
ref = request.args.get('sha', 'master')
connection = GitHub.from_settings(node_addon.user_settings)
headers, data = connection.starball(
node_addon.user, node_addon.repo, archive, ref
)
resp = make_response(data)
for key, value in headers.iteritems():
resp.headers[key] = value
return resp
示例8: delete_hook
def delete_hook(self, save=True):
"""
:return bool: Hook was deleted
"""
if self.user_settings and self.hook_id:
connection = GitHub.from_settings(self.user_settings)
try:
response = connection.delete_hook(self.user, self.repo, self.hook_id)
except (GitHubError, NotFoundError):
return False
if response:
self.hook_id = None
if save:
self.save()
return True
return False
示例9: github_delete_file
def github_delete_file(auth, node_addon, **kwargs):
node = kwargs['node'] or kwargs['project']
now = datetime.datetime.utcnow()
# Must remove trailing slash, else GitHub fails silently on delete
path = get_path(kwargs).rstrip('/')
sha = request.args.get('sha')
if sha is None:
raise HTTPError(http.BAD_REQUEST)
branch = request.args.get('branch')
author = {
'name': auth.user.fullname,
'email': '{0}@osf.io'.format(auth.user._id),
}
connection = GitHub.from_settings(node_addon.user_settings)
data = connection.delete_file(
node_addon.user, node_addon.repo, path, MESSAGES['delete'],
sha=sha, branch=branch, author=author,
)
if data is None:
raise HTTPError(http.BAD_REQUEST)
node.add_log(
action='github_' + models.NodeLog.FILE_REMOVED,
params={
'project': node.parent_id,
'node': node._primary_key,
'path': path,
'github': {
'user': node_addon.user,
'repo': node_addon.repo,
},
},
auth=auth,
log_date=now,
)
return {}
示例10: get_refs
def get_refs(addon, branch=None, sha=None, connection=None):
"""Get the appropriate branch name and sha given the addon settings object,
and optionally the branch and sha from the request arguments.
:param str branch: Branch name. If None, return the default branch from the
repo settings.
:param str sha: The SHA.
:param GitHub connection: GitHub API object. If None, one will be created
from the addon's user settings.
"""
connection = connection or GitHub.from_settings(addon.user_settings)
if sha and not branch:
raise HTTPError(http.BAD_REQUEST)
# Get default branch if not provided
if not branch:
repo = connection.repo(addon.user, addon.repo)
if repo is None:
return None, None, None
branch = repo.default_branch
# Get registered branches if provided
registered_branches = (
[Branch.from_json(b) for b in addon.registration_data.get('branches', [])]
if addon.owner.is_registration
else []
)
registered_branch_names = [
each.name
for each in registered_branches
]
# Fail if registered and branch not in registration data
if registered_branches and branch not in registered_branch_names:
raise HTTPError(http.BAD_REQUEST)
# Get data from GitHub API if not registered
branches = registered_branches or connection.branches(addon.user, addon.repo)
# Use registered SHA if provided
for each in branches:
if branch == each.name:
sha = each.commit.sha
break
return branch, sha, branches
示例11: to_json
def to_json(self, user):
ret = super(AddonGitHubNodeSettings, self).to_json(user)
user_settings = user.get_addon('github')
ret.update({
'user_has_auth': user_settings and user_settings.has_auth,
'is_registration': self.owner.is_registration,
})
if self.user_settings and self.user_settings.has_auth:
valid_credentials = False
owner = self.user_settings.owner
if user_settings and user_settings.owner == owner:
connection = GitHub.from_settings(user_settings)
# TODO: Fetch repo list client-side
# Since /user/repos excludes organization repos to which the
# current user has push access, we have to make extra requests to
# find them
valid_credentials = True
try:
repos = itertools.chain.from_iterable((connection.repos(), connection.my_org_repos()))
repo_names = [
'{0} / {1}'.format(repo.owner.login, repo.name)
for repo in repos
]
except GitHubError as error:
if error.code == http.UNAUTHORIZED:
repo_names = []
valid_credentials = False
ret.update({'repo_names': repo_names})
ret.update({
'node_has_auth': True,
'github_user': self.user or '',
'github_repo': self.repo or '',
'github_repo_full_name': '{0} / {1}'.format(self.user, self.repo),
'auth_osf_name': owner.fullname,
'auth_osf_url': owner.url,
'auth_osf_id': owner._id,
'github_user_name': self.user_settings.github_user_name,
'github_user_url': 'https://github.com/{0}'.format(self.user_settings.github_user_name),
'is_owner': owner == user,
'valid_credentials': valid_credentials,
'addons_url': web_url_for('user_addons'),
})
return ret
示例12: to_json
def to_json(self, user):
ret = super(AddonGitHubNodeSettings, self).to_json(user)
user_settings = user.get_addon("github")
ret.update(
{"user_has_auth": user_settings and user_settings.has_auth, "is_registration": self.owner.is_registration}
)
if self.user_settings and self.user_settings.has_auth:
valid_credentials = False
owner = self.user_settings.owner
if user_settings and user_settings.owner == owner:
connection = GitHub.from_settings(user_settings)
# TODO: Fetch repo list client-side
# Since /user/repos excludes organization repos to which the
# current user has push access, we have to make extra requests to
# find them
valid_credentials = True
try:
repos = itertools.chain.from_iterable((connection.repos(), connection.my_org_repos()))
repo_names = ["{0} / {1}".format(repo.owner.login, repo.name) for repo in repos]
except GitHubError as error:
if error.code == http.UNAUTHORIZED:
repo_names = []
valid_credentials = False
ret.update({"repo_names": repo_names})
ret.update(
{
"node_has_auth": True,
"github_user": self.user or "",
"github_repo": self.repo or "",
"github_repo_full_name": "{0} / {1}".format(self.user, self.repo),
"auth_osf_name": owner.fullname,
"auth_osf_url": owner.url,
"auth_osf_id": owner._id,
"github_user_name": self.user_settings.github_user_name,
"github_user_url": "https://github.com/{0}".format(self.user_settings.github_user_name),
"is_owner": owner == user,
"valid_credentials": valid_credentials,
"addons_url": web_url_for("user_addons"),
}
)
return ret
示例13: add_hook
def add_hook(self, save=True):
if self.user_settings:
connect = GitHub.from_settings(self.user_settings)
secret = utils.make_hook_secret()
hook = connect.add_hook(
self.user,
self.repo,
"web",
{
"url": urlparse.urljoin(hook_domain, os.path.join(self.owner.api_url, "github", "hook/")),
"content_type": github_settings.HOOK_CONTENT_TYPE,
"secret": secret,
},
)
if hook:
self.hook_id = hook.id
self.hook_secret = secret
if save:
self.save()
示例14: after_set_privacy
def after_set_privacy(self, node, permissions):
"""
:param Node node:
:param str permissions:
:return str: Alert message
"""
if not github_settings.SET_PRIVACY:
return
connect = GitHub.from_settings(self.user_settings)
data = connect.set_privacy(
self.user, self.repo, permissions == 'private'
)
if data is None or 'errors' in data:
repo = connect.repo(self.user, self.repo)
if repo is not None:
current_privacy = 'private' if repo.private else 'public'
else:
current_privacy = 'unknown'
return (
'Could not set privacy for repo {user}::{repo}. '
'Current privacy status is {perm}.'.format(
user=self.user,
repo=self.repo,
perm=current_privacy,
)
)
return (
'GitHub repo {user}::{repo} made {perm}.'.format(
user=self.user,
repo=self.repo,
perm=permissions,
)
)
示例15: github_download_file
def github_download_file(**kwargs):
node_settings = kwargs['node_addon']
path = get_path(kwargs)
ref = request.args.get('sha')
connection = GitHub.from_settings(node_settings.user_settings)
try:
name, data, _ = connection.file(
node_settings.user, node_settings.repo, path, ref=ref
)
except TooBigError:
raise HTTPError(
http.BAD_REQUEST,
data={
'message_short': 'File too large',
'message_long': 'This file is too large to download through '
'the GitHub API.',
},
)
if data is None:
raise HTTPError(http.NOT_FOUND)
# Build response
resp = make_response(data)
mimetype = get_mimetype(path, data)
# Add binary MIME type if mimetype not found
if mimetype is None:
resp.headers['Content-Type'] = 'application/octet-stream'
else:
resp.headers['Content-Type'] = mimetype
resp.headers['Content-Disposition'] = 'attachment; filename={0}'.format(
name)
return resp