diff --git a/main.go b/main.go index a46be4e..7f3d8e0 100644 --- a/main.go +++ b/main.go @@ -74,6 +74,7 @@ type request struct { Submissions []submissionType `json:"submissions"` // Data for submitted libraries. IndexEntry string `json:"indexEntry"` // Entry that will be made to the Library Manager index source file when the submission is accepted. IndexerLogsURLs string `json:"indexerLogsURLs"` // List of URLs where the logs from the Library Manager indexer for each submission are available for view. + Error string `json:"error"` // Error message. } // submissionType is the type of the data for each individual library submitted in the request. @@ -127,7 +128,7 @@ func main() { } var req request var submissionURLs []string - req.Type, req.ArduinoLintLibraryManagerSetting, submissionURLs = parseDiff(rawDiff, *listNameArgument) + req.Type, req.Error, req.ArduinoLintLibraryManagerSetting, submissionURLs = parseDiff(rawDiff, *listNameArgument) // Process the submissions. var indexEntries []string @@ -177,10 +178,16 @@ func errorExit(message string) { os.Exit(1) } -// parseDiff parses the request diff and returns the request type, `arduino-lint --library-manager` setting, and list of submission URLs. -func parseDiff(rawDiff []byte, listName string) (string, string, []string) { +// parseDiff parses the request diff and returns the request type, request error, `arduino-lint --library-manager` setting, and list of submission URLs. +func parseDiff(rawDiff []byte, listName string) (string, string, string, []string) { var submissionURLs []string + // Check if the PR has removed the final newline from a file, which would cause a spurious diff for the next PR if merged. + // Unfortunately, the diff package does not have this capability (only to detect missing newline in the original file). + if bytes.Contains(rawDiff, []byte("\\ No newline at end of file")) { + return "invalid", "Pull request removes newline from the end of a file.%0APlease add a blank line to the end of the file.", "", nil + } + diffs, err := diff.ParseMultiFileDiff(rawDiff) if err != nil { panic(err) @@ -188,7 +195,7 @@ func parseDiff(rawDiff []byte, listName string) (string, string, []string) { if (len(diffs) != 1) || (diffs[0].OrigName[2:] != listName) || (diffs[0].OrigName[2:] != diffs[0].NewName[2:]) { // Git diffs have a a/ or b/ prefix on file names. // This is not a Library Manager submission. - return "other", "", nil + return "other", "", "", nil } var addedCount int @@ -227,7 +234,7 @@ func parseDiff(rawDiff []byte, listName string) (string, string, []string) { arduinoLintLibraryManagerSetting = "update" } - return requestType, arduinoLintLibraryManagerSetting, submissionURLs + return requestType, "", arduinoLintLibraryManagerSetting, submissionURLs } // populateSubmission does the checks on the submission that aren't provided by Arduino Lint and gathers the necessary data on it. diff --git a/main_test.go b/main_test.go index e18c1eb..b4da57e 100644 --- a/main_test.go +++ b/main_test.go @@ -46,8 +46,9 @@ index cff484d..e14c179 100644 +https://github.com/foo/bar `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs := parseDiff(diff, "repositories.txt") + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs := parseDiff(diff, "repositories.txt") assert.Equal(t, "other", requestType, testName) + assert.Equal(t, "", requestError, testName) assert.Equal(t, "", arduinoLintLibraryManagerSetting, testName) assert.Nil(t, submissionURLs, testName) @@ -62,8 +63,9 @@ index d4edde0..807b76d 100644 +hello `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") assert.Equal(t, "other", requestType, testName) + assert.Equal(t, "", requestError, testName) assert.Equal(t, "", arduinoLintLibraryManagerSetting, testName) assert.Nil(t, submissionURLs, testName) @@ -80,8 +82,9 @@ index cff484d..e14c179 100644 +https://github.com/foo/bar `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") assert.Equal(t, "other", requestType, testName) + assert.Equal(t, "", requestError, testName) assert.Equal(t, "", arduinoLintLibraryManagerSetting, testName) assert.Nil(t, submissionURLs, testName) @@ -96,8 +99,9 @@ index cff484d..9f67763 100644 +https://github.com/foo/baz `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") assert.Equal(t, "submission", requestType, testName) + assert.Equal(t, "", requestError, testName) assert.Equal(t, "submit", arduinoLintLibraryManagerSetting, testName) assert.ElementsMatch(t, submissionURLs, []string{"https://github.com/foo/bar", "https://github.com/foo/baz"}, testName) @@ -112,10 +116,11 @@ index cff484d..1b0b80b 100644 \ No newline at end of file `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") - assert.Equal(t, "submission", requestType, testName) - assert.Equal(t, "submit", arduinoLintLibraryManagerSetting, testName) - assert.ElementsMatch(t, submissionURLs, []string{"https://github.com/foo/bar"}, testName) + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") + assert.Equal(t, "invalid", requestType, testName) + assert.Equal(t, "Pull request removes newline from the end of a file.%0APlease add a blank line to the end of the file.", requestError, testName) + assert.Equal(t, "", arduinoLintLibraryManagerSetting, testName) + assert.Nil(t, submissionURLs, testName) testName = "Submission w/ blank line" diff = []byte(` @@ -125,11 +130,12 @@ index cff484d..1b0b80b 100644 +++ b/repositories.txt @@ -3391,0 +3392 @@ https://github.com/lbernstone/plotutils +https://github.com/foo/bar -\ No newline at end of file ++ `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") assert.Equal(t, "submission", requestType, testName) + assert.Equal(t, "", requestError, testName) assert.Equal(t, "submit", arduinoLintLibraryManagerSetting, testName) assert.ElementsMatch(t, submissionURLs, []string{"https://github.com/foo/bar"}, testName) @@ -143,8 +149,9 @@ index cff484d..38e11d8 100644 -https://github.com/arduino-libraries/Ethernet `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") assert.Equal(t, "removal", requestType, testName) + assert.Equal(t, "", requestError, testName) assert.Equal(t, "", arduinoLintLibraryManagerSetting, testName) assert.Nil(t, submissionURLs, testName) @@ -159,8 +166,9 @@ index cff484d..8b401a1 100644 +https://github.com/foo/bar `) - requestType, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") + requestType, requestError, arduinoLintLibraryManagerSetting, submissionURLs = parseDiff(diff, "repositories.txt") assert.Equal(t, "modification", requestType, testName) + assert.Equal(t, "", requestError, testName) assert.Equal(t, "update", arduinoLintLibraryManagerSetting, testName) assert.Equal(t, submissionURLs, []string{"https://github.com/foo/bar"}, testName) } diff --git a/test/test_all.py b/test/test_all.py index b9b357e..449c3e8 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -21,16 +21,30 @@ @pytest.mark.parametrize( - "repopath_folder_name, expected_type, expected_submissions, expected_indexentry, expected_indexerlogsurls", + "repopath_folder_name," + "expected_type," + "expected_error," + "expected_submissions," + "expected_indexentry," + "expected_indexerlogsurls", [ - ("list-deleted-diff", "other", None, "", ""), - ("multi-file-diff", "other", None, "", ""), - ("non-list-diff", "other", None, "", ""), - ("list-rename-diff", "other", None, "", ""), - ("removal", "removal", None, "", ""), + ("list-deleted-diff", "other", "", None, "", ""), + ("multi-file-diff", "other", "", None, "", ""), + ("non-list-diff", "other", "", None, "", ""), + ("list-rename-diff", "other", "", None, "", ""), + ( + "no-final-newline-diff", + "invalid", + "Pull request removes newline from the end of a file.%0APlease add a blank line to the end of the file.", + None, + "", + "", + ), + ("removal", "removal", "", None, "", ""), ( "modification", "modification", + "", [ { "submissionURL": "https://github.com/arduino-libraries/ArduinoCloudThing", @@ -48,6 +62,7 @@ ( "url-error", "submission", + "", [ { "submissionURL": "foo", @@ -65,6 +80,7 @@ ( "url-404", "submission", + "", [ { "submissionURL": "http://httpstat.us/404", @@ -82,6 +98,7 @@ ( "not-supported-git-host", "submission", + "", [ { "submissionURL": "https://example.com", @@ -101,6 +118,7 @@ ( "not-git-clone-url", "submission", + "", [ { "submissionURL": "https://github.com/arduino-libraries/ArduinoCloudThing/releases", @@ -119,6 +137,7 @@ ( "already-in-library-manager", "submission", + "", [ { "submissionURL": "https://github.com/arduino-libraries/Servo", @@ -136,6 +155,7 @@ ( "type-arduino", "submission", + "", [ { "submissionURL": "https://github.com/arduino-libraries/ArduinoCloudThing", @@ -153,6 +173,7 @@ ( "type-partner", "submission", + "", [ { "submissionURL": "https://github.com/ms-iot/virtual-shields-arduino", @@ -170,6 +191,7 @@ ( "type-recommended", "submission", + "", [ { "submissionURL": "https://github.com/adafruit/Adafruit_TinyFlash", @@ -187,6 +209,7 @@ ( "type-contributed", "submission", + "", [ { "submissionURL": "https://github.com/sparkfun/SparkFun_Ublox_Arduino_Library", @@ -205,6 +228,7 @@ ( "no-tags", "submission", + "", [ { "submissionURL": "https://github.com/arduino/cloud-examples", @@ -224,6 +248,7 @@ ( "no-library-properties", "submission", + "", [ { "submissionURL": "https://github.com/arduino-libraries/WiFiLink-Firmware", @@ -242,6 +267,7 @@ ( "duplicates-in-submission", "submission", + "", [ { "submissionURL": "https://github.com/arduino-libraries/ArduinoCloudThing", @@ -273,6 +299,7 @@ def test_request( run_command, repopath_folder_name, expected_type, + expected_error, expected_submissions, expected_indexentry, expected_indexerlogsurls, @@ -286,6 +313,7 @@ def test_request( request = json.loads(result.stdout) assert request["type"] == expected_type + assert request["error"] == expected_error assert request["submissions"] == expected_submissions assert request["indexEntry"] == expected_indexentry assert request["submissions"] == expected_submissions diff --git a/test/testdata/no-final-newline-diff/diff.txt b/test/testdata/no-final-newline-diff/diff.txt new file mode 100644 index 0000000..390690b --- /dev/null +++ b/test/testdata/no-final-newline-diff/diff.txt @@ -0,0 +1,9 @@ +diff --git a/repositories.txt b/repositories.txt +index c080a7a..dcba162 100644 +--- a/repositories.txt ++++ b/repositories.txt +@@ -1,2 +1,3 @@ + https://github.com/arduino-libraries/Servo + https://github.com/arduino-libraries/Stepper ++https://github.com/arduino-libraries/ArduinoCloudThing +\ No newline at end of file diff --git a/test/testdata/no-final-newline-diff/repositories.txt b/test/testdata/no-final-newline-diff/repositories.txt new file mode 100644 index 0000000..c080a7a --- /dev/null +++ b/test/testdata/no-final-newline-diff/repositories.txt @@ -0,0 +1,2 @@ +https://github.com/arduino-libraries/Servo +https://github.com/arduino-libraries/Stepper