@@ -20,7 +20,9 @@ import (
20
20
"flag"
21
21
"fmt"
22
22
"os"
23
+ "strconv"
23
24
"strings"
25
+ "time"
24
26
)
25
27
26
28
var (
@@ -35,39 +37,49 @@ var (
35
37
36
38
// Environment contains metadata provided by the build environment.
37
39
type Environment struct {
38
- Name string // name of the environment
39
- Repo string // name of GitHub repo
40
- Commit , Branch , Tag string // Git info
41
- Buildnum string
42
- IsPullRequest bool
43
- IsCronJob bool
40
+ Name string // name of the environment
41
+ Repo string // name of GitHub repo
42
+ Commit , Date , Branch , Tag string // Git info
43
+ Buildnum string
44
+ IsPullRequest bool
45
+ IsCronJob bool
44
46
}
45
47
46
48
func (env Environment ) String () string {
47
- return fmt .Sprintf ("%s env (commit:%s branch:%s tag:%s buildnum:%s pr:%t)" ,
48
- env .Name , env .Commit , env .Branch , env .Tag , env .Buildnum , env .IsPullRequest )
49
+ return fmt .Sprintf ("%s env (commit:%s date:%s branch:%s tag:%s buildnum:%s pr:%t)" ,
50
+ env .Name , env .Commit , env .Date , env . Branch , env .Tag , env .Buildnum , env .IsPullRequest )
49
51
}
50
52
51
53
// Env returns metadata about the current CI environment, falling back to LocalEnv
52
54
// if not running on CI.
53
55
func Env () Environment {
54
56
switch {
55
57
case os .Getenv ("CI" ) == "true" && os .Getenv ("TRAVIS" ) == "true" :
58
+ commit := os .Getenv ("TRAVIS_PULL_REQUEST_SHA" )
59
+ if commit == "" {
60
+ os .Getenv ("TRAVIS_COMMIT" )
61
+ }
56
62
return Environment {
57
63
Name : "travis" ,
58
64
Repo : os .Getenv ("TRAVIS_REPO_SLUG" ),
59
- Commit : os .Getenv ("TRAVIS_COMMIT" ),
65
+ Commit : commit ,
66
+ Date : getDate (commit ),
60
67
Branch : os .Getenv ("TRAVIS_BRANCH" ),
61
68
Tag : os .Getenv ("TRAVIS_TAG" ),
62
69
Buildnum : os .Getenv ("TRAVIS_BUILD_NUMBER" ),
63
70
IsPullRequest : os .Getenv ("TRAVIS_PULL_REQUEST" ) != "false" ,
64
71
IsCronJob : os .Getenv ("TRAVIS_EVENT_TYPE" ) == "cron" ,
65
72
}
66
73
case os .Getenv ("CI" ) == "True" && os .Getenv ("APPVEYOR" ) == "True" :
74
+ commit := os .Getenv ("APPVEYOR_PULL_REQUEST_HEAD_COMMIT" )
75
+ if commit == "" {
76
+ os .Getenv ("APPVEYOR_REPO_COMMIT" )
77
+ }
67
78
return Environment {
68
79
Name : "appveyor" ,
69
80
Repo : os .Getenv ("APPVEYOR_REPO_NAME" ),
70
- Commit : os .Getenv ("APPVEYOR_REPO_COMMIT" ),
81
+ Commit : commit ,
82
+ Date : getDate (commit ),
71
83
Branch : os .Getenv ("APPVEYOR_REPO_BRANCH" ),
72
84
Tag : os .Getenv ("APPVEYOR_REPO_TAG_NAME" ),
73
85
Buildnum : os .Getenv ("APPVEYOR_BUILD_NUMBER" ),
@@ -84,14 +96,15 @@ func LocalEnv() Environment {
84
96
env := applyEnvFlags (Environment {Name : "local" , Repo : "ethereum/go-ethereum" })
85
97
86
98
head := readGitFile ("HEAD" )
87
- if splits := strings .Split (head , " " ); len (splits ) == 2 {
88
- head = splits [1 ]
99
+ if fields := strings .Fields (head ); len (fields ) == 2 {
100
+ head = fields [1 ]
89
101
} else {
90
102
return env
91
103
}
92
104
if env .Commit == "" {
93
105
env .Commit = readGitFile (head )
94
106
}
107
+ env .Date = getDate (env .Commit )
95
108
if env .Branch == "" {
96
109
if head != "HEAD" {
97
110
env .Branch = strings .TrimPrefix (head , "refs/heads/" )
@@ -107,6 +120,21 @@ func firstLine(s string) string {
107
120
return strings .Split (s , "\n " )[0 ]
108
121
}
109
122
123
+ func getDate (commit string ) string {
124
+ if commit == "" {
125
+ return ""
126
+ }
127
+ out := RunGit ("show" , "-s" , "--format=%ct" , commit )
128
+ if out == "" {
129
+ return ""
130
+ }
131
+ date , err := strconv .ParseInt (strings .TrimSpace (out ), 10 , 64 )
132
+ if err != nil {
133
+ panic (fmt .Sprintf ("failed to parse git commit date: %v" , err ))
134
+ }
135
+ return time .Unix (date , 0 ).Format ("20060102" )
136
+ }
137
+
110
138
func applyEnvFlags (env Environment ) Environment {
111
139
if ! flag .Parsed () {
112
140
panic ("you need to call flag.Parse before Env or LocalEnv" )
0 commit comments