add github actions is create code-language-detector.yml (#2113)

* feat: remove tools/codescan catalog

* feat: add code language detector github actions
pull/2110/head
Xinwei Xiong 9 months ago committed by GitHub
parent e795dce696
commit 1c2eafce25
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,7 @@
directory: ./
file_types:
- .go
- .yaml
- .yml
languages:
- Chinese

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

@ -3,7 +3,6 @@ go 1.19
use (
.
./test/typecheck
./tools/codescan
./tools/changelog
./tools/component
./tools/formitychecker

@ -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}"

@ -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
}

@ -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)
}
}

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

@ -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
}

@ -1,3 +0,0 @@
module github.com/openimsdk/open-im-server/tools/codescan
go 1.19
Loading…
Cancel
Save