Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Linter CI things and various shared types, methods #1

Merged
merged 5 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/mergify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
queue_rules:
- name: default
conditions:
# Conditions to get out of the queue (= merged)
- check-success=lint-test
- check-success=build

pull_request_rules:
- name: Automatic merge on approval
conditions:
- base=main
- "#approved-reviews-by>=1"
- "#changes-requested-reviews-by=0"
- check-success='lint-test'
- check-success='build'
- label!=do-not-merge
- label=ready-to-merge
actions:
queue:
method: merge
name: default
commit_message_template: |
{{ title }} (#{{ number }})

72 changes: 72 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# 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:
branches: [ main ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ main ]
schedule:
- cron: '34 3 * * 0'

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://aka.ms/codeql-docs/language-support

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

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
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.

# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# 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@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
31 changes: 31 additions & 0 deletions .github/workflows/push-pr-lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: lint and test
on: [pull_request, push]

jobs:
lint-test:
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Checkout code
uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --config .golangci.yml --timeout 2m
version: v1.52.0
- name: Test
run: go test ./...
build:
runs-on: ubuntu-latest
needs: [lint-test]
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Checkout code
uses: actions/checkout@v3
111 changes: 111 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# golangci.com configuration
# https://github.com/golangci/golangci/wiki/Configuration
service:
golangci-lint-version: 1.52.0 # use the fixed version to not introduce new linters unexpectedly

linters-settings:
govet:
enable:
- fieldalignment
auto-fix: true
check-shadowing: true
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
revive:
min-confidence: 0
gocyclo:
min-complexity: 12
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
auto-fix: true
lll:
line-length: 140
goimports:
local-prefixes: github.com/golangci/golangci-lint
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
gofumpt:
extra-rules: true
wsl:
auto-fix: true

linters:
enable:
- errcheck
- gosimple
- govet
- gofmt
- gocyclo
- ineffassign
- stylecheck
- misspell
- staticcheck
- unused
- prealloc
- typecheck
# additional linters
- bodyclose
- gocritic
- goerr113
- goimports
- revive
- gomnd
- misspell
- noctx
- stylecheck
- gosec
enable-all: false
disable-all: true

run:
# build-tags:
skip-dirs:
- internal/fixtures

issues:
exclude-rules:
- linters:
- gosec
text: "weak cryptographic primitive"

- linters:
- stylecheck
text: "ST1016"
exclude:
# Default excludes from `golangci-lint run --help` with EXC0002 removed
# EXC0001 errcheck: Almost all programs ignore errors on these functions and in most cases it's ok
- Error return value of .((os\.)?std(out|err)\..*|.*Close|.*Flush|os\.Remove(All)?|.*print(f|ln)?|os\.(Un)?Setenv). is not checked
# EXC0002 golint: Annoying issue about not having a comment. The rare codebase has such comments
# - (comment on exported (method|function|type|const)|should have( a package)? comment|comment should be of the form)
# EXC0003 golint: False positive when tests are defined in package 'test'
- func name will be used as test\.Test.* by other packages, and that stutters; consider calling this
# EXC0004 govet: Common false positives
- (possible misuse of unsafe.Pointer|should have signature)
# EXC0005 staticcheck: Developers tend to write in C-style with an explicit 'break' in a 'switch', so it's ok to ignore
- ineffective break statement. Did you mean to break out of the outer loop
# EXC0006 gosec: Too many false-positives on 'unsafe' usage
- Use of unsafe calls should be audited
# EXC0007 gosec: Too many false-positives for parametrized shell calls
- Subprocess launch(ed with variable|ing should be audited)
# EXC0008 gosec: Duplicated errcheck checks
- (G104|G307)
# EXC0009 gosec: Too many issues in popular repos
- (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)
# EXC0010 gosec: False positive is triggered by 'src, err := ioutil.ReadFile(filename)'
- Potential file inclusion via variable
exclude-use-default: false
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
.DEFAULT_GOAL := help

## lint
lint:
golangci-lint run --config .golangci.yml

## Go test
test: lint
CGO_ENABLED=0 go test -timeout 1m -v -covermode=atomic ./...

# https://gist.github.com/prwhite/8168133
# COLORS
GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
RESET := $(shell tput -Txterm sgr0)


TARGET_MAX_CHAR_NUM=20
## Show help
help:
@echo ''
@echo 'Usage:'
@echo ' ${YELLOW}make${RESET} ${GREEN}<target>${RESET}'
@echo ''
@echo 'Targets:'
@awk '/^[a-zA-Z\-\\_0-9]+:/ { \
helpMessage = match(lastLine, /^## (.*)/); \
if (helpMessage) { \
helpCommand = substr($$1, 0, index($$1, ":")-1); \
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
} \
} \
{ lastLine = $$0 }' $(MAKEFILE_LIST)
48 changes: 48 additions & 0 deletions condition/firmware_install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package condition

import "github.com/google/uuid"

const (
FirmwareInstall Kind = "firmwareInstall"
)

// FirmwareTaskParameters are the parameters set for a firmwareInstall condition
//
// nolint:govet // fieldalignment struct is easier to read in the current format
type FirmwareInstallTaskParameters struct {
// Inventory identifier for the asset to install firmware on.
AssetID uuid.UUID `json:"assetID"`

// Reset device BMC before firmware install
ResetBMCBeforeInstall bool `json:"resetBMCBeforeInstall,omitempty"`

// Force install given firmware regardless of current firmware version.
ForceInstall bool `json:"forceInstall,omitempty"`

// Task priority is the task priority between 0 and 3
// where 0 is the default and 3 is the max.
//
// Tasks are picked from the `queued` state based on the priority.
//
// When there are multiple tasks with the same priority,
// the task CreatedAt attribute is considered.
Priority int `json:"priority,omitempty"`

// Firmwares is the list of firmwares to be installed.
Firmwares []Firmware `json:"firmwares,omitempty"`

// FirmwareSetID specifies the firmware set to be applied.
FirmwareSetID uuid.UUID `json:"firmwareSetID,omitempty"`
}

// Firmware holds attributes for a firmware object
type Firmware struct {
ID string `yaml:"id"`
Vendor string `yaml:"vendor"`
FileName string `yaml:"filename"`
Version string `yaml:"version"`
URL string `yaml:"URL"`
Component string `yaml:"component"`
Checksum string `yaml:"checksum"`
Models []string `yaml:"models"`
}
42 changes: 42 additions & 0 deletions condition/inventory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package condition

import (
"github.com/google/uuid"
)

type InventoryMethod string

const (
// Condition kind
Inventory Kind = "inventory"

// Inventory methods
InbandInventory InventoryMethod = "inband"
OutofbandInventory InventoryMethod = "outofband"
)

// InventoryTaskParameters are the parameters set for an inventory collection condition
//
// nolint:govet // prefer readability over fieldalignment for this case
type InventoryTaskParameters struct {
// CollectBiosCfg defaults to true
CollectBiosCfg bool `json:"collect_bios_cfg"`

// CollectFirmwareStatus defaults to true
CollectFirwmareStatus bool `json:"collect_firmware_status"`

// Method defaults to Outofband
Method InventoryMethod `json:"inventory_method"`

// Asset identifier.
AssetID uuid.UUID `json:"asset_id"`
}

func NewInventoryTaskParameters(assetID uuid.UUID, method InventoryMethod, collectFirmwareStatus, collectBiosCfg bool) *InventoryTaskParameters {
return &InventoryTaskParameters{
AssetID: assetID,
CollectBiosCfg: collectBiosCfg,
CollectFirwmareStatus: collectFirmwareStatus,
Method: method,
}
}
Loading
Loading