Commit 79e63037 authored by zauberstuhl's avatar zauberstuhl

Store build reports in database

parent b393d50e
Pipeline #817 passed with stage
in 1 minute and 23 seconds
......@@ -21,8 +21,6 @@ import (
"bytes"
"text/template"
"net/http"
"regexp"
"strings"
"fmt"
)
......@@ -75,32 +73,11 @@ func reportsBadge(fqdn, slug string, w http.ResponseWriter, r *http.Request) {
return
}
reports, err := build.GetReports()
if err != nil {
logger.Println(err)
http.NotFound(w, r)
return
}
var success, fail, skip int
reSkip := regexp.MustCompile(`^ok.*#\sskip`)
reOk := regexp.MustCompile(`^ok`)
reNot := regexp.MustCompile(`^not`)
for _, report := range reports {
for _, line := range strings.Split(report, "\n") {
if reSkip.MatchString(line) {
skip += 1
continue
}
if reOk.MatchString(line) {
success += 1
continue
}
if reNot.MatchString(line) {
fail += 1
continue
}
}
var success, fail, skip uint
for _, report := range build.Reports {
success += report.TestsOkCnt
fail += report.TestsFailCnt
skip += report.TestsSkipCnt
}
var color = BadgeRed
......
......@@ -26,6 +26,7 @@ import (
"github.com/google/go-github/github"
"context"
"net/url"
"regexp"
"errors"
"encoding/json"
)
......@@ -60,6 +61,10 @@ func (build *Build) Run() {
// stop watching if it failed or succeeded
if status > STATUS_RUNNING {
err = build.Report() // fetch build report from remote
if err != nil {
logger.Printf("#%d: cannot fetch report: %+v\n", build.ID, err)
}
break
}
......@@ -77,6 +82,48 @@ func (build *Build) Run() {
logger.Printf("#%d: pipeline finished\n", build.ID)
}
func (build *Build) Report() error {
reports, err := build.GetReports()
if err != nil {
logger.Println(err)
return err
}
reSkip := regexp.MustCompile(`^ok.*#\sskip`)
reOk := regexp.MustCompile(`^ok`)
reNot := regexp.MustCompile(`^not`)
for _, report := range reports {
var success, fail, skip uint
for _, line := range strings.Split(report, "\n") {
if reSkip.MatchString(line) {
skip += 1
continue
}
if reOk.MatchString(line) {
success += 1
continue
}
if reNot.MatchString(line) {
fail += 1
continue
}
}
dbReport := Report{
BuildID: build.ID,
TestsOkCnt: success,
TestsFailCnt: fail,
TestsSkipCnt: skip,
Raw: report,
}
err = dbReport.Save()
if err != nil {
return err
}
}
return nil
}
func (build *Build) Pipeline() error {
values := url.Values{}
values.Set("PROJECT", build.Project.Name)
......
......@@ -41,6 +41,9 @@ func LoadSchema() {
mergeRequest := &MergeRequest{}
db.AutoMigrate(mergeRequest)
report := &Report{}
db.AutoMigrate(report)
}
// merge request table
......@@ -129,8 +132,9 @@ type Build struct {
PipelineID uint
Status BuildStatus
Project Project `gorm:"ForeignKey:ProjectID"`
MergeRequest MergeRequest `gorm:"ForeignKey:MergeRequestID"`
Reports Reports
Project Project
MergeRequest MergeRequest
}
type Builds []Build
......@@ -140,6 +144,11 @@ func (build *Build) AfterFind(db *gorm.DB) error {
if err != nil {
return err
}
if err := db.Model(build).Related(&build.Reports).Error; err != nil {
logger.Printf("Cannot find reports for build=%+v", *build)
}
return db.Model(build).Related(&build.MergeRequest).Error
}
......@@ -163,3 +172,28 @@ func (build *Build) FindLastByProjectID(id uint) error {
return db.Where("project_id = ?", id).
Order("pipeline_id desc").First(build).Error
}
// report table
type Report struct {
gorm.Model
BuildID uint
TestsOkCnt uint
TestsFailCnt uint
TestsSkipCnt uint
Raw string
}
type Reports []Report
func (report *Report) Save() error {
db, err := OpenDatabase()
if err != nil {
return err
}
defer db.Close()
return db.Save(report).Error
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment