|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +type MegaLinterSummary struct { |
| 11 | + Status string |
| 12 | + Table string |
| 13 | + ReportID string |
| 14 | +} |
| 15 | + |
| 16 | +func GetMegaLinterReport(githubWorkspace, workDir string) (*MegaLinterSummary, error) { |
| 17 | + reportPath := filepath.Join(githubWorkspace, workDir, "megalinter-reports", "megalinter-report.md") |
| 18 | + |
| 19 | + data, err := os.ReadFile(reportPath) |
| 20 | + if err != nil { |
| 21 | + return nil, fmt.Errorf("failed to read MegaLinter report: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + content := string(data) |
| 25 | + |
| 26 | + status := "UNKNOWN" |
| 27 | + if strings.Contains(content, "⚠️ WARNING") { |
| 28 | + status = "WARNING" |
| 29 | + } else if strings.Contains(content, "❌ ERROR") { |
| 30 | + status = "ERROR" |
| 31 | + } else if strings.Contains(content, "✅ SUCCESS") { |
| 32 | + status = "SUCCESS" |
| 33 | + } |
| 34 | + |
| 35 | + lines := strings.Split(content, "\n") |
| 36 | + var tableLines []string |
| 37 | + inTable := false |
| 38 | + |
| 39 | + for _, line := range lines { |
| 40 | + if strings.HasPrefix(line, "|") { |
| 41 | + inTable = true |
| 42 | + tableLines = append(tableLines, line) |
| 43 | + } else if inTable && len(line) == 0 { |
| 44 | + break |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + table := strings.Join(tableLines, "\n") |
| 49 | + |
| 50 | + reportID := fmt.Sprintf("megalinter-report-%d", os.Getpid()) |
| 51 | + |
| 52 | + return &MegaLinterSummary{ |
| 53 | + Status: status, |
| 54 | + Table: table, |
| 55 | + ReportID: reportID, |
| 56 | + }, nil |
| 57 | +} |
| 58 | + |
| 59 | +func FormatMegaLinterReport(summary *MegaLinterSummary, artifactLink string) string { |
| 60 | + var sb strings.Builder |
| 61 | + |
| 62 | + statusEmoji := "⚠️" |
| 63 | + if summary.Status == "SUCCESS" { |
| 64 | + statusEmoji = "✅" |
| 65 | + } else if summary.Status == "ERROR" { |
| 66 | + statusEmoji = "❌" |
| 67 | + } |
| 68 | + |
| 69 | + sb.WriteString(fmt.Sprintf("### %s **MegaLinter Results**\n\n", statusEmoji)) |
| 70 | + |
| 71 | + sb.WriteString(summary.Table) |
| 72 | + |
| 73 | + actionsLink := fmt.Sprintf("https://github.com/%s/actions/runs/%s", |
| 74 | + os.Getenv("GITHUB_REPOSITORY"), |
| 75 | + os.Getenv("GITHUB_RUN_ID")) |
| 76 | + |
| 77 | + sb.WriteString(fmt.Sprintf("\n- [MegaLinter Full Report](%s)\n", actionsLink)) |
| 78 | + |
| 79 | + // Use the provided artifact link instead of constructing one |
| 80 | + if artifactLink != "" { |
| 81 | + sb.WriteString(fmt.Sprintf("- [Download MegaLinter Report](%s)\n", artifactLink)) |
| 82 | + } |
| 83 | + |
| 84 | + return sb.String() |
| 85 | +} |
| 86 | + |
| 87 | +func GetGitHubRunID() string { |
| 88 | + return os.Getenv("GITHUB_RUN_ID") |
| 89 | +} |
0 commit comments