From f6ad67065b1c2bcbbbf5556b6716718756077c5b Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 10 Jan 2022 14:56:31 +0100 Subject: [PATCH 1/2] Add denylist import - As per https://github.com/go-gitea/gitea/pull/18222#issuecomment-1008386067 --- checks/blacklisted-imports.go | 36 +++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 37 insertions(+) create mode 100644 checks/blacklisted-imports.go diff --git a/checks/blacklisted-imports.go b/checks/blacklisted-imports.go new file mode 100644 index 0000000..969ef4b --- /dev/null +++ b/checks/blacklisted-imports.go @@ -0,0 +1,36 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package checks + +import ( + "strings" + + "golang.org/x/tools/go/analysis" +) + +var ( + deniedImports = []string{"io/ioutil"} + DenylistImports = &analysis.Analyzer{ + Name: "denylist_imports", + Doc: "check for denied imports", + Run: runDenylistImports, + } +) + +func runDenylistImports(pass *analysis.Pass) (interface{}, error) { + for _, file := range pass.Files { + for _, im := range file.Imports { + val := im.Path.Value + val = strings.TrimPrefix(val, `"`) + val = strings.TrimSuffix(val, `"`) + for _, deniedImport := range deniedImports { + if deniedImport == val { + pass.Reportf(im.Path.Pos(), `"`+deniedImport+"\" is not allowed to be imported") + } + } + } + } + return nil, nil +} diff --git a/main.go b/main.go index 5d4193d..52cd30f 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( func main() { unitchecker.Main( + checks.DenylistImports, checks.Imports, checks.License, checks.Migrations, -- 2.40.1 From bd3bf7cc6eab111a61a077bd85c83fe76707cf2c Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 10 Jan 2022 15:00:14 +0100 Subject: [PATCH 2/2] Rename file --- checks/{blacklisted-imports.go => denylisted-imports.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename checks/{blacklisted-imports.go => denylisted-imports.go} (100%) diff --git a/checks/blacklisted-imports.go b/checks/denylisted-imports.go similarity index 100% rename from checks/blacklisted-imports.go rename to checks/denylisted-imports.go -- 2.40.1