Skip to content

Commit 03946c1

Browse files
authored
Merge pull request #233 from adamayuda/232
Added endoint giving recent PR of dzcode.io
2 parents 9ee0f07 + 76e5b13 commit 03946c1

File tree

9 files changed

+110
-18
lines changed

9 files changed

+110
-18
lines changed

api/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"license": "MIT",
2626
"private": "true",
2727
"devDependencies": {
28+
"@types/axios": "^0.14.0",
2829
"@types/body-parser": "^1.19.0",
2930
"@types/express": "^4.17.8",
3031
"@types/morgan": "^1.9.1",
@@ -34,6 +35,7 @@
3435
"typescript": "^4.0.3"
3536
},
3637
"dependencies": {
38+
"axios": "^0.21.0",
3739
"body-parser": "^1.19.0",
3840
"express": "^4.17.1",
3941
"morgan": "^1.10.0"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Request, Response } from "express";
2+
3+
import * as Github from "../../services/github";
4+
5+
export const listRepositories = async (req: Request, res: Response) => {
6+
try {
7+
const repositories = await Github.listOrganizationRepositories({
8+
org: "dzcode-io",
9+
});
10+
11+
return res.status(200).json(repositories);
12+
} catch (e) {
13+
console.error(e);
14+
return res.sendStatus(400);
15+
}
16+
};
17+
18+
export const listPullRequestsByRepository = async (
19+
req: Request,
20+
res: Response,
21+
) => {
22+
try {
23+
const pullRequests = await Github.listPullRequests({
24+
owner: "dzcode-io",
25+
repo: "dzcode.io",
26+
});
27+
28+
return res.status(200).json(pullRequests);
29+
} catch (e) {
30+
console.error(e);
31+
return res.sendStatus(400);
32+
}
33+
};

api/src/controllers/helloWorld.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

api/src/routes/api/github/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import express, { Router } from "express";
2+
3+
import {
4+
listRepositories,
5+
listPullRequestsByRepository,
6+
} from "../../../controllers/github";
7+
8+
const router: Router = express.Router();
9+
10+
router.get("/repositories", listRepositories);
11+
router.get("/pull-requests", listPullRequestsByRepository);
12+
13+
export default router;

api/src/routes/api/helloWorld/index.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

api/src/routes/api/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import express, { Router } from "express";
22

3-
import helloWorld from "./helloWorld";
3+
import github from "./github";
44

55
const router: Router = express.Router();
66

7-
router.use("/helloWorld", helloWorld);
7+
router.use("/github", github);
88

99
export default router;

api/src/routes/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const router: Router = Router();
77
// API routes
88
router.use("/", api);
99

10+
// Health
11+
router.get("/health", (req: Request, res: Response) => res.sendStatus(204));
12+
1013
// Not found routes
1114
router.use("*", (req: Request, res: Response) => {
1215
return res.sendStatus(404);

api/src/services/github/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import axios from "axios";
2+
3+
export const listOrganizationRepositories = async ({
4+
org,
5+
}: {
6+
org: string;
7+
}) => {
8+
try {
9+
const response = await axios.get(
10+
`https://api.github.com/orgs/${org}/repos`,
11+
);
12+
13+
return response.data;
14+
} catch (error) {
15+
console.log("listOrganizationRepositories ERROR =>", error.response.data);
16+
return null;
17+
}
18+
};
19+
20+
export const listPullRequests = async ({
21+
owner,
22+
repo,
23+
}: {
24+
owner: string;
25+
repo: string;
26+
}) => {
27+
try {
28+
const response = await axios.get(
29+
`https://api.github.com/repos/${owner}/${repo}/pulls`,
30+
{ params: { state: "all", per_page: 10 } },
31+
);
32+
33+
return response.data;
34+
} catch (error) {
35+
console.log("listPullRequests ERROR =>", error.response.data);
36+
return null;
37+
}
38+
};

api/yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
dependencies:
1515
defer-to-connect "^1.0.1"
1616

17+
"@types/axios@^0.14.0":
18+
version "0.14.0"
19+
resolved "https://registry.yarnpkg.com/@types/axios/-/axios-0.14.0.tgz#ec2300fbe7d7dddd7eb9d3abf87999964cafce46"
20+
integrity sha1-7CMA++fX3d1+udOr+HmZlkyvzkY=
21+
dependencies:
22+
axios "*"
23+
1724
"@types/body-parser@*", "@types/body-parser@^1.19.0":
1825
version "1.19.0"
1926
resolved "https://registry.yarnpkg.com/@types/body-parser/-/body-parser-1.19.0.tgz#0685b3c47eb3006ffed117cdd55164b61f80538f"
@@ -138,6 +145,13 @@ [email protected]:
138145
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
139146
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=
140147

148+
axios@*, axios@^0.21.0:
149+
version "0.21.0"
150+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.0.tgz#26df088803a2350dff2c27f96fef99fe49442aca"
151+
integrity sha512-fmkJBknJKoZwem3/IKSSLpkdNXZeBu5Q7GA/aRsr2btgrptmSCxi2oFjZHqGdK9DoTil9PIHlPIZw2EcRJXRvw==
152+
dependencies:
153+
follow-redirects "^1.10.0"
154+
141155
balanced-match@^1.0.0:
142156
version "1.0.0"
143157
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
@@ -485,6 +499,11 @@ finalhandler@~1.1.2:
485499
statuses "~1.5.0"
486500
unpipe "~1.0.0"
487501

502+
follow-redirects@^1.10.0:
503+
version "1.13.0"
504+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db"
505+
integrity sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA==
506+
488507
forwarded@~0.1.2:
489508
version "0.1.2"
490509
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.1.2.tgz#98c23dab1175657b8c0573e8ceccd91b0ff18c84"

0 commit comments

Comments
 (0)