Skip to content

Commit

Permalink
Merge pull request #39 from drf/more-checks
Browse files Browse the repository at this point in the history
Add the CodeQL and golangci-lint actions
  • Loading branch information
bettio authored Feb 12, 2022
2 parents 6d09424 + 12e841f commit 244df46
Show file tree
Hide file tree
Showing 8 changed files with 416 additions and 179 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/codeql-analysis.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
pull_request:
schedule:
- cron: '15 9 * * 2'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
32 changes: 32 additions & 0 deletions .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# This file is part of Astarte.
#
# Copyright 2020 Ispirata Srl
#
# 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: "Static code checking"
on:
pull_request:
push:

jobs:
golangci-lint:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
# Run golint-ci
- uses: golangci/golangci-lint-action@v2
with:
version: v1.44
176 changes: 176 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# This file contains all available configuration options
# with their default values.

# options for analysis running
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
# Allow 10m, within actions it might take a lot
timeout: 10m

# which dirs to skip: issues from them won't be reported
skip-dirs:
- external


# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: line-number

# all available settings of specific linters
linters-settings:
dogsled:
# checks assignments with too many blank identifiers; default is 2
max-blank-identifiers: 2
dupl:
# tokens count to trigger issue, 150 by default
threshold: 100
errcheck:
# report about not checking of errors in type assertions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: false

# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: false

# [deprecated] comma-separated list of pairs of the form pkg:regex
# the regex is used to ignore names within pkg. (default "fmt:.*").
# see https://github.com/kisielk/errcheck#the-deprecated-method for details
ignore: SetPrerelease
funlen:
lines: 150
statements: 40
gocognit:
# Keep 30, so we report only truly insane things. Reconciliation functions will always be
# a little bit more complex than needed
min-complexity: 30
goconst:
# minimal length of string constant, 3 by default
min-len: 3
# minimal occurrences count to trigger, 3 by default
min-occurrences: 3
gocritic:
disabled-checks:
# This is a little bit *too* strict, disable it
- commentFormatting
gocyclo:
# minimal code complexity to report, 30 by default (but we recommend 10-20)
min-complexity: 15
godox:
# report any comments starting with keywords, this is useful for TODO or FIXME comments that
# might be left in the code accidentally and should be resolved before merging
keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting
- NOTE
- OPTIMIZE # marks code that should be optimized before merging
- HACK # marks hack-arounds that should be removed before merging
gofmt:
# simplify code: gofmt with `-s` option, true by default
simplify: true
goimports:
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
local-prefixes: github.com/astarte-platform/astarte-device-sdk-go
golint:
# minimal confidence for issues, default is 0.8
min-confidence: 0.8
gomnd:
settings:
mnd:
# the list of enabled checks, see https://github.com/tommy-muehle/go-mnd/#checks for description.
checks: [argument,case,condition,operation,return,assign]
govet:
# report about shadowed variables
check-shadowing: true
enable-all: true
disable:
- fieldalignment
lll:
# max line length, lines longer will be reported. Default is 120.
# '\t' is counted as 1 character by default, and can be changed with the tab-width option
line-length: 120
# tab width in spaces. Default to 1.
tab-width: 1
maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true
misspell:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
locale: US
nakedret:
prealloc:
rowserrcheck:
unparam:
unused:
whitespace:
multi-if: false # Enforces newlines (or comments) after every multi-line if statement
multi-func: false # Enforces newlines (or comments) after every multi-line function signature
wsl:
# If true append is only allowed to be cuddled if appending value is
# matching variables, fields or types on line above. Default is true.
strict-append: true
# Allow calls and assignments to be cuddled as long as the lines have any
# matching variables, fields or types. Default is true.
allow-assign-and-call: true
# Allow multiline assignments to be cuddled. Default is true.
allow-multiline-assign: true
# Allow declarations (var) to be cuddled.
allow-cuddle-declarations: false
# Allow trailing comments in ending of blocks
allow-trailing-comment: false
# Force newlines in end of case at this limit (0 = never).
force-case-trailing-whitespace: 0
# Force cuddling of err checks with err var assignment
force-err-cuddling: false
# Allow leading comments to be separated with empty liens
allow-separated-leading-comment: false

linters:
enable:
- bodyclose
- dupl
- funlen
- gocognit
- goconst
- gocritic
- gocyclo
- goimports
- goprintffuncname
- gosec
#- lll
- unconvert
- unparam

fast: false

# Excluding configuration per-path, per-linter, per-text and per-source
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
- funlen

# Exclude lll issues for long lines with go:generate
- linters:
- lll
source: "^//go:generate "

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0

# Show only new issues: if there are unstaged changes or untracked files,
# only those changes are analyzed, else only changes in HEAD~ are analyzed.
# It's a super-useful option for integration of golangci-lint into existing
# large codebase. It's not practical to fix all existing issues at the moment
# of integration: much better don't allow issues in new code.
# Default is false.
new: false
8 changes: 6 additions & 2 deletions device/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ func (d *Device) getTLSConfig() (*tls.Config, error) {

func (d *Device) getCryptoDir() string {
cryptoDir := filepath.Join(d.persistencyDir, "crypto")
os.MkdirAll(cryptoDir, 0700)
if err := os.MkdirAll(cryptoDir, 0700); err != nil {
fmt.Println("WARNING - could not access crypto dir!")
}
return cryptoDir
}

Expand Down Expand Up @@ -183,7 +185,9 @@ func (d *Device) ensureKeyPair() error {

// We need to generate the key
// First of all, clear the crypto dir, just to be sure.
d.ClearCrypto()
if err := d.ClearCrypto(); err != nil {
return err
}

reader := rand.Reader
// Certificates are short-lived, 2048 is fine.
Expand Down
6 changes: 3 additions & 3 deletions device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"io/ioutil"
"path/filepath"

mqtt "github.com/ispirata/paho.mqtt.golang"
"github.com/astarte-platform/astarte-go/client"
"github.com/astarte-platform/astarte-go/interfaces"
"github.com/astarte-platform/astarte-go/misc"
backoff "github.com/cenkalti/backoff/v4"
mqtt "github.com/ispirata/paho.mqtt.golang"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -115,14 +115,14 @@ func newDevice(deviceID, realm, credentialsSecret string, pairingBaseURL string,
}

if err := d.migrateDb(); err != nil {
errors.New("Database migration failed")
return nil, err
}

return d, nil
}

// Connect connects the device through a goroutine
//nolint
func (d *Device) Connect(result chan<- error) {
go func(result chan<- error) {
// Are we connected already?
Expand Down Expand Up @@ -177,7 +177,7 @@ func (d *Device) Connect(result chan<- error) {
}

// initialize the client
if err := d.initializeMQTTClient(); err != nil {
if err = d.initializeMQTTClient(); err != nil {
if result != nil {
result <- err
}
Expand Down
Loading

0 comments on commit 244df46

Please sign in to comment.