Skip to content

Commit cf5d97a

Browse files
feat: create github organization stats microservice
1 parent 46f9136 commit cf5d97a

File tree

8 files changed

+2142
-0
lines changed

8 files changed

+2142
-0
lines changed

gh-organization-stats/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.env

gh-organization-stats/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# GitHub Organization Stats API
2+
#### A simple API to get the stats for all the repositories in an organization in a single request, caching the data in a MongoDB database. This API will be used to showcase the club-wide stats of the current semester on [ufosc.org](https://ufosc.org).
3+
4+
## Install
5+
Clone the repository (requires [git](https://git-scm.com/)):
6+
```
7+
https://github.com/ufosc/OpenWebServices
8+
```
9+
10+
Navigate to the project directory and install the project dependencies (requires [Node.js](https://nodejs.org/en)):
11+
```
12+
cd OpenWebServices/gh-organization-stats
13+
npm install
14+
```
15+
## Usage
16+
<b>Starting the development server:</b>
17+
```
18+
npm run develop
19+
```
20+
You may access the server at http://localhost:8000
21+
22+
<b>Connect to MongoDB database by including this in .env file:</b>
23+
```
24+
DB_KEY=your_mongodb_connection_string
25+
```
26+
27+
## API Endpoints
28+
### POST ```/api/stats```
29+
- Description: Calls the GitHub API to get the number of commits and pull requests for all the repositories in request body, aggregating the totals and storing them in the MongoDB database. Should be executed daily with a cron job.
30+
- Request Body:
31+
```
32+
{
33+
"startDate": "2024-09-16T00:00:00Z",
34+
"repos": [
35+
["ufosc", "Alarm-Clock"],
36+
["ufosc", "Club_Website_2"],
37+
["ufosc", "OpenWebServices"]
38+
]
39+
}
40+
```
41+
- Response:
42+
* ```200 OK```: Returns the aggregated stats for all the repositories in the request body.
43+
```
44+
{
45+
"message": "Club-wide stats recorded successfully",
46+
"stats": {
47+
"totalCommits": 64,
48+
"totalOpenedPRs": 44
49+
}
50+
}
51+
```
52+
* ```400 Bad Request```: Returns an error message if the request body is invalid.
53+
```
54+
{
55+
"error": "Please provide startDate and repos."
56+
}
57+
```
58+
* ```404 Not Found```: Returns an error message if one of the repositories in the request body are not found.
59+
```
60+
{
61+
"error": "Error fetching stats for ufosc/fake-repo: Repository ufosc/fake-repo not found."
62+
}
63+
```
64+
* ```500 Internal Server Error```: Returns an error message if there is an issue saving to the MongoDB collection or with connecting to the GitHub API.
65+
66+
### GET ```/api/stats```
67+
- Description: Retrieves all the stats stored in the MongoDB database. Each semester's stats are stored in a separate document.
68+
- Response:
69+
* ```200 OK```: Returns all the stats stored in the MongoDB database.
70+
```
71+
{
72+
"stats": [
73+
{
74+
"_id": "6705e2d4c2ccbca1d8b52380",
75+
"start_date": "2024-09-16T00:00:00.000Z",
76+
"totalCommits": 9000,
77+
"totalOpenedPRs": 9000,
78+
"repos": [
79+
[
80+
"ufosc",
81+
"Alarm-Clock"
82+
],
83+
[
84+
"ufosc",
85+
"Club_Website_2"
86+
],
87+
...
88+
],
89+
"collection_date": "2024-10-09T00:00:00.000Z",
90+
"__v": 0
91+
}
92+
]
93+
}
94+
```

0 commit comments

Comments
 (0)