forked from evergreen-ci/evergreen
/
patch.go
84 lines (70 loc) · 1.83 KB
/
patch.go
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
package testutil
import (
"io/ioutil"
"strings"
"github.com/evergreen-ci/evergreen"
"github.com/evergreen-ci/evergreen/db"
"github.com/evergreen-ci/evergreen/model/build"
"github.com/evergreen-ci/evergreen/model/patch"
"github.com/pkg/errors"
)
// the revision we'll assume is the current one for the agent. this is the
// same as appears in testdata/executables/version
var agentRevision = "xxx"
const PatchId = "58d156352cfeb61064cf08b3"
type PatchTestMode int
const (
NoPatch PatchTestMode = iota
InlinePatch
ExternalPatch
)
func (m PatchTestMode) String() string {
switch m {
case NoPatch:
return "none"
case InlinePatch:
return "inline"
case ExternalPatch:
return "external"
}
return "unknown"
}
type PatchRequest struct {
ModuleName string
FilePath string
Githash string
}
func SetupPatches(patchMode PatchTestMode, b *build.Build, patches ...PatchRequest) (*patch.Patch, error) {
if patchMode == NoPatch {
return nil, errors.New("no patch defined")
}
ptch := &patch.Patch{
Id: patch.NewId(PatchId),
Status: evergreen.PatchCreated,
Version: b.Version,
Patches: []patch.ModulePatch{},
}
for _, p := range patches {
patchContent, err := ioutil.ReadFile(p.FilePath)
if err != nil {
return nil, err
}
if patchMode == InlinePatch {
ptch.Patches = append(ptch.Patches, patch.ModulePatch{
ModuleName: p.ModuleName,
Githash: p.Githash,
PatchSet: patch.PatchSet{Patch: string(patchContent)},
})
} else {
if err := db.WriteGridFile(patch.GridFSPrefix, string(ptch.Id), strings.NewReader(string(patchContent))); err != nil {
return nil, err
}
ptch.Patches = append(ptch.Patches, patch.ModulePatch{
ModuleName: p.ModuleName,
Githash: p.Githash,
PatchSet: patch.PatchSet{PatchFileId: string(ptch.Id)},
})
}
}
return ptch, ptch.Insert()
}