8000 Implemented #57. This allows batch outside collaborator repo adding. by mikegrima · Pull Request #79 · Netflix/hubcommander · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implemented #57. This allows batch outside collaborator repo adding. #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ sudo: required
language: python

python:
- "3.5"
- "3.6"

install:
Expand Down
18 changes: 18 additions & 0 deletions bot_components/parse_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ def extract_repo_name(plugin_obj, reponame, **kwargs):
return split_repo.replace(">", "")


def extract_multiple_repo_names(plugin_obj, repos, **kwargs):
"""
Does what the above does, but does it for a comma separated list of repos.
:param plugin_obj:
:param repos:
:param kwargs:
:return:
"""
repo_list = repos.split(",")

parsed_repos = []

for repo in repo_list:
parsed_repos.append(extract_repo_name(plugin_obj, repo, **kwargs))

return parsed_repos


def parse_toggles(plugin_obj, toggle, toggle_type="toggle", **kwargs):
"""
Parses typical toggle values, like off, on, enabled, disabled, true, false, etc.
Expand Down
6 changes: 3 additions & 3 deletions build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ if [ -z ${BUILD_TAG} ]; then
BUILD_TAG="latest"
fi

# If this is running in Travis, AND the Python version IS NOT 3.5, then don't build
# If this is running in Travis, AND the Python version IS NOT 3.6, then don't build
# the Docker image:
if [ $TRAVIS ]; then
PYTHON_VERSION=$( python --version )
if [[ $PYTHON_VERSION != *"3.5"* ]]; then
echo "This only builds Docker images in the Python 3.5 Travis job"
if [[ $PYTHON_VERSION != *"3.6"* ]]; then
echo "This only builds Docker images in the Python 3.6 Travis job"
exit 0
fi
fi
Expand Down
26 changes: 19 additions & 7 deletions command_plugins/github/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@
from hubcommander.bot_components.slack_comm import send_error


def repo_must_exist(org_arg="org", repo_arg="repo"):
def repo_must_exist(org_arg="org"):
def command_decorator(func):
def decorated_command(github_plugin, data, user_data, *args, **kwargs):
# Just 1 repo -- or multiple?
if kwargs.get("repo"):
repos = [kwargs["repo"]]
else:
repos = kwargs["repos"]

# Check if the specified GitHub repo exists:
if not github_plugin.check_if_repo_exists(data, user_data, kwargs[repo_arg], kwargs[org_arg]):
return
for repo in repos:
if not github_plugin.check_if_repo_exists(data, user_data, repo, kwargs[org_arg]):
return

# Run the next function:
return func(github_plugin, data, user_data, *args, **kwargs)
Expand All @@ -23,14 +30,16 @@ def decorated_command(github_plugin, data, user_data, *args, **kwargs):

return command_decorator


def team_must_exist(org_arg="org", team_arg="team"):
def command_decorator(func):
def decorated_command(github_plugin, data, user_data, *args, **kwargs):
# Check if the specified GitHub team exists:
kwargs['team_id'] = github_plugin.find_team_id_by_name(kwargs[org_arg], kwargs[team_arg])
if not kwargs.get("team_id"):
send_error(data["channel"], "@{}: The GitHub team: {} does not exist.".format(user_data["name"],
kwargs[team_arg]))
kwargs[team_arg]),
thread=data["ts"])
return
# Run the next function:
return func(github_plugin, data, user_data, *args, **kwargs)
Expand All @@ -39,6 +48,7 @@ def decorated_command(github_plugin, data, user_data, *args, **kwargs):

return command_decorator


def github_user_exists(user_arg):
def command_decorator(func):
def decorated_command(github_plugin, data, user_data, *args, **kwargs):
Expand All @@ -48,13 +58,15 @@ def decorated_command(github_plugin, data, user_data, *args, **kwargs):

if not found_user:
send_error(data["channel"], "@{}: The GitHub user: {} does not exist.".format(user_data["name"],
kwargs[user_arg]))
kwargs[user_arg]),
thread=data["ts"])
return

except Exception as e:
send_error(data["channel"],
"@{}: A problem was encountered communicating with GitHub to verify the user's GitHub "
"id. Here are the details:\n{}".format(user_data["name"], str(e)))
"id. Here are the details:\n{}".format(user_data["name"], str(e)),
thread=data["ts"])
return

# Run the next function:
Expand All @@ -81,7 +93,7 @@ def decorated_command(github_plugin, data, user_data, *args, **kwargs):
send_error(data["channel"],
"@{}: This repository does not have the branch: `{}`.".format(user_data["name"],
kwargs[branch_arg]),
markdown=True)
markdown=True, thread=data["ts"])
return

# Run the next function:
Expand Down
25 changes: 14 additions & 11 deletions command_plugins/github/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
from hubcommander.bot_components.bot_classes import BotCommander
from hubcommander.bot_components.decorators import hubcommander_command, auth
from hubcommander.bot_components.slack_comm import send_info, send_success, send_error, send_raw
from hubcommander.bot_components.parse_functions import extract_repo_name, parse_toggles
from hubcommander.bot_components.parse_functions import extract_repo_name, parse_toggles, extract_multiple_repo_names
from hubcommander.command_plugins.github.config import GITHUB_URL, GITHUB_VERSION, ORGS, USER_COMMAND_DICT
from hubcommander.command_plugins.github.parse_functions import lookup_real_org, validate_homepage
from hubcommander.command_plugins.github.decorators import repo_must_exist, github_user_exists, branch_must_exist, team_must_exist
from hubcommander.command_plugins.github.decorators import repo_must_exist, github_user_exists, branch_must_exist, \
team_must_exist


class GitHubPlugin(BotCommander):
Expand Down Expand Up @@ -273,14 +274,15 @@ def set_repo_homepage_command(self, data, user_data, org, repo, homepage):

@hubcommander_command(
name="!AddCollab",
usage="!AddCollab <OutsideCollabId> <OrgWithRepo> <Repo> <Permission>",
usage="!AddCollab <OutsideCollabId> <OrgWithRepo> <Repos(Comma separated if more than 1)> <Permission>",
description="This will add an outside collaborator to a repository with the given permission.",
required=[
dict(name="collab", properties=dict(type=str, help="The outside collaborator's GitHub ID.")),
dict(name="org", properties=dict(type=str, help="The organization that contains the repo."),
validation_func=lookup_real_org, validation_func_kwargs={}),
dict(name="repo", properties=dict(type=str, help="The repository to add the outside collaborator to."),
validation_func=extract_repo_name, validation_func_kwargs={}),
dict(name="repos", properties=dict(type=str, help="A comma separated list (or not if just 1) of repos to "
"add the collaborator to."),
validation_func=extract_multiple_repo_names, validation_func_kwargs={}),
dict(name="permission", properties=dict(type=str.lower, help="The permission to grant, must be one "
"of: `{values}`"),
choices="permitted_permissions"),
Expand All @@ -290,9 +292,9 @@ def set_repo_homepage_command(self, data, user_data, org, repo, homepage):
@auth()
@repo_must_exist()
@github_user_exists("collab")
def add_outside_collab_command(self, data, user_data, collab, org, repo, permission):
def add_outside_collab_command(self, data, user_data, collab, org, repos, permission):
"""
Adds an outside collaborator a repository with a specified permission.
Adds an outside collaborator to repository (or multiple repos) with a specified permission.

Command is as follows: !addcollab <outside_collab_id> <organization> <repo> <permission>
:param permission:
Expand All @@ -308,7 +310,8 @@ def add_outside_collab_command(self, data, user_data, collab, org, repo, permiss

# Grant access:
try:
self.add_outside_collab_to_repo(collab, repo, org, permission)
for r in repos:
self.add_outside_collab_to_repo(collab, r, org, permission)

except ValueError as ve:
send_error(data["channel"],
Expand All @@ -325,8 +328,8 @@ def add_outside_collab_command(self, data, user_data, collab, org, repo, permiss
# Done:
send_success(data["channel"],
"@{}: The GitHub user: `{}` has been added as an outside collaborator with `{}` "
"permissions to {}/{}.".format(user_data["name"], collab, permission,
org, repo),
"permissions to {} in {}.".format(user_data["name"], collab, permission,
", ".join(repos), org),
markdown=True, thread=data["ts"])

@hubcommander_command(
Expand Down Expand Up @@ -980,7 +983,7 @@ def check_if_repo_exists(self, data, user_data, reponame, real_org):

if not result:
send_error(data["channel"],
"@{}: This repository does not exist in {}.".format(user_data["name"], real_org),
"@{}: The repository {}/{} does not exist.".format(user_data["name"], real_org, reponame),
thread=data["ts"])
return False

Expand Down
6 changes: 3 additions & 3 deletions publish_via_travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
#
################################################################################

# If this is running in Travis, AND the Python version IS NOT 3.5, then don't build
# If this is running in Travis, AND the Python version IS NOT 3.6, then don't build
# the Docker image:
if [ $TRAVIS ]; then
PYTHON_VERSION=$( python --version )
if [[ $PYTHON_VERSION != *"3.5"* ]]; then
echo "This only publishes Docker images in the Python 3.5 Travis job"
if [[ $PYTHON_VERSION != *"3.6"* ]]; then
echo "This only publishes Docker images in the Python 3.6 Travis job"
exit 0
fi
else
Expand Down
10 changes: 10 additions & 0 deletions tests/test_parse_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def test_extract_repo_name():
assert extract_repo_name(None, uri) == repo


def test_extract_multiple_repo_names():
from hubcommander.bot_components.parse_functions import extract_multiple_repo_names

test_repos = ["www.foo.com", "foo", "HubCommander", "netflix.github.io"]
test_repo_strings = "<http://www.foo.com|www.foo.com>,foo,HubCommander," \
"<https://netflix.github.io|netflix.github.io>"

assert extract_multiple_repo_names(None, test_repo_strings) == test_repos


def test_parse_toggles():
from hubcommander.bot_components.parse_functions import parse_toggles, TOGGLE_ON_VALUES, \
TOGGLE_OFF_VALUES, ParseException
Expand Down
0