From 6ffbd67b6e16db70ff0796ace9f118b685cb9570 Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 23 Sep 2022 13:25:40 +0900 Subject: [PATCH 1/4] Fix indentrule causing runtime error when the file has a mix of LF and CRLF --- .../indentrule/issue_280_mix_lineending.proto | 68 +++++++++++++++++++ internal/addon/rules/indentRule_test.go | 19 ++++++ linter/fixer/fixer.go | 15 ++-- 3 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 _testdata/rules/indentrule/issue_280_mix_lineending.proto diff --git a/_testdata/rules/indentrule/issue_280_mix_lineending.proto b/_testdata/rules/indentrule/issue_280_mix_lineending.proto new file mode 100644 index 00000000..1ca2654f --- /dev/null +++ b/_testdata/rules/indentrule/issue_280_mix_lineending.proto @@ -0,0 +1,68 @@ +// https://github.com/pseudomuto/protoc-gen-doc/blob/master/examples/proto/Booking.proto + +/** + * Booking related messages. + * + * This file is really just an example. The data model is completely + * fictional. + */ +syntax = "proto3"; + +package com.example; + +import "google/api/annotations.proto"; +import "github.com/mwitkow/go-proto-validators/validator.proto"; + +/** + * Represents the booking status ID. + */ +message BookingStatusID { + int32 id = 1; /// Unique booking status ID. +} + +/** + * Represents the status of a vehicle booking. + */ +message BookingStatus { + int32 id = 1; /// Unique booking status ID. + string description = 2 [(validator.field) = {string_not_empty: true, length_lt: 255}]; /// Booking status description. E.g. "Active". +} + +/** + * Represents the booking of a vehicle. + * + * Vehicles are some cool shit. But drive carefully! + */ +message Booking { + int32 vehicle_id = 1; /// ID of booked vehicle. + int32 customer_id = 2; /// Customer that booked the vehicle. + BookingStatus status = 3; /// Status of the booking. + + /** Has booking confirmation been sent? */ + bool confirmation_sent = 4; + + /** Has payment been received? */ + bool payment_received = 5; + + string color_preference = 6 [deprecated=true]; // Color preference of the customer. +} + +// An empty message for testing +message EmptyBookingMessage { +} + +/** + * Service for handling vehicle bookings. + */ +service BookingService { + /// Used to book a vehicle. Pass in a Booking and a BookingStatus will be returned. + rpc BookVehicle (Booking) returns (BookingStatus) { + option (google.api.http) = { + post: "/api/bookings/vehicle/{vehicle_id}" + body: "*" + }; + } + + /// Used to subscribe to updates of the BookingStatus. + rpc BookingUpdates (BookingStatusID) returns (stream BookingStatus); +} diff --git a/internal/addon/rules/indentRule_test.go b/internal/addon/rules/indentRule_test.go index 54294e75..be2ff9e7 100644 --- a/internal/addon/rules/indentRule_test.go +++ b/internal/addon/rules/indentRule_test.go @@ -216,6 +216,25 @@ Fix https://github.com/yoheimuta/protolint/issues/139`, ), }, }, + { + name: `handle the case that the proto has a mixture of line ending formats like LF and CRLF. +Fix https://github.com/yoheimuta/protolint/issues/280`, + inputProtoPath: setting_test.TestDataPath("rules", "indentrule", "issue_280_mix_lineending.proto"), + wantFailures: []report.Failure{ + report.Failuref( + meta.Position{ + Filename: setting_test.TestDataPath("rules", "indentrule", "issue_280_mix_lineending.proto"), + Offset: 589, + Line: 27, + Column: 5, + }, + "INDENT", + `Found an incorrect indentation style "%s". "%s" is correct.`, + " ", + " ", + ), + }, + }, } for _, test := range tests { diff --git a/linter/fixer/fixer.go b/linter/fixer/fixer.go index 9af1e4db..3e063907 100644 --- a/linter/fixer/fixer.go +++ b/linter/fixer/fixer.go @@ -60,13 +60,14 @@ func NewBaseFixing(protoFileName string) (*BaseFixing, error) { if err != nil { return nil, err } - lineEnding, err := osutil.DetectLineEnding(string(content)) - if err != nil { - return nil, err - } - if len(lineEnding) == 0 { - lineEnding = "\n" - } + + // Regardless of the actual dominant line ending, the fixer will go with LF + // because the parser recognizes only LF as a line ending. + // + // It will work for most cases like used LF, CRLF, and a mix of LF and CRLF. + // See also https://github.com/yoheimuta/protolint/issues/280. + lineEnding := "\n" + return &BaseFixing{ content: content, lineEnding: lineEnding, From 51a4af0e906cc78eb9422f1386acf9e43b45ee55 Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 23 Sep 2022 13:42:56 +0900 Subject: [PATCH 2/4] tests: Stop uploading scan results for now --- .github/workflows/go.yml | 26 ++++++++++--------- .../indentrule/issue_280_mix_lineending.proto | 2 +- internal/addon/rules/indentRule_test.go | 2 +- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 846f162f..8e8f950d 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -39,12 +39,13 @@ jobs: ignore-unfixed: true output: trivy-results.sarif - - name: Upload Trivy scan results to GitHub Security tab - uses: github/codeql-action/upload-sarif@v2 - if: always() - with: - sarif_file: trivy-results.sarif - wait-for-processing: true + # Comment out due to some random failures. See https://github.com/yoheimuta/protolint/actions/runs/3110395412/jobs/5041533203 + #- name: Upload Trivy scan results to GitHub Security tab + # uses: github/codeql-action/upload-sarif@v2 + # if: always() + # with: + # sarif_file: trivy-results.sarif + # wait-for-processing: true - uses: hadolint/hadolint-action@v2.1.0 with: @@ -53,9 +54,10 @@ jobs: format: sarif output-file: hadolint-results.sarif - - name: Upload Hadolint scan results to GitHub Security tab - uses: github/codeql-action/upload-sarif@v2 - if: always() - with: - sarif_file: hadolint-results.sarif - wait-for-processing: true + # Comment out due to some random failures. See https://github.com/yoheimuta/protolint/actions/runs/3110395412/jobs/5041533203 + #- name: Upload Hadolint scan results to GitHub Security tab + # uses: github/codeql-action/upload-sarif@v2 + # if: always() + # with: + # sarif_file: hadolint-results.sarif + # wait-for-processing: true diff --git a/_testdata/rules/indentrule/issue_280_mix_lineending.proto b/_testdata/rules/indentrule/issue_280_mix_lineending.proto index 1ca2654f..9382eec5 100644 --- a/_testdata/rules/indentrule/issue_280_mix_lineending.proto +++ b/_testdata/rules/indentrule/issue_280_mix_lineending.proto @@ -24,7 +24,7 @@ message BookingStatusID { * Represents the status of a vehicle booking. */ message BookingStatus { - int32 id = 1; /// Unique booking status ID. + int32 id = 1; /// Unique booking status ID. string description = 2 [(validator.field) = {string_not_empty: true, length_lt: 255}]; /// Booking status description. E.g. "Active". } diff --git a/internal/addon/rules/indentRule_test.go b/internal/addon/rules/indentRule_test.go index be2ff9e7..aa71f140 100644 --- a/internal/addon/rules/indentRule_test.go +++ b/internal/addon/rules/indentRule_test.go @@ -224,7 +224,7 @@ Fix https://github.com/yoheimuta/protolint/issues/280`, report.Failuref( meta.Position{ Filename: setting_test.TestDataPath("rules", "indentrule", "issue_280_mix_lineending.proto"), - Offset: 589, + Offset: 580, Line: 27, Column: 5, }, From b2faf1eeb2c2934cdd233ddc15dcee0d7372c4ae Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 23 Sep 2022 14:02:05 +0900 Subject: [PATCH 3/4] tests: Remove the unstable part of CI process --- .github/workflows/go.yml | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 8e8f950d..3248d47c 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,39 +25,4 @@ jobs: run: make test - name: Build - run: make build/cmd/protolint - - - name: Build Docker image - run: docker build -t docker.io/yoheimuta/protolint:${{ github.sha }} . - - - name: Run Trivy vulnerability scanner - uses: aquasecurity/trivy-action@0.6.2 - with: - image-ref: docker.io/yoheimuta/protolint:${{ github.sha }} - exit-code: '1' - format: sarif - ignore-unfixed: true - output: trivy-results.sarif - - # Comment out due to some random failures. See https://github.com/yoheimuta/protolint/actions/runs/3110395412/jobs/5041533203 - #- name: Upload Trivy scan results to GitHub Security tab - # uses: github/codeql-action/upload-sarif@v2 - # if: always() - # with: - # sarif_file: trivy-results.sarif - # wait-for-processing: true - - - uses: hadolint/hadolint-action@v2.1.0 - with: - dockerfile: Dockerfile - failure-threshold: style - format: sarif - output-file: hadolint-results.sarif - - # Comment out due to some random failures. See https://github.com/yoheimuta/protolint/actions/runs/3110395412/jobs/5041533203 - #- name: Upload Hadolint scan results to GitHub Security tab - # uses: github/codeql-action/upload-sarif@v2 - # if: always() - # with: - # sarif_file: hadolint-results.sarif - # wait-for-processing: true + run: make build/cmd/protolint \ No newline at end of file From 54403225596365616875462c059009778360d769 Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Fri, 23 Sep 2022 14:20:07 +0900 Subject: [PATCH 4/4] fix yamllint --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 3248d47c..4cd9dfe4 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -25,4 +25,4 @@ jobs: run: make test - name: Build - run: make build/cmd/protolint \ No newline at end of file + run: make build/cmd/protolint