2013-09-23 16:37:40 +00:00
|
|
|
import logging
|
2013-10-02 04:48:03 +00:00
|
|
|
import stripe
|
2013-10-25 22:17:43 +00:00
|
|
|
import urlparse
|
|
|
|
import json
|
2013-09-23 16:37:40 +00:00
|
|
|
|
2014-02-18 23:09:14 +00:00
|
|
|
from flask import (request, make_response, jsonify, abort, url_for, Blueprint,
|
|
|
|
session)
|
2013-11-15 21:12:12 +00:00
|
|
|
from flask.ext.login import current_user, logout_user
|
2013-10-14 21:50:07 +00:00
|
|
|
from flask.ext.principal import identity_changed, AnonymousIdentity
|
2013-09-23 16:37:40 +00:00
|
|
|
from functools import wraps
|
2013-10-10 04:40:25 +00:00
|
|
|
from collections import defaultdict
|
2014-02-21 22:09:56 +00:00
|
|
|
from urllib import quote
|
2013-09-23 16:37:40 +00:00
|
|
|
|
2013-09-25 16:45:12 +00:00
|
|
|
from data import model
|
2013-12-19 22:06:04 +00:00
|
|
|
from data.plans import PLANS, get_plan
|
2013-09-23 16:37:40 +00:00
|
|
|
from app import app
|
2014-02-18 23:09:14 +00:00
|
|
|
from util.email import (send_confirmation_email, send_recovery_email,
|
|
|
|
send_change_email)
|
2013-11-20 21:13:03 +00:00
|
|
|
from util.names import parse_repository_name, format_robot_username
|
2013-09-27 22:15:31 +00:00
|
|
|
from util.gravatar import compute_hash
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-09-26 21:59:20 +00:00
|
|
|
from auth.permissions import (ReadRepositoryPermission,
|
2013-09-27 17:24:07 +00:00
|
|
|
ModifyRepositoryPermission,
|
2013-11-04 20:42:08 +00:00
|
|
|
AdministerRepositoryPermission,
|
2013-11-04 21:18:40 +00:00
|
|
|
CreateRepositoryPermission,
|
|
|
|
AdministerOrganizationPermission,
|
2013-11-04 21:25:54 +00:00
|
|
|
OrganizationMemberPermission,
|
2014-02-20 23:57:49 +00:00
|
|
|
ViewTeamPermission,
|
2014-02-21 19:52:40 +00:00
|
|
|
UserPermission)
|
2014-02-26 00:39:43 +00:00
|
|
|
from endpoints.common import (common_login, get_route_data, truthy_param,
|
|
|
|
start_build)
|
2014-02-24 21:36:49 +00:00
|
|
|
from endpoints.trigger import (BuildTrigger, TriggerActivationException,
|
2014-02-26 00:39:43 +00:00
|
|
|
TriggerDeactivationException,
|
|
|
|
EmptyRepositoryException)
|
2014-02-25 01:38:57 +00:00
|
|
|
|
2013-10-20 05:18:31 +00:00
|
|
|
from util.cache import cache_control
|
2013-12-06 20:59:59 +00:00
|
|
|
from datetime import datetime, timedelta
|
2013-10-18 18:31:14 +00:00
|
|
|
|
2013-11-07 04:21:12 +00:00
|
|
|
store = app.config['STORAGE']
|
|
|
|
user_files = app.config['USERFILES']
|
2014-02-04 00:08:37 +00:00
|
|
|
build_logs = app.config['BUILDLOGS']
|
2013-09-23 16:37:40 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2013-12-27 22:19:14 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
api = Blueprint('api', __name__)
|
|
|
|
|
2014-01-30 18:42:25 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.before_request
|
|
|
|
def csrf_protect():
|
|
|
|
if request.method != "GET" and request.method != "HEAD":
|
|
|
|
token = session.get('_csrf_token', None)
|
|
|
|
found_token = request.values.get('_csrf_token', None)
|
|
|
|
|
|
|
|
# TODO: add if not token here, once we are sure all sessions have a token.
|
|
|
|
if token != found_token:
|
2014-01-30 18:42:25 +00:00
|
|
|
msg = 'CSRF Failure. Session token was %s and request token was %s'
|
|
|
|
logger.error(msg, token, found_token)
|
|
|
|
|
|
|
|
if not token:
|
2014-01-31 00:36:21 +00:00
|
|
|
logger.warning('No CSRF token in session.')
|
2013-12-30 22:05:27 +00:00
|
|
|
|
|
|
|
|
2014-01-24 22:00:42 +00:00
|
|
|
def request_error(exception=None, **kwargs):
|
2014-01-24 19:12:04 +00:00
|
|
|
data = kwargs.copy()
|
|
|
|
if exception:
|
2014-01-24 22:00:42 +00:00
|
|
|
data['message'] = exception.message
|
2014-01-24 19:12:04 +00:00
|
|
|
|
|
|
|
return make_response(jsonify(data), 400)
|
|
|
|
|
|
|
|
|
2013-11-29 05:04:50 +00:00
|
|
|
def log_action(kind, user_or_orgname, metadata={}, repo=None):
|
2013-11-28 01:49:23 +00:00
|
|
|
performer = current_user.db_user()
|
2013-12-19 22:06:04 +00:00
|
|
|
model.log_action(kind, user_or_orgname, performer=performer,
|
|
|
|
ip=request.remote_addr, metadata=metadata, repository=repo)
|
|
|
|
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-09-30 21:51:07 +00:00
|
|
|
def api_login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if not current_user.is_authenticated():
|
|
|
|
abort(401)
|
2013-11-12 00:03:18 +00:00
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
if (current_user and current_user.db_user() and
|
|
|
|
current_user.db_user().organization):
|
2013-11-12 00:26:56 +00:00
|
|
|
abort(401)
|
2013-11-12 00:03:18 +00:00
|
|
|
|
2013-11-23 01:23:46 +00:00
|
|
|
if (current_user and current_user.db_user() and
|
|
|
|
current_user.db_user().robot):
|
|
|
|
abort(401)
|
|
|
|
|
2013-09-30 21:51:07 +00:00
|
|
|
return f(*args, **kwargs)
|
|
|
|
return decorated_view
|
|
|
|
|
|
|
|
|
2013-12-25 02:56:03 +00:00
|
|
|
def internal_api_call(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
decorated_view.__internal_call = True
|
|
|
|
return decorated_view
|
|
|
|
|
|
|
|
|
2013-12-26 22:45:16 +00:00
|
|
|
def org_api_call(user_call_name):
|
|
|
|
def internal_decorator(f):
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
|
|
|
|
decorated_view.__user_call = user_call_name
|
|
|
|
return decorated_view
|
|
|
|
|
|
|
|
return internal_decorator
|
|
|
|
|
2013-11-05 19:40:45 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/discovery')
|
2013-12-26 22:45:16 +00:00
|
|
|
def discovery():
|
2013-12-27 22:19:14 +00:00
|
|
|
return jsonify(get_route_data())
|
2013-12-26 22:45:16 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/')
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-09-23 16:37:40 +00:00
|
|
|
def welcome():
|
2014-01-24 19:12:04 +00:00
|
|
|
return jsonify({'version': '0.5'})
|
2013-09-23 16:37:40 +00:00
|
|
|
|
2013-11-05 19:40:45 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/plans/')
|
2013-12-25 02:56:03 +00:00
|
|
|
def list_plans():
|
2013-11-05 19:40:45 +00:00
|
|
|
return jsonify({
|
2013-12-19 22:06:04 +00:00
|
|
|
'plans': PLANS,
|
2013-11-05 19:40:45 +00:00
|
|
|
})
|
|
|
|
|
2013-09-23 16:37:40 +00:00
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
def user_view(user):
|
2013-10-31 22:17:26 +00:00
|
|
|
def org_view(o):
|
2013-11-04 21:18:40 +00:00
|
|
|
admin_org = AdministerOrganizationPermission(o.username)
|
2013-10-31 22:17:26 +00:00
|
|
|
return {
|
|
|
|
'name': o.username,
|
|
|
|
'gravatar': compute_hash(o.email),
|
2013-11-07 05:49:13 +00:00
|
|
|
'is_org_admin': admin_org.can(),
|
2014-01-16 00:15:38 +00:00
|
|
|
'can_create_repo': admin_org.can() or CreateRepositoryPermission(o.username).can(),
|
|
|
|
'preferred_namespace': not (o.stripe_id is None)
|
2013-10-31 22:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
organizations = model.get_user_organizations(user.username)
|
|
|
|
|
2014-01-14 20:23:44 +00:00
|
|
|
def login_view(login):
|
|
|
|
return {
|
|
|
|
'service': login.service.name,
|
|
|
|
'service_identifier': login.service_ident,
|
|
|
|
}
|
|
|
|
|
|
|
|
logins = model.list_federated_logins(user)
|
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
return {
|
2013-09-26 23:59:58 +00:00
|
|
|
'verified': user.verified,
|
|
|
|
'anonymous': False,
|
|
|
|
'username': user.username,
|
|
|
|
'email': user.email,
|
2013-09-27 22:15:31 +00:00
|
|
|
'gravatar': compute_hash(user.email),
|
2013-10-10 17:44:34 +00:00
|
|
|
'askForPassword': user.password_hash is None,
|
2013-11-07 05:49:13 +00:00
|
|
|
'organizations': [org_view(o) for o in organizations],
|
2014-01-14 20:23:44 +00:00
|
|
|
'logins': [login_view(login) for login in logins],
|
2013-11-15 19:42:31 +00:00
|
|
|
'can_create_repo': True,
|
2014-01-16 00:15:38 +00:00
|
|
|
'invoice_email': user.invoice_email,
|
|
|
|
'preferred_namespace': not (user.stripe_id is None)
|
2013-11-15 19:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/', methods=['GET'])
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-11-15 19:42:31 +00:00
|
|
|
def get_logged_in_user():
|
|
|
|
if current_user.is_anonymous():
|
|
|
|
return jsonify({'anonymous': True})
|
|
|
|
|
|
|
|
user = current_user.db_user()
|
|
|
|
if not user or user.organization:
|
|
|
|
return jsonify({'anonymous': True})
|
|
|
|
|
|
|
|
return jsonify(user_view(user))
|
2013-09-26 23:59:58 +00:00
|
|
|
|
2013-10-31 22:17:26 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/private', methods=['GET'])
|
2013-12-19 04:03:19 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-12-19 04:03:19 +00:00
|
|
|
def get_user_private_count():
|
|
|
|
user = current_user.db_user()
|
|
|
|
private_repos = model.get_private_repo_count(user.username)
|
|
|
|
repos_allowed = 0
|
|
|
|
|
|
|
|
if user.stripe_id:
|
|
|
|
cus = stripe.Customer.retrieve(user.stripe_id)
|
|
|
|
if cus.subscription:
|
|
|
|
plan = get_plan(cus.subscription.plan.id)
|
|
|
|
if plan:
|
|
|
|
repos_allowed = plan['privateRepos']
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'privateCount': private_repos,
|
|
|
|
'reposAllowed': repos_allowed
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/convert', methods=['POST'])
|
2013-11-07 21:33:56 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-11-07 21:33:56 +00:00
|
|
|
def convert_user_to_organization():
|
|
|
|
user = current_user.db_user()
|
|
|
|
convert_data = request.get_json()
|
|
|
|
|
|
|
|
# Ensure that the new admin user is the not user being converted.
|
|
|
|
admin_username = convert_data['adminUser']
|
|
|
|
if admin_username == user.username:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(reason='invaliduser',
|
|
|
|
message='The admin user is not valid')
|
2013-11-07 21:33:56 +00:00
|
|
|
|
|
|
|
# Ensure that the sign in credentials work.
|
|
|
|
admin_password = convert_data['adminPassword']
|
|
|
|
if not model.verify_user(admin_username, admin_password):
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(reason='invaliduser',
|
|
|
|
message='The admin user credentials are not valid')
|
2013-11-07 21:33:56 +00:00
|
|
|
|
2013-11-08 03:08:23 +00:00
|
|
|
# Subscribe the organization to the new plan.
|
|
|
|
plan = convert_data['plan']
|
2013-12-19 22:10:09 +00:00
|
|
|
subscribe(user, plan, None, True) # Require business plans
|
2013-11-08 03:08:23 +00:00
|
|
|
|
2013-11-07 21:33:56 +00:00
|
|
|
# Convert the user to an organization.
|
|
|
|
model.convert_user_to_organization(user, model.get_user(admin_username))
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('account_convert', user.username)
|
2013-11-07 21:33:56 +00:00
|
|
|
|
|
|
|
# And finally login with the admin credentials.
|
|
|
|
return conduct_signin(admin_username, admin_password)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/', methods=['PUT'])
|
2013-10-10 17:44:34 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-10-10 17:44:34 +00:00
|
|
|
def change_user_details():
|
2013-10-15 18:48:49 +00:00
|
|
|
user = current_user.db_user()
|
2013-11-15 21:12:12 +00:00
|
|
|
user_data = request.get_json()
|
2013-10-10 17:44:34 +00:00
|
|
|
|
|
|
|
try:
|
2013-11-06 22:56:31 +00:00
|
|
|
if 'password' in user_data:
|
2013-10-10 17:44:34 +00:00
|
|
|
logger.debug('Changing password for user: %s', user.username)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('account_change_password', user.username)
|
2013-10-10 17:44:34 +00:00
|
|
|
model.change_password(user, user_data['password'])
|
2013-11-15 19:42:31 +00:00
|
|
|
|
|
|
|
if 'invoice_email' in user_data:
|
|
|
|
logger.debug('Changing invoice_email for user: %s', user.username)
|
|
|
|
model.change_invoice_email(user, user_data['invoice_email'])
|
|
|
|
|
2014-01-17 22:04:05 +00:00
|
|
|
if 'email' in user_data and user_data['email'] != user.email:
|
|
|
|
new_email = user_data['email']
|
|
|
|
if model.find_user_by_email(new_email):
|
|
|
|
# Email already used.
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(message='E-mail address already used')
|
2014-01-17 22:04:05 +00:00
|
|
|
|
2014-01-24 22:00:42 +00:00
|
|
|
logger.debug('Sending email to change email address for user: %s',
|
|
|
|
user.username)
|
2014-01-17 22:04:05 +00:00
|
|
|
code = model.create_confirm_email_code(user, new_email=new_email)
|
|
|
|
send_change_email(user.username, user_data['email'], code.code)
|
|
|
|
|
2013-10-10 17:44:34 +00:00
|
|
|
except model.InvalidPasswordException, ex:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(exception=ex)
|
2013-10-10 17:44:34 +00:00
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
return jsonify(user_view(user))
|
2013-09-26 23:59:58 +00:00
|
|
|
|
2013-11-05 19:40:45 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/', methods=['POST'])
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
|
|
|
def create_new_user():
|
2013-10-01 23:37:33 +00:00
|
|
|
user_data = request.get_json()
|
2013-11-06 22:09:22 +00:00
|
|
|
|
2013-10-02 02:13:43 +00:00
|
|
|
existing_user = model.get_user(user_data['username'])
|
|
|
|
if existing_user:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(message='The username already exists')
|
2013-10-02 02:13:43 +00:00
|
|
|
|
2013-10-01 23:37:33 +00:00
|
|
|
try:
|
|
|
|
new_user = model.create_user(user_data['username'], user_data['password'],
|
|
|
|
user_data['email'])
|
|
|
|
code = model.create_confirm_email_code(new_user)
|
|
|
|
send_confirmation_email(new_user.username, new_user.email, code.code)
|
|
|
|
return make_response('Created', 201)
|
|
|
|
except model.DataModelException as ex:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(exception=ex)
|
2013-10-01 23:37:33 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/signin', methods=['POST'])
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
|
|
|
def signin_user():
|
2013-10-14 21:50:07 +00:00
|
|
|
signin_data = request.get_json()
|
2014-01-31 21:19:29 +00:00
|
|
|
if not signin_data:
|
|
|
|
abort(404)
|
2013-10-14 21:50:07 +00:00
|
|
|
|
|
|
|
username = signin_data['username']
|
|
|
|
password = signin_data['password']
|
|
|
|
|
2013-11-07 21:33:56 +00:00
|
|
|
return conduct_signin(username, password)
|
|
|
|
|
|
|
|
|
2014-01-16 21:14:38 +00:00
|
|
|
def conduct_signin(username_or_email, password):
|
2013-10-14 21:50:07 +00:00
|
|
|
needs_email_verification = False
|
|
|
|
invalid_credentials = False
|
|
|
|
|
2014-01-16 21:14:38 +00:00
|
|
|
verified = model.verify_user(username_or_email, password)
|
2013-10-14 21:50:07 +00:00
|
|
|
if verified:
|
|
|
|
if common_login(verified):
|
2014-01-24 19:12:04 +00:00
|
|
|
return jsonify({'success': True})
|
2013-10-14 21:50:07 +00:00
|
|
|
else:
|
|
|
|
needs_email_verification = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
invalid_credentials = True
|
|
|
|
|
|
|
|
response = jsonify({
|
|
|
|
'needsEmailVerification': needs_email_verification,
|
|
|
|
'invalidCredentials': invalid_credentials,
|
|
|
|
})
|
|
|
|
response.status_code = 403
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route("/signout", methods=['POST'])
|
2013-10-14 21:50:07 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-10-14 21:50:07 +00:00
|
|
|
def logout():
|
|
|
|
logout_user()
|
|
|
|
identity_changed.send(app, identity=AnonymousIdentity())
|
2014-01-24 19:12:04 +00:00
|
|
|
return jsonify({'success': True})
|
2013-10-14 21:50:07 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route("/recovery", methods=['POST'])
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
|
|
|
def request_recovery_email():
|
2013-10-14 21:50:07 +00:00
|
|
|
email = request.get_json()['email']
|
|
|
|
code = model.create_reset_password_email_code(email)
|
|
|
|
send_recovery_email(email, code.code)
|
|
|
|
return make_response('Created', 201)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/users/<prefix>', methods=['GET'])
|
2013-09-30 21:54:06 +00:00
|
|
|
@api_login_required
|
2013-09-27 23:21:54 +00:00
|
|
|
def get_matching_users(prefix):
|
|
|
|
users = model.get_matching_users(prefix)
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'users': [user.username for user in users]
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/entities/<prefix>', methods=['GET'])
|
2013-11-02 01:48:10 +00:00
|
|
|
@api_login_required
|
|
|
|
def get_matching_entities(prefix):
|
|
|
|
teams = []
|
|
|
|
|
2013-12-18 03:56:28 +00:00
|
|
|
namespace_name = request.args.get('namespace', '')
|
2013-11-20 23:23:59 +00:00
|
|
|
robot_namespace = None
|
2013-11-02 01:48:10 +00:00
|
|
|
organization = None
|
2013-12-18 03:56:28 +00:00
|
|
|
|
2013-11-20 23:23:59 +00:00
|
|
|
try:
|
|
|
|
organization = model.get_organization(namespace_name)
|
|
|
|
|
|
|
|
# namespace name was an org
|
|
|
|
permission = OrganizationMemberPermission(namespace_name)
|
2013-11-05 00:11:01 +00:00
|
|
|
if permission.can():
|
2013-11-20 23:23:59 +00:00
|
|
|
robot_namespace = namespace_name
|
|
|
|
|
2014-02-16 23:59:24 +00:00
|
|
|
if truthy_param(request.args.get('includeTeams', False)):
|
2013-11-20 23:23:59 +00:00
|
|
|
teams = model.get_matching_teams(prefix, organization)
|
2013-11-02 01:48:10 +00:00
|
|
|
|
2013-11-20 23:23:59 +00:00
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# namespace name was a user
|
|
|
|
if current_user.db_user().username == namespace_name:
|
|
|
|
robot_namespace = namespace_name
|
2013-11-02 01:48:10 +00:00
|
|
|
|
2013-11-20 23:23:59 +00:00
|
|
|
users = model.get_matching_users(prefix, robot_namespace, organization)
|
2013-11-04 23:52:38 +00:00
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
def entity_team_view(team):
|
2013-11-05 03:58:21 +00:00
|
|
|
result = {
|
2013-11-02 01:48:10 +00:00
|
|
|
'name': team.name,
|
2013-11-05 00:06:56 +00:00
|
|
|
'kind': 'team',
|
2013-11-05 03:58:21 +00:00
|
|
|
'is_org_member': True
|
2013-11-02 01:48:10 +00:00
|
|
|
}
|
2013-11-08 04:35:20 +00:00
|
|
|
return result
|
2013-11-02 01:48:10 +00:00
|
|
|
|
|
|
|
def user_view(user):
|
2013-11-05 22:10:14 +00:00
|
|
|
user_json = {
|
2013-11-02 01:48:10 +00:00
|
|
|
'name': user.username,
|
2013-11-21 00:43:19 +00:00
|
|
|
'kind': 'user',
|
|
|
|
'is_robot': user.is_robot,
|
2013-11-02 01:48:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-18 03:56:28 +00:00
|
|
|
if organization is not None:
|
2013-11-23 01:14:44 +00:00
|
|
|
user_json['is_org_member'] = user.is_robot or user.is_org_member
|
2013-11-05 22:10:14 +00:00
|
|
|
|
|
|
|
return user_json
|
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
team_data = [entity_team_view(team) for team in teams]
|
2013-11-02 01:48:10 +00:00
|
|
|
user_data = [user_view(user) for user in users]
|
2014-01-31 22:54:01 +00:00
|
|
|
|
2013-11-02 01:48:10 +00:00
|
|
|
return jsonify({
|
|
|
|
'results': team_data + user_data
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
def team_view(orgname, team):
|
|
|
|
view_permission = ViewTeamPermission(orgname, team.name)
|
|
|
|
role = model.get_team_org_role(team).name
|
2013-11-05 01:17:58 +00:00
|
|
|
return {
|
2013-11-15 21:12:12 +00:00
|
|
|
'id': team.id,
|
|
|
|
'name': team.name,
|
|
|
|
'description': team.description,
|
2013-11-05 03:58:21 +00:00
|
|
|
'can_view': view_permission.can(),
|
|
|
|
'role': role
|
2013-11-05 01:17:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 19:40:45 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/', methods=['POST'])
|
2013-11-07 20:19:52 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
|
|
|
def create_organization():
|
2013-11-07 20:19:52 +00:00
|
|
|
org_data = request.get_json()
|
|
|
|
existing = None
|
|
|
|
|
|
|
|
try:
|
2014-01-31 22:54:01 +00:00
|
|
|
existing = model.get_organization(org_data['name'])
|
2013-11-15 21:12:12 +00:00
|
|
|
except model.InvalidOrganizationException:
|
2013-11-07 20:19:52 +00:00
|
|
|
pass
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
if not existing:
|
|
|
|
try:
|
|
|
|
existing = model.get_user(org_data['name'])
|
|
|
|
except model.InvalidUserException:
|
|
|
|
pass
|
|
|
|
|
2013-11-07 20:19:52 +00:00
|
|
|
if existing:
|
2014-01-24 22:00:42 +00:00
|
|
|
msg = 'A user or organization with this name already exists'
|
|
|
|
return request_error(message=msg)
|
2013-11-07 20:19:52 +00:00
|
|
|
|
|
|
|
try:
|
2013-11-15 21:12:12 +00:00
|
|
|
model.create_organization(org_data['name'], org_data['email'],
|
|
|
|
current_user.db_user())
|
2013-11-07 20:19:52 +00:00
|
|
|
return make_response('Created', 201)
|
|
|
|
except model.DataModelException as ex:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(exception=ex)
|
2013-11-07 20:19:52 +00:00
|
|
|
|
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
def org_view(o, teams):
|
|
|
|
admin_org = AdministerOrganizationPermission(o.username)
|
|
|
|
is_admin = admin_org.can()
|
|
|
|
view = {
|
|
|
|
'name': o.username,
|
|
|
|
'email': o.email if is_admin else '',
|
|
|
|
'gravatar': compute_hash(o.email),
|
|
|
|
'teams': {t.name : team_view(o.username, t) for t in teams},
|
|
|
|
'is_admin': is_admin
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_admin:
|
|
|
|
view['invoice_email'] = o.invoice_email
|
|
|
|
|
|
|
|
return view
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>', methods=['GET'])
|
2013-11-06 22:09:22 +00:00
|
|
|
@api_login_required
|
2013-10-31 22:17:26 +00:00
|
|
|
def get_organization(orgname):
|
2013-11-06 22:56:31 +00:00
|
|
|
permission = OrganizationMemberPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
2013-10-31 22:17:26 +00:00
|
|
|
|
2013-11-06 22:56:31 +00:00
|
|
|
teams = model.get_teams_within_org(org)
|
|
|
|
return jsonify(org_view(org, teams))
|
2013-10-31 22:17:26 +00:00
|
|
|
|
2013-11-06 22:56:31 +00:00
|
|
|
abort(403)
|
2013-10-25 05:14:38 +00:00
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>', methods=['PUT'])
|
2013-11-15 19:42:31 +00:00
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('change_user_details')
|
2013-11-15 19:42:31 +00:00
|
|
|
def change_organization_details(orgname):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
org_data = request.get_json()
|
2013-11-15 19:42:31 +00:00
|
|
|
if 'invoice_email' in org_data:
|
|
|
|
logger.debug('Changing invoice_email for organization: %s', org.username)
|
|
|
|
model.change_invoice_email(org, org_data['invoice_email'])
|
2014-01-20 21:12:23 +00:00
|
|
|
|
|
|
|
if 'email' in org_data and org_data['email'] != org.email:
|
|
|
|
new_email = org_data['email']
|
|
|
|
if model.find_user_by_email(new_email):
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(message='E-mail address already used')
|
2014-01-20 21:12:23 +00:00
|
|
|
|
|
|
|
logger.debug('Changing email address for organization: %s', org.username)
|
|
|
|
model.update_email(org, new_email)
|
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
teams = model.get_teams_within_org(org)
|
|
|
|
return jsonify(org_view(org, teams))
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
2014-01-21 22:32:49 +00:00
|
|
|
def prototype_view(proto, org_members):
|
2014-01-21 22:43:47 +00:00
|
|
|
def prototype_user_view(user):
|
|
|
|
return {
|
|
|
|
'name': user.username,
|
|
|
|
'is_robot': user.robot,
|
2014-01-21 22:32:49 +00:00
|
|
|
'kind': 'user',
|
2014-01-21 23:02:24 +00:00
|
|
|
'is_org_member': user.robot or user.username in org_members,
|
2014-01-21 19:18:20 +00:00
|
|
|
}
|
2014-01-21 22:43:47 +00:00
|
|
|
|
|
|
|
if proto.delegate_user:
|
|
|
|
delegate_view = prototype_user_view(proto.delegate_user)
|
2014-01-21 22:32:49 +00:00
|
|
|
else:
|
|
|
|
delegate_view = {
|
|
|
|
'name': proto.delegate_team.name,
|
|
|
|
'kind': 'team',
|
2014-01-21 19:18:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2014-01-21 23:34:54 +00:00
|
|
|
'activating_user': prototype_user_view(proto.activating_user) if proto.activating_user else None,
|
2014-01-21 22:32:49 +00:00
|
|
|
'delegate': delegate_view,
|
|
|
|
'role': proto.role.name,
|
|
|
|
'id': proto.uuid,
|
2014-01-21 19:18:20 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
@api.route('/organization/<orgname>/prototypes', methods=['GET'])
|
2014-01-21 19:18:20 +00:00
|
|
|
@api_login_required
|
|
|
|
def get_organization_prototype_permissions(orgname):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
permissions = model.get_prototype_permissions(org)
|
2014-01-21 22:32:49 +00:00
|
|
|
org_members = model.get_organization_member_set(orgname)
|
|
|
|
return jsonify({'prototypes': [prototype_view(p, org_members)
|
|
|
|
for p in permissions]})
|
2014-01-21 19:18:20 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-01-21 22:04:00 +00:00
|
|
|
def log_prototype_action(action_kind, orgname, prototype, **kwargs):
|
|
|
|
username = current_user.db_user().username
|
|
|
|
log_params = {
|
|
|
|
'prototypeid': prototype.uuid,
|
|
|
|
'username': username,
|
2014-01-21 23:34:54 +00:00
|
|
|
'activating_username': prototype.activating_user.username if prototype.activating_user else None,
|
2014-01-21 22:04:00 +00:00
|
|
|
'role': prototype.role.name
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
log_params[key] = value
|
|
|
|
|
|
|
|
if prototype.delegate_user:
|
|
|
|
log_params['delegate_user'] = prototype.delegate_user.username
|
|
|
|
elif prototype.delegate_team:
|
|
|
|
log_params['delegate_team'] = prototype.delegate_team.name
|
|
|
|
|
|
|
|
log_action(action_kind, orgname, log_params)
|
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
@api.route('/organization/<orgname>/prototypes', methods=['POST'])
|
2014-01-21 19:18:20 +00:00
|
|
|
@api_login_required
|
|
|
|
def create_organization_prototype_permission(orgname):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
details = request.get_json()
|
2014-01-21 23:34:54 +00:00
|
|
|
activating_username = None
|
|
|
|
|
2014-01-24 22:00:42 +00:00
|
|
|
if ('activating_user' in details and details['activating_user'] and
|
|
|
|
'name' in details['activating_user']):
|
2014-01-21 23:34:54 +00:00
|
|
|
activating_username = details['activating_user']['name']
|
2014-01-21 19:18:20 +00:00
|
|
|
|
2014-01-31 23:54:31 +00:00
|
|
|
delegate = details['delegate'] if 'delegate' in details else {}
|
|
|
|
delegate_kind = delegate.get('kind', None)
|
|
|
|
delegate_name = delegate.get('name', None)
|
2014-01-21 19:18:20 +00:00
|
|
|
|
2014-01-21 20:09:47 +00:00
|
|
|
delegate_username = delegate_name if delegate_kind == 'user' else None
|
|
|
|
delegate_teamname = delegate_name if delegate_kind == 'team' else None
|
2014-01-21 19:18:20 +00:00
|
|
|
|
2014-01-21 23:34:54 +00:00
|
|
|
activating_user = (model.get_user(activating_username)
|
|
|
|
if activating_username else None)
|
2014-01-21 22:32:49 +00:00
|
|
|
delegate_user = (model.get_user(delegate_username)
|
|
|
|
if delegate_username else None)
|
|
|
|
delegate_team = (model.get_organization_team(orgname, delegate_teamname)
|
|
|
|
if delegate_teamname else None)
|
2014-01-21 20:09:47 +00:00
|
|
|
|
2014-01-21 23:34:54 +00:00
|
|
|
if activating_username and not activating_user:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(message='Unknown activating user')
|
2014-01-21 19:18:20 +00:00
|
|
|
|
|
|
|
if not delegate_user and not delegate_team:
|
2014-01-31 23:54:31 +00:00
|
|
|
return request_error(message='Missing delegate user or team')
|
2014-01-21 19:18:20 +00:00
|
|
|
|
2014-01-21 20:09:47 +00:00
|
|
|
role_name = details['role']
|
2014-01-21 22:04:00 +00:00
|
|
|
|
2014-01-21 22:32:49 +00:00
|
|
|
prototype = model.add_prototype_permission(org, role_name, activating_user,
|
|
|
|
delegate_user, delegate_team)
|
2014-01-21 22:04:00 +00:00
|
|
|
log_prototype_action('create_prototype_permission', orgname, prototype)
|
2014-01-21 22:32:49 +00:00
|
|
|
org_members = model.get_organization_member_set(orgname)
|
|
|
|
return jsonify(prototype_view(prototype, org_members))
|
2014-01-21 19:18:20 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
@api.route('/organization/<orgname>/prototypes/<prototypeid>',
|
2014-01-21 22:32:49 +00:00
|
|
|
methods=['DELETE'])
|
2014-01-21 19:18:20 +00:00
|
|
|
@api_login_required
|
|
|
|
def delete_organization_prototype_permission(orgname, prototypeid):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
prototype = model.delete_prototype_permission(org, prototypeid)
|
|
|
|
if not prototype:
|
|
|
|
abort(404)
|
|
|
|
|
2014-01-21 22:04:00 +00:00
|
|
|
log_prototype_action('delete_prototype_permission', orgname, prototype)
|
|
|
|
|
2014-01-21 19:18:20 +00:00
|
|
|
return make_response('Deleted', 204)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
@api.route('/organization/<orgname>/prototypes/<prototypeid>',
|
2014-01-21 22:32:49 +00:00
|
|
|
methods=['PUT'])
|
2014-01-21 19:18:20 +00:00
|
|
|
@api_login_required
|
|
|
|
def update_organization_prototype_permission(orgname, prototypeid):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
|
|
|
|
2014-01-21 22:04:00 +00:00
|
|
|
existing = model.get_prototype_permission(org, prototypeid)
|
|
|
|
if not existing:
|
|
|
|
abort(404)
|
|
|
|
|
2014-01-21 19:18:20 +00:00
|
|
|
details = request.get_json()
|
|
|
|
role_name = details['role']
|
|
|
|
prototype = model.update_prototype_permission(org, prototypeid, role_name)
|
|
|
|
if not prototype:
|
|
|
|
abort(404)
|
|
|
|
|
2014-01-21 22:32:49 +00:00
|
|
|
log_prototype_action('modify_prototype_permission', orgname, prototype,
|
|
|
|
original_role=existing.role.name)
|
|
|
|
org_members = model.get_organization_member_set(orgname)
|
|
|
|
return jsonify(prototype_view(prototype, org_members))
|
2014-01-21 19:18:20 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
2013-11-15 19:42:31 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/members', methods=['GET'])
|
2013-11-07 00:06:59 +00:00
|
|
|
@api_login_required
|
|
|
|
def get_organization_members(orgname):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
2013-11-15 21:12:12 +00:00
|
|
|
if permission.can():
|
2013-11-07 00:06:59 +00:00
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
# Loop to create the members dictionary. Note that the members collection
|
|
|
|
# will return an entry for *every team* a member is on, so we will have
|
|
|
|
# duplicate keys (which is why we pre-build the dictionary).
|
|
|
|
members_dict = {}
|
|
|
|
members = model.get_organization_members_with_teams(org)
|
|
|
|
for member in members:
|
|
|
|
if not member.user.username in members_dict:
|
2014-01-21 21:23:00 +00:00
|
|
|
members_dict[member.user.username] = {'name': member.user.username,
|
|
|
|
'kind': 'user',
|
2013-11-26 19:37:55 +00:00
|
|
|
'is_robot': member.user.robot,
|
2013-11-15 21:12:12 +00:00
|
|
|
'teams': []}
|
2013-11-07 00:06:59 +00:00
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
members_dict[member.user.username]['teams'].append(member.team.name)
|
2013-11-07 00:06:59 +00:00
|
|
|
|
|
|
|
return jsonify({'members': members_dict})
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
2013-12-07 00:25:27 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/members/<membername>', methods=['GET'])
|
2013-12-07 00:25:27 +00:00
|
|
|
@api_login_required
|
|
|
|
def get_organization_member(orgname, membername):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
org = model.get_organization(orgname)
|
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
member_dict = None
|
2013-12-19 22:06:04 +00:00
|
|
|
member_teams = model.get_organization_members_with_teams(org, membername=membername)
|
2013-12-07 00:25:27 +00:00
|
|
|
for member in member_teams:
|
|
|
|
if not member_dict:
|
2014-01-21 21:23:00 +00:00
|
|
|
member_dict = {'name': member.user.username,
|
|
|
|
'kind': 'user',
|
2013-12-07 00:25:27 +00:00
|
|
|
'is_robot': member.user.robot,
|
|
|
|
'teams': []}
|
|
|
|
|
|
|
|
member_dict['teams'].append(member.team.name)
|
|
|
|
|
|
|
|
if not member_dict:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return jsonify({'member': member_dict})
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/private', methods=['GET'])
|
2013-11-06 22:09:22 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-11-05 19:55:05 +00:00
|
|
|
def get_organization_private_allowed(orgname):
|
|
|
|
permission = CreateRepositoryPermission(orgname)
|
|
|
|
if permission.can():
|
2013-11-05 19:53:48 +00:00
|
|
|
organization = model.get_organization(orgname)
|
2013-11-01 23:13:58 +00:00
|
|
|
|
2013-11-05 19:55:05 +00:00
|
|
|
private_repos = model.get_private_repo_count(organization.username)
|
|
|
|
if organization.stripe_id:
|
|
|
|
cus = stripe.Customer.retrieve(organization.stripe_id)
|
|
|
|
if cus.subscription:
|
2013-12-19 04:03:19 +00:00
|
|
|
repos_allowed = 0
|
|
|
|
plan = get_plan(cus.subscription.plan.id)
|
|
|
|
if plan:
|
|
|
|
repos_allowed = plan['privateRepos']
|
|
|
|
|
2013-11-05 19:55:05 +00:00
|
|
|
return jsonify({
|
|
|
|
'privateAllowed': (private_repos < repos_allowed)
|
|
|
|
})
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'privateAllowed': False
|
|
|
|
})
|
2013-11-01 23:13:58 +00:00
|
|
|
|
2013-11-05 19:55:05 +00:00
|
|
|
abort(403)
|
2013-11-01 23:13:58 +00:00
|
|
|
|
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
def member_view(member):
|
2013-11-04 20:31:38 +00:00
|
|
|
return {
|
2014-01-21 21:23:00 +00:00
|
|
|
'name': member.username,
|
|
|
|
'kind': 'user',
|
2013-11-21 00:43:19 +00:00
|
|
|
'is_robot': member.robot,
|
2013-11-04 20:31:38 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 01:17:58 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/team/<teamname>',
|
2013-11-05 01:17:58 +00:00
|
|
|
methods=['PUT', 'POST'])
|
2013-11-06 22:09:22 +00:00
|
|
|
@api_login_required
|
2013-11-05 01:17:58 +00:00
|
|
|
def update_organization_team(orgname, teamname):
|
|
|
|
edit_permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if edit_permission.can():
|
|
|
|
team = None
|
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
details = request.get_json()
|
2013-11-05 03:58:21 +00:00
|
|
|
is_existing = False
|
2013-11-05 01:17:58 +00:00
|
|
|
try:
|
|
|
|
team = model.get_organization_team(orgname, teamname)
|
2013-11-05 03:58:21 +00:00
|
|
|
is_existing = True
|
2013-11-15 21:12:12 +00:00
|
|
|
except model.InvalidTeamException:
|
2013-11-05 03:58:21 +00:00
|
|
|
# Create the new team.
|
2013-11-15 21:12:12 +00:00
|
|
|
description = details['description'] if 'description' in details else ''
|
|
|
|
role = details['role'] if 'role' in details else 'member'
|
2013-11-05 03:58:21 +00:00
|
|
|
|
2013-11-05 22:20:43 +00:00
|
|
|
org = model.get_organization(orgname)
|
|
|
|
team = model.create_team(teamname, org, role, description)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('org_create_team', orgname, {'team': teamname})
|
2013-11-05 03:58:21 +00:00
|
|
|
|
|
|
|
if is_existing:
|
2013-12-19 22:06:04 +00:00
|
|
|
if ('description' in details and
|
|
|
|
team.description != details['description']):
|
2013-11-15 21:12:12 +00:00
|
|
|
team.description = details['description']
|
2013-11-05 03:58:21 +00:00
|
|
|
team.save()
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('org_set_team_description', orgname,
|
|
|
|
{'team': teamname, 'description': team.description})
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
if 'role' in details:
|
2013-11-29 05:04:50 +00:00
|
|
|
role = model.get_team_org_role(team).name
|
|
|
|
if role != details['role']:
|
|
|
|
team = model.set_team_org_permission(team, details['role'],
|
|
|
|
current_user.db_user().username)
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('org_set_team_role', orgname,
|
|
|
|
{'team': teamname, 'role': details['role']})
|
2013-11-29 05:04:50 +00:00
|
|
|
|
2013-11-05 23:37:28 +00:00
|
|
|
resp = jsonify(team_view(orgname, team))
|
|
|
|
if not is_existing:
|
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
2013-11-05 01:17:58 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/team/<teamname>',
|
2013-11-05 19:53:48 +00:00
|
|
|
methods=['DELETE'])
|
2013-11-06 22:09:22 +00:00
|
|
|
@api_login_required
|
2013-11-05 19:53:48 +00:00
|
|
|
def delete_organization_team(orgname, teamname):
|
2013-11-05 20:50:56 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2013-11-05 23:37:28 +00:00
|
|
|
model.remove_team(orgname, teamname, current_user.db_user().username)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('org_delete_team', orgname, {'team': teamname})
|
2013-11-05 19:53:48 +00:00
|
|
|
return make_response('Deleted', 204)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/team/<teamname>/members',
|
2013-11-04 21:18:40 +00:00
|
|
|
methods=['GET'])
|
2013-11-06 22:09:22 +00:00
|
|
|
@api_login_required
|
2013-11-04 19:56:54 +00:00
|
|
|
def get_organization_team_members(orgname, teamname):
|
2013-11-04 21:25:54 +00:00
|
|
|
view_permission = ViewTeamPermission(orgname, teamname)
|
|
|
|
edit_permission = AdministerOrganizationPermission(orgname)
|
2013-11-04 19:56:54 +00:00
|
|
|
|
2013-11-04 21:25:54 +00:00
|
|
|
if view_permission.can():
|
2013-11-04 21:18:40 +00:00
|
|
|
team = None
|
|
|
|
try:
|
|
|
|
team = model.get_organization_team(orgname, teamname)
|
2013-11-15 21:12:12 +00:00
|
|
|
except model.InvalidTeamException:
|
2013-11-04 21:18:40 +00:00
|
|
|
abort(404)
|
|
|
|
|
|
|
|
members = model.get_organization_team_members(team.id)
|
|
|
|
return jsonify({
|
2013-11-04 21:25:54 +00:00
|
|
|
'members': { m.username : member_view(m) for m in members },
|
|
|
|
'can_edit': edit_permission.can()
|
2013-11-04 21:18:40 +00:00
|
|
|
})
|
2013-11-04 21:21:49 +00:00
|
|
|
|
2013-11-04 21:18:40 +00:00
|
|
|
abort(403)
|
2013-11-04 19:56:54 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/team/<teamname>/members/<membername>',
|
2013-11-04 21:18:40 +00:00
|
|
|
methods=['PUT', 'POST'])
|
2013-11-06 22:09:22 +00:00
|
|
|
@api_login_required
|
2013-11-04 19:56:54 +00:00
|
|
|
def update_organization_team_member(orgname, teamname, membername):
|
2013-11-04 21:18:40 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
team = None
|
|
|
|
user = None
|
2013-11-04 19:56:54 +00:00
|
|
|
|
2013-11-04 21:18:40 +00:00
|
|
|
# Find the team.
|
|
|
|
try:
|
|
|
|
team = model.get_organization_team(orgname, teamname)
|
2013-11-15 21:12:12 +00:00
|
|
|
except model.InvalidTeamException:
|
2013-11-04 21:18:40 +00:00
|
|
|
abort(404)
|
2013-11-04 19:56:54 +00:00
|
|
|
|
2013-11-04 21:18:40 +00:00
|
|
|
# Find the user.
|
|
|
|
user = model.get_user(membername)
|
|
|
|
if not user:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(message='Unknown user')
|
2013-11-04 21:18:40 +00:00
|
|
|
|
|
|
|
# Add the user to the team.
|
|
|
|
model.add_user_to_team(user, team)
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('org_add_team_member', orgname,
|
|
|
|
{'member': membername, 'team': teamname})
|
2013-11-04 21:18:40 +00:00
|
|
|
return jsonify(member_view(user))
|
2013-11-04 19:56:54 +00:00
|
|
|
|
2013-11-04 21:18:40 +00:00
|
|
|
abort(403)
|
2013-11-04 19:56:54 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/team/<teamname>/members/<membername>',
|
2013-11-04 21:18:40 +00:00
|
|
|
methods=['DELETE'])
|
2013-11-06 22:09:22 +00:00
|
|
|
@api_login_required
|
2013-11-04 19:56:54 +00:00
|
|
|
def delete_organization_team_member(orgname, teamname, membername):
|
2013-11-04 21:18:40 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
# Remote the user from the team.
|
2013-11-05 23:37:28 +00:00
|
|
|
invoking_user = current_user.db_user().username
|
|
|
|
model.remove_user_from_team(orgname, teamname, membername, invoking_user)
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('org_remove_team_member', orgname,
|
|
|
|
{'member': membername, 'team': teamname})
|
2013-11-05 19:53:48 +00:00
|
|
|
return make_response('Deleted', 204)
|
2013-11-04 19:56:54 +00:00
|
|
|
|
2013-11-04 21:18:40 +00:00
|
|
|
abort(403)
|
2013-11-04 19:56:54 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository', methods=['POST'])
|
2013-09-30 21:51:07 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
def create_repo():
|
2013-10-25 19:13:11 +00:00
|
|
|
owner = current_user.db_user()
|
2013-11-15 21:12:12 +00:00
|
|
|
req = request.get_json()
|
|
|
|
namespace_name = req['namespace'] if 'namespace' in req else owner.username
|
2013-10-25 05:14:38 +00:00
|
|
|
|
2013-11-04 20:47:27 +00:00
|
|
|
permission = CreateRepositoryPermission(namespace_name)
|
2013-11-04 20:42:08 +00:00
|
|
|
if permission.can():
|
2013-11-15 21:12:12 +00:00
|
|
|
repository_name = req['repository']
|
|
|
|
visibility = req['visibility']
|
2013-10-31 19:04:07 +00:00
|
|
|
|
2013-11-04 20:47:27 +00:00
|
|
|
existing = model.get_repository(namespace_name, repository_name)
|
|
|
|
if existing:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(message='Repository already exists')
|
2013-10-25 05:14:38 +00:00
|
|
|
|
2013-11-15 21:12:12 +00:00
|
|
|
visibility = req['visibility']
|
2013-10-25 05:14:38 +00:00
|
|
|
|
2013-11-04 20:47:27 +00:00
|
|
|
repo = model.create_repository(namespace_name, repository_name, owner,
|
|
|
|
visibility)
|
2013-11-15 21:12:12 +00:00
|
|
|
repo.description = req['description']
|
2013-11-04 20:47:27 +00:00
|
|
|
repo.save()
|
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('create_repo', namespace_name,
|
|
|
|
{'repo': repository_name, 'namespace': namespace_name},
|
|
|
|
repo=repo)
|
2013-11-04 20:42:08 +00:00
|
|
|
return jsonify({
|
|
|
|
'namespace': namespace_name,
|
|
|
|
'name': repository_name
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403)
|
2013-09-23 16:37:40 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/find/repository', methods=['GET'])
|
2013-12-25 02:56:03 +00:00
|
|
|
def find_repos():
|
2013-10-01 21:02:49 +00:00
|
|
|
prefix = request.args.get('query', '')
|
2013-10-08 15:29:42 +00:00
|
|
|
|
2013-09-27 23:21:54 +00:00
|
|
|
def repo_view(repo):
|
|
|
|
return {
|
|
|
|
'namespace': repo.namespace,
|
|
|
|
'name': repo.name,
|
2013-09-28 21:11:10 +00:00
|
|
|
'description': repo.description
|
2013-09-27 23:21:54 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 15:29:42 +00:00
|
|
|
username = None
|
|
|
|
if current_user.is_authenticated():
|
2013-10-15 18:48:49 +00:00
|
|
|
username = current_user.db_user().username
|
2013-10-08 15:29:42 +00:00
|
|
|
|
2013-09-28 03:25:57 +00:00
|
|
|
matching = model.get_matching_repositories(prefix, username)
|
2013-09-27 23:21:54 +00:00
|
|
|
response = {
|
2013-09-28 03:25:57 +00:00
|
|
|
'repositories': [repo_view(repo) for repo in matching]
|
2013-09-27 23:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonify(response)
|
|
|
|
|
2013-09-26 21:59:20 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/', methods=['GET'])
|
2013-12-25 02:56:03 +00:00
|
|
|
def list_repos():
|
2013-09-28 04:05:32 +00:00
|
|
|
def repo_view(repo_obj):
|
2013-09-23 16:37:40 +00:00
|
|
|
return {
|
2013-09-28 04:05:32 +00:00
|
|
|
'namespace': repo_obj.namespace,
|
|
|
|
'name': repo_obj.name,
|
|
|
|
'description': repo_obj.description,
|
2013-11-08 03:44:18 +00:00
|
|
|
'is_public': repo_obj.visibility.name == 'public',
|
2013-09-23 16:37:40 +00:00
|
|
|
}
|
2013-10-08 15:29:42 +00:00
|
|
|
|
2014-01-13 19:49:05 +00:00
|
|
|
page = request.args.get('page', None)
|
2013-10-02 04:28:24 +00:00
|
|
|
limit = request.args.get('limit', None)
|
2013-10-31 22:17:26 +00:00
|
|
|
namespace_filter = request.args.get('namespace', None)
|
2014-02-16 23:59:24 +00:00
|
|
|
include_public = truthy_param(request.args.get('public', True))
|
|
|
|
include_private = truthy_param(request.args.get('private', True))
|
|
|
|
sort = truthy_param(request.args.get('sort', False))
|
|
|
|
include_count = truthy_param(request.args.get('count', False))
|
2013-09-23 16:37:40 +00:00
|
|
|
|
2013-10-02 04:28:24 +00:00
|
|
|
try:
|
|
|
|
limit = int(limit) if limit else None
|
2013-11-15 21:12:12 +00:00
|
|
|
except TypeError:
|
2013-10-02 04:28:24 +00:00
|
|
|
limit = None
|
|
|
|
|
2014-01-13 19:49:05 +00:00
|
|
|
if page:
|
|
|
|
try:
|
|
|
|
page = int(page)
|
2014-01-24 22:00:42 +00:00
|
|
|
except Exception:
|
2014-01-13 19:49:05 +00:00
|
|
|
page = None
|
2013-10-02 04:28:24 +00:00
|
|
|
|
2013-10-08 15:29:42 +00:00
|
|
|
username = None
|
|
|
|
if current_user.is_authenticated() and include_private:
|
2013-10-15 18:48:49 +00:00
|
|
|
username = current_user.db_user().username
|
2013-10-08 15:29:42 +00:00
|
|
|
|
2014-01-13 19:49:05 +00:00
|
|
|
repo_count = None
|
|
|
|
if include_count:
|
|
|
|
repo_count = model.get_visible_repository_count(username,
|
|
|
|
include_public=include_public,
|
|
|
|
namespace=namespace_filter)
|
|
|
|
|
|
|
|
repo_query = model.get_visible_repositories(username, limit=limit, page=page,
|
2013-10-08 15:29:42 +00:00
|
|
|
include_public=include_public,
|
2013-11-08 03:44:18 +00:00
|
|
|
sort=sort,
|
|
|
|
namespace=namespace_filter)
|
2014-01-13 19:49:05 +00:00
|
|
|
|
2013-10-08 15:29:42 +00:00
|
|
|
repos = [repo_view(repo) for repo in repo_query]
|
2013-09-23 16:37:40 +00:00
|
|
|
response = {
|
|
|
|
'repositories': repos
|
|
|
|
}
|
|
|
|
|
2014-01-13 19:49:05 +00:00
|
|
|
if include_count:
|
|
|
|
response['count'] = repo_count
|
|
|
|
|
2013-09-23 16:37:40 +00:00
|
|
|
return jsonify(response)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>', methods=['PUT'])
|
2013-09-30 21:51:07 +00:00
|
|
|
@api_login_required
|
2013-09-23 16:37:40 +00:00
|
|
|
@parse_repository_name
|
2013-12-25 02:56:03 +00:00
|
|
|
def update_repo(namespace, repository):
|
2013-09-26 21:59:20 +00:00
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
2013-09-28 00:03:07 +00:00
|
|
|
if permission.can():
|
2013-09-26 21:59:20 +00:00
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
if repo:
|
|
|
|
values = request.get_json()
|
|
|
|
repo.description = values['description']
|
|
|
|
repo.save()
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('set_repo_description', namespace,
|
|
|
|
{'repo': repository, 'description': values['description']},
|
2013-11-29 05:04:50 +00:00
|
|
|
repo=repo)
|
2013-09-26 21:59:20 +00:00
|
|
|
return jsonify({
|
|
|
|
'success': True
|
|
|
|
})
|
2013-09-28 00:03:07 +00:00
|
|
|
|
2013-11-07 04:21:12 +00:00
|
|
|
abort(403)
|
2013-09-23 16:37:40 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/changevisibility',
|
2013-10-08 15:29:42 +00:00
|
|
|
methods=['POST'])
|
2013-09-30 21:51:07 +00:00
|
|
|
@api_login_required
|
2013-09-28 21:11:10 +00:00
|
|
|
@parse_repository_name
|
2013-12-25 02:56:03 +00:00
|
|
|
def change_repo_visibility(namespace, repository):
|
2013-09-28 21:11:10 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
if repo:
|
|
|
|
values = request.get_json()
|
|
|
|
model.set_repository_visibility(repo, values['visibility'])
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('change_repo_visibility', namespace,
|
|
|
|
{'repo': repository, 'visibility': values['visibility']},
|
2013-11-29 05:04:50 +00:00
|
|
|
repo=repo)
|
2013-09-28 21:11:10 +00:00
|
|
|
return jsonify({
|
|
|
|
'success': True
|
|
|
|
})
|
|
|
|
|
2013-11-06 22:56:31 +00:00
|
|
|
abort(403)
|
2013-09-28 21:11:10 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>', methods=['DELETE'])
|
2013-10-01 16:13:25 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def delete_repository(namespace, repository):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
model.purge_repository(namespace, repository)
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('delete_repo', namespace,
|
|
|
|
{'repo': repository, 'namespace': namespace})
|
2013-10-01 16:13:25 +00:00
|
|
|
return make_response('Deleted', 204)
|
|
|
|
|
2013-11-06 22:56:31 +00:00
|
|
|
abort(403)
|
2013-10-01 16:13:25 +00:00
|
|
|
|
|
|
|
|
2013-09-27 21:01:45 +00:00
|
|
|
def image_view(image):
|
2014-02-17 19:52:46 +00:00
|
|
|
extended_props = image
|
|
|
|
if image.storage and image.storage.id:
|
|
|
|
extended_props = image.storage
|
|
|
|
|
2014-02-16 22:38:47 +00:00
|
|
|
command = extended_props.command
|
2013-09-27 21:01:45 +00:00
|
|
|
return {
|
2013-10-01 18:14:39 +00:00
|
|
|
'id': image.docker_image_id,
|
2014-02-16 22:38:47 +00:00
|
|
|
'created': extended_props.created,
|
|
|
|
'comment': extended_props.comment,
|
|
|
|
'command': json.loads(command) if command else None,
|
2013-10-10 04:40:18 +00:00
|
|
|
'ancestors': image.ancestors,
|
|
|
|
'dbid': image.id,
|
2014-02-16 22:38:47 +00:00
|
|
|
'size': extended_props.image_size,
|
2013-09-27 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>', methods=['GET'])
|
2013-09-23 16:37:40 +00:00
|
|
|
@parse_repository_name
|
2013-12-25 02:56:03 +00:00
|
|
|
def get_repo(namespace, repository):
|
2013-09-27 18:56:14 +00:00
|
|
|
logger.debug('Get repo: %s/%s' % (namespace, repository))
|
2013-09-28 00:03:07 +00:00
|
|
|
|
2013-09-26 21:59:20 +00:00
|
|
|
def tag_view(tag):
|
|
|
|
image = model.get_tag_image(namespace, repository, tag.name)
|
|
|
|
if not image:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'name': tag.name,
|
2013-09-27 17:24:07 +00:00
|
|
|
'image': image_view(image),
|
2013-09-26 21:59:20 +00:00
|
|
|
}
|
|
|
|
|
2013-11-02 01:48:10 +00:00
|
|
|
organization = None
|
|
|
|
try:
|
|
|
|
organization = model.get_organization(namespace)
|
2013-11-15 21:12:12 +00:00
|
|
|
except model.InvalidOrganizationException:
|
2013-11-02 01:48:10 +00:00
|
|
|
pass
|
|
|
|
|
2013-09-26 21:59:20 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
2013-09-28 21:11:10 +00:00
|
|
|
is_public = model.repository_is_public(namespace, repository)
|
|
|
|
if permission.can() or is_public:
|
2013-09-26 21:59:20 +00:00
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
if repo:
|
|
|
|
tags = model.list_repository_tags(namespace, repository)
|
2013-09-27 17:24:07 +00:00
|
|
|
tag_dict = {tag.name: tag_view(tag) for tag in tags}
|
|
|
|
can_write = ModifyRepositoryPermission(namespace, repository).can()
|
2013-09-27 18:56:14 +00:00
|
|
|
can_admin = AdministerRepositoryPermission(namespace, repository).can()
|
2013-10-27 20:00:44 +00:00
|
|
|
active_builds = model.list_repository_builds(namespace, repository,
|
|
|
|
include_inactive=False)
|
2013-10-25 19:13:11 +00:00
|
|
|
|
2013-09-27 17:24:07 +00:00
|
|
|
return jsonify({
|
|
|
|
'namespace': namespace,
|
|
|
|
'name': repository,
|
|
|
|
'description': repo.description,
|
|
|
|
'tags': tag_dict,
|
|
|
|
'can_write': can_write,
|
2013-09-27 18:56:14 +00:00
|
|
|
'can_admin': can_admin,
|
2013-10-25 19:13:11 +00:00
|
|
|
'is_public': is_public,
|
2014-02-19 21:08:33 +00:00
|
|
|
'is_building': len(list(active_builds)) > 0,
|
2013-12-10 20:22:22 +00:00
|
|
|
'is_organization': bool(organization)
|
2013-09-27 17:24:07 +00:00
|
|
|
})
|
2013-09-26 21:59:20 +00:00
|
|
|
|
2013-11-06 22:56:31 +00:00
|
|
|
abort(404) # Not found
|
2013-09-27 17:24:07 +00:00
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2014-02-19 21:08:33 +00:00
|
|
|
def trigger_view(trigger):
|
|
|
|
if trigger and trigger.uuid:
|
2014-02-20 23:57:49 +00:00
|
|
|
config_dict = json.loads(trigger.config)
|
|
|
|
build_trigger = BuildTrigger.get_trigger_for_service(trigger.service.name)
|
2014-02-19 21:08:33 +00:00
|
|
|
return {
|
|
|
|
'service': trigger.service.name,
|
2014-02-20 23:57:49 +00:00
|
|
|
'config': config_dict,
|
2014-02-19 21:08:33 +00:00
|
|
|
'id': trigger.uuid,
|
|
|
|
'connected_user': trigger.connected_user.username,
|
2014-02-20 23:57:49 +00:00
|
|
|
'is_active': build_trigger.is_active(config_dict)
|
2014-02-19 21:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-02-17 22:28:20 +00:00
|
|
|
def build_status_view(build_obj, can_write=False):
|
2014-02-04 00:08:37 +00:00
|
|
|
status = build_logs.get_status(build_obj.uuid)
|
|
|
|
return {
|
|
|
|
'id': build_obj.uuid,
|
|
|
|
'phase': build_obj.phase,
|
2014-02-11 17:25:38 +00:00
|
|
|
'started': build_obj.started,
|
2014-02-12 18:52:12 +00:00
|
|
|
'display_name': build_obj.display_name,
|
2014-02-04 00:08:37 +00:00
|
|
|
'status': status,
|
2014-02-24 21:11:23 +00:00
|
|
|
'job_config': json.loads(build_obj.job_config) if can_write else None,
|
2014-02-19 21:08:33 +00:00
|
|
|
'is_writer': can_write,
|
|
|
|
'trigger': trigger_view(build_obj.trigger),
|
2014-03-03 23:45:40 +00:00
|
|
|
'resource_key': build_obj.resource_key,
|
2014-02-04 00:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/build/', methods=['GET'])
|
2013-10-25 19:13:11 +00:00
|
|
|
@parse_repository_name
|
|
|
|
def get_repo_builds(namespace, repository):
|
2014-01-27 18:55:40 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
|
|
|
is_public = model.repository_is_public(namespace, repository)
|
|
|
|
if permission.can() or is_public:
|
2014-02-17 22:28:20 +00:00
|
|
|
can_write = ModifyRepositoryPermission(namespace, repository).can()
|
2013-10-25 19:13:11 +00:00
|
|
|
builds = model.list_repository_builds(namespace, repository)
|
|
|
|
return jsonify({
|
2014-02-17 22:28:20 +00:00
|
|
|
'builds': [build_status_view(build, can_write) for build in builds]
|
2014-02-04 00:08:37 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
|
|
|
@api.route('/repository/<path:repository>/build/<build_uuid>/status',
|
|
|
|
methods=['GET'])
|
|
|
|
@parse_repository_name
|
|
|
|
def get_repo_build_status(namespace, repository, build_uuid):
|
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
|
|
|
is_public = model.repository_is_public(namespace, repository)
|
|
|
|
if permission.can() or is_public:
|
|
|
|
build = model.get_repository_build(namespace, repository, build_uuid)
|
2014-02-17 22:28:20 +00:00
|
|
|
if not build:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
can_write = ModifyRepositoryPermission(namespace, repository).can()
|
|
|
|
return jsonify(build_status_view(build, can_write))
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
|
|
|
@api.route('/repository/<path:repository>/build/<build_uuid>/archiveurl',
|
|
|
|
methods=['GET'])
|
|
|
|
@parse_repository_name
|
|
|
|
def get_repo_build_archive_url(namespace, repository, build_uuid):
|
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
build = model.get_repository_build(namespace, repository, build_uuid)
|
|
|
|
if not build:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
url = user_files.get_file_url(build.resource_key)
|
|
|
|
return jsonify({
|
|
|
|
'url': url
|
|
|
|
})
|
2014-02-04 00:08:37 +00:00
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
|
|
|
@api.route('/repository/<path:repository>/build/<build_uuid>/logs',
|
|
|
|
methods=['GET'])
|
|
|
|
@parse_repository_name
|
|
|
|
def get_repo_build_logs(namespace, repository, build_uuid):
|
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2014-02-11 00:12:43 +00:00
|
|
|
response_obj = {}
|
|
|
|
|
2014-02-04 00:08:37 +00:00
|
|
|
build = model.get_repository_build(namespace, repository, build_uuid)
|
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
start = int(request.args.get('start', 0))
|
2014-02-04 00:08:37 +00:00
|
|
|
|
2014-02-12 23:58:40 +00:00
|
|
|
count, logs = build_logs.get_log_entries(build.uuid, start)
|
2014-02-04 00:08:37 +00:00
|
|
|
|
2014-02-11 00:12:43 +00:00
|
|
|
response_obj.update({
|
2014-02-04 00:08:37 +00:00
|
|
|
'start': start,
|
|
|
|
'total': count,
|
|
|
|
'logs': [log for log in logs],
|
2013-10-25 19:13:11 +00:00
|
|
|
})
|
|
|
|
|
2014-02-11 00:12:43 +00:00
|
|
|
return jsonify(response_obj)
|
|
|
|
|
2014-01-27 18:55:40 +00:00
|
|
|
abort(403) # Permission denied
|
2013-10-25 19:47:34 +00:00
|
|
|
|
2013-10-25 19:13:11 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/build/', methods=['POST'])
|
2013-10-26 21:41:29 +00:00
|
|
|
@api_login_required
|
2013-10-25 22:17:43 +00:00
|
|
|
@parse_repository_name
|
|
|
|
def request_repo_build(namespace, repository):
|
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
logger.debug('User requested repository initialization.')
|
2013-10-26 21:20:59 +00:00
|
|
|
dockerfile_id = request.get_json()['file_id']
|
2013-10-25 22:17:43 +00:00
|
|
|
|
2014-02-24 21:11:23 +00:00
|
|
|
# Check if the dockerfile resource has already been used. If so, then it
|
|
|
|
# can only be reused if the user has access to the repository for which it
|
|
|
|
# was used.
|
2014-02-14 23:37:06 +00:00
|
|
|
associated_repository = model.get_repository_for_resource(dockerfile_id)
|
|
|
|
if associated_repository:
|
2014-02-24 21:11:23 +00:00
|
|
|
if not ModifyRepositoryPermission(associated_repository.namespace,
|
|
|
|
associated_repository.name):
|
2014-02-14 23:37:06 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
# Start the build.
|
2013-10-25 22:17:43 +00:00
|
|
|
repo = model.get_repository(namespace, repository)
|
2014-02-12 18:52:12 +00:00
|
|
|
display_name = user_files.get_file_checksum(dockerfile_id)
|
2014-02-26 00:39:43 +00:00
|
|
|
|
|
|
|
build_request = start_build(repo, dockerfile_id, ['latest'], display_name,
|
|
|
|
'', True)
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2014-02-17 22:28:20 +00:00
|
|
|
resp = jsonify(build_status_view(build_request, True))
|
2014-02-04 00:08:37 +00:00
|
|
|
repo_string = '%s/%s' % (namespace, repository)
|
|
|
|
resp.headers['Location'] = url_for('api.get_repo_build_status',
|
|
|
|
repository=repo_string,
|
|
|
|
build_uuid=build_request.uuid)
|
2013-11-07 17:54:21 +00:00
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
2013-10-25 22:17:43 +00:00
|
|
|
|
|
|
|
abort(403) # Permissions denied
|
|
|
|
|
|
|
|
|
2013-11-15 21:45:02 +00:00
|
|
|
def webhook_view(webhook):
|
|
|
|
return {
|
|
|
|
'public_id': webhook.public_id,
|
|
|
|
'parameters': json.loads(webhook.parameters),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/webhook/', methods=['POST'])
|
2013-11-15 21:45:02 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def create_webhook(namespace, repository):
|
2013-11-15 22:45:37 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
2013-11-15 21:45:02 +00:00
|
|
|
if permission.can():
|
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
webhook = model.create_webhook(repo, request.get_json())
|
|
|
|
resp = jsonify(webhook_view(webhook))
|
|
|
|
repo_string = '%s/%s' % (namespace, repository)
|
2014-01-30 22:23:14 +00:00
|
|
|
resp.headers['Location'] = url_for('api.get_webhook',
|
|
|
|
repository=repo_string,
|
2013-11-15 21:45:02 +00:00
|
|
|
public_id=webhook.public_id)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('add_repo_webhook', namespace,
|
2013-12-19 22:06:04 +00:00
|
|
|
{'repo': repository, 'webhook_id': webhook.public_id},
|
|
|
|
repo=repo)
|
2013-11-15 22:45:37 +00:00
|
|
|
return resp
|
2013-11-15 21:45:02 +00:00
|
|
|
|
|
|
|
abort(403) # Permissions denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/webhook/<public_id>',
|
2013-11-15 21:45:02 +00:00
|
|
|
methods=['GET'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def get_webhook(namespace, repository, public_id):
|
2013-11-15 22:45:37 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
2013-11-15 21:45:02 +00:00
|
|
|
if permission.can():
|
2014-02-03 23:18:33 +00:00
|
|
|
try:
|
|
|
|
webhook = model.get_webhook(namespace, repository, public_id)
|
|
|
|
except model.InvalidWebhookException:
|
|
|
|
abort(404)
|
|
|
|
|
2013-11-15 21:45:02 +00:00
|
|
|
return jsonify(webhook_view(webhook))
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/webhook/', methods=['GET'])
|
2013-11-15 21:45:02 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def list_webhooks(namespace, repository):
|
2013-11-15 22:45:37 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
2013-11-15 21:45:02 +00:00
|
|
|
if permission.can():
|
|
|
|
webhooks = model.list_webhooks(namespace, repository)
|
|
|
|
return jsonify({
|
|
|
|
'webhooks': [webhook_view(webhook) for webhook in webhooks]
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/webhook/<public_id>',
|
2013-11-15 22:45:37 +00:00
|
|
|
methods=['DELETE'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def delete_webhook(namespace, repository, public_id):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
model.delete_webhook(namespace, repository, public_id)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('delete_repo_webhook', namespace,
|
2013-11-27 07:29:31 +00:00
|
|
|
{'repo': repository, 'webhook_id': public_id},
|
|
|
|
repo=model.get_repository(namespace, repository))
|
2013-11-15 22:45:37 +00:00
|
|
|
return make_response('No Content', 204)
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>',
|
|
|
|
methods=['GET'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def get_build_trigger(namespace, repository, trigger_uuid):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
trigger = model.get_build_trigger(namespace, repository, trigger_uuid)
|
|
|
|
except model.InvalidBuildTriggerException:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return jsonify(trigger_view(trigger))
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2014-02-21 22:09:56 +00:00
|
|
|
def _prepare_webhook_url(scheme, username, password, hostname, path):
|
|
|
|
auth_hostname = '%s:%s@%s' % (quote(username), quote(password), hostname)
|
|
|
|
return urlparse.urlunparse((scheme, auth_hostname, path, '', '', ''))
|
|
|
|
|
|
|
|
|
2014-02-25 01:36:54 +00:00
|
|
|
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>/subdir',
|
|
|
|
methods=['POST'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def list_build_trigger_subdirs(namespace, repository, trigger_uuid):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
trigger = model.get_build_trigger(namespace, repository, trigger_uuid)
|
|
|
|
except model.InvalidBuildTriggerException:
|
|
|
|
abort(404)
|
|
|
|
return
|
|
|
|
|
|
|
|
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
|
|
|
|
user_permission = UserPermission(trigger.connected_user.username)
|
|
|
|
if user_permission.can():
|
|
|
|
new_config_dict = request.get_json()
|
|
|
|
|
|
|
|
try:
|
|
|
|
subdirs = handler.list_build_subdirs(trigger.auth_token, new_config_dict)
|
|
|
|
return jsonify({
|
|
|
|
'subdir': subdirs,
|
|
|
|
'status': 'success'
|
|
|
|
})
|
|
|
|
except EmptyRepositoryException as e:
|
|
|
|
return jsonify({
|
|
|
|
'status': 'error',
|
|
|
|
'message': e.msg
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2014-02-20 23:57:49 +00:00
|
|
|
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>/activate',
|
|
|
|
methods=['POST'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def activate_build_trigger(namespace, repository, trigger_uuid):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
trigger = model.get_build_trigger(namespace, repository, trigger_uuid)
|
|
|
|
except model.InvalidBuildTriggerException:
|
|
|
|
abort(404)
|
|
|
|
return
|
|
|
|
|
|
|
|
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
|
|
|
|
existing_config_dict = json.loads(trigger.config)
|
|
|
|
if handler.is_active(existing_config_dict):
|
|
|
|
abort(400)
|
|
|
|
return
|
|
|
|
|
|
|
|
user_permission = UserPermission(trigger.connected_user.username)
|
|
|
|
if user_permission.can():
|
|
|
|
new_config_dict = request.get_json()
|
|
|
|
|
2014-02-21 22:09:56 +00:00
|
|
|
token_name = 'Build Trigger: %s' % trigger.service.name
|
|
|
|
token = model.create_delegate_token(namespace, repository, token_name,
|
|
|
|
'write')
|
|
|
|
|
2014-02-20 23:57:49 +00:00
|
|
|
try:
|
2014-02-25 23:22:55 +00:00
|
|
|
repository_path = '%s/%s' % (trigger.repository.namespace,
|
|
|
|
trigger.repository.name)
|
2014-02-26 00:39:43 +00:00
|
|
|
path = url_for('webhooks.build_trigger_webhook',
|
|
|
|
repository=repository_path, trigger_uuid=trigger.uuid)
|
2014-02-21 22:09:56 +00:00
|
|
|
authed_url = _prepare_webhook_url(app.config['URL_SCHEME'], '$token',
|
|
|
|
token.code, app.config['URL_HOST'],
|
|
|
|
path)
|
|
|
|
|
2014-02-24 21:36:49 +00:00
|
|
|
final_config = handler.activate(trigger.uuid, authed_url,
|
|
|
|
trigger.auth_token, new_config_dict)
|
2014-02-20 23:57:49 +00:00
|
|
|
except TriggerActivationException as e:
|
2014-02-21 22:09:56 +00:00
|
|
|
token.delete_instance()
|
2014-02-26 23:43:16 +00:00
|
|
|
return request_error(message=e.message)
|
2014-02-20 23:57:49 +00:00
|
|
|
|
|
|
|
# Save the updated config.
|
2014-02-24 21:36:49 +00:00
|
|
|
trigger.config = json.dumps(final_config)
|
2014-02-21 22:09:56 +00:00
|
|
|
trigger.write_token = token
|
2014-02-20 23:57:49 +00:00
|
|
|
trigger.save()
|
|
|
|
|
2014-02-25 23:22:55 +00:00
|
|
|
# Log the trigger setup.
|
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
log_action('setup_repo_trigger', namespace,
|
|
|
|
{'repo': repository, 'namespace': namespace,
|
|
|
|
'trigger_id': trigger.uuid, 'service': trigger.service.name,
|
|
|
|
'config': final_config}, repo=repo)
|
|
|
|
|
2014-02-20 23:57:49 +00:00
|
|
|
return jsonify(trigger_view(trigger))
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2014-02-26 00:39:43 +00:00
|
|
|
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>/start',
|
|
|
|
methods=['POST'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def manually_start_build_trigger(namespace, repository, trigger_uuid):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
trigger = model.get_build_trigger(namespace, repository, trigger_uuid)
|
|
|
|
except model.InvalidBuildTriggerException:
|
|
|
|
abort(404)
|
|
|
|
return
|
|
|
|
|
|
|
|
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
|
|
|
|
existing_config_dict = json.loads(trigger.config)
|
2014-02-26 00:53:41 +00:00
|
|
|
if not handler.is_active(existing_config_dict):
|
2014-02-26 00:39:43 +00:00
|
|
|
abort(400)
|
|
|
|
return
|
|
|
|
|
|
|
|
specs = handler.manual_start(trigger.auth_token,
|
|
|
|
json.loads(trigger.config))
|
|
|
|
dockerfile_id, tags, name, subdir = specs
|
|
|
|
|
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
|
|
|
|
build_request = start_build(repo, dockerfile_id, tags, name, subdir, True)
|
|
|
|
|
|
|
|
resp = jsonify(build_status_view(build_request, True))
|
|
|
|
repo_string = '%s/%s' % (namespace, repository)
|
|
|
|
resp.headers['Location'] = url_for('api.get_repo_build_status',
|
|
|
|
repository=repo_string,
|
|
|
|
build_uuid=build_request.uuid)
|
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2014-02-19 21:08:33 +00:00
|
|
|
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>/builds',
|
|
|
|
methods=['GET'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def list_trigger_recent_builds(namespace, repository, trigger_uuid):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
limit = request.args.get('limit', 5)
|
|
|
|
builds = model.list_trigger_builds(namespace, repository, trigger_uuid,
|
|
|
|
limit)
|
|
|
|
return jsonify({
|
|
|
|
'builds': [build_status_view(build, True) for build in builds]
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
2014-02-20 23:57:49 +00:00
|
|
|
|
|
|
|
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>/sources',
|
|
|
|
methods=['GET'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def list_trigger_build_sources(namespace, repository, trigger_uuid):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
try:
|
|
|
|
trigger = model.get_build_trigger(namespace, repository, trigger_uuid)
|
|
|
|
except model.InvalidBuildTriggerException:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
user_permission = UserPermission(trigger.connected_user.username)
|
|
|
|
if user_permission.can():
|
|
|
|
trigger_handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'sources': trigger_handler.list_build_sources(trigger.auth_token)
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-18 20:50:15 +00:00
|
|
|
@api.route('/repository/<path:repository>/trigger/', methods=['GET'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def list_build_triggers(namespace, repository):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
triggers = model.list_build_triggers(namespace, repository)
|
|
|
|
return jsonify({
|
|
|
|
'triggers': [trigger_view(trigger) for trigger in triggers]
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
|
|
|
@api.route('/repository/<path:repository>/trigger/<trigger_uuid>',
|
|
|
|
methods=['DELETE'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def delete_build_trigger(namespace, repository, trigger_uuid):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2014-02-24 21:36:49 +00:00
|
|
|
try:
|
|
|
|
trigger = model.get_build_trigger(namespace, repository, trigger_uuid)
|
|
|
|
except model.InvalidBuildTriggerException:
|
|
|
|
abort(404)
|
|
|
|
return
|
|
|
|
|
|
|
|
handler = BuildTrigger.get_trigger_for_service(trigger.service.name)
|
|
|
|
config_dict = json.loads(trigger.config)
|
|
|
|
if handler.is_active(config_dict):
|
|
|
|
try:
|
|
|
|
handler.deactivate(trigger.auth_token, config_dict)
|
|
|
|
except TriggerDeactivationException as ex:
|
|
|
|
# We are just going to eat this error
|
|
|
|
logger.warning('Trigger deactivation problem.', ex)
|
|
|
|
|
2014-02-25 23:22:55 +00:00
|
|
|
log_action('delete_repo_trigger', namespace,
|
|
|
|
{'repo': repository, 'trigger_id': trigger_uuid,
|
|
|
|
'service': trigger.service.name, 'config': config_dict},
|
|
|
|
repo=model.get_repository(namespace, repository))
|
|
|
|
|
2014-02-24 21:36:49 +00:00
|
|
|
trigger.delete_instance()
|
2014-02-18 20:50:15 +00:00
|
|
|
return make_response('No Content', 204)
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/filedrop/', methods=['POST'])
|
2013-11-15 21:12:12 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-11-15 21:12:12 +00:00
|
|
|
def get_filedrop_url():
|
|
|
|
mime_type = request.get_json()['mimeType']
|
|
|
|
(url, file_id) = user_files.prepare_for_drop(mime_type)
|
|
|
|
return jsonify({
|
|
|
|
'url': url,
|
|
|
|
'file_id': file_id
|
|
|
|
})
|
|
|
|
|
2013-11-04 23:52:38 +00:00
|
|
|
def role_view(repo_perm_obj):
|
2013-09-27 18:56:14 +00:00
|
|
|
return {
|
2013-11-01 23:34:46 +00:00
|
|
|
'role': repo_perm_obj.role.name,
|
2013-09-27 18:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-11-21 00:43:19 +00:00
|
|
|
def wrap_role_view_user(role_json, user):
|
|
|
|
role_json['is_robot'] = user.robot
|
|
|
|
return role_json
|
|
|
|
|
|
|
|
|
2013-11-23 01:14:44 +00:00
|
|
|
def wrap_role_view_org(role_json, user, org_members):
|
|
|
|
role_json['is_org_member'] = user.robot or user.username in org_members
|
2013-11-05 22:10:14 +00:00
|
|
|
return role_json
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/image/', methods=['GET'])
|
2013-10-10 04:40:25 +00:00
|
|
|
@parse_repository_name
|
|
|
|
def list_repository_images(namespace, repository):
|
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can() or model.repository_is_public(namespace, repository):
|
|
|
|
all_images = model.get_repository_images(namespace, repository)
|
|
|
|
all_tags = model.list_repository_tags(namespace, repository)
|
|
|
|
|
|
|
|
tags_by_image_id = defaultdict(list)
|
|
|
|
for tag in all_tags:
|
|
|
|
tags_by_image_id[tag.image.docker_image_id].append(tag.name)
|
|
|
|
|
|
|
|
|
|
|
|
def add_tags(image_json):
|
|
|
|
image_json['tags'] = tags_by_image_id[image_json['id']]
|
|
|
|
return image_json
|
|
|
|
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'images': [add_tags(image_view(image)) for image in all_images]
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/image/<image_id>',
|
2013-10-19 02:28:46 +00:00
|
|
|
methods=['GET'])
|
|
|
|
@parse_repository_name
|
|
|
|
def get_image(namespace, repository, image_id):
|
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can() or model.repository_is_public(namespace, repository):
|
|
|
|
image = model.get_repo_image(namespace, repository, image_id)
|
|
|
|
if not image:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return jsonify(image_view(image))
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/image/<image_id>/changes',
|
2013-10-17 22:25:19 +00:00
|
|
|
methods=['GET'])
|
2013-10-20 05:18:31 +00:00
|
|
|
@cache_control(max_age=60*60) # Cache for one hour
|
2013-10-17 22:25:19 +00:00
|
|
|
@parse_repository_name
|
2013-10-19 02:28:46 +00:00
|
|
|
def get_image_changes(namespace, repository, image_id):
|
2013-10-17 22:25:19 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can() or model.repository_is_public(namespace, repository):
|
2014-02-16 22:38:47 +00:00
|
|
|
image = model.get_repo_image(namespace, repository, image_id)
|
|
|
|
|
|
|
|
if not image:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
uuid = image.storage and image.storage.uuid
|
|
|
|
diffs_path = store.image_file_diffs_path(namespace, repository, image_id,
|
|
|
|
uuid)
|
2013-10-18 18:31:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
response_json = store.get_content(diffs_path)
|
|
|
|
return make_response(response_json)
|
|
|
|
except IOError:
|
|
|
|
abort(404)
|
2013-10-17 22:25:19 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2014-01-31 22:54:01 +00:00
|
|
|
@api.route('/repository/<path:repository>/tag/<tag>',
|
2014-01-06 20:20:58 +00:00
|
|
|
methods=['DELETE'])
|
|
|
|
@parse_repository_name
|
|
|
|
def delete_full_tag(namespace, repository, tag):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2014-02-16 22:38:47 +00:00
|
|
|
model.delete_tag(namespace, repository, tag)
|
|
|
|
model.garbage_collect_repository(namespace, repository)
|
2014-01-06 20:20:58 +00:00
|
|
|
|
|
|
|
username = current_user.db_user().username
|
|
|
|
log_action('delete_tag', namespace,
|
|
|
|
{'username': username, 'repo': repository, 'tag': tag},
|
|
|
|
repo=model.get_repository(namespace, repository))
|
|
|
|
|
|
|
|
return make_response('Deleted', 204)
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/tag/<tag>/images',
|
2013-09-28 00:03:07 +00:00
|
|
|
methods=['GET'])
|
2013-09-27 21:01:45 +00:00
|
|
|
@parse_repository_name
|
|
|
|
def list_tag_images(namespace, repository, tag):
|
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
2013-09-28 04:05:32 +00:00
|
|
|
if permission.can() or model.repository_is_public(namespace, repository):
|
2013-11-06 22:09:22 +00:00
|
|
|
try:
|
|
|
|
tag_image = model.get_tag_image(namespace, repository, tag)
|
|
|
|
except model.DataModelException:
|
|
|
|
abort(404)
|
|
|
|
|
2013-09-30 19:30:00 +00:00
|
|
|
parent_images = model.get_parent_images(tag_image)
|
|
|
|
|
|
|
|
parents = list(parent_images)
|
|
|
|
parents.reverse()
|
|
|
|
all_images = [tag_image] + parents
|
2013-09-28 00:03:07 +00:00
|
|
|
|
2013-09-27 21:01:45 +00:00
|
|
|
return jsonify({
|
2013-09-30 19:30:00 +00:00
|
|
|
'images': [image_view(image) for image in all_images]
|
2013-09-27 21:01:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/team/',
|
2013-11-04 21:18:40 +00:00
|
|
|
methods=['GET'])
|
2013-11-02 01:48:10 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def list_repo_team_permissions(namespace, repository):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
repo_perms = model.get_all_repo_teams(namespace, repository)
|
|
|
|
|
|
|
|
return jsonify({
|
2013-11-04 23:52:38 +00:00
|
|
|
'permissions': {repo_perm.team.name: role_view(repo_perm)
|
2013-11-02 01:48:10 +00:00
|
|
|
for repo_perm in repo_perms}
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/user/',
|
2013-11-04 21:18:40 +00:00
|
|
|
methods=['GET'])
|
2013-09-30 21:51:07 +00:00
|
|
|
@api_login_required
|
2013-09-27 17:24:07 +00:00
|
|
|
@parse_repository_name
|
2013-11-02 01:48:10 +00:00
|
|
|
def list_repo_user_permissions(namespace, repository):
|
2013-09-27 17:24:07 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2013-11-23 01:14:44 +00:00
|
|
|
# Lookup the organization (if any).
|
|
|
|
org = None
|
2013-11-05 22:10:14 +00:00
|
|
|
try:
|
2013-11-23 01:14:44 +00:00
|
|
|
org = model.get_organization(namespace) # Will raise an error if not org
|
2013-11-05 22:10:14 +00:00
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# This repository isn't under an org
|
|
|
|
pass
|
2013-09-27 17:24:07 +00:00
|
|
|
|
2013-11-23 01:14:44 +00:00
|
|
|
# Determine how to wrap the role(s).
|
|
|
|
def wrapped_role_view(repo_perm):
|
|
|
|
return wrap_role_view_user(role_view(repo_perm), repo_perm.user)
|
|
|
|
|
|
|
|
role_view_func = wrapped_role_view
|
|
|
|
|
|
|
|
if org:
|
|
|
|
org_members = model.get_organization_member_set(namespace)
|
|
|
|
current_func = role_view_func
|
|
|
|
|
|
|
|
def wrapped_role_org_view(repo_perm):
|
2013-12-19 22:06:04 +00:00
|
|
|
return wrap_role_view_org(current_func(repo_perm), repo_perm.user,
|
|
|
|
org_members)
|
2013-11-23 01:14:44 +00:00
|
|
|
|
|
|
|
role_view_func = wrapped_role_org_view
|
|
|
|
|
|
|
|
# Load and return the permissions.
|
2013-11-05 22:10:14 +00:00
|
|
|
repo_perms = model.get_all_repo_users(namespace, repository)
|
2013-09-27 17:24:07 +00:00
|
|
|
return jsonify({
|
2013-11-05 22:10:14 +00:00
|
|
|
'permissions': {perm.user.username: role_view_func(perm)
|
|
|
|
for perm in repo_perms}
|
2013-09-27 17:24:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
2013-09-27 18:56:14 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/user/<username>',
|
2013-09-27 18:56:14 +00:00
|
|
|
methods=['GET'])
|
2013-09-30 21:51:07 +00:00
|
|
|
@api_login_required
|
2013-09-27 18:56:14 +00:00
|
|
|
@parse_repository_name
|
2013-11-02 01:48:10 +00:00
|
|
|
def get_user_permissions(namespace, repository, username):
|
2013-09-27 18:56:14 +00:00
|
|
|
logger.debug('Get repo: %s/%s permissions for user %s' %
|
|
|
|
(namespace, repository, username))
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2013-09-27 19:53:39 +00:00
|
|
|
perm = model.get_user_reponame_permission(username, namespace, repository)
|
2013-11-21 00:43:19 +00:00
|
|
|
perm_view = wrap_role_view_user(role_view(perm), perm.user)
|
2013-11-05 22:10:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
model.get_organization(namespace)
|
|
|
|
org_members = model.get_organization_member_set(namespace)
|
2013-11-23 01:14:44 +00:00
|
|
|
perm_view = wrap_role_view_org(perm_view, perm.user, org_members)
|
2013-11-05 22:10:14 +00:00
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# This repository is not part of an organization
|
|
|
|
pass
|
|
|
|
|
|
|
|
return jsonify(perm_view)
|
2013-09-27 18:56:14 +00:00
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/team/<teamname>',
|
2013-11-02 01:48:10 +00:00
|
|
|
methods=['GET'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def get_team_permissions(namespace, repository, teamname):
|
|
|
|
logger.debug('Get repo: %s/%s permissions for team %s' %
|
|
|
|
(namespace, repository, teamname))
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2013-11-07 17:54:21 +00:00
|
|
|
perm = model.get_team_reponame_permission(teamname, namespace, repository)
|
2013-11-04 23:52:38 +00:00
|
|
|
return jsonify(role_view(perm))
|
2013-11-02 01:48:10 +00:00
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/user/<username>',
|
2013-09-27 18:56:14 +00:00
|
|
|
methods=['PUT', 'POST'])
|
2013-09-30 21:51:07 +00:00
|
|
|
@api_login_required
|
2013-09-27 17:24:07 +00:00
|
|
|
@parse_repository_name
|
2013-11-02 01:48:10 +00:00
|
|
|
def change_user_permissions(namespace, repository, username):
|
2013-09-27 17:24:07 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
new_permission = request.get_json()
|
|
|
|
|
2013-09-27 18:56:14 +00:00
|
|
|
logger.debug('Setting permission to: %s for user %s' %
|
|
|
|
(new_permission['role'], username))
|
2013-09-27 19:53:39 +00:00
|
|
|
|
2013-11-07 17:54:21 +00:00
|
|
|
perm = model.set_user_repo_permission(username, namespace, repository,
|
|
|
|
new_permission['role'])
|
2013-11-21 00:43:19 +00:00
|
|
|
perm_view = wrap_role_view_user(role_view(perm), perm.user)
|
2013-11-05 22:10:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
model.get_organization(namespace)
|
|
|
|
org_members = model.get_organization_member_set(namespace)
|
2013-11-23 01:14:44 +00:00
|
|
|
perm_view = wrap_role_view_org(perm_view, perm.user, org_members)
|
2013-11-05 22:10:14 +00:00
|
|
|
except model.InvalidOrganizationException:
|
|
|
|
# This repository is not part of an organization
|
|
|
|
pass
|
2013-11-08 04:35:27 +00:00
|
|
|
except model.DataModelException as ex:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(exception=ex)
|
2013-11-08 04:35:27 +00:00
|
|
|
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('change_repo_permission', namespace,
|
2013-12-19 22:06:04 +00:00
|
|
|
{'username': username, 'repo': repository,
|
|
|
|
'role': new_permission['role']},
|
2013-11-27 07:29:31 +00:00
|
|
|
repo=model.get_repository(namespace, repository))
|
2013-11-05 22:10:14 +00:00
|
|
|
|
|
|
|
resp = jsonify(perm_view)
|
2013-09-27 18:56:14 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
2013-09-28 00:03:07 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/team/<teamname>',
|
2013-11-02 01:48:10 +00:00
|
|
|
methods=['PUT', 'POST'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def change_team_permissions(namespace, repository, teamname):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
new_permission = request.get_json()
|
|
|
|
|
|
|
|
logger.debug('Setting permission to: %s for team %s' %
|
|
|
|
(new_permission['role'], teamname))
|
|
|
|
|
2013-11-07 17:54:21 +00:00
|
|
|
perm = model.set_team_repo_permission(teamname, namespace, repository,
|
|
|
|
new_permission['role'])
|
2013-11-02 01:48:10 +00:00
|
|
|
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('change_repo_permission', namespace,
|
2013-12-19 22:06:04 +00:00
|
|
|
{'team': teamname, 'repo': repository,
|
|
|
|
'role': new_permission['role']},
|
2013-11-27 07:29:31 +00:00
|
|
|
repo=model.get_repository(namespace, repository))
|
|
|
|
|
2013-11-04 23:52:38 +00:00
|
|
|
resp = jsonify(role_view(perm))
|
2013-11-02 01:48:10 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/user/<username>',
|
2013-09-27 18:56:14 +00:00
|
|
|
methods=['DELETE'])
|
2013-09-30 21:51:07 +00:00
|
|
|
@api_login_required
|
2013-09-27 18:56:14 +00:00
|
|
|
@parse_repository_name
|
2013-11-02 01:48:10 +00:00
|
|
|
def delete_user_permissions(namespace, repository, username):
|
2013-09-27 18:56:14 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2013-11-08 04:35:27 +00:00
|
|
|
try:
|
|
|
|
model.delete_user_permission(username, namespace, repository)
|
|
|
|
except model.DataModelException as ex:
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(exception=ex)
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('delete_repo_permission', namespace,
|
|
|
|
{'username': username, 'repo': repository},
|
2013-11-29 05:04:50 +00:00
|
|
|
repo=model.get_repository(namespace, repository))
|
2013-11-08 04:35:27 +00:00
|
|
|
|
2013-09-27 18:56:14 +00:00
|
|
|
return make_response('Deleted', 204)
|
2013-09-27 17:24:07 +00:00
|
|
|
|
2013-09-27 18:56:14 +00:00
|
|
|
abort(403) # Permission denied
|
2013-10-02 04:48:03 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/permissions/team/<teamname>',
|
2013-11-02 01:48:10 +00:00
|
|
|
methods=['DELETE'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def delete_team_permissions(namespace, repository, teamname):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2013-11-07 17:54:21 +00:00
|
|
|
model.delete_team_permission(teamname, namespace, repository)
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('delete_repo_permission', namespace,
|
|
|
|
{'team': teamname, 'repo': repository},
|
2013-11-29 05:04:50 +00:00
|
|
|
repo=model.get_repository(namespace, repository))
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-11-02 01:48:10 +00:00
|
|
|
return make_response('Deleted', 204)
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
def token_view(token_obj):
|
|
|
|
return {
|
|
|
|
'friendlyName': token_obj.friendly_name,
|
|
|
|
'code': token_obj.code,
|
|
|
|
'role': token_obj.role.name,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/tokens/', methods=['GET'])
|
2013-10-16 18:24:10 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def list_repo_tokens(namespace, repository):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
tokens = model.get_repository_delegate_tokens(namespace, repository)
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'tokens': {token.code: token_view(token) for token in tokens}
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/tokens/<code>', methods=['GET'])
|
2013-10-16 18:24:10 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def get_tokens(namespace, repository, code):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2014-02-03 23:18:33 +00:00
|
|
|
try:
|
|
|
|
perm = model.get_repo_delegate_token(namespace, repository, code)
|
|
|
|
except model.InvalidTokenException:
|
|
|
|
abort(404)
|
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
return jsonify(token_view(perm))
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/tokens/', methods=['POST'])
|
2013-10-16 18:24:10 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def create_token(namespace, repository):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
token_params = request.get_json()
|
|
|
|
|
|
|
|
token = model.create_delegate_token(namespace, repository,
|
|
|
|
token_params['friendlyName'])
|
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
log_action('add_repo_accesstoken', namespace,
|
|
|
|
{'repo': repository, 'token': token_params['friendlyName']},
|
2013-11-27 07:29:31 +00:00
|
|
|
repo = model.get_repository(namespace, repository))
|
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
resp = jsonify(token_view(token))
|
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/tokens/<code>', methods=['PUT'])
|
2013-10-16 18:24:10 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def change_token(namespace, repository, code):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
new_permission = request.get_json()
|
|
|
|
|
|
|
|
logger.debug('Setting permission to: %s for code %s' %
|
|
|
|
(new_permission['role'], code))
|
|
|
|
|
|
|
|
token = model.set_repo_delegate_token_role(namespace, repository, code,
|
|
|
|
new_permission['role'])
|
|
|
|
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('change_repo_permission', namespace,
|
2013-12-19 22:06:04 +00:00
|
|
|
{'repo': repository, 'token': token.friendly_name, 'code': code,
|
|
|
|
'role': new_permission['role']},
|
2013-11-29 05:04:50 +00:00
|
|
|
repo = model.get_repository(namespace, repository))
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
resp = jsonify(token_view(token))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/tokens/<code>',
|
2013-10-16 18:24:10 +00:00
|
|
|
methods=['DELETE'])
|
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
|
|
|
def delete_token(namespace, repository, code):
|
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
2013-11-29 05:04:50 +00:00
|
|
|
token = model.delete_delegate_token(namespace, repository, code)
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('delete_repo_accesstoken', namespace,
|
2013-12-19 22:06:04 +00:00
|
|
|
{'repo': repository, 'token': token.friendly_name,
|
|
|
|
'code': code},
|
2013-11-29 05:04:50 +00:00
|
|
|
repo = model.get_repository(namespace, repository))
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-10-16 18:24:10 +00:00
|
|
|
return make_response('Deleted', 204)
|
|
|
|
|
|
|
|
abort(403) # Permission denied
|
|
|
|
|
|
|
|
|
2013-10-08 15:29:42 +00:00
|
|
|
def subscription_view(stripe_subscription, used_repos):
|
2013-10-02 04:48:03 +00:00
|
|
|
return {
|
2013-10-02 05:40:11 +00:00
|
|
|
'currentPeriodStart': stripe_subscription.current_period_start,
|
|
|
|
'currentPeriodEnd': stripe_subscription.current_period_end,
|
2013-10-02 04:48:03 +00:00
|
|
|
'plan': stripe_subscription.plan.id,
|
2013-10-02 05:40:11 +00:00
|
|
|
'usedPrivateRepos': used_repos,
|
2013-10-02 04:48:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/card', methods=['GET'])
|
2013-11-15 23:17:12 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
|
|
|
def get_user_card():
|
2013-11-15 23:17:12 +00:00
|
|
|
user = current_user.db_user()
|
2013-11-19 22:06:17 +00:00
|
|
|
return get_card(user)
|
2013-11-15 23:17:12 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/card', methods=['GET'])
|
2013-11-15 23:17:12 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('get_user_card')
|
2013-12-25 02:56:03 +00:00
|
|
|
def get_org_card(orgname):
|
2013-11-15 23:17:12 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
organization = model.get_organization(orgname)
|
2013-11-19 22:06:17 +00:00
|
|
|
return get_card(organization)
|
2013-11-15 23:17:12 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/card', methods=['POST'])
|
2013-11-15 23:17:12 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
|
|
|
def set_user_card():
|
2013-11-15 23:17:12 +00:00
|
|
|
user = current_user.db_user()
|
|
|
|
token = request.get_json()['token']
|
2013-11-27 07:29:31 +00:00
|
|
|
response = set_card(user, token)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('account_change_cc', user.username)
|
2013-11-27 07:29:31 +00:00
|
|
|
return response
|
2013-11-15 23:17:12 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/card', methods=['POST'])
|
2013-11-15 23:17:12 +00:00
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('set_user_card')
|
2013-12-25 02:56:03 +00:00
|
|
|
def set_org_card(orgname):
|
2013-11-15 23:17:12 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
organization = model.get_organization(orgname)
|
|
|
|
token = request.get_json()['token']
|
2013-11-27 07:29:31 +00:00
|
|
|
response = set_card(organization, token)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('account_change_cc', orgname)
|
2013-11-27 07:29:31 +00:00
|
|
|
return response
|
2013-11-15 23:17:12 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
def set_card(user, token):
|
|
|
|
if user.stripe_id:
|
|
|
|
cus = stripe.Customer.retrieve(user.stripe_id)
|
|
|
|
if cus:
|
2013-11-19 22:06:17 +00:00
|
|
|
try:
|
|
|
|
cus.card = token
|
|
|
|
cus.save()
|
|
|
|
except stripe.CardError as e:
|
|
|
|
return carderror_response(e)
|
2014-02-03 23:18:33 +00:00
|
|
|
except stripe.InvalidRequestError as e:
|
|
|
|
return carderror_response(e)
|
2013-11-15 23:17:12 +00:00
|
|
|
|
|
|
|
return get_card(user)
|
|
|
|
|
|
|
|
|
|
|
|
def get_card(user):
|
|
|
|
card_info = {
|
|
|
|
'is_valid': False
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.stripe_id:
|
|
|
|
cus = stripe.Customer.retrieve(user.stripe_id)
|
|
|
|
if cus and cus.default_card:
|
|
|
|
# Find the default card.
|
|
|
|
default_card = None
|
|
|
|
for card in cus.cards.data:
|
|
|
|
if card.id == cus.default_card:
|
|
|
|
default_card = card
|
|
|
|
break
|
|
|
|
|
|
|
|
if default_card:
|
|
|
|
card_info = {
|
2013-12-19 22:06:04 +00:00
|
|
|
'owner': default_card.name,
|
|
|
|
'type': default_card.type,
|
|
|
|
'last4': default_card.last4
|
2013-11-15 23:17:12 +00:00
|
|
|
}
|
|
|
|
|
2013-11-19 22:06:17 +00:00
|
|
|
return jsonify({'card': card_info})
|
2013-11-15 23:17:12 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/plan', methods=['PUT'])
|
2013-10-02 04:48:03 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
|
|
|
def update_user_subscription():
|
2013-10-02 04:48:03 +00:00
|
|
|
request_data = request.get_json()
|
|
|
|
plan = request_data['plan']
|
2013-11-06 19:19:56 +00:00
|
|
|
token = request_data['token'] if 'token' in request_data else None
|
2013-10-15 18:48:49 +00:00
|
|
|
user = current_user.db_user()
|
2013-12-19 22:10:09 +00:00
|
|
|
return subscribe(user, plan, token, False) # Business features not required
|
2013-11-12 00:02:42 +00:00
|
|
|
|
|
|
|
|
2013-11-19 22:06:17 +00:00
|
|
|
def carderror_response(e):
|
|
|
|
resp = jsonify({
|
|
|
|
'carderror': e.message,
|
|
|
|
})
|
|
|
|
resp.status_code = 402
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
def subscribe(user, plan, token, require_business_plan):
|
2013-11-06 19:48:59 +00:00
|
|
|
plan_found = None
|
2013-12-19 22:06:04 +00:00
|
|
|
for plan_obj in PLANS:
|
2013-11-06 19:48:59 +00:00
|
|
|
if plan_obj['stripeId'] == plan:
|
|
|
|
plan_found = plan_obj
|
|
|
|
|
2013-12-23 19:50:01 +00:00
|
|
|
if not plan_found or plan_found['deprecated']:
|
|
|
|
logger.warning('Plan not found or deprecated: %s', plan)
|
2013-11-06 19:48:59 +00:00
|
|
|
abort(404)
|
|
|
|
|
2013-12-23 19:50:01 +00:00
|
|
|
if (require_business_plan and not plan_found['bus_features'] and not
|
|
|
|
plan_found['price'] == 0):
|
|
|
|
logger.warning('Business attempting to subscribe to personal plan: %s',
|
|
|
|
user.username)
|
2014-01-24 22:00:42 +00:00
|
|
|
return request_error(message='No matching plan found')
|
2013-12-19 22:06:04 +00:00
|
|
|
|
2013-10-02 05:40:11 +00:00
|
|
|
private_repos = model.get_private_repo_count(user.username)
|
2013-10-02 04:48:03 +00:00
|
|
|
|
2013-11-12 00:02:42 +00:00
|
|
|
# This is the default response
|
|
|
|
response_json = {
|
|
|
|
'plan': plan,
|
|
|
|
'usedPrivateRepos': private_repos,
|
|
|
|
}
|
|
|
|
status_code = 200
|
|
|
|
|
2013-10-02 04:48:03 +00:00
|
|
|
if not user.stripe_id:
|
2013-11-12 00:02:42 +00:00
|
|
|
# Check if a non-paying user is trying to subscribe to a free plan
|
|
|
|
if not plan_found['price'] == 0:
|
2013-11-15 20:31:05 +00:00
|
|
|
# They want a real paying plan, create the customer and plan
|
2013-11-12 00:02:42 +00:00
|
|
|
# simultaneously
|
|
|
|
card = token
|
2013-11-19 22:06:17 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
cus = stripe.Customer.create(email=user.email, plan=plan, card=card)
|
|
|
|
user.stripe_id = cus.id
|
|
|
|
user.save()
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('account_change_plan', user.username, {'plan': plan})
|
2013-11-19 22:06:17 +00:00
|
|
|
except stripe.CardError as e:
|
|
|
|
return carderror_response(e)
|
2013-10-08 15:29:42 +00:00
|
|
|
|
2013-11-12 00:02:42 +00:00
|
|
|
response_json = subscription_view(cus.subscription, private_repos)
|
|
|
|
status_code = 201
|
2013-10-02 04:48:03 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
# Change the plan
|
|
|
|
cus = stripe.Customer.retrieve(user.stripe_id)
|
2013-10-02 06:05:53 +00:00
|
|
|
|
2013-11-06 19:48:59 +00:00
|
|
|
if plan_found['price'] == 0:
|
2013-11-11 16:58:30 +00:00
|
|
|
if cus.subscription is not None:
|
|
|
|
# We only have to cancel the subscription if they actually have one
|
|
|
|
cus.cancel_subscription()
|
|
|
|
cus.save()
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('account_change_plan', user.username, {'plan': plan})
|
2013-10-02 06:05:53 +00:00
|
|
|
|
2013-10-04 18:35:51 +00:00
|
|
|
else:
|
|
|
|
# User may have been a previous customer who is resubscribing
|
2013-11-06 19:19:56 +00:00
|
|
|
if token:
|
|
|
|
cus.card = token
|
2013-10-02 16:43:25 +00:00
|
|
|
|
2013-11-19 22:06:17 +00:00
|
|
|
cus.plan = plan
|
|
|
|
|
|
|
|
try:
|
|
|
|
cus.save()
|
|
|
|
except stripe.CardError as e:
|
|
|
|
return carderror_response(e)
|
|
|
|
|
2013-10-04 18:35:51 +00:00
|
|
|
response_json = subscription_view(cus.subscription, private_repos)
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('account_change_plan', user.username, {'plan': plan})
|
2013-10-02 06:05:53 +00:00
|
|
|
|
2013-11-12 00:02:42 +00:00
|
|
|
resp = jsonify(response_json)
|
|
|
|
resp.status_code = status_code
|
|
|
|
return resp
|
2013-10-02 06:05:53 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/invoices', methods=['GET'])
|
2013-12-21 03:38:53 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
def list_user_invoices():
|
2013-12-21 03:38:53 +00:00
|
|
|
user = current_user.db_user()
|
|
|
|
if not user.stripe_id:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return get_invoices(user.stripe_id)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/invoices', methods=['GET'])
|
2013-11-13 22:47:45 +00:00
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('list_user_invoices')
|
2013-12-25 02:56:03 +00:00
|
|
|
def list_org_invoices(orgname):
|
2013-12-21 03:38:53 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
organization = model.get_organization(orgname)
|
|
|
|
if not organization.stripe_id:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return get_invoices(organization.stripe_id)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
|
|
|
def get_invoices(customer_id):
|
2013-11-13 22:47:45 +00:00
|
|
|
def invoice_view(i):
|
|
|
|
return {
|
|
|
|
'id': i.id,
|
|
|
|
'date': i.date,
|
|
|
|
'period_start': i.period_start,
|
|
|
|
'period_end': i.period_end,
|
|
|
|
'paid': i.paid,
|
|
|
|
'amount_due': i.amount_due,
|
|
|
|
'next_payment_attempt': i.next_payment_attempt,
|
|
|
|
'attempted': i.attempted,
|
|
|
|
'closed': i.closed,
|
|
|
|
'total': i.total,
|
2013-12-04 19:32:55 +00:00
|
|
|
'plan': i.lines.data[0].plan.id if i.lines.data[0].plan else None
|
2013-11-13 22:47:45 +00:00
|
|
|
}
|
|
|
|
|
2013-12-21 03:38:53 +00:00
|
|
|
invoices = stripe.Invoice.all(customer=customer_id, count=12)
|
|
|
|
return jsonify({
|
|
|
|
'invoices': [invoice_view(i) for i in invoices.data]
|
|
|
|
})
|
2013-11-13 22:47:45 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/plan', methods=['PUT'])
|
2013-11-06 19:19:56 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('update_user_subscription')
|
2013-12-25 02:56:03 +00:00
|
|
|
def update_org_subscription(orgname):
|
2013-11-06 19:19:56 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
request_data = request.get_json()
|
|
|
|
plan = request_data['plan']
|
|
|
|
token = request_data['token'] if 'token' in request_data else None
|
|
|
|
organization = model.get_organization(orgname)
|
2013-12-19 22:10:09 +00:00
|
|
|
return subscribe(organization, plan, token, True) # Business plan required
|
2013-11-06 19:19:56 +00:00
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/plan', methods=['GET'])
|
2013-10-02 06:05:53 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-12-26 22:45:16 +00:00
|
|
|
def get_user_subscription():
|
2013-10-15 18:48:49 +00:00
|
|
|
user = current_user.db_user()
|
2013-10-04 18:35:51 +00:00
|
|
|
private_repos = model.get_private_repo_count(user.username)
|
2013-10-02 06:05:53 +00:00
|
|
|
|
|
|
|
if user.stripe_id:
|
|
|
|
cus = stripe.Customer.retrieve(user.stripe_id)
|
|
|
|
|
2013-11-01 23:13:58 +00:00
|
|
|
if cus.subscription:
|
2013-10-04 18:35:51 +00:00
|
|
|
return jsonify(subscription_view(cus.subscription, private_repos))
|
|
|
|
|
|
|
|
return jsonify({
|
|
|
|
'plan': 'free',
|
|
|
|
'usedPrivateRepos': private_repos,
|
2013-10-08 15:29:42 +00:00
|
|
|
})
|
2013-11-06 19:19:56 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/plan', methods=['GET'])
|
2013-11-06 19:19:56 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
@internal_api_call
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('get_user_subscription')
|
2013-11-06 19:19:56 +00:00
|
|
|
def get_org_subscription(orgname):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
private_repos = model.get_private_repo_count(orgname)
|
|
|
|
organization = model.get_organization(orgname)
|
|
|
|
if organization.stripe_id:
|
|
|
|
cus = stripe.Customer.retrieve(organization.stripe_id)
|
|
|
|
|
|
|
|
if cus.subscription:
|
|
|
|
return jsonify(subscription_view(cus.subscription, private_repos))
|
|
|
|
|
|
|
|
return jsonify({
|
2013-12-20 02:51:46 +00:00
|
|
|
'plan': 'free',
|
2013-11-06 19:19:56 +00:00
|
|
|
'usedPrivateRepos': private_repos,
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403)
|
2013-11-20 21:13:03 +00:00
|
|
|
|
|
|
|
|
2013-11-22 23:20:51 +00:00
|
|
|
def robot_view(name, token):
|
2013-11-20 21:13:03 +00:00
|
|
|
return {
|
|
|
|
'name': name,
|
2013-11-22 23:20:51 +00:00
|
|
|
'token': token,
|
2013-11-20 21:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/robots', methods=['GET'])
|
2013-11-20 21:13:03 +00:00
|
|
|
@api_login_required
|
|
|
|
def get_user_robots():
|
|
|
|
user = current_user.db_user()
|
|
|
|
robots = model.list_entity_robots(user.username)
|
|
|
|
return jsonify({
|
|
|
|
'robots': [robot_view(name, password) for name, password in robots]
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/robots', methods=['GET'])
|
2013-11-20 21:13:03 +00:00
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('get_user_robots')
|
2013-11-20 21:13:03 +00:00
|
|
|
def get_org_robots(orgname):
|
2013-12-10 20:49:34 +00:00
|
|
|
permission = OrganizationMemberPermission(orgname)
|
2013-11-20 21:13:03 +00:00
|
|
|
if permission.can():
|
|
|
|
robots = model.list_entity_robots(orgname)
|
|
|
|
return jsonify({
|
|
|
|
'robots': [robot_view(name, password) for name, password in robots]
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/robots/<robot_shortname>', methods=['PUT'])
|
2013-11-20 21:13:03 +00:00
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
def create_user_robot(robot_shortname):
|
2013-11-20 21:13:03 +00:00
|
|
|
parent = current_user.db_user()
|
|
|
|
robot, password = model.create_robot(robot_shortname, parent)
|
|
|
|
resp = jsonify(robot_view(robot.username, password))
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('create_robot', parent.username, {'robot': robot_shortname})
|
2013-11-20 21:13:03 +00:00
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/robots/<robot_shortname>',
|
2013-11-20 21:13:03 +00:00
|
|
|
methods=['PUT'])
|
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('create_user_robot')
|
2013-11-20 21:13:03 +00:00
|
|
|
def create_org_robot(orgname, robot_shortname):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
parent = model.get_organization(orgname)
|
|
|
|
robot, password = model.create_robot(robot_shortname, parent)
|
|
|
|
resp = jsonify(robot_view(robot.username, password))
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('create_robot', orgname, {'robot': robot_shortname})
|
2013-11-20 21:13:03 +00:00
|
|
|
resp.status_code = 201
|
|
|
|
return resp
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/robots/<robot_shortname>', methods=['DELETE'])
|
2013-11-20 21:13:03 +00:00
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
def delete_user_robot(robot_shortname):
|
2013-11-20 21:13:03 +00:00
|
|
|
parent = current_user.db_user()
|
|
|
|
model.delete_robot(format_robot_username(parent.username, robot_shortname))
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('delete_robot', parent.username, {'robot': robot_shortname})
|
2014-01-24 19:12:04 +00:00
|
|
|
return make_response('Deleted', 204)
|
2013-11-20 21:13:03 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/robots/<robot_shortname>',
|
2013-11-20 21:13:03 +00:00
|
|
|
methods=['DELETE'])
|
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('delete_user_robot')
|
2013-11-20 21:13:03 +00:00
|
|
|
def delete_org_robot(orgname, robot_shortname):
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
model.delete_robot(format_robot_username(orgname, robot_shortname))
|
2013-11-29 05:04:50 +00:00
|
|
|
log_action('delete_robot', orgname, {'robot': robot_shortname})
|
2014-01-24 19:12:04 +00:00
|
|
|
return make_response('Deleted', 204)
|
2013-11-20 21:13:03 +00:00
|
|
|
|
|
|
|
abort(403)
|
2013-11-27 07:29:31 +00:00
|
|
|
|
|
|
|
|
2013-12-02 19:55:04 +00:00
|
|
|
def log_view(log):
|
2013-12-19 22:06:04 +00:00
|
|
|
view = {
|
|
|
|
'kind': log.kind.name,
|
|
|
|
'metadata': json.loads(log.metadata_json),
|
|
|
|
'ip': log.ip,
|
|
|
|
'datetime': log.datetime,
|
|
|
|
}
|
2013-11-27 07:29:31 +00:00
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
if log.performer:
|
|
|
|
view['performer'] = {
|
2014-01-21 21:23:00 +00:00
|
|
|
'kind': 'user',
|
|
|
|
'name': log.performer.username,
|
2013-12-19 22:06:04 +00:00
|
|
|
'is_robot': log.performer.robot,
|
|
|
|
}
|
2013-12-02 18:50:51 +00:00
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
return view
|
2013-12-02 18:50:51 +00:00
|
|
|
|
2013-12-09 22:28:23 +00:00
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/repository/<path:repository>/logs', methods=['GET'])
|
2013-12-09 22:28:23 +00:00
|
|
|
@api_login_required
|
|
|
|
@parse_repository_name
|
2013-12-25 02:56:03 +00:00
|
|
|
def list_repo_logs(namespace, repository):
|
2013-12-09 22:28:23 +00:00
|
|
|
permission = AdministerRepositoryPermission(namespace, repository)
|
|
|
|
if permission.can():
|
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
if not repo:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
start_time = request.args.get('starttime', None)
|
|
|
|
end_time = request.args.get('endtime', None)
|
|
|
|
return get_logs(namespace, start_time, end_time, repository=repo)
|
|
|
|
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/organization/<orgname>/logs', methods=['GET'])
|
2013-12-02 19:55:04 +00:00
|
|
|
@api_login_required
|
2013-12-26 22:45:16 +00:00
|
|
|
@org_api_call('list_user_logs')
|
2013-12-25 02:56:03 +00:00
|
|
|
def list_org_logs(orgname):
|
2013-11-27 07:29:31 +00:00
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2013-12-07 00:25:27 +00:00
|
|
|
performer_name = request.args.get('performer', None)
|
2013-12-09 22:28:23 +00:00
|
|
|
start_time = request.args.get('starttime', None)
|
|
|
|
end_time = request.args.get('endtime', None)
|
2013-12-07 00:25:27 +00:00
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
return get_logs(orgname, start_time, end_time,
|
|
|
|
performer_name=performer_name)
|
2013-11-27 07:29:31 +00:00
|
|
|
|
|
|
|
abort(403)
|
2013-12-02 19:55:04 +00:00
|
|
|
|
2013-12-09 22:28:23 +00:00
|
|
|
|
2013-12-30 22:05:27 +00:00
|
|
|
@api.route('/user/logs', methods=['GET'])
|
2013-12-21 03:38:53 +00:00
|
|
|
@api_login_required
|
2013-12-25 02:56:03 +00:00
|
|
|
def list_user_logs():
|
2013-12-21 03:38:53 +00:00
|
|
|
performer_name = request.args.get('performer', None)
|
|
|
|
start_time = request.args.get('starttime', None)
|
|
|
|
end_time = request.args.get('endtime', None)
|
|
|
|
|
|
|
|
return get_logs(current_user.db_user().username, start_time, end_time,
|
|
|
|
performer_name=performer_name)
|
|
|
|
|
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
def get_logs(namespace, start_time, end_time, performer_name=None,
|
|
|
|
repository=None):
|
2013-12-09 22:28:23 +00:00
|
|
|
performer = None
|
|
|
|
if performer_name:
|
|
|
|
performer = model.get_user(performer_name)
|
|
|
|
|
|
|
|
if start_time:
|
|
|
|
try:
|
2013-12-10 02:13:21 +00:00
|
|
|
start_time = datetime.strptime(start_time + ' UTC', '%m/%d/%Y %Z')
|
2013-12-09 22:28:23 +00:00
|
|
|
except ValueError:
|
|
|
|
start_time = None
|
|
|
|
|
|
|
|
if not start_time:
|
|
|
|
start_time = datetime.today() - timedelta(7) # One week
|
|
|
|
|
|
|
|
if end_time:
|
|
|
|
try:
|
2013-12-10 02:13:21 +00:00
|
|
|
end_time = datetime.strptime(end_time + ' UTC', '%m/%d/%Y %Z')
|
|
|
|
end_time = end_time + timedelta(days=1)
|
2013-12-09 22:28:23 +00:00
|
|
|
except ValueError:
|
|
|
|
end_time = None
|
|
|
|
|
|
|
|
if not end_time:
|
|
|
|
end_time = datetime.today()
|
|
|
|
|
2013-12-19 22:06:04 +00:00
|
|
|
logs = model.list_logs(namespace, start_time, end_time, performer=performer,
|
|
|
|
repository=repository)
|
2013-12-09 22:28:23 +00:00
|
|
|
return jsonify({
|
|
|
|
'start_time': start_time,
|
|
|
|
'end_time': end_time,
|
|
|
|
'logs': [log_view(log) for log in logs]
|
|
|
|
})
|
|
|
|
|