From 1c2eafce2567a242f6d34cfb7b1f155d54db5a54 Mon Sep 17 00:00:00 2001 From: Xinwei Xiong <3293172751NSS@gmail.com> Date: Mon, 18 Mar 2024 10:33:29 +0800 Subject: [PATCH] add github actions is create code-language-detector.yml (#2113) * feat: remove tools/codescan catalog * feat: add code language detector github actions --- .github/code-language-detector.yml | 7 ++ .github/workflows/code-language-detector.yml | 13 +++ go.work | 1 - scripts/verify-annotation-language.sh | 46 -------- tools/codescan/checker/checker.go | 104 ------------------- tools/codescan/codescan.go | 34 ------ tools/codescan/config.yaml | 21 ---- tools/codescan/config/config.go | 49 --------- tools/codescan/go.mod | 3 - 9 files changed, 20 insertions(+), 258 deletions(-) create mode 100644 .github/code-language-detector.yml create mode 100644 .github/workflows/code-language-detector.yml delete mode 100755 scripts/verify-annotation-language.sh delete mode 100644 tools/codescan/checker/checker.go delete mode 100644 tools/codescan/codescan.go delete mode 100644 tools/codescan/config.yaml delete mode 100644 tools/codescan/config/config.go delete mode 100644 tools/codescan/go.mod diff --git a/.github/code-language-detector.yml b/.github/code-language-detector.yml new file mode 100644 index 000000000..9a81236b8 --- /dev/null +++ b/.github/code-language-detector.yml @@ -0,0 +1,7 @@ +directory: ./ +file_types: + - .go + - .yaml + - .yml +languages: + - Chinese \ No newline at end of file diff --git a/.github/workflows/code-language-detector.yml b/.github/workflows/code-language-detector.yml new file mode 100644 index 000000000..22307576a --- /dev/null +++ b/.github/workflows/code-language-detector.yml @@ -0,0 +1,13 @@ +name: Language Check Workflow Test + +on: [pull_request] + +jobs: + comment-language-detector: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Code Language Detector + uses: kubecub/comment-lang-detector@v1.0.0 \ No newline at end of file diff --git a/go.work b/go.work index 8dc91a631..56eb874d4 100644 --- a/go.work +++ b/go.work @@ -3,7 +3,6 @@ go 1.19 use ( . ./test/typecheck - ./tools/codescan ./tools/changelog ./tools/component ./tools/formitychecker diff --git a/scripts/verify-annotation-language.sh b/scripts/verify-annotation-language.sh deleted file mode 100755 index 6b863776c..000000000 --- a/scripts/verify-annotation-language.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/usr/bin/env bash -# Copyright © 2023 OpenIM. All rights reserved. -# -# 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. - - -# This script verifies whether codes follow golang convention. -# Usage: `scripts/verify-pkg-names.sh`. - -set -o errexit - -OPENIM_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. -source "${OPENIM_ROOT}/scripts/lib/init.sh" - -openim::golang::verify_go_version - -openim::golang::verify_go_version - -OPENIM_OUTPUT_HOSTBIN_TOOLS="${OPENIM_ROOT}/_output/bin/tools/linux/amd64" -CODESCAN_BINARY="${OPENIM_OUTPUT_HOSTBIN_TOOLS}/codescan" - -if [[ ! -f "${CODESCAN_BINARY}" ]]; then - echo "codescan binary not found, building..." - pushd "${OPENIM_ROOT}" >/dev/null - make build BINS="codescan" - popd >/dev/null -fi - -if [[ ! -f "${CODESCAN_BINARY}" ]]; then - echo "Failed to build codescan binary." - exit 1 -fi - -CONFIG_PATH="${OPENIM_ROOT}/tools/codescan/config.yaml" - -"${CODESCAN_BINARY}" -config "${CONFIG_PATH}" \ No newline at end of file diff --git a/tools/codescan/checker/checker.go b/tools/codescan/checker/checker.go deleted file mode 100644 index ad724dd5b..000000000 --- a/tools/codescan/checker/checker.go +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package checker - -import ( - "bufio" - "fmt" - "os" - "path/filepath" - "regexp" - "strings" - - "github.com/openimsdk/open-im-server/tools/codescan/config" -) - -type CheckResult struct { - FilePath string - Lines []int -} - -func checkFileForChineseComments(filePath string) ([]CheckResult, error) { - file, err := os.Open(filePath) - if err != nil { - return nil, err - } - defer file.Close() - - var results []CheckResult - scanner := bufio.NewScanner(file) - reg := regexp.MustCompile(`[\p{Han}]+`) - lineNumber := 0 - - var linesWithChinese []int - for scanner.Scan() { - lineNumber++ - if reg.FindString(scanner.Text()) != "" { - linesWithChinese = append(linesWithChinese, lineNumber) - } - } - - if len(linesWithChinese) > 0 { - results = append(results, CheckResult{ - FilePath: filePath, - Lines: linesWithChinese, - }) - } - - if err := scanner.Err(); err != nil { - return nil, err - } - - return results, nil -} - -func WalkDirAndCheckComments(cfg config.Config) error { - var allResults []CheckResult - err := filepath.Walk(cfg.Directory, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - if info.IsDir() { - return nil - } - for _, fileType := range cfg.FileTypes { - if filepath.Ext(path) == fileType { - results, err := checkFileForChineseComments(path) - if err != nil { - return err - } - if len(results) > 0 { - allResults = append(allResults, results...) - } - } - } - return nil - }) - - if err != nil { - return err - } - - if len(allResults) > 0 { - var errMsg strings.Builder - errMsg.WriteString("Files containing Chinese comments:\n") - for _, result := range allResults { - errMsg.WriteString(fmt.Sprintf("%s: Lines %v\n", result.FilePath, result.Lines)) - } - return fmt.Errorf(errMsg.String()) - } - - return nil -} diff --git a/tools/codescan/codescan.go b/tools/codescan/codescan.go deleted file mode 100644 index a83e895fc..000000000 --- a/tools/codescan/codescan.go +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package main - -import ( - "log" - - "github.com/openimsdk/open-im-server/tools/codescan/checker" - "github.com/openimsdk/open-im-server/tools/codescan/config" -) - -func main() { - cfg, err := config.ParseConfig() - if err != nil { - log.Fatalf("Error parsing config: %v", err) - } - - err = checker.WalkDirAndCheckComments(cfg) - if err != nil { - panic(err) - } -} diff --git a/tools/codescan/config.yaml b/tools/codescan/config.yaml deleted file mode 100644 index 32a8c1f54..000000000 --- a/tools/codescan/config.yaml +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright © 2024 OpenIM. All rights reserved. -# -# 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. - -directory: ./ -file_types: - - .go - - .yaml - - .yml -languages: - - Chinese \ No newline at end of file diff --git a/tools/codescan/config/config.go b/tools/codescan/config/config.go deleted file mode 100644 index aebf0d4f7..000000000 --- a/tools/codescan/config/config.go +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright © 2024 OpenIM. All rights reserved. -// -// 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. - -package config - -import ( - "flag" - "log" - "os" - - "gopkg.in/yaml.v2" -) - -type Config struct { - Directory string `yaml:"directory"` - FileTypes []string `yaml:"file_types"` - Languages []string `yaml:"languages"` -} - -func ParseConfig() (Config, error) { - var configPath string - flag.StringVar(&configPath, "config", "./", "Path to config file") - flag.Parse() - - var config Config - if configPath != "" { - configFile, err := os.ReadFile(configPath) - if err != nil { - return Config{}, err - } - if err := yaml.Unmarshal(configFile, &config); err != nil { - return Config{}, err - } - } else { - log.Fatal("Config file must be provided") - } - return config, nil -} diff --git a/tools/codescan/go.mod b/tools/codescan/go.mod deleted file mode 100644 index 2ad132101..000000000 --- a/tools/codescan/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module github.com/openimsdk/open-im-server/tools/codescan - -go 1.19