Skip to content

Commit

Permalink
capture file metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Aug 2, 2017
1 parent dba3701 commit 60cd607
Show file tree
Hide file tree
Showing 22 changed files with 369 additions and 93 deletions.
5 changes: 5 additions & 0 deletions cmd/drone-agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func run(ctx context.Context, client rpc.Peer, filter rpc.Filter) error {
file.Data, _ = ioutil.ReadAll(limitedPart)
file.Size = len(file.Data)
file.Time = time.Now().Unix()
file.Meta = map[string]string{}

for key, value := range part.Header() {
file.Meta[key] = value[0]
}

if serr := client.Upload(ctxmeta, work.ID, file); serr != nil {
log.Printf("pipeline: cannot upload artifact: %s: %s: %s", work.ID, file.Mime, serr)
Expand Down
19 changes: 11 additions & 8 deletions model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ type FileStore interface {

// File represents a pipeline artifact.
type File struct {
ID int64 `json:"id" meddler:"file_id,pk"`
BuildID int64 `json:"-" meddler:"file_build_id"`
ProcID int64 `json:"proc_id" meddler:"file_proc_id"`
PID int `json:"pid" meddler:"file_pid"`
Name string `json:"name" meddler:"file_name"`
Size int `json:"size" meddler:"file_size"`
Mime string `json:"mime" meddler:"file_mime"`
Time int64 `json:"time" meddler:"file_time"`
ID int64 `json:"id" meddler:"file_id,pk"`
BuildID int64 `json:"-" meddler:"file_build_id"`
ProcID int64 `json:"proc_id" meddler:"file_proc_id"`
PID int `json:"pid" meddler:"file_pid"`
Name string `json:"name" meddler:"file_name"`
Size int `json:"size" meddler:"file_size"`
Mime string `json:"mime" meddler:"file_mime"`
Time int64 `json:"time" meddler:"file_time"`
Passed int `json:"passed" meddler:"file_meta_passed"`
Failed int `json:"failed" meddler:"file_meta_failed"`
Skipped int `json:"skipped" meddler:"file_meta_skipped"`
}
34 changes: 32 additions & 2 deletions server/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,43 @@ func (s *RPC) Upload(c context.Context, id string, file *rpc.File) error {
)
}

return Config.Storage.Files.FileCreate(&model.File{
report := &model.File{
BuildID: proc.BuildID,
ProcID: proc.ID,
PID: proc.PID,
Mime: file.Mime,
Name: file.Name,
Size: file.Size,
Time: file.Time,
},
}
if d, ok := file.Meta["X-Tests-Passed"]; ok {
report.Passed, _ = strconv.Atoi(d)
}
if d, ok := file.Meta["X-Tests-Failed"]; ok {
report.Failed, _ = strconv.Atoi(d)
}
if d, ok := file.Meta["X-Tests-Skipped"]; ok {
report.Skipped, _ = strconv.Atoi(d)
}

if d, ok := file.Meta["X-Checks-Passed"]; ok {
report.Passed, _ = strconv.Atoi(d)
}
if d, ok := file.Meta["X-Checks-Failed"]; ok {
report.Failed, _ = strconv.Atoi(d)
}

if d, ok := file.Meta["X-Coverage-Lines"]; ok {
report.Passed, _ = strconv.Atoi(d)
}
if d, ok := file.Meta["X-Coverage-Total"]; ok {
if total, _ := strconv.Atoi(d); total != 0 {
report.Failed = total - report.Passed
}
}

return Config.Storage.Files.FileCreate(
report,
bytes.NewBuffer(file.Data),
)
}
Expand Down Expand Up @@ -579,7 +607,9 @@ func (s *DroneServer) Upload(c oldcontext.Context, req *proto.UploadRequest) (*p
Proc: req.GetFile().GetProc(),
Size: int(req.GetFile().GetSize()),
Time: req.GetFile().GetTime(),
Meta: req.GetFile().GetMeta(),
}

res := new(proto.Empty)
err := peer.Upload(c, req.GetId(), file)
return res, err
Expand Down
35 changes: 35 additions & 0 deletions store/datastore/ddl/mysql/ddl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ var migrations = []struct {
name: "alter-table-add-file-pid",
stmt: alterTableAddFilePid,
},
{
name: "alter-table-add-file-meta-passed",
stmt: alterTableAddFileMetaPassed,
},
{
name: "alter-table-add-file-meta-failed",
stmt: alterTableAddFileMetaFailed,
},
{
name: "alter-table-add-file-meta-skipped",
stmt: alterTableAddFileMetaSkipped,
},
{
name: "alter-table-update-file-meta",
stmt: alterTableUpdateFileMeta,
},
}

// Migrate performs the database migration. If the migration fails
Expand Down Expand Up @@ -587,3 +603,22 @@ CREATE INDEX ix_perms_user ON perms (perm_user_id);
var alterTableAddFilePid = `
ALTER TABLE files ADD COLUMN file_pid INTEGER
`

var alterTableAddFileMetaPassed = `
ALTER TABLE files ADD COLUMN file_meta_passed INTEGER
`

var alterTableAddFileMetaFailed = `
ALTER TABLE files ADD COLUMN file_meta_failed INTEGER
`

var alterTableAddFileMetaSkipped = `
ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER
`

var alterTableUpdateFileMeta = `
UPDATE files SET
file_meta_passed=0
,file_meta_failed=0
,file_meta_skipped=0
`
19 changes: 19 additions & 0 deletions store/datastore/ddl/mysql/files/018_add_column_file_pid.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
-- name: alter-table-add-file-pid

ALTER TABLE files ADD COLUMN file_pid INTEGER

-- name: alter-table-add-file-meta-passed

ALTER TABLE files ADD COLUMN file_meta_passed INTEGER

-- name: alter-table-add-file-meta-failed

ALTER TABLE files ADD COLUMN file_meta_failed INTEGER

-- name: alter-table-add-file-meta-skipped

ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER

-- name: alter-table-update-file-meta

UPDATE files SET
file_meta_passed=0
,file_meta_failed=0
,file_meta_skipped=0
35 changes: 35 additions & 0 deletions store/datastore/ddl/postgres/ddl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,22 @@ var migrations = []struct {
name: "alter-table-add-file-pid",
stmt: alterTableAddFilePid,
},
{
name: "alter-table-add-file-meta-passed",
stmt: alterTableAddFileMetaPassed,
},
{
name: "alter-table-add-file-meta-failed",
stmt: alterTableAddFileMetaFailed,
},
{
name: "alter-table-add-file-meta-skipped",
stmt: alterTableAddFileMetaSkipped,
},
{
name: "alter-table-update-file-meta",
stmt: alterTableUpdateFileMeta,
},
}

// Migrate performs the database migration. If the migration fails
Expand Down Expand Up @@ -587,3 +603,22 @@ CREATE INDEX IF NOT EXISTS ix_perms_user ON perms (perm_user_id);
var alterTableAddFilePid = `
ALTER TABLE files ADD COLUMN file_pid INTEGER
`

var alterTableAddFileMetaPassed = `
ALTER TABLE files ADD COLUMN file_meta_passed INTEGER
`

var alterTableAddFileMetaFailed = `
ALTER TABLE files ADD COLUMN file_meta_failed INTEGER
`

var alterTableAddFileMetaSkipped = `
ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER
`

var alterTableUpdateFileMeta = `
UPDATE files SET
file_meta_passed=0
,file_meta_failed=0
,file_meta_skipped=0
`
19 changes: 19 additions & 0 deletions store/datastore/ddl/postgres/files/018_add_column_file_pid.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
-- name: alter-table-add-file-pid

ALTER TABLE files ADD COLUMN file_pid INTEGER

-- name: alter-table-add-file-meta-passed

ALTER TABLE files ADD COLUMN file_meta_passed INTEGER

-- name: alter-table-add-file-meta-failed

ALTER TABLE files ADD COLUMN file_meta_failed INTEGER

-- name: alter-table-add-file-meta-skipped

ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER

-- name: alter-table-update-file-meta

UPDATE files SET
file_meta_passed=0
,file_meta_failed=0
,file_meta_skipped=0
35 changes: 35 additions & 0 deletions store/datastore/ddl/sqlite/ddl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ var migrations = []struct {
name: "alter-table-add-file-pid",
stmt: alterTableAddFilePid,
},
{
name: "alter-table-add-file-meta-passed",
stmt: alterTableAddFileMetaPassed,
},
{
name: "alter-table-add-file-meta-failed",
stmt: alterTableAddFileMetaFailed,
},
{
name: "alter-table-add-file-meta-skipped",
stmt: alterTableAddFileMetaSkipped,
},
{
name: "alter-table-update-file-meta",
stmt: alterTableUpdateFileMeta,
},
}

// Migrate performs the database migration. If the migration fails
Expand Down Expand Up @@ -588,3 +604,22 @@ CREATE INDEX IF NOT EXISTS ix_perms_user ON perms (perm_user_id);
var alterTableAddFilePid = `
ALTER TABLE files ADD COLUMN file_pid INTEGER
`

var alterTableAddFileMetaPassed = `
ALTER TABLE files ADD COLUMN file_meta_passed INTEGER
`

var alterTableAddFileMetaFailed = `
ALTER TABLE files ADD COLUMN file_meta_failed INTEGER
`

var alterTableAddFileMetaSkipped = `
ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER
`

var alterTableUpdateFileMeta = `
UPDATE files SET
file_meta_passed=0
,file_meta_failed=0
,file_meta_skipped=0
`
19 changes: 19 additions & 0 deletions store/datastore/ddl/sqlite/files/018_add_column_file_pid.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
-- name: alter-table-add-file-pid

ALTER TABLE files ADD COLUMN file_pid INTEGER

-- name: alter-table-add-file-meta-passed

ALTER TABLE files ADD COLUMN file_meta_passed INTEGER

-- name: alter-table-add-file-meta-failed

ALTER TABLE files ADD COLUMN file_meta_failed INTEGER

-- name: alter-table-add-file-meta-skipped

ALTER TABLE files ADD COLUMN file_meta_skipped INTEGER

-- name: alter-table-update-file-meta

UPDATE files SET
file_meta_passed=0
,file_meta_failed=0
,file_meta_skipped=0
6 changes: 6 additions & 0 deletions store/datastore/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (db *datastore) FileCreate(file *model.File, r io.Reader) error {
Size: file.Size,
Mime: file.Mime,
Time: file.Time,
Passed: file.Passed,
Failed: file.Failed,
Skipped: file.Skipped,
Data: d,
}
return meddler.Insert(db, "files", &f)
Expand All @@ -61,5 +64,8 @@ type fileData struct {
Size int `meddler:"file_size"`
Mime string `meddler:"file_mime"`
Time int64 `meddler:"file_time"`
Passed int `meddler:"file_meta_passed"`
Failed int `meddler:"file_meta_failed"`
Skipped int `meddler:"file_meta_skipped"`
Data []byte `meddler:"file_data"`
}
9 changes: 9 additions & 0 deletions store/datastore/sql/mysql/files/files.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ SELECT
,file_mime
,file_size
,file_time
,file_meta_passed
,file_meta_failed
,file_meta_skipped
FROM files
WHERE file_build_id = ?

Expand All @@ -23,6 +26,9 @@ SELECT
,file_mime
,file_size
,file_time
,file_meta_passed
,file_meta_failed
,file_meta_skipped
FROM files
WHERE file_proc_id = ?
AND file_name = ?
Expand All @@ -38,6 +44,9 @@ SELECT
,file_mime
,file_size
,file_time
,file_meta_passed
,file_meta_failed
,file_meta_skipped
,file_data
FROM files
WHERE file_proc_id = ?
Expand Down
9 changes: 9 additions & 0 deletions store/datastore/sql/mysql/sql_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ SELECT
,file_mime
,file_size
,file_time
,file_meta_passed
,file_meta_failed
,file_meta_skipped
FROM files
WHERE file_build_id = ?
`
Expand All @@ -184,6 +187,9 @@ SELECT
,file_mime
,file_size
,file_time
,file_meta_passed
,file_meta_failed
,file_meta_skipped
FROM files
WHERE file_proc_id = ?
AND file_name = ?
Expand All @@ -199,6 +205,9 @@ SELECT
,file_mime
,file_size
,file_time
,file_meta_passed
,file_meta_failed
,file_meta_skipped
,file_data
FROM files
WHERE file_proc_id = ?
Expand Down
Loading

0 comments on commit 60cd607

Please sign in to comment.