diff --git a/e2e-tests/playwright/e2e/github-discovery.spec.ts b/e2e-tests/playwright/e2e/github-discovery.spec.ts index f3745eeb4d..c34c1bb587 100644 --- a/e2e-tests/playwright/e2e/github-discovery.spec.ts +++ b/e2e-tests/playwright/e2e/github-discovery.spec.ts @@ -1,8 +1,7 @@ -import { test as base } from "@playwright/test"; +import { test as base, expect } from "@playwright/test"; import GithubApi from "../support/api/github"; import { CATALOG_FILE, JANUS_QE_ORG } from "../utils/constants"; import { Common } from "../utils/common"; -import { assert } from "console"; import { Catalog } from "../support/pages/catalog"; type GithubDiscoveryFixture = { catalogPage: Catalog; @@ -12,7 +11,7 @@ type GithubDiscoveryFixture = { const test = base.extend({ catalogPage: async ({ page }, use) => { - await new Common(page).loginAsGithubUser(); + await new Common(page).loginAsGuest(); const catalog = new Catalog(page); await catalog.go(); await use(catalog); @@ -22,34 +21,40 @@ const test = base.extend({ }); test.describe("Github Discovery Catalog", () => { - test.beforeAll(async () => { - test.info().annotations.push({ - type: "component", - description: "api", - }); - }); - - //TODO: https://issues.redhat.com/browse/RHDHBUGS-2576 - test.fixme(`Discover Organization's Catalog`, async ({ + test(`Discover Organization's Catalog`, async ({ catalogPage, githubApi, testOrganization, }) => { const organizationRepos = await githubApi.getReposFromOrg(testOrganization); - const reposNames: string[] = organizationRepos.map((repo) => repo["name"]); - const realComponents: string[] = reposNames.filter( - async (repo) => - await githubApi.fileExistsOnRepo( - `${testOrganization}/${repo}`, - CATALOG_FILE, + + const reposNames: string[] = (organizationRepos as Array<{ name?: string }>) + .map((repo) => repo.name) + .filter((name): name is string => typeof name === "string") + // filter for subset of organization repositories where the repository name matches the entity name + .filter((name) => name.startsWith("test-annotator")) + .slice(0, 5); + + const reposWithCatalogInfo: string[] = ( + await Promise.all( + reposNames.map(async (repo) => + (await githubApi.fileExistsInRepo( + testOrganization, + repo, + CATALOG_FILE, + )) + ? repo + : null, ), - ); + ) + ).filter((repo): repo is string => typeof repo === "string"); + + expect(reposWithCatalogInfo.length).toBeGreaterThan(0); - for (let i = 0; i != realComponents.length; i++) { - const repo = realComponents[i]; + for (const repo of reposWithCatalogInfo) { await catalogPage.search(repo); const row = await catalogPage.tableRow(repo); - assert(await row.isVisible()); + await expect(row).toBeVisible(); } }); }); diff --git a/e2e-tests/playwright/support/api/github.ts b/e2e-tests/playwright/support/api/github.ts index b68736fbcd..62f1e4f499 100644 --- a/e2e-tests/playwright/support/api/github.ts +++ b/e2e-tests/playwright/support/api/github.ts @@ -1,71 +1,28 @@ import { JANUS_ORG } from "../../utils/constants"; -import { APIResponse, request } from "@playwright/test"; -import { GetOrganizationResponse } from "./github-structures"; +import { APIHelper } from "../../utils/api-helper"; +import { GITHUB_API_ENDPOINTS } from "../../utils/api-endpoints"; // https://docs.github.com/en/rest?apiVersion=2022-11-28 export default class GithubApi { - private readonly apiUrl = "https://api.github.com"; - private readonly apiVersion = "2022-11-28"; - private readonly authHeader = { - Accept: "application/vnd.github+json", - Authorization: `Bearer ${process.env.GH_RHDH_QE_USER_TOKEN}`, - "X-GitHub-Api-Version": this.apiVersion, - }; - - public async getOrganization( - org = JANUS_ORG, - ): Promise { - const req = await this._organization(org).get(); - return new GetOrganizationResponse(req.json()); - } - public async getReposFromOrg(org = JANUS_ORG) { - const req = await this._organization(org).repos(); - return req.json(); + return APIHelper.getGithubPaginatedRequest( + GITHUB_API_ENDPOINTS.orgRepos(org), + ); } - public async fileExistsOnRepo(repo: string, file: string): Promise { - const req = await this._repo(repo).getContent(file); - const status = req.status(); - if (status == 403) { - throw Error("You don-t have permissions to see this path"); + public async fileExistsInRepo( + owner: string, + repo: string, + file: string, + ): Promise { + const resp = await APIHelper.githubRequest( + "GET", + `${GITHUB_API_ENDPOINTS.contents(owner, repo)}/${file}`, + ); + const status = resp.status(); + if (status === 403) { + throw Error("You don't have permissions to see this path"); } return [200, 302, 304].includes(status); } - - private _myContext = request.newContext({ - baseURL: this.apiUrl, - extraHTTPHeaders: this.authHeader, - }); - - private _repo(repo: string) { - const url = `/repos/${repo}/`; - return { - getContent: async (path: string) => { - path = url + path; - const context = await this._myContext; - return context.get(path); - }, - }; - } - - private _organization(organization: string) { - const url = "/orgs/"; - - return { - get: async (): Promise => { - const path: string = url + organization; - const context = await this._myContext; - return context.get(path); - }, - - repos: async (): Promise => { - const context = await this._myContext; - const organizationResponse = await new GithubApi() - ._organization(organization) - .get(); - return context.get((await organizationResponse.json()).repos_url); - }, - }; - } } diff --git a/e2e-tests/playwright/utils/api-endpoints.ts b/e2e-tests/playwright/utils/api-endpoints.ts index 35917ea0c2..2e39911c97 100644 --- a/e2e-tests/playwright/utils/api-endpoints.ts +++ b/e2e-tests/playwright/utils/api-endpoints.ts @@ -11,6 +11,8 @@ export const GITHUB_API_ENDPOINTS = { pull: (owner: string, repo: string, state: "open" | "closed" | "all") => `${getRepoUrl(owner, repo)}/pulls?per_page=${perPage}&state=${state}`, + orgRepos: (owner: string) => `${getOrgUrl(owner)}/repos?per_page=${perPage}`, + issues: (state: string) => `${backstageShowcaseAPI}/issues?per_page=${perPage}&sort=updated&state=${state}`,