def _create_jenkins_token_auth(url: str, domain_key: str, username: str,
                               password: str):
    """
        Creates and saves the jenkins access token.
    """
    # Use a nice name for the access token so it's clear from which
    # machine this token is used. e.g.
    # git apple-llvm for [email protected]
    token_name = f'git apple-llvm for {getuser()}@{gethostname()}'
    ret = requests.post(
        f'{url}/me/descriptorByName/jenkins.security.ApiTokenProperty/generateNewToken',
        data=f'newTokenName={urllib.parse.quote(token_name)}',
        headers={
            'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        auth=(username, password))
    if ret.status_code != 200:
        raise CIDispatchError(
            ret.url, ret.status_code,
            'failed to construct a jenkins api token:' + ret.text)
    result = ret.json()
    log.debug('jenkins API token: %s', result)
    if result['status'] != 'ok':
        raise CIDispatchError(
            ret.url, ret.status_code,
            'failed to construct a jenkins api token:' + ret.text)
    token_value = result['data']['tokenValue']

    # Save the access token.
    write_config(f'jenkins-{domain_key}',
                 json.dumps({
                     'username': username,
                     'token': token_value
                 }))
    return (username, token_value)
Esempio n. 2
0
def test_cli_tool_test_jenkins_swift_plans(
        cd_to_pr_tool_repo_clone_adjust_jenkins_ci):
    write_config('jenkins-test.foo-bar',
                 '{"username": "******", "token": "123"}')
    mock_tool = MockPRTool()
    mock_tool.create_pull_request('My test', 'This tests important things',
                                  'master')
    git_apple_llvm.pr.main.pr_tool = create_pr_tool(mock_tool,
                                                    PRToolType.GitHub)

    def request_callback(request, uri, response_headers):
        return [201, response_headers, '']

    url1 = f'{JENKINS_TEST_API_URL}/view/monorepo/job/pr-build-test/buildWithParameters?token=GIT_APPLE_LLVM'
    url1 += '&cause=started%20by%20user%20using%20git%20apple-llvm&pullRequestID=1'
    url1 += '&test_targets=check-llvm'
    httpretty.register_uri(httpretty.POST, url1, body=request_callback)
    result = CliRunner().invoke(pr, ['test', '#1', '--test', 'pr'])
    if result.exit_code != 0:
        raise AssertionError
    if 'Triggering pull request testing for pr #1 by <author>:' not in result.output:
        raise AssertionError
    if 'My test' not in result.output:
        raise AssertionError
    if '✅ requested pr [a-RA] ci job for PR #1' not in result.output:
        raise AssertionError
def test_config_file(config_dir):
    write_config('config', 'test')
    if not os.path.isfile(os.path.join(config_dir, 'config')):
        raise AssertionError
    if read_config('config') != 'test':
        raise AssertionError
    if read_config('none') is not None:
        raise AssertionError
def test_pr_tool_list(config_dir: str, cd_to_pr_tool_repo, capfd):
    write_config('jenkins-test.foo-bar',
                 '{"username": "******", "token": "123"}')

    def request_callback(request, uri, response_headers):
        return [201, response_headers, '']

    url1 = f'{TEST_API_URL}/view/monorepo/job/pr-build-test/buildWithParameters?token=GIT_APPLE_LLVM'
    url1 += '&cause=started%20by%20user%20using%20git%20apple-llvm&pullRequestID=9&monorepo_projects='
    url1 += '&test_targets=check-llvm&build_variant=a'
    httpretty.register_uri(httpretty.POST,
                           url1,
                           body=request_callback,
                           match_querystring=True)
    url2 = f'{TEST_API_URL}/view/monorepo/job/pr-build-test/buildWithParameters?token=GIT_APPLE_LLVM'
    url2 += '&cause=started%20by%20user%20using%20git%20apple-llvm&pullRequestID=9&monorepo_projects='
    url2 += '&test_targets=check-llvm&build_variant=b'
    httpretty.register_uri(httpretty.POST,
                           url2,
                           body=request_callback,
                           match_querystring=True)

    tp = test_plans.TestPlanDispatcher()
    tp.dispatch_test_plan_for_pull_request('check-llvm', 9)
    out, err = capfd.readouterr()
    if out != """✅ requested check-llvm [a-RA] ci job for PR #9
✅ requested check-llvm [b-RA] ci job for PR #9
""":
        raise AssertionError

    def request_callback_err(request, uri, response_headers):
        return [402, response_headers, 'problem on server']

    url1 = f'{TEST_API_URL}/view/monorepo/job/pr-build-test/buildWithParameters?token=GIT_APPLE_LLVM'
    url1 += '&cause=started%20by%20user%20using%20git%20apple-llvm&pullRequestID=1&monorepo_projects='
    url1 += '&test_targets=check-llvm&build_variant=a'
    httpretty.register_uri(httpretty.POST,
                           url1,
                           body=request_callback_err,
                           match_querystring=True)
    with pytest.raises(jenkins.CIDispatchError) as err:
        tp.dispatch_test_plan_for_pull_request('check-llvm', 1)
    if err.value.status_code != 402:
        raise AssertionError
    if err.value.error != 'problem on server':
        raise AssertionError
def _create_access_token(domain: str, username: str, password: str):
    """
        Creates and saves the github access token.
    """
    # Use a nice name for the access token so it's clear from which
    # machine this token is used. e.g.
    # git apple-llvm for [email protected]
    note = f'git apple-llvm for {getuser()}@{gethostname()}'

    gh = github3.GitHub()
    auth = gh.authorize(username, password,
                        scopes=['repo'],
                        note=note,
                        note_url='https://github.com/apple/apple-llvm-infrastructure-tools')

    # Save the access token.
    write_config(f'pr-{domain}',
                 json.dumps({'user': username, 'token': auth.token}))
    return auth
Esempio n. 6
0
def test_config_file(config_dir):
    write_config('config', 'test')
    assert os.path.isfile(os.path.join(config_dir, 'config'))
    assert read_config('config') == 'test'
    assert read_config('none') is None