11import os
2- from github import Github , Auth
3- from dataCollection .formatJSON import format_json_data
4- from config .configs import GIT_TOKEN
5- from dataCollection .pullRequest import get_pr_data
6- from dataCollection .issue import get_issue_data
7- from dataCollection .commit import get_commit_data
8- import pandas as pd
92import json
3+ import argparse
4+ from datetime import datetime
5+ from github import Github , Auth
6+ from Backend .config .configs import GIT_TOKEN
7+ from Backend .dataCollection .formatJSON import format_json_data
8+ from Backend .dataCollection .pullRequest import get_pr_data
9+ from Backend .dataCollection .issue import get_issue_data
10+ from Backend .dataCollection .commit import get_commit_data
1011
11- def test_pr_data ():
12- repo_name = "oss-slu/oss_dev_analytics"
13- g = Github (auth = Auth .Token (GIT_TOKEN ))
14- sprint = 7
15- pr_data = get_pr_data (g , repo_name , sprint )
16- print (pr_data .head ())
17- return pr_data
18- def test_issue_data ():
19- repo_name = "oss-slu/oss_dev_analytics"
20- g = Github (auth = Auth .Token (GIT_TOKEN ))
21- sprint = 7
22- issue_data = get_issue_data (g , repo_name , sprint )
23- with pd .option_context ('display.max_rows' , None , 'display.max_columns' , None ): #just want to make sure the df looks correct
24- print (issue_data )
25- return issue_data
26- def test_commit_data ():
27- repo_name = "oss-slu/oss_dev_analytics"
28- g = Github (auth = Auth .Token (GIT_TOKEN ))
29- sprint = 7
30- commit_data = get_commit_data (g , repo_name , sprint )
31- with pd .option_context ('display.max_rows' , None , 'display.max_columns' , None ): #just want to make sure the df looks correct
32- print (commit_data )
33- return commit_data
3412
35- def dev_analytics_test ():
36- repo_name = "oss-slu/oss_dev_analytics"
37- g = Github (auth = Auth .Token (GIT_TOKEN ))
38- sprint = 7
39- issue_data = get_issue_data (g , repo_name , sprint )
40- pr_data = get_pr_data (g , repo_name , sprint )
41- commit_data = get_commit_data (g , repo_name , sprint )
42- out = {
43- "issues" : issue_data .to_dict (orient = 'records' ),
44- "pull_requests" : pr_data .to_dict (orient = 'records' ),
45- "commits" : commit_data .to_dict (orient = 'records' )
46- }
47- #now send the temp .json to a function that puts it into the format we need to show analytics
48- formatted = format_json_data (out , sprint )
49- with open ("test_data.json" , "w" ) as outfile :
50- json .dump (formatted , outfile , indent = 4 , default = str )
51- def other_repo (repo , sprint = - 1 ):
52- repo_name = "oss-slu/" + repo
13+ # This function checks which sprint is currently active
14+ # It reads the sprint_schedule_json file and compares
15+ # today's date with the start and end dates
16+ def get_current_sprint ():
17+ try :
18+ with open ("data/sprint_schedule.json" , "r" ) as file :
19+ schedule = json .load (file )
20+
21+ today = datetime .today ().date ()
22+
23+ for semester in schedule :
24+ for sprint in schedule [semester ]:
25+ start = datetime .strptime (
26+ sprint ["start" ], "%Y-%m-%d"
27+ ).date ()
28+ end = datetime .strptime (
29+ sprint ["end" ], "%Y-%m-%d"
30+ ).date ()
31+
32+ # If today is inside the sprint window,
33+ # return that sprint number
34+ if start <= today <= end :
35+ return sprint ["sprint" ]
36+
37+ # If no sprint matches, return None
38+ return None
39+
40+ except Exception as e :
41+ print (f"Error reading sprint schedule: { e } " )
42+ return None
43+
44+ # Collect data for one repository
45+ # sprint = -1 means lifetime mode (full history)
46+ # Any other sprint number means sprint-only data
47+ def other_repo (repo , sprint = - 1 ):
48+ repo_name = "oss-slu/" + repo
5349 g = Github (auth = Auth .Token (GIT_TOKEN ))
50+
51+ # Get issue, PR, and commit data
5452 issue_data = get_issue_data (g , repo_name , sprint )
5553 pr_data = get_pr_data (g , repo_name , sprint )
5654 commit_data = get_commit_data (g , repo_name , sprint )
57- out = {
58- "issues" : issue_data .to_dict (orient = 'records' ),
59- "pull_requests" : pr_data .to_dict (orient = 'records' ),
60- "commits" : commit_data .to_dict (orient = 'records' )
55+
56+ # Converting DataFrame into dictionary format
57+ out = {
58+ "issues" : issue_data .to_dict (orient = "records" ),
59+ "pull_requests" : pr_data .to_dict (orient = "records" ),
60+ "commits" : commit_data .to_dict (orient = "records" )
6161 }
62+
63+ # Format data into the structure the dashboard expects
6264 formatted = format_json_data (out , sprint )
63- path = "test_data.json"
64- #Using one org wide json file rather than a bunch of individual ones
65- #First need to check if it exists otherwise we are creating it for the first time
66- if (not os .path .exists (path )):
65+
66+ # Deciding which file to write to
67+ # Lifetime data goes into lifetime_data.json
68+ # Sprint data goes into sprint_data.json
69+ path = (
70+ "data/lifetime_data.json"
71+ if sprint == - 1
72+ else "data/sprint_data.json"
73+ )
74+
75+ # Making sure the data folder exists
76+ os .makedirs ("data" , exist_ok = True )
77+
78+ # If file does not exist yet, create it
79+ if not os .path .exists (path ):
6780 with open (path , "w" ) as outfile :
6881 json .dump ({}, outfile )
69- data = {}
70- else :
71- with open (path , "r" ) as outfile :
72- data = json .load (outfile )
73- #now it is created or loaded, we can add new repo data. Keys are the repo (for lifetime data) or repo_sprint_X for specific sprint data
82+
83+ # Loading existing data
84+ with open (path , "r" ) as outfile :
85+ data = json .load (outfile )
86+
87+ # Using repo name as key for lifetime data
88+ # For sprint mode, include sprint number in the key
7489 key = repo if sprint == - 1 else f"{ repo } _sprint_{ sprint } "
7590 data [key ] = formatted
91+
92+ # Writing updated data back to file
7693 with open (path , "w" ) as outfile :
7794 json .dump (data , outfile , indent = 4 , default = str )
95+
96+ # This is the entry point when the workflow runs the script
97+ # The workflow passes either --mode lifetime or --mode sprint
7898if __name__ == "__main__" :
79- #dev_analytics_test()
80- other_repo ("lrda_mobile" , - 1 )
81- other_repo ("oss_dev_analytics" , - 1 )
82- other_repo ("lrda_mobile" , 7 )
83- other_repo ("oss_dev_analytics" , 7 )
99+ parser = argparse .ArgumentParser ()
100+ parser .add_argument (
101+ "--mode" ,
102+ choices = ["lifetime" , "sprint" ],
103+ required = True
104+ )
105+ args = parser .parse_args ()
106+
107+ repos = ["lrda_mobile" , "oss_dev_analytics" ]
108+
109+ if args .mode == "lifetime" :
110+ # Run fill history collection
111+ for repo in repos :
112+ other_repo (repo , - 1 )
113+
114+ elif args .mode == "sprint" :
115+ # First check which sprint is active
116+ sprint = get_current_sprint ()
117+
118+ if sprint is None :
119+ # If we are outside sprint dates, do nothing
120+ print ("No active sprint found" )
121+ else :
122+ for repo in repos :
123+ other_repo (repo , sprint )
0 commit comments