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"
"flag"
"fmt"
"log/slog"
"os"
"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)
}
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.
func main() {
var info []healthcheck.CheckStage
@ -45,7 +53,7 @@ func main() {
parseMultiValueFlag(&metaFile, "meta", "")
parseMultiValueFlag(&releaseTags, "releaseTags", "")
flag.Parse()
setupSlog()
tmp = healthcheck.RepoSize()
info = append(info, tmp)

View File

@ -1,11 +1,9 @@
package healthcheck
import (
// "encoding/json"
"fmt"
"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"
)

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
package healthcheck
import (
"fmt"
"log/slog"
"os/exec"
"strconv"
"strings"
@ -24,7 +24,7 @@ func RepoSize() (jsonOut CheckStage) {
if err != nil {
jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error running git command:%w", err)
slog.Error("running git command:", "err", err)
return jsonOut
}
@ -38,7 +38,7 @@ func RepoSize() (jsonOut CheckStage) {
if err != nil {
jsonOut.StdOut += "Failed"
jsonOut.ExitCode = 1
jsonOut.ErrorLog = fmt.Errorf("Error running git command:%w", err)
slog.Error("running git command:", "err", err)
return jsonOut
}
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
// 1 for unrecoverable error and 0 for succeses
type CheckStage struct {
Name string `json:"name of check"`
Name string `json:"name"`
StdOut string `json:"stdout"`
ExitCode int `json:"exit code"`
StdErr string `json:"stderr"`
ErrorLog error `json:"errorLog"`
}
// addExt appends the specified extension to each file name in the given fileList.