~james/core

core/workflow/compile.go -rw-r--r-- 3.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 14 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package workflow

import (
	"errors"
	"fmt"
)

type RawWorkflow struct {
	Name     string
	Contents []byte
}

type RawPipeline = []RawWorkflow

type Compiler struct {
	Trigger      TriggerMetadata
	ChangedFiles []string
	Diagnostics  Diagnostics
}

type Diagnostics struct {
	Errors   []Error
	Warnings []Warning
}

func (d *Diagnostics) IsEmpty() bool {
	return len(d.Errors) == 0 && len(d.Warnings) == 0
}

func (d *Diagnostics) Combine(o Diagnostics) {
	d.Errors = append(d.Errors, o.Errors...)
	d.Warnings = append(d.Warnings, o.Warnings...)
}

func (d *Diagnostics) AddWarning(path string, kind WarningKind, reason string) {
	d.Warnings = append(d.Warnings, Warning{path, kind, reason})
}

func (d *Diagnostics) AddError(path string, err error) {
	d.Errors = append(d.Errors, Error{path, err})
}

func (d Diagnostics) IsErr() bool {
	return len(d.Errors) != 0
}

type Error struct {
	Path  string
	Error error
}

func (e Error) String() string {
	return fmt.Sprintf("error: %s: %s", e.Path, e.Error.Error())
}

type Warning struct {
	Path   string
	Type   WarningKind
	Reason string
}

func (w Warning) String() string {
	return fmt.Sprintf("warning: %s: %s: %s", w.Path, w.Type, w.Reason)
}

var (
	MissingEngine error = errors.New("missing engine")
)

type WarningKind string

var (
	WorkflowSkipped      WarningKind = "workflow skipped"
	InvalidConfiguration WarningKind = "invalid configuration"
)

func (compiler *Compiler) Parse(p RawPipeline) Pipeline {
	var pp Pipeline

	for _, w := range p {
		wf, err := FromFile(w.Name, w.Contents)
		if err != nil {
			compiler.Diagnostics.AddError(w.Name, err)
			continue
		}

		pp = append(pp, wf)
	}

	return pp
}

// convert a repositories' workflow files into a fully compiled pipeline that runners accept
func (compiler *Compiler) Compile(p Pipeline) PipelineSpec {
	cp := PipelineSpec{
		TriggerMetadata: &compiler.Trigger,
	}

	for _, wf := range p {
		cw := compiler.compileWorkflow(wf)

		if cw == nil {
			continue
		}

		cp.Workflows = append(cp.Workflows, cw)
	}

	return cp
}

func (compiler *Compiler) compileWorkflow(w Workflow) *WorkflowSpec {
	matched, err := w.Match(compiler.Trigger, compiler.ChangedFiles)
	if err != nil {
		compiler.Diagnostics.AddError(
			w.Name,
			fmt.Errorf("failed to execute workflow: %w", err),
		)
		return nil
	}
	if !matched {
		compiler.Diagnostics.AddWarning(
			w.Name,
			WorkflowSkipped,
			fmt.Sprintf("did not match trigger %s", compiler.Trigger.Kind),
		)
		return nil
	}

	// validate clone options
	compiler.analyzeCloneOptions(w)

	if w.Engine == "" {
		compiler.Diagnostics.AddError(w.Name, MissingEngine)
		return nil
	}

	o := w.CloneOpts.AsRecord()
	cw := &WorkflowSpec{
		Clone:  &o,
		Engine: w.Engine,
		Name:   w.Name,
		Raw:    w.Raw,
		RunsOn: w.RunsOn,
	}
	if w.Audience != "" {
		cw.Audience = &w.Audience
	}

	return cw
}

func (compiler *Compiler) analyzeCloneOptions(w Workflow) {
	if !w.CloneOpts.Skip {
		return
	}

	warn := func(key string) {
		compiler.Diagnostics.AddWarning(
			w.Name,
			InvalidConfiguration,
			fmt.Sprintf("cannot apply `clone.skip` and `clone.%s`", key),
		)
	}
	if w.CloneOpts.Tags != nil {
		warn("tags")
	}
	if w.CloneOpts.IncludeSubmodules != nil {
		warn("submodules")
	}
	if w.CloneOpts.Depth > 0 {
		warn("depth")
	}
}