feat: repo health check (#16) #17

Merged
张泊明518370910136 merged 37 commits from file_check into master 2024-09-11 20:09:27 +08:00
8 changed files with 24 additions and 16 deletions
Showing only changes of commit ca590175d9 - Show all commits

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"log/slog"
"os" "os"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/pkg/healthcheck" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/pkg/healthcheck"
@ -28,6 +29,13 @@ func (m *multiStringValue) String() string {
return fmt.Sprintf("%v", *m) return fmt.Sprintf("%v", *m)
} }
func setupSlog() {
opts := &slog.HandlerOptions{}
handler := slog.NewTextHandler(os.Stderr, opts)
logger := slog.New(handler)
slog.SetDefault(logger)
}
// Generally, err is used for runtime errors, and checkRes is used for the result of the checks. // Generally, err is used for runtime errors, and checkRes is used for the result of the checks.
func main() { func main() {
var info []healthcheck.CheckStage var info []healthcheck.CheckStage
@ -45,7 +53,7 @@ func main() {
parseMultiValueFlag(&metaFile, "meta", "") parseMultiValueFlag(&metaFile, "meta", "")
parseMultiValueFlag(&releaseTags, "releaseTags", "") parseMultiValueFlag(&releaseTags, "releaseTags", "")
flag.Parse() flag.Parse()
setupSlog()
tmp = healthcheck.RepoSize() tmp = healthcheck.RepoSize()
info = append(info, tmp) info = append(info, tmp)

View File

@ -1,11 +1,9 @@
package healthcheck package healthcheck
import ( import (
// "encoding/json"
"fmt" "fmt"
"focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage" "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/internal/stage"
// "focs.ji.sjtu.edu.cn/git/FOCS-dev/JOJ3/pkg/healthcheck"
"github.com/criyle/go-judge/envexec" "github.com/criyle/go-judge/envexec"
) )

View File

@ -1,7 +1,7 @@
package healthcheck package healthcheck
import ( import (
"fmt" "log/slog"
"strings" "strings"
"unicode" "unicode"
@ -27,7 +27,7 @@ func NonAsciiMsg(root string) (jsonOut CheckStage) {
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error openning git repo%w", err) slog.Error("openning git repo", "err", err)
return jsonOut return jsonOut
} }
@ -35,14 +35,14 @@ func NonAsciiMsg(root string) (jsonOut CheckStage) {
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error getting reference%w", err) slog.Error("getting reference", "err", err)
return jsonOut return jsonOut
} }
commits, err := repo.Log(&git.LogOptions{From: ref.Hash()}) commits, err := repo.Log(&git.LogOptions{From: ref.Hash()})
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error getting commits%w", err) slog.Error("getting commits", "err", err)
return jsonOut return jsonOut
} }
@ -54,7 +54,7 @@ func NonAsciiMsg(root string) (jsonOut CheckStage) {
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error converting log to string%w", err) slog.Error("converting log to string", "err", err)
return jsonOut return jsonOut
} }

View File

@ -2,6 +2,7 @@ package healthcheck
import ( import (
"fmt" "fmt"
"log/slog"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -67,7 +68,7 @@ func ForbiddenCheck(rootDir string, regexList []string, repo string, droneBranch
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = err slog.Error("get forbiddens", "error", err)
return jsonOut return jsonOut
} }

View File

@ -2,6 +2,7 @@ package healthcheck
import ( import (
"fmt" "fmt"
"log/slog"
"os" "os"
) )
@ -70,7 +71,7 @@ func MetaCheck(rootDir string, fileList []string) (jsonOut CheckStage) {
if err != nil { if err != nil {
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = err slog.Error("get metas", "err", err)
return jsonOut return jsonOut
} }

View File

@ -3,6 +3,7 @@ package healthcheck
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"log/slog"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -71,7 +72,7 @@ func NonAsciiFiles(root string) (jsonOut CheckStage) {
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = err slog.Error("get non-ascii", "err", err)
return jsonOut return jsonOut
} }

View File

@ -1,7 +1,7 @@
package healthcheck package healthcheck
import ( import (
"fmt" "log/slog"
"os/exec" "os/exec"
"strconv" "strconv"
"strings" "strings"
@ -24,7 +24,7 @@ func RepoSize() (jsonOut CheckStage) {
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error running git command:%w", err) slog.Error("running git command:", "err", err)
return jsonOut return jsonOut
} }
@ -38,7 +38,7 @@ func RepoSize() (jsonOut CheckStage) {
if err != nil { if err != nil {
jsonOut.StdOut += "Failed" jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1 jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error running git command:%w", err) slog.Error("running git command:", "err", err)
return jsonOut return jsonOut
} }
sum += size sum += size

View File

@ -8,11 +8,10 @@ import (
// For ExitCode, see https://focs.ji.sjtu.edu.cn/git/TAs/resources/src/branch/drone/dronelib.checks // For ExitCode, see https://focs.ji.sjtu.edu.cn/git/TAs/resources/src/branch/drone/dronelib.checks
// 1 for unrecoverable error and 0 for succeses // 1 for unrecoverable error and 0 for succeses
type CheckStage struct { type CheckStage struct {
Name string `json:"name of check"` Name string `json:"name"`
StdOut string `json:"stdout"` StdOut string `json:"stdout"`
ExitCode int `json:"exit code"` ExitCode int `json:"exit code"`
StdErr string `json:"stderr"` StdErr string `json:"stderr"`
ErrorLog error `json:"errorLog"`
} }
// addExt appends the specified extension to each file name in the given fileList. // addExt appends the specified extension to each file name in the given fileList.