-
Notifications
You must be signed in to change notification settings - Fork 413
/
Copy pathwebsite-pr-validation.rb
226 lines (213 loc) · 8.16 KB
/
website-pr-validation.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
require "rubygems"
require "octokit"
require "json"
require "logger"
require "safe_yaml"
CLIENT = Octokit::Client.new(:access_token => ENV["GITHUB_TOKEN"])
REPOSITORY= ENV["INDIA_REPO_NWO"]
BASE_PATH = "website/data/open-source"
@logger = Logger.new(STDOUT)
# Flag for checking if the issues are present
$ISSUES_PRESENT = false
# Array to store maintainers with failed validations in {BASE_PATH}/maintainers.yml file
MAINTAINERS_FAILED_VALIDATION = []
# Array to store projects with failed validation in {BASE_PATH}/projects.yml file
OSSPROJECTS_FAILED_VALIDATION = []
# Array to store projects with failed validation in {BASE_PATH}/social-good-projects.yml file
SOCIALGOOD_FAILED_VALIDATION = []
# Function to sleep the script for sometime when the API limit is hit
def waitTillLimitReset
timeTillReset = CLIENT.rate_limit.resets_in + 5
@logger.info("API limit reached while fetching... Sleeping for #{timeTillReset} seconds 😴 brb")
sleep(timeTillReset)
end
# Function to prepare the job summary to be added to the PR if it has any issues
# Params:
# category: Category of the issue (maintainers/ossProjects/socialGoodProjects)
# issues: Array of issues present for the PR
# title: Name of the project/maintainer
def prepareJobSummary(category, issues, title)
$ISSUES_PRESENT = true
body = {
:title => title,
:issues => issues
}
if category == "maintainers"
MAINTAINERS_FAILED_VALIDATION.push(body)
elsif category == "ossProjects"
OSSPROJECTS_FAILED_VALIDATION.push(body)
else
SOCIALGOOD_FAILED_VALIDATION.push(body)
end
end
# Function to create job summary
def createJobSummary
comment = "PR cannot be merged due to following issues:\n"
if MAINTAINERS_FAILED_VALIDATION.length() != 0
comment += "- Maintainers\n"
for issueObject in MAINTAINERS_FAILED_VALIDATION do
comment += "\t- `#{issueObject[:title]}`\n"
for issue in issueObject[:issues] do
comment += "\t\t- #{issue}\n"
end
end
end
if OSSPROJECTS_FAILED_VALIDATION.length() != 0
comment += "- OSS Projects\n"
for issueObject in OSSPROJECTS_FAILED_VALIDATION do
comment += "\t- `#{issueObject[:title]}`\n"
for issue in issueObject[:issues] do
comment += "\t\t- #{issue}\n"
end
end
end
if SOCIALGOOD_FAILED_VALIDATION.length() != 0
comment += "- Social Good Projects\n"
for issueObject in SOCIALGOOD_FAILED_VALIDATION do
comment += "\t- `#{issueObject[:title]}`\n"
for issue in issueObject[:issues] do
comment += "\t\t- #{issue}\n"
end
end
end
@logger.info("Summary: #{comment}")
File.write(ENV["GITHUB_STEP_SUMMARY"], comment)
end
# Function for fetching the details of a maintainer
# Params:
# maintainer: Name of the maintainer whos details need be fetched
# Returns:
# maintainer object
def getMaintainer(maintainer)
data = CLIENT.user(maintainer)
return data
end
# Function for validating if the maintainer is valid
# Returns: Array of failed checks
def validateMaintainer(data)
fails = []
# Check if the user has atleast 1 follower
if data.followers < 1
fails.push("Maintainer has less than 1 follower")
end
return fails
end
# Function for fetching the details of a project
# Params:
# projectName: Name of the project whos details need be fetched
# Returns:
# project object
def getProject(projectName)
data = CLIENT.repository(projectName)
return data
end
# Function for validating if the project is valid
# Returns: Array of failed checks
def validateProject(data, isSocialGood = false)
fails = []
# Check if project is private
if data.private
fails.push("Project is either private or doesn't exist!")
end
# Check if project has license
if data.license == nil
fails.push("Project doesn't have a license")
end
# Check if project has atleast 100 stars
if data.stargazers_count < 100 && !isSocialGood
fails.push("Project has less than 100 stars")
end
return fails
end
# Function for fetching all the details of the maintainers
# from the maintainers list at {BASE_PATH}/maintainers.yml
# and check if the maintainers are valid or not
def checkMaintainersData()
maintainersList = JSON.parse(YAML.load(File.open("#{BASE_PATH}/maintainers.yml"), :safe => true).to_json)
for city in maintainersList.keys do
for maintainerName in maintainersList[city] do
begin
maintainer = getMaintainer(maintainerName)
issues = validateMaintainer(maintainer)
if issues.length() != 0
preparePRComments("maintainers", issues, maintainerName)
end
rescue => e
@logger.info("Error #{e.response_status}")
if e.response_status == 403
waitTillLimitReset()
maintainer = getMaintainer(maintainerName)
issues = validateMaintainer(maintainer)
if issues.length() != 0
preparePRComments("maintainers", issues, maintainerName)
end
else
@logger.info("Error on maintainer: #{maintainerName}")
preparePRComments("maintainers", ["User with username #{maintainerName} doesn't exist!"], maintainerName)
end
end
end
end
end
# Function for fetching all the details of the oss projects and social good projects
# from the projects list at {BASE_PATH}/{fileName}
# and check if the projects are valid or not
# Params:
# fileName:
# - Indicates the file location of the list of projects present
# - Values can be either "projects.yml" or "social-good-projects.yml"
def checkProjectsData(fileName)
projectsList = JSON.parse(YAML.load(File.open("#{BASE_PATH}/#{fileName}"), :safe => true).to_json)
if fileName == "projects.yml"
issueCategory = "ossProjects"
else
issueCategory = "socialGoodProjects"
end
for category in projectsList.keys do
if projectsList[category] == nil then
preparePRComments(issueCategory, ["Each category should contain atleast 1 project."], "#{category} in #{fileName}")
next
end
for projectName in projectsList[category] do
begin
project = getProject(projectName)
issues = validateProject(project, issueCategory == "socialGoodProjects")
if issues.length() != 0
preparePRComments(issueCategory, issues, projectName)
end
rescue => e
@logger.info("Error: #{e.response_status}")
if e.response_status == 403
waitTillLimitReset()
project = getProject(projectName)
issues = validateProject(project, issueCategory == "socialGoodProjects")
if issues.length() != 0
preparePRComments(issueCategory, issues, projectName)
end
else
@logger.info("Error on project: #{projectName}")
preparePRComments(issueCategory, ["Project #{projectName} is either private or doesn't exist!"], projectName)
end
end
end
end
end
@logger.info("-------------------------------")
@logger.info("Checking Maintainers...")
checkMaintainersData()
@logger.info("Maintainers data checked")
@logger.info("-------------------------------")
@logger.info("Checking OSS Projects...")
checkProjectsData("projects.yml")
@logger.info("OSS Projects data checked")
@logger.info("-------------------------------")
@logger.info("Checking Social Good Projects...")
checkProjectsData("social-good-projects.yml")
@logger.info("Social Good Projects data checked")
@logger.info("-------------------------------")
if MAINTAINERS_FAILED_VALIDATION.length() != 0 || OSSPROJECTS_FAILED_VALIDATION.length() != 0 || SOCIALGOOD_FAILED_VALIDATION.length() != 0
@logger.info("Creating Comment")
createPRSummary()
exit(1)
end
@logger.info("-------------------------------")