-
Notifications
You must be signed in to change notification settings - Fork 15
/
integration_helpers.go
120 lines (102 loc) · 2.64 KB
/
integration_helpers.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
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
package main
import (
"fmt"
"io"
"os/exec"
"path"
"regexp"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/mendersoftware/integration-test-runner/git"
)
var gitUpdateMutex = &sync.Mutex{}
func updateIntegrationRepo(conf *config) error {
gitUpdateMutex.Lock()
defer gitUpdateMutex.Unlock()
gitcmd := git.Command("pull", "--rebase", "origin")
gitcmd.Dir = conf.integrationDirectory
// timeout and kill process after gitOperationTimeout seconds
t := time.AfterFunc(gitOperationTimeout*time.Second, func() {
_ = gitcmd.Process.Kill()
})
defer t.Stop()
if err := gitcmd.Run(); err != nil {
return fmt.Errorf("failed to 'git pull' integration folder: %s", err.Error())
}
return nil
}
func getChangelogText(repo, versionRange string, conf *config) (stdout,
stderr string, retErr error) {
c := exec.Command(
path.Join(conf.integrationDirectory,
"extra/changelog-generator/changelog-generator"),
"--repo",
"--sort-changelog",
"--query-github",
"--github-repo", repo,
versionRange,
)
stdout, stderr, retErr = getBothStdoutAndStderr(c)
// Replace commit IDs in warnings, to avoid multiple postings when they
// change.
matcher, err := regexp.Compile("Commit [0-9a-f]{40} had a number")
if err == nil {
stderr = matcher.ReplaceAllString(stderr,
"One commit had a number")
}
return stdout, stderr, retErr
}
// All this because exec doesn't have a SplitOutputs() function...
func getBothStdoutAndStderr(c *exec.Cmd) (stdout, stderr string, retErr error) {
outPipe, err := c.StdoutPipe()
if err != nil {
return "", "", errors.Wrap(err, "getBothStdoutAndStderr: StdoutPipe")
}
errPipe, err := c.StderrPipe()
if err != nil {
return "", "", errors.Wrap(err, "getBothStdoutAndStderr: StderrPipe")
}
err = c.Start()
if err != nil {
return "", "", errors.Wrap(err, "getBothStdoutAndStderr: Start")
}
defer func() {
err := c.Wait()
if err != nil && retErr == nil {
retErr = err
}
}()
type stringAndError struct {
string string
err error
}
outChan := make(chan stringAndError)
errChan := make(chan stringAndError)
go func() {
var s strings.Builder
_, err := io.Copy(&s, outPipe)
outChan <- stringAndError{
s.String(),
err,
}
}()
go func() {
var s strings.Builder
_, err := io.Copy(&s, errPipe)
errChan <- stringAndError{
s.String(),
err,
}
}()
stdoutAndError := <-outChan
if stdoutAndError.err != nil {
return "", "", errors.Wrap(err, "getBothStdoutAndStderr: outChan")
}
stderrAndError := <-errChan
if stderrAndError.err != nil {
return "", "", errors.Wrap(err, "getBothStdoutAndStderr: errChan")
}
return stdoutAndError.string, stderrAndError.string, nil
}