~james/core

core/spindle/db/artifacts.go -rw-r--r-- 1.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package db

import (
	"errors"

	"tangled.org/core/spindle/models"
)

type FinishedLog struct {
	LeaseID  string
	Workflow string
	Ref      string
	Hash     string
}

func (d *DB) GetFinishedLog(wid models.WorkflowId) (*FinishedLog, error) {
	if err := validateWorkflowIdentity(wid); err != nil {
		return nil, err
	}
	var fl FinishedLog
	err := d.QueryRow(
		`select lease_id, workflow, ref, hash
		 from mill_artifacts
		 where pipeline_id = ? and workflow = ?
		 order by id desc limit 1`,
		string(wid.PipelineId), wid.Name,
	).Scan(&fl.LeaseID, &fl.Workflow, &fl.Ref, &fl.Hash)
	if err != nil {
		return nil, err
	}
	return &fl, nil
}

func (d *DB) SaveArtifactRef(leaseID, repoDid string, wid models.WorkflowId, ref, hash string) error {
	if err := validateWorkflowIdentity(wid); err != nil {
		return err
	}
	_, err := d.Exec(
		`insert into mill_artifacts (lease_id, repo_did, pipeline_id, workflow, ref, hash)
		 values (?, ?, ?, ?, ?, ?)`,
		leaseID, repoDid, string(wid.PipelineId), wid.Name, ref, hash,
	)
	return err
}

func validateWorkflowIdentity(wid models.WorkflowId) error {
	if wid.PipelineId == "" || wid.Name == "" {
		return errors.New("incomplete workflow identity")
	}
	return nil
}