Skip to content

Commit

Permalink
🎨 Improve PR action for checking name attribute (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuoqiu-Yingyi authored Jan 13, 2025
1 parent 9c9c84e commit f34bff2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
7 changes: 7 additions & 0 deletions actions/check/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var CheckResultTestExample = CheckResult{
Pass: true,
Name: Name{
Pass: true,
Exist: true,
Unique: true,
Valid: true,
Value: "icon-sample",
Expand Down Expand Up @@ -102,6 +103,7 @@ var CheckResultTestExample = CheckResult{
Pass: false,
Name: Name{
Pass: false,
Exist: false,
Unique: false,
Valid: false,
},
Expand Down Expand Up @@ -157,6 +159,7 @@ var CheckResultTestExample = CheckResult{
Pass: true,
Name: Name{
Pass: true,
Exist: true,
Unique: true,
Valid: true,
Value: "plugin-sample",
Expand Down Expand Up @@ -369,6 +372,7 @@ var CheckResultTestExample = CheckResult{
Pass: true,
Name: Name{
Pass: true,
Exist: true,
Unique: true,
Valid: true,
Value: "theme-sample",
Expand Down Expand Up @@ -420,6 +424,7 @@ var CheckResultTestExample = CheckResult{
Pass: false,
Name: Name{
Pass: false,
Exist: false,
Unique: false,
Valid: false,
},
Expand Down Expand Up @@ -475,6 +480,7 @@ var CheckResultTestExample = CheckResult{
Pass: true,
Name: Name{
Pass: true,
Exist: true,
Unique: true,
Valid: true,
Value: "widget-sample",
Expand Down Expand Up @@ -526,6 +532,7 @@ var CheckResultTestExample = CheckResult{
Pass: false,
Name: Name{
Pass: false,
Exist: false,
Unique: false,
Valid: false,
},
Expand Down
36 changes: 16 additions & 20 deletions actions/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,36 +260,31 @@ func checkRepo(
if attrsCheckResult, err = checkManifestAttrs(manifestFileUrl); err != nil {
logger.Warnf("check repo <\033[7m%s\033[0m> manifest file <\033[7m%s\033[0m> failed: %s", repoPath, manifestFileUrl, err)
} else {
name := strings.ToLower(attrsCheckResult.Name.Value)
if name != "" {
// 字段唯一性检查
// 字段有效性检查
attrsCheckResult.Name.Valid = isValieName(attrsCheckResult.Name.Value)

// 字段唯一性检查
if attrsCheckResult.Name.Valid {
name := strings.ToLower(attrsCheckResult.Name.Value)
if isKeyInSet(name, nameSet) {
logger.Warnf("repo <\033[7m%s\033[0m> name <\033[7m%s\033[0m> already exists", repoPath, name)
} else {
nameSet[name] = nil // 新的 name 添加到检查集合中

attrsCheckResult.Name.Unique = true // name 字段通过唯一性检查
}

// 字段有效性检查
if attrsCheckResult.Name.Valid, err = isValieName(name); err != nil {
logger.Warn(err)
}

attrsCheckResult.Name.Pass = attrsCheckResult.Name.Unique &&
attrsCheckResult.Name.Valid
if attrsCheckResult.Name.Value != repoName {
attrsCheckResult.Name.Pass = false
logger.Warnf("repo <\033[7m%s\033[0m> name <\033[7m%s\033[0m> is not equal to repo name <\033[7m%s\033[0m>", repoPath, attrsCheckResult.Name.Value, repoName)
}

attrsCheckResult.Pass = attrsCheckResult.Name.Pass &&
attrsCheckResult.Version.Pass &&
attrsCheckResult.Author.Pass &&
attrsCheckResult.URL.Pass
}
}

attrsCheckResult.Name.Pass = attrsCheckResult.Name.Exist &&
attrsCheckResult.Name.Valid &&
attrsCheckResult.Name.Unique

attrsCheckResult.Pass = attrsCheckResult.Name.Pass &&
attrsCheckResult.Version.Pass &&
attrsCheckResult.Author.Pass &&
attrsCheckResult.URL.Pass

// 检查文件
var filesCheckResult interface{} // 文件检查结果

Expand Down Expand Up @@ -697,6 +692,7 @@ func checkManifestAttrs(fileURL string) (attrsCheckResult *Attrs, err error) {
if name := manifest["name"]; name != nil {
if value := name.(string); value != "" {
attrsCheckResult.Name.Value = value
attrsCheckResult.Name.Exist = true
}
}
if version := manifest["version"]; version != nil {
Expand Down
5 changes: 3 additions & 2 deletions actions/check/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,15 @@ type Attrs struct {
}

type Name struct {
Pass bool `json:"pass"` // name 字段是否存在
Pass bool `json:"pass"` // name 字段检查是否通过
Value string `json:"value"` // name 字段值

Exist bool `json:"exist"` // name 字段是否存在
Valid bool `json:"valid"` // name 字段值是否有效 (在不同平台均为合法的目录名)
Unique bool `json:"unique"` // name 字段值在同类资源中是否唯一 (大小写不敏感)
}

type Attr struct {
Pass bool `json:"pass"` // 配置文件属性是否存在
Pass bool `json:"pass"` // 配置文件属性检查是否通过
Value string `json:"value"` // 配置文件属性值
}
16 changes: 12 additions & 4 deletions actions/check/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,35 @@ func isKeyInSet(
}

// isValieName 判断资源名称是否有效
func isValieName(name string) (valid bool, err error) {
func isValieName(name string) (valid bool) {
var err error

// 是否为空字符串
if name == "" {
logger.Warnf("name is empty")
return
}

// 是否均为可打印的 ASCii 字符
if valid, err = regexp.MatchString("^[\\x20-\\x7E]+$", name); err != nil {
panic(err)
} else if !valid {
err = fmt.Errorf("name <\033[7m%s\033[0m> contains characters other than printable ASCII characters", name)
logger.Warnf("name <\033[7m%s\033[0m> contains characters other than printable ASCII characters", name)
return
}

// 是否均为有效字符
if valid, err = regexp.MatchString("^[^\\\\/:*?\"<>|. ][^\\\\/:*?\"<>|]*[^\\\\/:*?\"<>|. ]$", name); err != nil {
panic(err)
} else if !valid {
err = fmt.Errorf("name <\033[7m%s\033[0m> contains invalid characters", name)
logger.Warnf("name <\033[7m%s\033[0m> contains invalid characters", name)
return
}

// 是否为保留字
// REF https://learn.microsoft.com/zh-cn/windows/win32/fileio/naming-a-file#naming-conventions
if valid = !isKeyInSet(strings.ToUpper(name), RESERVED_WORDS); !valid {
err = fmt.Errorf("name <\033[7m%s\033[0m> is a reserved word", name)
logger.Warnf("name <\033[7m%s\033[0m> is a reserved word", name)
return
}

Expand Down
1 change: 1 addition & 0 deletions templates/check-result.md.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- {{ if $repo.Files.ReadmeMd.Pass }}[x] [README.md](<{{ $repo.Files.ReadmeMd.URL }}>){{ else }}[ ] `README.md`{{ end }}
- {{ if $repo.Attrs.Pass }}[x]{{ else }}[ ]{{ end }} Attributes that must exist in `plugin.json`
- {{ if $repo.Attrs.Name.Pass }}[x] `name`: `{{ $repo.Attrs.Name.Value }}`{{ else }}[ ] `name`{{ end }}
- {{ if $repo.Attrs.Name.Exist }}[x]{{ else }}[ ]{{ end }} The Attribute exists
- {{ if $repo.Attrs.Name.Valid }}[x]{{ else }}[ ]{{ end }} Is a valid name
- {{ if $repo.Attrs.Name.Unique }}[x]{{ else }}[ ]{{ end }} Not conflict with other plugin name
- {{ if $repo.Attrs.Version.Pass }}[x] `version`: `{{ $repo.Attrs.Version.Value }}`{{ else }}[ ] `version`{{ end }}
Expand Down

0 comments on commit f34bff2

Please sign in to comment.