Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions e2e-tests/playwright/e2e/github-discovery.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +11,7 @@ type GithubDiscoveryFixture = {

const test = base.extend<GithubDiscoveryFixture>({
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);
Expand All @@ -22,34 +21,40 @@ const test = base.extend<GithubDiscoveryFixture>({
});

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();
}
});
});
77 changes: 17 additions & 60 deletions e2e-tests/playwright/support/api/github.ts
Original file line number Diff line number Diff line change
@@ -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<GetOrganizationResponse> {
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<boolean> {
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<boolean> {
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<APIResponse> => {
const path: string = url + organization;
const context = await this._myContext;
return context.get(path);
},

repos: async (): Promise<APIResponse> => {
const context = await this._myContext;
const organizationResponse = await new GithubApi()
._organization(organization)
.get();
return context.get((await organizationResponse.json()).repos_url);
},
};
}
}
2 changes: 2 additions & 0 deletions e2e-tests/playwright/utils/api-endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,

Expand Down
Loading