1+ #!/usr/bin/env python
2+
3+ import os
4+ import json
5+
6+ '''
7+ This script requires following environment variables:
8+
9+ - REPO_NAME:
10+ > example: 'iamwatchdogs/test'
11+ > GitHub action variable: ${{ github.repository }}
12+
13+ - PR_NUMBER:
14+ > example: '5'
15+ > GitHub action variable: ${{ github.event.pull_request.number }}
16+ '''
17+
18+ def get_project_title (pr_data ):
19+
20+ # Setting default value
21+ project_title = 'root'
22+
23+ # Iterating through the "files" list
24+ for i in pr_data ["files" ]:
25+ if '/' in i ["path" ]:
26+ project_title = i ["path" ]
27+ break
28+
29+ # If we find a directory
30+ if project_title != 'root' :
31+ project_title = project_title .split ('/' )[0 ]
32+
33+ return project_title
34+
35+ def get_contributor_name (pr_data ):
36+ return pr_data ["author" ]["login" ]
37+
38+ def get_demo_path (pr_data ):
39+
40+ # Getting required values
41+ REPO_NAME = os .environ .get ('REPO_NAME' )
42+ PROJECT_NAME = get_project_title (pr_data )
43+
44+ # Handling a base case
45+ if PROJECT_NAME == 'root' :
46+ return f'https://github.com/{ REPO_NAME } /'
47+
48+ # Setting default value
49+ demo_path = f'https://github.com/{ REPO_NAME } /tree/main/{ PROJECT_NAME } '
50+ found_required_path = False
51+
52+ # Iterating through the "files" list
53+ for file_data in pr_data ["files" ]:
54+ path = file_data ["path" ]
55+ if "index.html" in path :
56+ demo_path = path
57+ found_required_path = True
58+ break
59+ elif path .lower ().endswith ('index.md' ) or path .lower ().endswith ('readme.md' ):
60+ demo_path = path
61+ found_required_path = True
62+
63+ # Modifying demo path as a route
64+ if found_required_path :
65+ demo_path = '/' .join (demo_path .split ('/' )[:- 1 ])
66+
67+ # Checking out for spaces:
68+ if ' ' in demo_path :
69+ demo_path = '%20' .join (demo_path .split ())
70+
71+ return demo_path
72+
73+ def main ():
74+
75+ # Setting required file paths
76+ CURRENT_PR_DETAILS_PATH = 'pr.json'
77+ CONTRIBUTORS_LOG_PATH = '.github/data/contributors-log.json'
78+
79+ # Reading contents from the current pr
80+ with open (CURRENT_PR_DETAILS_PATH , 'r' ) as json_file :
81+ current_pr = json .load (json_file )
82+
83+ # Getting required value for update
84+ PROJECT_TITLE = get_project_title (current_pr )
85+ CONTRIBUTOR_NAME = get_contributor_name (current_pr )
86+ PR_NUMBER = os .environ .get ('PR_NUMBER' )
87+ DEMO_PATH = get_demo_path (current_pr )
88+
89+ # Creating a new dict objects for JSON conversion
90+ existing_data = None
91+ new_data = {
92+ PROJECT_TITLE : {
93+ "contributor-name" : [CONTRIBUTOR_NAME ],
94+ "pull-request-number" : [PR_NUMBER ],
95+ "demo-path" : DEMO_PATH
96+ }
97+ }
98+
99+ # Processing the data dumps
100+ operation_name = None
101+ if os .path .exists (CONTRIBUTORS_LOG_PATH ):
102+
103+ # Reading existing Log file
104+ with open (CONTRIBUTORS_LOG_PATH , 'r' ) as json_file :
105+ existing_data = json .load (json_file )
106+
107+ # performing updation or addition based on `PROJECT_TITLE`
108+ if PROJECT_TITLE in existing_data :
109+ if CONTRIBUTOR_NAME not in existing_data [PROJECT_TITLE ]["contributor-name" ]:
110+ existing_data [PROJECT_TITLE ]["contributor-name" ].append (CONTRIBUTOR_NAME )
111+ if PR_NUMBER not in existing_data [PROJECT_TITLE ]["pull-request-number" ]:
112+ existing_data [PROJECT_TITLE ]["pull-request-number" ].append (PR_NUMBER )
113+ operation_name = 'Updated'
114+ else :
115+ existing_data .update (new_data )
116+ operation_name = 'Appended data to'
117+ else :
118+ existing_data = new_data
119+ operation_name = 'Created'
120+
121+ # Dumping the data into log file
122+ with open (CONTRIBUTORS_LOG_PATH , 'w' ) as json_file :
123+ json .dump (existing_data , json_file , indent = 2 )
124+
125+ # Output message
126+ print (f'Successfully { operation_name } the log file' )
127+
128+ if __name__ == '__main__' :
129+ main ()
0 commit comments