Exemplo n.º 1
0
def test_get_details_from_hook_payload_PUSH():
    payload = {
        'ref': 'refs/heads/some-branch',
        'repository': {
            'name': 'test-repo',
            'owner': {
                'name': 'test-owner'
            }
        },
        'head_commit': {
            'id': 'test-sha',
            'message': 'Test message',
            'author': {
                'username': '******',
            }
        },
    }
    assert get_details_from_hook_payload(payload) == {
        'owner': 'test-owner',
        'repo': 'test-repo',
        'sha': 'test-sha',
        'is_pr': False,
        'commit_message': 'Test message',
        'author_name': 'test-user',
        'branch_name': 'some-branch',
    }

    payload = {
        'ref': 'refs/heads/some-branch',
        'repository': {
            'name': 'test-repo',
            'owner': {
                'name': 'test-owner'
            }
        },
        'head_commit': {
            'id': 'test-sha',
            'message': 'Test message',
            'author': {
                'name': 'no-username',
            }
        },
        'sender': {
            'login': '******'
        },
    }
    assert get_details_from_hook_payload(payload) == {
        'owner': 'test-owner',
        'repo': 'test-repo',
        'sha': 'test-sha',
        'is_pr': False,
        'commit_message': 'Test message',
        'author_name': 'test-user',
        'branch_name': 'some-branch',
    }
Exemplo n.º 2
0
def test_get_details_from_hook_payload_PR_other_action():
    payload = {
        'action': 'labeled',
        'pull_request': {
            'head': {}
        },
    }
    assert get_details_from_hook_payload(payload) == {}
Exemplo n.º 3
0
def create_job():

    # Validate signature (throws 400 on invalid)
    if not github.debug:
        key = settings.GITHUB_HOOK_SECRET
        text = request.data
        signature = request.headers.get('X-Hub-Signature', '')
        if not validate_signature(key, text, signature):
            msg = 'Wrong signature for GitHub payload'
            log.error(msg)
            abort(400, msg)

    # Get payload details (throws 400 if no data or bad JSON)
    payload = request.get_json()
    details = get_details_from_hook_payload(payload)
    if details is None:
        msg = 'Wrong payload received'
        log.error(msg)
        abort(400, msg)
    if details == {}:
        return jsonify({})

    # Get source (throw 400 if no source)
    source_owner = details['owner']
    source_repo = details['repo']
    if details['is_pr']:
        source_owner = details['base_owner']
        source_repo = details['base_repo']
    source = database['session'].query(GithubRepo).filter(
        GithubRepo.name == '%s/%s' % (source_owner, source_repo)).one_or_none()
    if not source:
        msg = 'A job was requested on a repository not present in the DB'
        log.error(msg)
        abort(400, msg)

    # Create and start job (throw 400 if not started)
    job_id = source.create_and_start_job(conf=details)
    if not job_id:
        msg = 'A job was requested but can\'t be started'
        log.error(msg)
        abort(400, msg)

    return jsonify({'job_id': job_id})
Exemplo n.º 4
0
def test_get_details_from_hook_payload_PR():
    payload = {
        'action': 'opened',
        'pull_request': {
            'number': 3,
            'title': 'Test PR',
            'user': {
                'login': '******',
            },
            'head': {
                'repo': {
                    'name': 'test-repo',
                    'owner': {
                        'login': '******'
                    }
                },
                'sha': 'test-sha',
            },
            'base': {
                'repo': {
                    'name': 'test-repo',
                    'owner': {
                        'login': '******'
                    }
                },
            },
        },
    }
    assert get_details_from_hook_payload(payload) == {
        'owner': 'test-owner',
        'repo': 'test-repo',
        'base_owner': 'test-owner',
        'base_repo': 'test-repo',
        'sha': 'test-sha',
        'is_pr': True,
        'pr_number': 3,
        'pr_title': 'Test PR',
        'author_name': 'test-user',
    }
Exemplo n.º 5
0
def test_get_details_from_hook_payload_bad_payload():
    payload = {
        'key': 'value',
    }
    assert get_details_from_hook_payload(payload) is None