From 59613c7e918b6a10714c18abca136ac487033f55 Mon Sep 17 00:00:00 2001 From: Brett Logan Date: Fri, 24 Jul 2020 13:50:31 -0400 Subject: [PATCH] Add Linters Signed-off-by: Brett Logan --- ci/azure-pipelines.yml | 49 ++++++++++++++++++++++++++++++++++++++++++ ci/scripts/lint.sh | 47 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100755 ci/scripts/lint.sh diff --git a/ci/azure-pipelines.yml b/ci/azure-pipelines.yml index 926d6e6f..9875d0da 100644 --- a/ci/azure-pipelines.yml +++ b/ci/azure-pipelines.yml @@ -8,6 +8,8 @@ trigger: variables: FABRIC_VERSION: 2.2 + GO_BIN: $(Build.Repository.LocalPath)/bin + GO_VER: 1.14.6 NODE_VER: 12.x PATH: $(Build.Repository.LocalPath)/bin:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin @@ -68,6 +70,53 @@ jobs: - template: templates/install-deps.yml - template: templates/fabcar/azure-pipelines-typescript.yml + - job: Lint + displayName: Lint + pool: + vmImage: ubuntu-18.04 + strategy: + matrix: + Basic-Application-Javascript: + DIRECTORY: asset-transfer-basic + LANGUAGE: javascript + TYPE: application + Basic-Chaincode-Go: + DIRECTORY: asset-transfer-basic + LANGUAGE: go + TYPE: chaincode + Basic-Chaincode-Javascript: + DIRECTORY: asset-transfer-basic + LANGUAGE: javascript + TYPE: chaincode + Basic-Chaincode-Typescript: + DIRECTORY: asset-transfer-basic + LANGUAGE: typescript + TYPE: chaincode + Ledger-Chaincode-Go: + DIRECTORY: asset-transfer-ledger-queries + LANGUAGE: go + TYPE: chaincode + Private-Chaincode-Go: + DIRECTORY: asset-transfer-private-data + LANGUAGE: go + TYPE: chaincode + Secured-Chaincode-Go: + DIRECTORY: asset-transfer-secured-agreement + LANGUAGE: go + TYPE: chaincode + steps: + - task: GoTool@0 + inputs: + goBin: $(GO_BIN) + version: $(GO_VER) + displayName: Install GoLang + - task: NodeTool@0 + inputs: + versionSpec: $(NODE_VER) + displayName: Install Node.js + - script: ./ci/scripts/lint.sh + displayName: Lint Code + - job: TestNetworkBasic displayName: Test Network pool: diff --git a/ci/scripts/lint.sh b/ci/scripts/lint.sh new file mode 100755 index 00000000..9e8fd4e3 --- /dev/null +++ b/ci/scripts/lint.sh @@ -0,0 +1,47 @@ +set -euo pipefail + +function print() { + GREEN='\033[0;32m' + NC='\033[0m' + echo + echo -e "${GREEN}${1}${NC}" +} + +if [[ "${LANGUAGE}" == "go" ]]; then + go get golang.org/x/tools/cmd/goimports + + cd "${DIRECTORY}/${TYPE}-${LANGUAGE}" + print "Running go vet" + go vet ./... + + print "Running gofmt" + output=$(gofmt -l -s $(go list -f '{{.Dir}}' ./...)) + if [[ "${output}" != "" ]]; then + print "The following files contain formatting errors, please run 'gofmt -l -w ' to fix these issues:" + echo "${output}" + fi + + print "Running goimports" + output=$(goimports -l $(go list -f '{{.Dir}}' ./...)) + if [[ "${output}" != "" ]]; then + print "The following files contain import errors, please run 'goimports -l -w ' to fix these issues:" + echo "${output}" + fi +elif [[ "${LANGUAGE}" == "javascript" ]]; then + npm install -g eslint + cd "${DIRECTORY}/${TYPE}-${LANGUAGE}" + print "Running ESLint" + if [[ "${TYPE}" == "chaincode" ]]; then + eslint *.js */**.js + else + eslint *.js + fi +elif [[ "${LANGUAGE}" == "typescript" ]]; then + npm install -g typescript tslint + cd "${DIRECTORY}/${TYPE}-${LANGUAGE}" + print "Running TSLint" + tslint --project . +else + echo "Language not supported" + exit 1 +fi