@octokit/rest#RestEndpointMethodTypes TypeScript Examples

The following examples show how to use @octokit/rest#RestEndpointMethodTypes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SingleInstanceGithubCredentialsProvider.ts    From backstage with Apache License 2.0 6 votes vote down vote up
async getAllInstallations(): Promise<
    RestEndpointMethodTypes['apps']['listInstallations']['response']['data']
  > {
    if (!this.apps.length) {
      return [];
    }

    const installs = await Promise.all(
      this.apps.map(app => app.getInstallations()),
    );

    return installs.flat();
  }
Example #2
Source File: GithubActionsClient.ts    From backstage with Apache License 2.0 6 votes vote down vote up
async listWorkflowRuns({
    hostname,
    owner,
    repo,
    pageSize = 100,
    page = 0,
    branch,
  }: {
    hostname?: string;
    owner: string;
    repo: string;
    pageSize?: number;
    page?: number;
    branch?: string;
  }): Promise<
    RestEndpointMethodTypes['actions']['listWorkflowRuns']['response']['data']
  > {
    const octokit = await this.getOctokit(hostname);
    const workflowRuns = await octokit.actions.listWorkflowRunsForRepo({
      owner,
      repo,
      per_page: pageSize,
      page,
      ...(branch ? { branch } : {}),
    });
    return workflowRuns.data;
  }
Example #3
Source File: GithubActionsClient.ts    From backstage with Apache License 2.0 6 votes vote down vote up
async getWorkflow({
    hostname,
    owner,
    repo,
    id,
  }: {
    hostname?: string;
    owner: string;
    repo: string;
    id: number;
  }): Promise<
    RestEndpointMethodTypes['actions']['getWorkflow']['response']['data']
  > {
    const octokit = await this.getOctokit(hostname);
    const workflow = await octokit.actions.getWorkflow({
      owner,
      repo,
      workflow_id: id,
    });
    return workflow.data;
  }
Example #4
Source File: GithubActionsClient.ts    From backstage with Apache License 2.0 6 votes vote down vote up
async getWorkflowRun({
    hostname,
    owner,
    repo,
    id,
  }: {
    hostname?: string;
    owner: string;
    repo: string;
    id: number;
  }): Promise<
    RestEndpointMethodTypes['actions']['getWorkflowRun']['response']['data']
  > {
    const octokit = await this.getOctokit(hostname);
    const run = await octokit.actions.getWorkflowRun({
      owner,
      repo,
      run_id: id,
    });
    return run.data;
  }
Example #5
Source File: GithubActionsClient.ts    From backstage with Apache License 2.0 6 votes vote down vote up
async listJobsForWorkflowRun({
    hostname,
    owner,
    repo,
    id,
    pageSize = 100,
    page = 0,
  }: {
    hostname?: string;
    owner: string;
    repo: string;
    id: number;
    pageSize?: number;
    page?: number;
  }): Promise<
    RestEndpointMethodTypes['actions']['listJobsForWorkflowRun']['response']['data']
  > {
    const octokit = await this.getOctokit(hostname);
    const jobs = await octokit.actions.listJobsForWorkflowRun({
      owner,
      repo,
      run_id: id,
      per_page: pageSize,
      page,
    });
    return jobs.data;
  }
Example #6
Source File: GithubActionsClient.ts    From backstage with Apache License 2.0 6 votes vote down vote up
async downloadJobLogsForWorkflowRun({
    hostname,
    owner,
    repo,
    runId,
  }: {
    hostname?: string;
    owner: string;
    repo: string;
    runId: number;
  }): Promise<
    RestEndpointMethodTypes['actions']['downloadJobLogsForWorkflowRun']['response']['data']
  > {
    const octokit = await this.getOctokit(hostname);
    const workflow = await octokit.actions.downloadJobLogsForWorkflowRun({
      owner,
      repo,
      job_id: runId,
    });
    return workflow.data;
  }
Example #7
Source File: SingleInstanceGithubCredentialsProvider.ts    From backstage with Apache License 2.0 5 votes vote down vote up
getInstallations(): Promise<
    RestEndpointMethodTypes['apps']['listInstallations']['response']['data']
  > {
    return this.appClient.paginate(this.appClient.apps.listInstallations);
  }
Example #8
Source File: auth.ts    From bext with MIT License 5 votes vote down vote up
user$ = new BehaviorSubject<{
  user?: RestEndpointMethodTypes['users']['getAuthenticated']['response']['data'];
  status: 'unauth' | 'loading' | 'error' | 'complete';
}>({ status: 'unauth' })
Example #9
Source File: SingleInstanceGithubCredentialsProvider.test.ts    From backstage with Apache License 2.0 4 votes vote down vote up
describe('SingleInstanceGithubCredentialsProvider tests', () => {
  let github: GithubCredentialsProvider;

  beforeEach(() => {
    jest.resetAllMocks();
    github = SingleInstanceGithubCredentialsProvider.create({
      host: 'github.com',
      apps: [
        {
          appId: 1,
          privateKey: 'privateKey',
          webhookSecret: '123',
          clientId: 'CLIENT_ID',
          clientSecret: 'CLIENT_SECRET',
        },
      ],
      token: 'hardcoded_token',
    });
  });
  it('create repository specific tokens', async () => {
    octokit.apps.listInstallations.mockResolvedValue({
      headers: {
        etag: '123',
      },
      data: [
        {
          id: 1,
          repository_selection: 'selected',
          account: null,
        },
        {
          id: 2,
          repository_selection: 'selected',
          account: {
            login: 'backstage',
          },
        },
      ],
    } as RestEndpointMethodTypes['apps']['listInstallations']['response']);

    octokit.apps.createInstallationAccessToken.mockResolvedValueOnce({
      data: {
        expires_at: DateTime.local().plus({ hours: 1 }).toString(),
        token: 'secret_token',
      },
    } as RestEndpointMethodTypes['apps']['createInstallationAccessToken']['response']);

    const { token, headers, type } = await github.getCredentials({
      url: 'https://github.com/backstage/foobar',
    });
    expect(type).toEqual('app');
    expect(token).toEqual('secret_token');
    expect(headers).toEqual({ Authorization: 'Bearer secret_token' });

    // fallback to the configured token if no application is matching
    await expect(
      github.getCredentials({
        url: 'https://github.com/404/foobar',
      }),
    ).resolves.toEqual({
      headers: {
        Authorization: 'Bearer hardcoded_token',
      },
      token: 'hardcoded_token',
      type: 'token',
    });
  });

  it('creates tokens for an organization', async () => {
    octokit.apps.listInstallations.mockResolvedValue({
      headers: {
        etag: '123',
      },
      data: [
        {
          id: 1,
          repository_selection: 'all',
          account: {
            login: 'backstage',
          },
        },
      ],
    } as RestEndpointMethodTypes['apps']['listInstallations']['response']);

    octokit.apps.createInstallationAccessToken.mockResolvedValueOnce({
      data: {
        expires_at: DateTime.local().plus({ hours: 1 }).toString(),
        token: 'secret_token',
      },
    } as RestEndpointMethodTypes['apps']['createInstallationAccessToken']['response']);

    const { token, headers } = await github.getCredentials({
      url: 'https://github.com/backstage',
    });

    expect(headers).toEqual({ Authorization: 'Bearer secret_token' });
    expect(token).toEqual('secret_token');
  });

  it('does not return a token where the organisation is not in the allowedInstallationsList', async () => {
    github = SingleInstanceGithubCredentialsProvider.create({
      host: 'github.com',
      apps: [
        {
          appId: 1,
          privateKey: 'privateKey',
          webhookSecret: '123',
          clientId: 'CLIENT_ID',
          clientSecret: 'CLIENT_SECRET',
          allowedInstallationOwners: ['backstage'],
        },
      ],
    });

    octokit.apps.listInstallations.mockResolvedValue({
      headers: {
        etag: '123',
      },
      data: [
        {
          id: 1,
          repository_selection: 'all',
          account: {
            login: 'backstage',
          },
        },
      ],
    } as RestEndpointMethodTypes['apps']['listInstallations']['response']);

    const { token, headers } = await github.getCredentials({
      url: 'https://github.com/RoadiehHQ',
    });

    expect(headers).toEqual(undefined);
    expect(token).toEqual(undefined);
  });

  it('should not fail to issue tokens for an organization when the app is installed for a single repo', async () => {
    octokit.apps.listInstallations.mockResolvedValue({
      headers: {
        etag: '123',
      },
      data: [
        {
          id: 1,
          repository_selection: 'selected',
          account: {
            login: 'backstage',
          },
        },
      ],
    } as RestEndpointMethodTypes['apps']['listInstallations']['response']);

    octokit.apps.createInstallationAccessToken.mockResolvedValueOnce({
      data: {
        expires_at: DateTime.local().plus({ hours: 1 }).toString(),
        token: 'secret_token',
      },
    } as RestEndpointMethodTypes['apps']['createInstallationAccessToken']['response']);

    const { token, headers } = await github.getCredentials({
      url: 'https://github.com/backstage',
    });
    const expectedToken = 'secret_token';
    expect(headers).toEqual({ Authorization: `Bearer ${expectedToken}` });
    expect(token).toEqual('secret_token');
  });

  it('should throw if the app is suspended', async () => {
    octokit.apps.listInstallations.mockResolvedValue({
      headers: {
        etag: '123',
      },
      data: [
        {
          id: 1,
          suspended_by: {
            login: 'admin',
          },
          repository_selection: 'all',
          account: {
            login: 'backstage',
          },
        },
      ],
    } as RestEndpointMethodTypes['apps']['listInstallations']['response']);

    await expect(
      github.getCredentials({
        url: 'https://github.com/backstage',
      }),
    ).rejects.toThrow('The GitHub application for backstage is suspended');
  });

  it('should return the default token when the call to github return a status that is not recognized', async () => {
    octokit.apps.listInstallations.mockRejectedValue({
      status: 404,
      message: 'NotFound',
    });

    await expect(
      github.getCredentials({
        url: 'https://github.com/backstage',
      }),
    ).rejects.toEqual({ status: 404, message: 'NotFound' });
  });

  it('should return the default token if no app is configured', async () => {
    const githubProvider = SingleInstanceGithubCredentialsProvider.create({
      host: 'github.com',
      apps: [],
      token: 'fallback_token',
    });

    await expect(
      githubProvider.getCredentials({
        url: 'https://github.com/404/foobar',
      }),
    ).resolves.toEqual(expect.objectContaining({ token: 'fallback_token' }));
  });

  it('should return the configured token if there are no installations', async () => {
    const githubProvider = SingleInstanceGithubCredentialsProvider.create({
      host: 'github.com',
      apps: [
        {
          appId: 1,
          privateKey: 'privateKey',
          webhookSecret: '123',
          clientId: 'CLIENT_ID',
          clientSecret: 'CLIENT_SECRET',
        },
      ],
      token: 'hardcoded_token',
    });
    octokit.apps.listInstallations.mockResolvedValue({
      data: [],
    } as unknown as RestEndpointMethodTypes['apps']['listInstallations']['response']);

    await expect(
      githubProvider.getCredentials({
        url: 'https://github.com/backstage',
      }),
    ).resolves.toEqual(expect.objectContaining({ token: 'hardcoded_token' }));
  });

  it('should return undefined if no token or apps are configured', async () => {
    const githubProvider = SingleInstanceGithubCredentialsProvider.create({
      host: 'github.com',
    });

    await expect(
      githubProvider.getCredentials({
        url: 'https://github.com/backstage',
      }),
    ).resolves.toEqual({ headers: undefined, token: undefined, type: 'token' });
  });

  it('should to create a token for the organization ignoring case sensitive', async () => {
    octokit.apps.listInstallations.mockResolvedValue({
      headers: {
        etag: '123',
      },
      data: [
        {
          id: 1,
          repository_selection: 'all',
          account: {
            login: 'BACKSTAGE',
          },
        },
      ],
    } as RestEndpointMethodTypes['apps']['listInstallations']['response']);

    octokit.apps.createInstallationAccessToken.mockResolvedValueOnce({
      data: {
        expires_at: DateTime.local().plus({ hours: 1 }).toString(),
        token: 'secret_token',
      },
    } as RestEndpointMethodTypes['apps']['createInstallationAccessToken']['response']);

    const { token, headers } = await github.getCredentials({
      url: 'https://github.com/backstage',
    });

    expect(headers).toEqual({ Authorization: 'Bearer secret_token' });
    expect(token).toEqual('secret_token');
  });
});