Skip to content

Commit

Permalink
Add basic checks to workflow
Browse files Browse the repository at this point in the history
Signed-off-by: asararatnakar <[email protected]>
  • Loading branch information
asararatnakar committed Dec 8, 2023
1 parent e044966 commit 184d8b2
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 3 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/basic-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# Copyright contributors to the Hyperledger Fabric Operator project
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: basic-tests

on:
push:
branches: [api]
pull_request:
branches: [api]

env:
GO_VER: 1.18

jobs:
make-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VER }}
- name: license header checks
run: scripts/check-license.sh
- name: gosec
run: make go-sec
- name: Run vet and format checks
run: make test
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Copyright contributors to the Hyperledger Fabric Operator project
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

## license checks
check-license:
@scripts/check-licenses.sh

# Run go fmt against code
fmt:
go fmt ./...

# Run go vet against code
vet:
@scripts/checks.sh

checks: fmt vet

# gosec checks
gosec:
@scripts/go-sec.sh
1 change: 0 additions & 1 deletion pkg/apis/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,3 @@ type ConsoleImages struct {
// MustgatherDigest is the digest of the mustgather image
MustgatherDigest string `json:"mustgatherDigest,omitempty"`
}

1 change: 0 additions & 1 deletion pkg/apis/peer/v2/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,3 @@ type KeepAlive struct {
Client v1.KeepAliveClient `json:"client,omitempty"`
DeliveryClient v1.KeepAliveClient `json:"deliveryClient,omitempty"`
}

127 changes: 127 additions & 0 deletions scripts/check-licenses.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/bin/bash
#
# Copyright contributors to the Hyperledger Fabric Operator project
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

cat << EOB > golang_copyright.txt
/*
* Copyright contributors to the Hyperledger Fabric Operator project
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
EOB

cat << EOB > shell_copyright.txt
#
# Copyright contributors to the Hyperledger Fabric Operator project
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
EOB

function filterGeneratedFiles {
for f in $@; do
head -n5 $f | grep -qE 'Code generated by.*DO NOT EDIT' || echo $f
done
}

function filterExcludedFiles {
CHECK=`echo "$CHECK" \
| grep -v "^\.git/" \
| grep -v "^\.gitignore" \
| grep -v "\.txt$" \
| grep -v "vendor/" \
| grep -v "go.mod" \
| grep -v "go.sum" \
| grep -v .deepcopy.go \
| sort -u`

CHECK=$(filterGeneratedFiles "$CHECK")
}

CHECK=$(git diff --name-only --diff-filter=ACMRTUXB HEAD)
filterExcludedFiles
if [[ -z "$CHECK" ]]; then
CHECK=$(git diff-tree --no-commit-id --name-only --diff-filter=ACMRTUXB -r "HEAD^..HEAD")
filterExcludedFiles
fi

if [[ -z "$CHECK" ]]; then
echo "All files are excluded from having license headers"
exit 0
fi

missing=`echo "$CHECK" | xargs ls -d 2>/dev/null | xargs grep -L "SPDX-License-Identifier: Apache-2.0"`
if [[ -z "$missing" ]]; then
echo "All files have SPDX-License-Identifier: Apache-2.0"
exit 0
fi

TMPFILE="./tmpfile"

for FILE in ${missing}; do
EXT="${FILE##*.}"
echo "Adding copyright notice to $FILE"
if [ "${EXT}" = "go" ]; then
cat golang_copyright.txt ${FILE} > ${TMPFILE}
cat ${TMPFILE} > ${FILE}
rm -f ${TMPFILE}
echo " ${FILE} copyright notice added"
elif [ "${EXT}" = "yaml" ]; then
cat shell_copyright.txt ${FILE} > ${TMPFILE}
cat ${TMPFILE} > ${FILE}
rm -f ${TMPFILE}
echo " ${FILE} copyright notice added"
elif [ "${EXT}" = "sh" ]; then
cat shell_copyright.txt ${FILE} > ${TMPFILE}
cat ${TMPFILE} > ${FILE}
rm -f ${TMPFILE}
echo " ${FILE} copyright notice added"
else
echo "invalid file extension"
fi
done

rm golang_copyright.txt shell_copyright.txt

exit 0
44 changes: 44 additions & 0 deletions scripts/checks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
#
# Copyright contributors to the Hyperledger Fabric Operator project
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Need to run this before go vet
go mod download

echo "Running 'go vet'"
OUTPUT=`go vet -all ./... 2>&1`
if [ -n "$OUTPUT" ]; then
echo "The following files contain go vet errors"
echo $OUTPUT
exit 1
fi
echo "No 'go vet' issues found"

cd /tmp
go install golang.org/x/tools/cmd/goimports@ff88973b1e4e
cd -
echo "Checking imports ..."
found=`goimports -l \`find . -path ./vendor -prune -o -name "*.go" -print\` 2>&1`
found=$(echo "$found" | grep -v generated)
if [ "$found" != "" ]; then
echo "The following files have import problems:"
echo "$found"
echo "You may run 'goimports -w <file>' to fix each file."
exit 1
fi
echo "All files are properly formatted"
4 changes: 3 additions & 1 deletion scripts/copy_apis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ do
file_name=$(basename "$file_path")
cat ${file_path} | head -${line} > /tmp/${file_name}
mv /tmp/${file_name} ${file_path}
done

done
## format the files
go fmt ../...
23 changes: 23 additions & 0 deletions scripts/go-sec.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

#
# Copyright contributors to the Hyperledger Fabric Operator project
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

curl -sfL https://raw.githubusercontent.com/securego/gosec/master/install.sh | sh -s -- -b $(go env GOPATH)/bin $RELEASE

gosec ./...

0 comments on commit 184d8b2

Please sign in to comment.