Skip to content
18 changes: 0 additions & 18 deletions api/src/app/controllers/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,3 @@ export const listWatchersByRepository = async (req: Request, res: Response) => {
return res.sendStatus(400);
}
};

export const getGithubUserByUsername = async (req: Request, res: Response) => {
try {
const githubUser = await Github.getUser({
username: req.params.username,
});
const { name, avatar_url, login, html_url } = githubUser;
return res.status(200).json({
name: name,
avatar_url: avatar_url,
login: login,
html_url: html_url,
});
} catch (e) {
console.log(e);
return res.sendStatus(400);
}
};
3 changes: 2 additions & 1 deletion api/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Container from "typedi";
import { ContributorController } from "../contributor/controller";
import { DocsMiddleware } from "./middlewares/docs";
import { ErrorMiddleware } from "./middlewares/error";
import { GithubUserController } from "../github-user/controller";
import { LoggerMiddleware } from "./middlewares/logger";
import { LoggerService } from "../logger/service";
import { SecurityMiddleware } from "./middlewares/security";
Expand All @@ -18,7 +19,7 @@ useContainer(Container);

// Create the app:
export const routingControllersOptions = {
controllers: [ContributorController],
controllers: [ContributorController, GithubUserController],
middlewares: [
// middlewares:
SecurityMiddleware,
Expand Down
2 changes: 0 additions & 2 deletions api/src/app/routes/api/github/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import express, { Router } from "express";
import {
getGithubUserByUsername,
listBranchesByRepository,
listCommitsByRepository,
listForksByRepository,
Expand All @@ -23,6 +22,5 @@ router.get("/issues/:repo", listIssuesByRepository);
router.get("/count-starts/:repo", listStarsByRepository);
router.get("/stargazers/:repo/:page", listStargazersByRepository);
router.get("/watchers/:repo", listWatchersByRepository);
router.get("/user/:username", getGithubUserByUsername);

export default router;
12 changes: 0 additions & 12 deletions api/src/app/services/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,3 @@ export const listWatchers = async ({
return null;
}
};

export const getUser = async ({ username }: { username: string }) => {
try {
const response = await axios.get(
`https://api.github.com/users/${username}`,
);
return response.data;
} catch (error) {
console.log("getUser =>", error.response.data);
return null;
}
};
28 changes: 28 additions & 0 deletions api/src/github-user/controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Controller, Get, Param } from "routing-controllers";
import { OpenAPI, ResponseSchema } from "routing-controllers-openapi";
import { GetUserResponseDto } from "./types";
import { GithubService } from "../github/service";
import { Service } from "typedi";

@Service()
@Controller("/GithubUsers")
export class GithubUserController {
constructor(private readonly githubService: GithubService) {}

@Get("/:username")
@OpenAPI({
summary: "Return a github user with publicly available information",
})
@ResponseSchema(GetUserResponseDto)
public async getUserByUsername(
@Param("username") username: string,
): Promise<GetUserResponseDto> {
const user = await this.githubService.getUser({
username,
});

return {
user,
};
}
}
8 changes: 8 additions & 0 deletions api/src/github-user/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { GeneralResponseDto } from "../app/types";
import { GithubUserDto } from "../github/dto";
import { ValidateNested } from "class-validator";

export class GetUserResponseDto extends GeneralResponseDto {
@ValidateNested()
user?: GithubUserDto;
}
6 changes: 3 additions & 3 deletions api/src/github/dto.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { IsNumber, IsString } from "class-validator";
import { GithubUser } from "@dzcode.io/common/dist/types";
import { IsString } from "class-validator";

export class GithubUserDto implements GithubUser {
@IsString()
avatar_url!: string; // eslint-disable-line camelcase
@IsString()
html_url!: string; // eslint-disable-line camelcase
@IsString()
id!: string;
@IsNumber()
id!: number;
@IsString()
login!: string;
@IsString()
Expand Down
16 changes: 15 additions & 1 deletion api/src/github/service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { GeneralGithubQuery, ListContributorsResponse } from "./types";
import {
GeneralGithubQuery,
GetUserInput,
GitHubUserApiResponse,
ListContributorsResponse,
} from "./types";

import { Service } from "typedi";
import axios from "axios";
Expand Down Expand Up @@ -28,5 +33,14 @@ export class GithubService {
return contributors;
};

public getUser = async ({
username,
}: GetUserInput): Promise<GitHubUserApiResponse> => {
const response = await axios.get<GitHubUserApiResponse>(
`${this.apiURL}/users/${username}`,
);
return response.data;
};

private apiURL = "https://api.github.com";
}
40 changes: 40 additions & 0 deletions api/src/github/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,43 @@ export interface GeneralGithubQuery {
repo: string;
path: string;
}

export interface GetUserInput {
username: string;
}

/* eslint-disable camelcase */
export interface GitHubUserApiResponse {
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;
name: string;
company: string;
blog: string;
location: string;
email: string;
hireable: boolean;
bio: string;
twitter_username: string;
public_repos: number;
public_gists: number;
followers: number;
following: number;
created_at: string;
updated_at: string;
}
6 changes: 3 additions & 3 deletions api/test/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import { GithubUser } from "@dzcode.io/common/dist/types";
export const githubUserMock: GithubUser = {
avatar_url: "avatar_url", // eslint-disable-line camelcase
html_url: "html_url", // eslint-disable-line camelcase
id: "id",
id: 1,
login: "login",
type: "type",
};

export const githubUserMock2: GithubUser = {
avatar_url: "avatar_url2", // eslint-disable-line camelcase
html_url: "html_url2", // eslint-disable-line camelcase
id: "id2",
id: 2,
login: "login2",
type: "type2",
};

export const githubUserMock3: GithubUser = {
avatar_url: "avatar_url3", // eslint-disable-line camelcase
html_url: "html_url3", // eslint-disable-line camelcase
id: "id3",
id: 3,
login: "login3",
type: "type3",
};
2 changes: 1 addition & 1 deletion common/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type Environment = "development" | "staging" | "production";

export interface GithubUser {
login: string;
id: string;
id: number;
// eslint-disable-next-line camelcase
avatar_url: string;
// eslint-disable-next-line camelcase
Expand Down
2 changes: 1 addition & 1 deletion web/src/apps/main/redux/actions/articles-page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ const fetchCurrentArticleAuthors = (): ThunkResult<
const githubAuthors = (
await Promise.all(
currentArticle.authors?.map((author) => {
return Axios.get<GithubUser>(apiURL + `/github/user/${author}`);
return Axios.get<GithubUser>(apiURL + `/v2/GithubUsers/${author}`);
}) || [],
)
).map((response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const fetchCurrentDocumentAuthors = (): ThunkResult<
const githubAuthors = (
await Promise.all(
currentDocument.authors?.map((author) => {
return Axios.get<GithubUser>(apiURL + `/github/user/${author}`);
return Axios.get<GithubUser>(apiURL + `/v2/GithubUsers/${author}`);
}) || [],
)
).map((response) => {
Expand Down