'Update an Organization\'s Release
Update a release. This can change some metadata associated with
the release (the ref, url, and dates).
:pparam string organization_slug: the slug of the organization the
release belongs to.
:pparam string version: the version identifier of the release.
:param string ref: an optional commit reference.  This is useful if
a tagged version has been provided.
:param url url: a URL that points to the release.  This can be the
path to an online interface to the sourcecode
for instance.
:param datetime dateReleased: an optional date that indicates when
the release went live.  If not provided
the current time is assumed.
:param array commits: an optional list of commit data to be associated
with the release. Commits must include parameters
``id`` (the sha of the commit), and can optionally
include ``repository``, ``message``, ``author_name``,
``author_email``, and ``timestamp``.
:param array refs: an optional way to indicate the start and end commits
for each repository included in a release. Head commits
must include parameters ``repository`` and ``commit``
(the HEAD sha). They can optionally include ``previousCommit``
(the sha of the HEAD of the previous release), which should
be specified if this is the first time you\'ve sent commit data.
:auth: required'

@attach_scenarios([update_organization_release_scenario])
def put(self, request, organization, version):

    try:
        release = Release.objects.get(organization_id=organization, version=version)
    except Release.DoesNotExist:
        raise ResourceDoesNotExist
    if (not self.has_release_permission(request, organization, release)):
        raise PermissionDenied
    serializer = ReleaseSerializer(data=request.DATA, partial=True)
    if (not serializer.is_valid()):
        return Response(serializer.errors, status=400)
    result = serializer.object
    was_released = bool(release.date_released)
    kwargs = {}
    if result.get('dateReleased'):
        kwargs['date_released'] = result['dateReleased']
    if result.get('ref'):
        kwargs['ref'] = result['ref']
    if result.get('url'):
        kwargs['url'] = result['url']
    if kwargs:
        release.update(**kwargs)
    commit_list = result.get('commits')
    if commit_list:
        release.set_commits(commit_list)
    refs = result.get('refs')
    if (not refs):
        refs = [{'repository': r['repository'], 'previousCommit': r.get('previousId'), 'commit': r['currentId']} for r in result.get('headCommits', [])]
    if refs:
        if (not request.user.is_authenticated()):
            return Response({'refs': ['You    must    use    an    authenticated    API    token    to    fetch    refs']}, status=400)
        fetch_commits = (not commit_list)
        try:
            release.set_refs(refs, request.user, fetch=fetch_commits)
        except InvalidRepository as exc:
            return Response({'refs': [exc.message]}, status=400)
    if ((not was_released) and release.date_released):
        for project in release.projects.all():
            Activity.objects.create(type=Activity.RELEASE, project=project, ident=release.version, data={'version': release.version}, datetime=release.date_released)
    return Response(serialize(release, request.user))
