Exemple #1
0
def test_landing_revision_calls_transplant_service(db, client, phabfactory,
                                                   monkeypatch, s3):
    # Mock the phabricator response data
    phabfactory.revision()

    # Build the patch we expect to see
    phabclient = PhabricatorClient('someapi')
    revision = phabclient.get_revision('D1')
    diff_id = phabclient.get_diff(phid=revision['activeDiffPHID'])['id']
    gitdiff = phabclient.get_rawdiff(diff_id)
    author = phabclient.get_revision_author(revision)
    hgpatch = build_patch_for_revision(gitdiff, author, revision)
    patch_url = 's3://landoapi.test.bucket/L1_D1_1.patch'

    # The repo we expect to see
    repo_uri = phabclient.get_revision_repo(revision)['uri']

    tsclient = MagicMock(spec=TransplantClient)
    tsclient().land.return_value = 1
    monkeypatch.setattr('landoapi.models.landing.TransplantClient', tsclient)
    client.post('/landings?api_key=api-key',
                data=json.dumps({
                    'revision_id': 'D1',
                    'diff_id': int(diff_id)
                }),
                content_type='application/json')
    tsclient().land.assert_called_once_with(
        '*****@*****.**', patch_url, repo_uri,
        '{}/landings/1/update'.format(os.getenv('PINGBACK_HOST_URL')))
    body = s3.Object('landoapi.test.bucket',
                     'L1_D1_1.patch').get()['Body'].read().decode("utf-8")
    assert body == hgpatch
Exemple #2
0
def test_get_repo_for_revision(phabfactory):
    repo_response = phabfactory.repo()
    phabfactory.revision(id='D5')
    expected_repo = first_result_in_response(repo_response)

    phab = PhabricatorClient(api_key='api-key')
    revision = phab.get_revision(id='D5')
    repo = phab.get_revision_repo(revision)

    assert repo == expected_repo
Exemple #3
0
    def create(cls, revision_id, diff_id, phabricator_api_key=None):
        """Land revision.

        A typical successful story:
            * Revision and Diff are loaded from Phabricator.
            * Patch is created and uploaded to S3 bucket.
            * Landing object is created (without request_id)
            * A request to land the patch is send to Transplant client.
            * Created landing object is updated with returned `request_id`,
              it is then saved and returned.

        Args:
            revision_id: The id of the revision to be landed
            diff_id: The id of the diff to be landed
            phabricator_api_key: API Key to identify in Phabricator

        Returns:
            A new Landing object

        Raises:
            RevisionNotFoundException: PhabricatorClient returned no revision
                for given revision_id
            LandingNotCreatedException: landing request in Transplant failed
        """
        phab = PhabricatorClient(phabricator_api_key)
        revision = phab.get_revision(id=revision_id)

        if not revision:
            raise RevisionNotFoundException(revision_id)

        repo = phab.get_revision_repo(revision)

        # Save landing to make sure we've got the callback URL.
        landing = cls(revision_id=revision_id, diff_id=diff_id)
        landing.save()

        patch = Patch(landing.id, revision, diff_id)
        patch.upload(phab)

        # Define the pingback URL with the port.
        callback = '{host_url}/landings/{id}/update'.format(
            host_url=current_app.config['PINGBACK_HOST_URL'], id=landing.id)

        trans = TransplantClient()
        # The LDAP username used here has to be the username of the patch
        # pusher (the person who pushed the 'Land it!' button).
        # FIXME: change [email protected] to the real data retrieved
        #        from Auth0 userinfo
        request_id = trans.land('*****@*****.**', patch.s3_url,
                                repo['uri'], callback)
        if not request_id:
            raise LandingNotCreatedException

        landing.request_id = request_id
        landing.status = TRANSPLANT_JOB_STARTED
        landing.save()

        logger.info(
            {
                'revision_id': revision_id,
                'landing_id': landing.id,
                'msg': 'landing created for revision'
            }, 'landing.success')

        return landing