-class BuildLog:
- FILE = 0
- LINE_NUMBER = 1
- WARNING_CODE = 2
- MESSAGE = 3
-
- def __init__(self, filePath):
- self._file = open(filePath, 'r')
- self._parseLog()
-
- def _parseLog(self):
- self._lines = self._file.readlines()
- self._lines[:] = [line for line in self._lines if ": warning" in line]
- self.warnings = [[0 for i in range(4)] for y in range(len(self._lines))]
+import re
- for index, line in enumerate(self._lines):
- self._lines[index] = line.split(":")
- self._lines[index][0] = self._lines[index][0][:-1].split("(")
-
- self.warnings[index][self.FILE] = self._lines[index][0][0]
- self.warnings[index][self.LINE_NUMBER] = int(self._lines[index][0][1].split(",")[0])
- self.warnings[index][self.WARNING_CODE] = self._lines[index][1]
- self.warnings[index][self.MESSAGE] = self._lines[index][2].split("[/")[0]
+class BuildLog:
+ def __init__(self, filePath):
+ self.warnings = []
+ self.errors = []
+ self._file = open(filePath, 'r')
+ self._parseLog()
- def __del__(self):
- self._file.close()
+ def _parseLog(self):
+ errorPattern = re.compile('(.+)\(([0-9]+),[0-9]+\): (error|warning) ([A-Z0-9]+): (.+) \[/')
+ for line in self._file.readlines():
+ m = errorPattern.match(line)
+ if m is not None:
+ item = {'file': m.group(1), 'line': int(m.group(2)), 'type': m.group(3), 'code': m.group(4), 'message': m.group(5)}
+ if item['type'] == 'warning':
+ self.warnings.append(item)
+ elif item['type'] == 'error':
+ self.errors.append(item)
-if __name__ == "__main__":
- logPath = "../../build.log"
- logs = BuildLog(logPath)
- print(logs.warnings)
+ def __del__(self):
+ self._file.close()
if self._CheckComment(file.filename, _position, body):
self.pr.create_review_comment(body, self.latestCommit, file.filename, _position)
-if __name__ == "__main__":
- pr = 9
- gh = PRManager(pr)
- for file in gh.changedFiles:
- print("Chaged File: " + file.filename)
-
+ def CreateIssueComment(self, body):
+ self.pr.create_issue_comment(body)
logPath = "./build.log"
if __name__ == "__main__":
- if len(sys.argv) < 3:
- print("Execute with token and the PR number")
- print(" ~$ python main.py [Token] [PR Number]")
- exit(1)
- logs = BuildLog(logPath)
- pr = PRManager(sys.argv[1], int(sys.argv[2]))
- warningsInFile = []
- for file in pr.changedFiles:
- if file.patch is None:
- continue
- warningsInFile = [warning for warning in logs.warnings if file.filename.endswith(warning[logs.FILE])]
- for diffLine in pr.fileDiffHunkPairs[file]:
- for warning in warningsInFile:
- if (diffLine[0]) <= warning[BuildLog.LINE_NUMBER] and warning[BuildLog.LINE_NUMBER] < (diffLine[0] + diffLine[1]):
- print("{}, Warning on line {}! -> ".format(file.filename, warning[BuildLog.LINE_NUMBER]) + warning[BuildLog.MESSAGE])
- _warningMessage = warning[BuildLog.WARNING_CODE] + ":" +warning[BuildLog.MESSAGE]
- pr.CreateReviewComment(file, warning[BuildLog.LINE_NUMBER], _warningMessage)
+ if len(sys.argv) < 3:
+ print("Execute with token and the PR number")
+ print(" ~$ python main.py [Token] [PR Number]")
+ exit(1)
+ # Parse build.log
+ logs = BuildLog(logPath)
+
+ # Parse PullRequest
+ pr = PRManager(sys.argv[1], int(sys.argv[2]))
+
+ # Report Warnings
+ for file in pr.changedFiles:
+ if file.patch is None:
+ continue
+ warningsInFile = [warning for warning in logs.warnings if file.filename.endswith(warning['file'])]
+ for diffLine in pr.fileDiffHunkPairs[file]:
+ for warning in warningsInFile:
+ if (diffLine[0]) <= warning['line'] and warning['line'] < (diffLine[0] + diffLine[1]):
+ _warningMessage = 'warning ' + warning['code'] + ": " + warning['message']
+ print("{}({}): {}".format(file.filename, warning['line'], _warningMessage))
+ pr.CreateReviewComment(file, warning['line'], _warningMessage)
+
+ # Report Errors
+ if len(logs.erros) > 0:
+ errBody = '### Build Error:\n'
+ for err in logs.errors:
+ errBody += '> {}({}): {}: {}\n'.format(err['file'], err['line'], err['code'], err['message'])
+ pr.CreateIssueComment(errBody)