'Create a New Release for a Project
Create a new release and/or associate a project with a release.
Release versions that are the same across multiple projects
within an Organization will be treated as the same release in Sentry.
Releases are used by Sentry to improve its error reporting abilities
by correlating first seen events with the release that might have
introduced the problem.
Releases are also necessary for sourcemaps and other debug features
that require manual upload for functioning well.
:pparam string organization_slug: the slug of the organization the
release belongs to.
:pparam string project_slug: the slug of the project to create a
release for.
:param string version: a version identifier for this release.  Can
be a version number, a commit hash etc.
: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.
:auth: required'

def post(self, request, project):

    serializer = ReleaseSerializer(data=request.DATA)
    if serializer.is_valid():
        result = serializer.object
        try:
            with transaction.atomic():
                (release, created) = (Release.objects.create(organization_id=project.organization_id, version=result['version'], ref=result.get('ref'), url=result.get('url'), owner=result.get('owner'), date_released=result.get('dateReleased')), True)
            was_released = False
        except IntegrityError:
            (release, created) = (Release.objects.get(organization_id=project.organization_id, version=result['version']), False)
            was_released = bool(release.date_released)
        created = release.add_project(project)
        commit_list = result.get('commits')
        if commit_list:
            hook = ReleaseHook(project)
            hook.set_commits(release.version, commit_list)
        if ((not was_released) and release.date_released):
            Activity.objects.create(type=Activity.RELEASE, project=project, ident=result['version'], data={'version': result['version']}, datetime=release.date_released)
        if (not created):
            status = 208
        else:
            status = 201
        return Response(serialize(release, request.user), status=status)
    return Response(serializer.errors, status=400)
