Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
NOISSUE - Fix Linting Errors (#65)
Browse files Browse the repository at this point in the history
* style: fix linting errors

Fix code formatting and remove unnecessary
comments

* chore: Exclude string occurrences

Excluded the occurrences of the strings "Usage:" and "For example:" from being marked as issues in the golangci.yml file.

Signed-off-by: Rodney Osodo <[email protected]>

---------

Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo authored Nov 21, 2023
1 parent 7c5ad09 commit 0bc8490
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 71 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0

name: Continuous Integration

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
ci:
name: Continuous Integration
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 1.21.x
cache-dependency-path: "go.sum"

- name: Checkout code
uses: actions/checkout@v4

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Build Binaries
run: |
go mod vendor
make all -j $(nproc)
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ run:
issues:
max-issues-per-linter: 10
max-same-issues: 10
exclude:
- "string `Usage:\n` has (\\d+) occurrences, make it a constant"
- "string `For example:\n` has (\\d+) occurrences, make it a constant"

linters-settings:
gocritic:
Expand Down
1 change: 0 additions & 1 deletion auth/api/http/domains/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func decodeListDomainRequest(ctx context.Context, r *http.Request) (interface{},
}

return req, nil

}

func decodeEnableDomainRequest(_ context.Context, r *http.Request) (interface{}, error) {
Expand Down
2 changes: 0 additions & 2 deletions auth/api/http/domains/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func retrieveDomainEndpoint(svc auth.Service) endpoint.Endpoint {
return nil, err
}
return retrieveDomainRes{Data: domain}, nil

}
}

Expand Down Expand Up @@ -98,7 +97,6 @@ func listDomainsEndpoint(svc auth.Service) endpoint.Endpoint {
return nil, err
}
return listDomainsRes{Data: dp}, nil

}
}

Expand Down
29 changes: 25 additions & 4 deletions auth/api/http/domains/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ type createDomainRes struct {
func (res createDomainRes) Code() int {
return http.StatusOK
}

func (res createDomainRes) Headers() map[string]string {
return map[string]string{}
}

func (res createDomainRes) Empty() bool {
return false
}

func (res createDomainRes) MarshalJSON() ([]byte, error) {
return json.Marshal(res.Data)
}
Expand All @@ -42,12 +45,15 @@ type retrieveDomainRes struct {
func (res retrieveDomainRes) Code() int {
return http.StatusOK
}

func (res retrieveDomainRes) Headers() map[string]string {
return map[string]string{}
}

func (res retrieveDomainRes) Empty() bool {
return false
}

func (res retrieveDomainRes) MarshalJSON() ([]byte, error) {
return json.Marshal(res.Data)
}
Expand All @@ -59,12 +65,15 @@ type updateDomainRes struct {
func (res updateDomainRes) Code() int {
return http.StatusOK
}

func (res updateDomainRes) Headers() map[string]string {
return map[string]string{}
}

func (res updateDomainRes) Empty() bool {
return false
}

func (res updateDomainRes) MarshalJSON() ([]byte, error) {
return json.Marshal(res.Data)
}
Expand All @@ -76,38 +85,43 @@ type listDomainsRes struct {
func (res listDomainsRes) Code() int {
return http.StatusOK
}

func (res listDomainsRes) Headers() map[string]string {
return map[string]string{}
}

func (res listDomainsRes) Empty() bool {
return false
}

func (res listDomainsRes) MarshalJSON() ([]byte, error) {
return json.Marshal(res.Data)
}

type enableDomainRes struct {
}
type enableDomainRes struct{}

func (res enableDomainRes) Code() int {
return http.StatusOK
}

func (res enableDomainRes) Headers() map[string]string {
return map[string]string{}
}

func (res enableDomainRes) Empty() bool {
return true
}

type disableDomainRes struct {
}
type disableDomainRes struct{}

func (res disableDomainRes) Code() int {
return http.StatusOK
}

func (res disableDomainRes) Headers() map[string]string {
return map[string]string{}
}

func (res disableDomainRes) Empty() bool {
return true
}
Expand All @@ -117,9 +131,11 @@ type assignUsersRes struct{}
func (res assignUsersRes) Code() int {
return http.StatusCreated
}

func (res assignUsersRes) Headers() map[string]string {
return map[string]string{}
}

func (res assignUsersRes) Empty() bool {
return true
}
Expand All @@ -129,9 +145,11 @@ type unassignUsersRes struct{}
func (res unassignUsersRes) Code() int {
return http.StatusNoContent
}

func (res unassignUsersRes) Headers() map[string]string {
return map[string]string{}
}

func (res unassignUsersRes) Empty() bool {
return true
}
Expand All @@ -143,12 +161,15 @@ type listUserDomainsRes struct {
func (res listUserDomainsRes) Code() int {
return http.StatusOK
}

func (res listUserDomainsRes) Headers() map[string]string {
return map[string]string{}
}

func (res listUserDomainsRes) Empty() bool {
return false
}

func (res listUserDomainsRes) MarshalJSON() ([]byte, error) {
return json.Marshal(res.Data)
}
7 changes: 7 additions & 0 deletions auth/mocks/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,42 @@ func (m *DomainsRepo) Save(ctx context.Context, d auth.Domain) (auth.Domain, err

return ret.Get(0).(auth.Domain), ret.Error(1)
}

func (m *DomainsRepo) RetrieveByID(ctx context.Context, id string) (auth.Domain, error) {
ret := m.Called(ctx, id)
return ret.Get(0).(auth.Domain), ret.Error(1)
}

func (m *DomainsRepo) RetrieveAllByIDs(ctx context.Context, pm auth.Page) (auth.DomainsPage, error) {
ret := m.Called(ctx, pm)

return ret.Get(0).(auth.DomainsPage), ret.Error(1)
}

func (m *DomainsRepo) ListDomains(ctx context.Context, pm auth.Page) (auth.DomainsPage, error) {
ret := m.Called(ctx, pm)

return ret.Get(0).(auth.DomainsPage), ret.Error(1)
}

func (m *DomainsRepo) Update(ctx context.Context, id string, userID string, d auth.DomainReq) (auth.Domain, error) {
ret := m.Called(ctx, d, id, userID)

return ret.Get(0).(auth.Domain), ret.Error(1)
}

func (m *DomainsRepo) Delete(ctx context.Context, id string) error {
ret := m.Called(ctx, id)

return ret.Error(0)
}

func (m *DomainsRepo) SavePolicies(ctx context.Context, pcs ...auth.Policy) error {
ret := m.Called(ctx, pcs)

return ret.Error(0)
}

func (m *DomainsRepo) DeletePolicies(ctx context.Context, pcs ...auth.Policy) error {
ret := m.Called(ctx, pcs)

Expand Down
3 changes: 0 additions & 3 deletions auth/postgres/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (

var _ auth.DomainsRepository = (*domainRepo)(nil)

var errRollbackTx = errors.New("failed to rollback transaction")

type domainRepo struct {
db postgres.Database
}
Expand All @@ -36,7 +34,6 @@ func NewDomainRepository(db postgres.Database) auth.DomainsRepository {
}
}

// Save the domain to database
func (repo domainRepo) Save(ctx context.Context, d auth.Domain) (ad auth.Domain, err error) {
q := `INSERT INTO domains (id, name, tags, alias, metadata, created_at, updated_at, updated_by, created_by, status)
VALUES (:id, :name, :tags, :alias, :metadata, :created_at, :updated_at, :updated_by, :created_by, :status)
Expand Down
20 changes: 10 additions & 10 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (svc service) Issue(ctx context.Context, token string, key Key) (Token, err
case InvitationKey:
return svc.tmpKey(invitationDuration, key)
default:
return svc.accessKey(key)
return svc.accessKey(ctx, key)
}
}

Expand Down Expand Up @@ -300,12 +300,12 @@ func (svc service) tmpKey(duration time.Duration, key Key) (Token, error) {
return Token{AccessToken: value}, nil
}

func (svc service) accessKey(key Key) (Token, error) {
func (svc service) accessKey(ctx context.Context, key Key) (Token, error) {
var err error
key.Type = AccessKey
key.ExpiresAt = time.Now().Add(svc.loginDuration)

key.Subject, err = svc.checkUserDomain(key)
key.Subject, err = svc.checkUserDomain(ctx, key)
if err != nil {
return Token{}, err
}
Expand Down Expand Up @@ -339,7 +339,7 @@ func (svc service) refreshKey(ctx context.Context, token string, key Key) (Token
key.User = k.User
key.Type = AccessKey

key.Subject, err = svc.checkUserDomain(key)
key.Subject, err = svc.checkUserDomain(ctx, key)
if err != nil {
return Token{}, err
}
Expand All @@ -359,10 +359,10 @@ func (svc service) refreshKey(ctx context.Context, token string, key Key) (Token
return Token{AccessToken: access, RefreshToken: refresh}, nil
}

func (svc service) checkUserDomain(key Key) (subject string, err error) {
func (svc service) checkUserDomain(ctx context.Context, key Key) (subject string, err error) {
if key.Domain != "" {
// Check user is platform admin
if err = svc.Authorize(context.Background(), PolicyReq{
// Check user is platform admin.
if err = svc.Authorize(ctx, PolicyReq{
Subject: key.User,
SubjectType: UserType,
Permission: AdminPermission,
Expand All @@ -371,9 +371,9 @@ func (svc service) checkUserDomain(key Key) (subject string, err error) {
}); err == nil {
return key.User, nil
}
//Check user is domain member
// Check user is domain member.
domainUserSubject := EncodeDomainUserID(key.Domain, key.User)
if err = svc.Authorize(context.Background(), PolicyReq{
if err = svc.Authorize(ctx, PolicyReq{
Subject: domainUserSubject,
SubjectType: UserType,
Permission: MembershipPermission,
Expand Down Expand Up @@ -611,7 +611,7 @@ func (svc service) UnassignUsers(ctx context.Context, token string, id string, u
return svc.removeDomainPolicies(ctx, id, relation, userIds...)
}

// IMPROVEMENT NOTE: Take decision: Only Patform admin or both Patform and domain admins can see others users domain
// IMPROVEMENT NOTE: Take decision: Only Patform admin or both Patform and domain admins can see others users domain.
func (svc service) ListUserDomains(ctx context.Context, token string, userID string, p Page) (DomainsPage, error) {
res, err := svc.Identify(ctx, token)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions auth/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"github.com/stretchr/testify/require"
)

var idProvider = uuid.New()

const (
secret = "secret"
email = "[email protected]"
Expand All @@ -33,10 +31,6 @@ const (
loginDuration = 30 * time.Minute
refreshDuration = 24 * time.Hour
accessToken = "access"

readPolicy = "read"
writePolicy = "write"
deletePolicy = "delete"
)

func newService() (auth.Service, *mocks.Keys) {
Expand Down
2 changes: 1 addition & 1 deletion internal/apiutil/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,6 @@ var (
// ErrUnsupportedContentType indicates unacceptable or lack of Content-Type.
ErrUnsupportedContentType = errors.New("unsupported content type")

// ErrRollbackTx indicates failed to rollback transaction
// ErrRollbackTx indicates failed to rollback transaction.
ErrRollbackTx = errors.New("failed to rollback transaction")
)
2 changes: 1 addition & 1 deletion internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (svc service) ListGroups(ctx context.Context, token, memberKind, memberID s
return svc.groups.RetrieveByIDs(ctx, gm, ids...)
}

// IMPROVEMENT NOTE: remove this function and all its related auxillary function, ListMembers are moved to respective service
// IMPROVEMENT NOTE: remove this function and all its related auxiliary function, ListMembers are moved to respective service.
func (svc service) ListMembers(ctx context.Context, token, groupID, permission, memberKind string) (groups.MembersPage, error) {
_, err := svc.authorize(ctx, auth.UserType, token, auth.ViewPermission, auth.GroupType, groupID)
if err != nil {
Expand Down
16 changes: 0 additions & 16 deletions things/api/http/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,6 @@ func decodeUpdateClientCredentials(_ context.Context, r *http.Request) (interfac
return req, nil
}

func decodeUpdateClientOwner(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
}

req := updateClientOwnerReq{
token: apiutil.ExtractBearerToken(r),
id: chi.URLParam(r, "thingID"),
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, errors.Wrap(errors.ErrMalformedEntity, err))
}

return req, nil
}

func decodeCreateClientReq(_ context.Context, r *http.Request) (interface{}, error) {
if !strings.Contains(r.Header.Get("Content-Type"), api.ContentType) {
return nil, errors.Wrap(apiutil.ErrValidation, apiutil.ErrUnsupportedContentType)
Expand Down
Loading

0 comments on commit 0bc8490

Please sign in to comment.