-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SQL Linter using Squawk #94
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
<?php | ||
/** | ||
* Lints SQL files using Squawk | ||
*/ | ||
final class SquawkLinter extends NodeExternalLinter { | ||
|
||
private $config = null; | ||
|
||
public function getInfoName() { | ||
return 'Squawk'; | ||
} | ||
|
||
public function getInfoURI() { | ||
return 'https://squawkhq.com/'; | ||
} | ||
|
||
public function getInfoDescription() { | ||
return 'Lint SQL using Squawk'; | ||
} | ||
|
||
public function getLinterName() { | ||
return 'SQUAWK'; | ||
} | ||
|
||
public function getLinterConfigurationName() { | ||
return 'squawk'; | ||
} | ||
|
||
public function getVersion() { | ||
list($err, $stdout, $stderr) = exec_manual('%C --version', $this->getExecutableCommand()); | ||
return trim($stdout); | ||
} | ||
|
||
public function getLinterConfigurationOptions() { | ||
$options = array( | ||
'squawk.config' => array( | ||
'type' => 'string', | ||
'help' => pht('Configuration file to use'), | ||
), | ||
); | ||
return $options + parent::getLinterConfigurationOptions(); | ||
} | ||
|
||
public function setLinterConfigurationValue($key, $value) { | ||
switch ($key) { | ||
case 'squawk.config': | ||
$this->config = $value; | ||
return; | ||
} | ||
return parent::setLinterConfigurationValue($key, $value); | ||
} | ||
|
||
public function getNodeBinary() { | ||
return 'squawk'; | ||
} | ||
|
||
public function getNpmPackageName() { | ||
return 'squawk-cli'; | ||
} | ||
|
||
protected function getMandatoryFlags() { | ||
return array('-c', $this->config); | ||
} | ||
|
||
protected function getDefaultFlags() { | ||
return array('--reporter', 'Json'); | ||
} | ||
|
||
public function shouldExpectCommandErrors() { | ||
return true; | ||
} | ||
|
||
public function getLintSeverityMap() { | ||
return array( | ||
'Error' => ArcanistLintSeverity::SEVERITY_ERROR, | ||
'Warning' => ArcanistLintSeverity::SEVERITY_WARNING | ||
); | ||
} | ||
|
||
protected function parseLinterOutput($path, $err, $stdout, $stderr) { | ||
/* | ||
[ | ||
{ | ||
"file": "path/to.sql", | ||
"line": 0, | ||
"column": 0, | ||
"level": "Error", | ||
"messages": [ | ||
{ | ||
"Note": "Postgres failed to parse query: syntax error at or near \"some_random_stuffs\"" | ||
}, | ||
{ | ||
"Help": "Modify your Postgres statement to use valid syntax." | ||
} | ||
], | ||
"rule_name": "invalid-statement" | ||
}, | ||
{ | ||
"file": "path/to_second.sql", | ||
"line": 1, | ||
"column": 0, | ||
"level": "Warning", | ||
"messages": [ | ||
{ | ||
"Note": "Dropping a column may break existing clients." | ||
} | ||
], | ||
"rule_name": "ban-drop-column" | ||
} | ||
] | ||
*/ | ||
$json = json_decode($stdout, true); | ||
|
||
$messages = array(); | ||
foreach ($json as $item) { | ||
$level = $item['level']; | ||
$rulename = $item['rule_name']; | ||
$description = "$level ($rulename): "; | ||
|
||
foreach($item['messages'] as $message) { | ||
$value = ''; | ||
|
||
if (array_key_exists('Note', $message)) { | ||
$value = $message['Note']; | ||
} | ||
|
||
if (array_key_exists('Help', $message)) { | ||
$value = $message['Help']; | ||
} | ||
|
||
$description.="\n$value"; | ||
} | ||
|
||
$line = $item['line']; | ||
$column = $item['column']; | ||
|
||
// $line=0 means the file has broken syntax | ||
// otherwise sum the $line and $ column to get the first row of error | ||
if ($line == '0') { | ||
$line = 1; | ||
} else if ($column != '0') { | ||
$line = $line + $column - 1; | ||
} | ||
|
||
$messages[] = id(new ArcanistLintMessage()) | ||
->setName($this->getLinterName()) | ||
->setCode($rulename) | ||
->setPath(idx($item, 'file', $path)) | ||
->setLine($line) | ||
->setChar(1) | ||
->setDescription($description) | ||
->setSeverity($this->getLintMessageSeverity($level)); | ||
} | ||
|
||
return $messages; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have expecting
$column
to be used here to set the "character" offset, but it looks like it's being used to adjust the$line
above?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the
$column
here is showing how many rows are being warned by the linterfor example you have drop column like this:
the first one will have line 1 column 0
the second one will have line 5 column 2 (so I added line + column - 1)
the third one will have line 8 column 3 (8+3-1 = 10)
so the calculated line is used to show which line is the starting one (includes comment)
and the char offset is not shown in the linter, so it's put as one