# All files / src/services/generate-token generate-token.impl.ts

37.74% Statements 20/53
16.67% Branches 1/6
35.71% Functions 5/14
37.74% Lines 20/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    1182x
    2x

    2x

    2x

    2x

    1x

    1x
    1x

    1x

    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x

    2x

     import {Container} from 'typescript-ioc';
    import {Browser, JSHandle, launch, Page} from 'puppeteer';

    import {GenerateToken} from './generate-token.api';
    import {GenerateTokenOptions} from './generate-token-options.model';
    import {timer} from '@ibmgaragecloud/cloud-native-toolkit-cli/dist/util/timer';

    export class GenerateTokenImpl implements GenerateToken {

      isAvailable(): boolean {
        return true;
      }

      async generateToken(commandOptions: GenerateTokenOptions, notifyStatus: (status: string) => void = () => {
      }): Promise<string> {

        const loginUrl = this.buildLoginUrl(commandOptions);
        const url = this.buildUserProfileUrl(commandOptions);

        const browser: Browser = await this.buildDriver();

        try {
          const page: Page = await browser.newPage();

          notifyStatus(`Logging into Jenkins: ${loginUrl}`);

          await this.login(page, loginUrl, commandOptions.username, commandOptions.password);

          await timer(2000);

          notifyStatus(`Generating token`);

          return await this.genToken(page, url);
        } finally {
          await browser.close();
        }
      }

      async buildDriver() {
        return launch({
          args: ['--no-sandbox', '--disable-setuid-sandbox']
        });
      }

      buildLoginUrl({url}: {url: string}): string {
        return `${url}/login`;
      }

      buildUserProfileUrl({url, username}: {url: string, username: string}): string {
        return `${url}/user/${username}/configure`;
      }

      async login(page: Page, loginUrl: string, username: string, password: string) {
        try {
          await page.goto(loginUrl);
        } catch (err) {
          await timer(3000);

          await page.goto(loginUrl);
        }

        await timer(2000);

        await page.evaluateHandle((user: string, pwd: string) => {
          document.querySelector<HTMLInputElement>('input[name=j_username]').value = user;
          document.querySelector<HTMLInputElement>('input[name=j_password]').value = pwd;
          document.querySelector<HTMLInputElement>('input.submit-button').click();

          return user + ':' + pwd;
        }, username, password);
      }

      async genToken(page: Page, url: string): Promise<string> {

        await page.goto(url);

        await timer(2000);

        const buttonHandle: JSHandle = await page.evaluateHandle(() => {
          const button = document.getElementById('yui-gen2-button');
          if (button) {
            button.click();
          }

          return !!button;
        });

        const buttonClicked: boolean = (await buttonHandle.jsonValue()) as any;
        if (!buttonClicked) {
          const content = await page.browserContext();
          console.log('content ', content);

          throw new Error('Unable to login');
        }

        await timer(500);

        await page.evaluateHandle(() => {
          document.querySelector<HTMLInputElement>('input[name=tokenName]').value = 'pipeline-token';
          document.querySelector<HTMLButtonElement>('.token-save button').click();
        });

        await timer(1000);

        const tokenHandle: JSHandle = await page.evaluateHandle(() => {
          const tokenElement = document.querySelector('.new-token-value');
          return tokenElement.innerHTML;
        });

        return (await tokenHandle.jsonValue()) as string;
      }
    }

    export async function generateToken(commandOptions: GenerateTokenOptions, notifyStatus: (status: string) => void = () => {
    }): Promise<string> {
      return (Container.get(GenerateToken) as GenerateToken).generateToken(commandOptions, notifyStatus);
    }