diff --git a/api/src/app/index.ts b/api/src/app/index.ts index 0d182be81..c1922b3cf 100644 --- a/api/src/app/index.ts +++ b/api/src/app/index.ts @@ -7,6 +7,7 @@ import Container from "typedi"; import { ContributorController } from "../contributor/controller"; import { DocsMiddleware } from "./middlewares/docs"; import { ErrorMiddleware } from "./middlewares/error"; +import { GitHubController } from "../github/controller"; import { LoggerMiddleware } from "./middlewares/logger"; import { LoggerService } from "../logger/service"; import { SecurityMiddleware } from "./middlewares/security"; @@ -18,7 +19,7 @@ useContainer(Container); // Create the app: export const routingControllersOptions = { - controllers: [ContributorController], + controllers: [ContributorController, GitHubController], middlewares: [ // middlewares: SecurityMiddleware, diff --git a/api/src/github/controller.ts b/api/src/github/controller.ts new file mode 100644 index 000000000..f42a9652c --- /dev/null +++ b/api/src/github/controller.ts @@ -0,0 +1,27 @@ +import { Controller, Get } from "routing-controllers"; +import { OpenAPI, ResponseSchema } from "routing-controllers-openapi"; +import { GetRepositoriesResponseDto } from "./types"; +import { GithubService } from "../github/service"; +import { Service } from "typedi"; + +@Service() +@Controller("/Github") +export class GitHubController { + constructor(private readonly githubService: GithubService) {} + @Get("/Repositories") + @OpenAPI({ + summary: "Return a list of repositories for dzcode-id", + }) + @ResponseSchema(GetRepositoriesResponseDto) + public async getRepositories(): Promise { + const repositories = await this.githubService.listOrganizationRepositories({ + org: "dzcode-io", + }); + + if (!repositories) throw Error(); + + return { + repositories, + }; + } +} diff --git a/api/src/github/service.ts b/api/src/github/service.ts index 63d493776..55f7a2427 100644 --- a/api/src/github/service.ts +++ b/api/src/github/service.ts @@ -1,10 +1,17 @@ -import { GeneralGithubQuery, ListContributorsResponse } from "./types"; +import { + GeneralGithubQuery, + GitHubRepositoriesApiReponse, + ListContributorsResponse, + ListOrganizationRepositoriesInput, +} from "./types"; import { Service } from "typedi"; import axios from "axios"; @Service() export class GithubService { + private apiURL = "https://api.github.com"; + public listContributors = async ({ owner, repo, @@ -28,5 +35,12 @@ export class GithubService { return contributors; }; - private apiURL = "https://api.github.com"; + public listOrganizationRepositories = async ({ + org, + }: ListOrganizationRepositoriesInput) => { + const response = await axios.get( + `https://api.github.com/orgs/${org}/repos`, + ); + return response.data; + }; } diff --git a/api/src/github/types.ts b/api/src/github/types.ts index 864d5deb0..a912ff87c 100644 --- a/api/src/github/types.ts +++ b/api/src/github/types.ts @@ -1,4 +1,6 @@ +import { GeneralResponseDto } from "../app/types"; import { GithubUser } from "@dzcode.io/common/dist/types"; +import { ValidateNested } from "class-validator"; export type ListContributorsResponse = Array<{ author: GithubUser; @@ -10,3 +12,117 @@ export interface GeneralGithubQuery { repo: string; path: string; } + +export interface ListOrganizationRepositoriesInput { + org: string; +} + +export type GitHubRepositoriesApiReponse = Array; + +/* eslint-disable camelcase */ +export interface GitHubRepositoryApiData { + id: number; + node_id: string; + name: string; + full_name: string; + private: boolean; + owner: { + login: string; + id: number; + node_id: string; + avatar_url: string; + gravatar_id: string; + url: string; + html_url: string; + followers_url: string; + following_url: string; + gists_url: string; + starred_url: string; + subscriptions_url: string; + organizations_url: string; + repos_url: string; + events_url: string; + received_events_url: string; + type: string; + site_admin: boolean; + }; + html_url: string; + description: string; + fork: boolean; + url: string; + forks_url: string; + keys_url: string; + collaborators_url: string; + teams_url: string; + hooks_url: string; + issue_events_url: string; + events_url: string; + assignees_url: string; + branches_url: string; + tags_url: string; + blobs_url: string; + git_tags_url: string; + git_refs_url: string; + trees_url: string; + statuses_url: string; + languages_url: string; + stargazers_url: string; + contributors_url: string; + subscribers_url: string; + subscription_url: string; + commits_url: string; + git_commits_url: string; + comments_url: string; + issue_comment_url: string; + contents_url: string; + compare_url: string; + merges_url: string; + archive_url: string; + downloads_url: string; + issues_url: string; + pulls_url: string; + milestones_url: string; + notifications_url: string; + labels_url: string; + releases_url: string; + deployments_url: string; + created_at: string; + updated_at: string; + pushed_at: string; + git_url: string; + ssh_url: string; + clone_url: string; + svn_url: string; + homepage: string; + size: number; + stargazers_count: number; + watchers_count: number; + language: string; + has_issues: boolean; + has_projects: boolean; + has_downloads: boolean; + has_wiki: boolean; + has_pages: boolean; + forks_count: number; + mirror_url: string; + archived: boolean; + disabled: boolean; + open_issues_count: number; + license: { + key: string; + name: string; + spdx_id: string; + url: string; + node_id: string; + }; + forks: number; + open_issues: number; + watchers: number; + default_branch: string; + permissions: { admin: boolean; push: boolean; pull: boolean }; +} + +export class GetRepositoriesResponseDto extends GeneralResponseDto { + @ValidateNested({ each: true }) + repositories?: GitHubRepositoriesApiReponse; +}