Skip to content

Commit

Permalink
Merge pull request #111 from pgaubatz/commit-timestamp
Browse files Browse the repository at this point in the history
add `timestamp` to commit datasource
  • Loading branch information
ethanmdavidson authored Jan 3, 2024
2 parents 9a852a7 + 94574c9 commit 5cee619
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions .web-docs/components/data-source/commit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ for the list of supported values. Defaults to 'HEAD'.
- `branches` (string) - The short names of branches at the selected commit.
- `author` (string) - The author of the commit, in standard `A U Thor <author@example.com>` format.
- `committer` (string) - The committer of the commit, in same format as author.
- `timestamp` (string) - The timestamp of the commit, in RFC3339 format (e.g. `2024-01-02T09:38:19Z`).
- `pgp_signature` (string) - The PGP signature attached to the commit.
- `message` (string) - The commit message.
- `tree_hash` (string) - The hash of the root tree of the commit.
Expand Down
5 changes: 5 additions & 0 deletions datasource/commit/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package commit

import (
"log"
"time"

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

output.Timestamp = commit.Committer.When.UTC().Format(time.RFC3339)
log.Printf("output.Timestamp: '%s'\n", output.Timestamp)

output.PGPSignature = commit.PGPSignature
log.Printf("len(output.PGPSignature): '%d'\n", len(output.PGPSignature))

Expand Down
2 changes: 2 additions & 0 deletions datasource/commit/data.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions datasource/commit/data_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestAccGitCommitDatasource(t *testing.T) {
branchLog := "null.basic-example: num_branches: [0-9]*"
authorLog := "null.basic-example: author: [^\\n]*<[^\\n]*>"
committerLog := "null.basic-example: committer: [^\\n]*<[^\\n]*>"
timestampLog := "null.basic-example: timestamp: \\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z"
//Can't test pgp_signature since that isn't set on most of my commits
messageLog := "null.basic-example: message: .*"
treeHashLog := "null.basic-example: tree_hash: [0-9a-f]{5,40}"
Expand All @@ -61,6 +62,7 @@ func TestAccGitCommitDatasource(t *testing.T) {
checkMatch(t, logsString, "num_branches", branchLog)
checkMatch(t, logsString, "author", authorLog)
checkMatch(t, logsString, "committer", committerLog)
checkMatch(t, logsString, "timestamp", timestampLog)
checkMatch(t, logsString, "message", messageLog)
checkMatch(t, logsString, "tree_hash", treeHashLog)
checkMatch(t, logsString, "first_parent", parentLog)
Expand Down
1 change: 1 addition & 0 deletions datasource/commit/test-fixtures/template.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ build {
"echo 'num_branches: ${length(data.git-commit.test.branches)}'",
"echo 'author: ${data.git-commit.test.author}'",
"echo 'committer: ${data.git-commit.test.committer}'",
"echo 'timestamp: ${data.git-commit.test.timestamp}'",
"echo 'pgp_signature: ${data.git-commit.test.pgp_signature}'",
"echo 'message: ${local.message}'",
"echo 'tree_hash: ${data.git-commit.test.tree_hash}'",
Expand Down
1 change: 1 addition & 0 deletions docs/datasources/commit.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ for the list of supported values. Defaults to 'HEAD'.
- `branches` (string) - The short names of branches at the selected commit.
- `author` (string) - The author of the commit, in standard `A U Thor <author@example.com>` format.
- `committer` (string) - The committer of the commit, in same format as author.
- `timestamp` (string) - The timestamp of the commit, in RFC3339 format (e.g. `2024-01-02T09:38:19Z`).
- `pgp_signature` (string) - The PGP signature attached to the commit.
- `message` (string) - The commit message.
- `tree_hash` (string) - The hash of the root tree of the commit.
Expand Down
1 change: 1 addition & 0 deletions example/commit.pkr.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ build {
"echo 'branches: ${local.branchesString}'",
"echo 'author: ${data.git-commit.test.author}'",
"echo 'committer: ${data.git-commit.test.committer}'",
"echo 'timestamp: ${data.git-commit.test.timestamp}'",
"echo 'pgp_signature: ${data.git-commit.test.pgp_signature}'",
"echo 'message: ${local.message}'",
"echo 'tree_hash: ${data.git-commit.test.tree_hash}'",
Expand Down

0 comments on commit 5cee619

Please sign in to comment.