github.com/purpleclay/gitz@v0.8.2-0.20240515052600-43f80eea2fe1/testdata/commitsigned.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  
     7  	git "github.com/purpleclay/gitz"
     8  )
     9  
    10  var (
    11  	gpgPublicKeyID = os.Getenv("GPG_PUBLIC_KEY_ID")
    12  	gpgFingerprint = os.Getenv("GPG_FINGERPRINT")
    13  )
    14  
    15  func main() {
    16  	gitc, _ := git.NewClient()
    17  	gitc.ConfigSetL("user.signingkey", gpgPublicKeyID)
    18  
    19  	if _, err := gitc.Commit("this is a signed commit", git.WithGpgSign(), git.WithAllowEmpty()); err != nil {
    20  		log.Fatal(err.Error())
    21  	}
    22  	gLog, _ := gitc.Log(git.WithTake(1))
    23  
    24  	verif, err := gitc.VerifyCommit(gLog.Commits[0].Hash)
    25  	if err != nil {
    26  		log.Fatal(err.Error())
    27  	}
    28  
    29  	if verif.Author.Name != "batman" {
    30  		log.Fatalf("invalid author name, expecting: 'batman' but received: '%s'", verif.Author.Name)
    31  	}
    32  
    33  	if verif.Author.Email != "batman@dc.com" {
    34  		log.Fatalf("invalid author email, expecting: 'batman@dc.com' but received: '%s'", verif.Author.Email)
    35  	}
    36  
    37  	if verif.Committer.Name != "batman" {
    38  		log.Fatalf("invalid committer name, expecting: 'batman' but received: '%s'", verif.Committer.Name)
    39  	}
    40  
    41  	if verif.Committer.Email != "batman@dc.com" {
    42  		log.Fatalf("invalid committer email, expecting: 'batman@dc.com' but received: '%s'", verif.Committer.Email)
    43  	}
    44  
    45  	if verif.Message != "this is a signed commit" {
    46  		log.Fatalf("invalid commit message, expecting: 'this is a signed commit' but received: '%s'", verif.Message)
    47  	}
    48  
    49  	if verif.Signature.Fingerprint != gpgFingerprint {
    50  		log.Fatalf("invalid fingerprint, expecting: '%s' but received: '%s'", gpgFingerprint, verif.Signature.Fingerprint)
    51  	}
    52  
    53  	if verif.Signature.Author.Name != "batman" {
    54  		log.Fatalf("invalid signed-by name, expecting: 'batman' but received: '%s'", verif.Signature.Author.Name)
    55  	}
    56  
    57  	if verif.Signature.Author.Email != "batman@dc.com" {
    58  		log.Fatalf("invalid signed-by email, expecting: 'batman@dc.com' but received: '%s'", verif.Signature.Author.Email)
    59  	}
    60  }