Skip to content

Commit 5cee619

Browse files
Merge pull request #111 from pgaubatz/commit-timestamp
add `timestamp` to commit datasource
2 parents 9a852a7 + 94574c9 commit 5cee619

File tree

7 files changed

+13
-0
lines changed

7 files changed

+13
-0
lines changed

.web-docs/components/data-source/commit/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ for the list of supported values. Defaults to 'HEAD'.
2727
- `branches` (string) - The short names of branches at the selected commit.
2828
- `author` (string) - The author of the commit, in standard `A U Thor <author@example.com>` format.
2929
- `committer` (string) - The committer of the commit, in same format as author.
30+
- `timestamp` (string) - The timestamp of the commit, in RFC3339 format (e.g. `2024-01-02T09:38:19Z`).
3031
- `pgp_signature` (string) - The PGP signature attached to the commit.
3132
- `message` (string) - The commit message.
3233
- `tree_hash` (string) - The hash of the root tree of the commit.

datasource/commit/data.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package commit
55

66
import (
77
"log"
8+
"time"
89

910
"github.com/ethanmdavidson/packer-plugin-git/common"
1011
"github.com/go-git/go-git/v5"
@@ -29,6 +30,7 @@ type DatasourceOutput struct {
2930
Branches []string `mapstructure:"branches"`
3031
Author string `mapstructure:"author"`
3132
Committer string `mapstructure:"committer"`
33+
Timestamp string `mapstructure:"timestamp"`
3234
PGPSignature string `mapstructure:"pgp_signature"`
3335
Message string `mapstructure:"message"`
3436
TreeHash string `mapstructure:"tree_hash"`
@@ -112,6 +114,9 @@ func (d *Datasource) Execute() (cty.Value, error) {
112114
output.Committer = commit.Committer.String()
113115
log.Printf("output.Committer: '%s'\n", output.Committer)
114116

117+
output.Timestamp = commit.Committer.When.UTC().Format(time.RFC3339)
118+
log.Printf("output.Timestamp: '%s'\n", output.Timestamp)
119+
115120
output.PGPSignature = commit.PGPSignature
116121
log.Printf("len(output.PGPSignature): '%d'\n", len(output.PGPSignature))
117122

datasource/commit/data.hcl2spec.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

datasource/commit/data_acc_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func TestAccGitCommitDatasource(t *testing.T) {
5252
branchLog := "null.basic-example: num_branches: [0-9]*"
5353
authorLog := "null.basic-example: author: [^\\n]*<[^\\n]*>"
5454
committerLog := "null.basic-example: committer: [^\\n]*<[^\\n]*>"
55+
timestampLog := "null.basic-example: timestamp: \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"
5556
//Can't test pgp_signature since that isn't set on most of my commits
5657
messageLog := "null.basic-example: message: .*"
5758
treeHashLog := "null.basic-example: tree_hash: [0-9a-f]{5,40}"
@@ -61,6 +62,7 @@ func TestAccGitCommitDatasource(t *testing.T) {
6162
checkMatch(t, logsString, "num_branches", branchLog)
6263
checkMatch(t, logsString, "author", authorLog)
6364
checkMatch(t, logsString, "committer", committerLog)
65+
checkMatch(t, logsString, "timestamp", timestampLog)
6466
checkMatch(t, logsString, "message", messageLog)
6567
checkMatch(t, logsString, "tree_hash", treeHashLog)
6668
checkMatch(t, logsString, "first_parent", parentLog)

datasource/commit/test-fixtures/template.pkr.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ build {
2323
"echo 'num_branches: ${length(data.git-commit.test.branches)}'",
2424
"echo 'author: ${data.git-commit.test.author}'",
2525
"echo 'committer: ${data.git-commit.test.committer}'",
26+
"echo 'timestamp: ${data.git-commit.test.timestamp}'",
2627
"echo 'pgp_signature: ${data.git-commit.test.pgp_signature}'",
2728
"echo 'message: ${local.message}'",
2829
"echo 'tree_hash: ${data.git-commit.test.tree_hash}'",

docs/datasources/commit.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ for the list of supported values. Defaults to 'HEAD'.
3737
- `branches` (string) - The short names of branches at the selected commit.
3838
- `author` (string) - The author of the commit, in standard `A U Thor <author@example.com>` format.
3939
- `committer` (string) - The committer of the commit, in same format as author.
40+
- `timestamp` (string) - The timestamp of the commit, in RFC3339 format (e.g. `2024-01-02T09:38:19Z`).
4041
- `pgp_signature` (string) - The PGP signature attached to the commit.
4142
- `message` (string) - The commit message.
4243
- `tree_hash` (string) - The hash of the root tree of the commit.

example/commit.pkr.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ build {
2323
"echo 'branches: ${local.branchesString}'",
2424
"echo 'author: ${data.git-commit.test.author}'",
2525
"echo 'committer: ${data.git-commit.test.committer}'",
26+
"echo 'timestamp: ${data.git-commit.test.timestamp}'",
2627
"echo 'pgp_signature: ${data.git-commit.test.pgp_signature}'",
2728
"echo 'message: ${local.message}'",
2829
"echo 'tree_hash: ${data.git-commit.test.tree_hash}'",

0 commit comments

Comments
 (0)