dehub/commit_credential.go
mediocregopher a8c7f92328 include commit hashes as part of credential commit when accrediting change commi...
---
type: change
message: include commit hashes as part of credential commit when accrediting change
  commits
change_hash: AF5g6f2/qrEn0z3JA2PJ9wec71TwWGuMYfrsPPlRTxla
credentials:
- type: pgp_signature
  pub_key_id: 95C46FA6A41148AC
  body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl578NwACgkQlcRvpqQRSKzoVRAAmWfVIA//2yVHJ8VgJyhnfVY/zqFk4dmX+ynxuyBrH2Bp7HVgZBscxLSdrBLBPB68NHinAbK6kPXw2U/NIGcUdHoHfo4Oh6crCtfsMhHb6k4afJKshVKCWDsIws/4gWo4/usvJZEKAjLXys+wmXz6HEsXj1DOP1Z+WjrApuDoOKDxKXVvSmwqKKpfJPhLJJOxKLlfJtwa5a4EuLNvXDL99eOPlvIIaxtl5hj499torpin6Wra6WJikFxEk6jECxz9K7xiVNmKCJ/eT0YQc42misi3kEV5HkIlHpl3IJ6g40dAr92kKVTy2xbgAa2KJYfbU5eF2YoAlByuX6g1upKFSPQBzAuCRLqxIwSDHApSkLG+S56Z8vuxOjrUo868VzXyu2z6VJRvcLJeTpMzm35u1B8WffEqyNnnI+v4m4z7ge7g01HakT1kJuyxBtR3INl3vYT3uXD8yXs0oxexyT12ZRA0l9rKlqdkp7DcxNAkm8SJUUkuG2nArWfqYYtagWKk26espGh1tiBTsTqkbwfdu1mxl1qBwU6n80eUT5oAo0LccjH3sKSPObNKwMLiKn6O9kvSX9yO9a1XiuqdfcTxcvBkqR9hHKdQtmbN2R5HvUv1jNQz/jsqh48u9h01GN6zCDt3AaCEAn6cB/DxCvYbDZwdAVJX3ulONnVs4c6k1UI=
  account: mediocregopher
2020-03-25 18:01:32 -06:00

79 lines
2.3 KiB
Go

package dehub
import (
"dehub/yamlutil"
"encoding/base64"
"fmt"
"strings"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// CommitCredential describes the structure of a credential commit message.
type CommitCredential struct {
CredentialedHash yamlutil.Blob `yaml:"credentialed_hash"`
// CommitHashes represents the commits which this credential is accrediting.
// It is only present for informational purposes, as commits don't not have
// any bearing on the CredentialedHash itself.
CommitHashes []string `yaml:"commits,omitempty"`
}
var _ CommitInterface = CommitCredential{}
// NewCommitCredential constructs and returns a Commit populated with a
// CommitCredential encompassing the given hash. The Credentials of the returned
// Commit will _not_ be filled in.
func (r *Repo) NewCommitCredential(hash []byte) (Commit, error) {
return Commit{
Credential: &CommitCredential{
CredentialedHash: hash,
},
}, nil
}
// NewCommitCredentialFromChanges constructs and returns a Comit populated with
// a CommitCredential for all changes in the given range of GitCommits. The
// message of the last change commit in the range is used when generating the
// hash.
func (r *Repo) NewCommitCredentialFromChanges(commits []GitCommit) (Commit, error) {
info, err := r.changeRangeInfo(commits)
if err != nil {
return Commit{}, err
}
commitCred, err := r.NewCommitCredential(info.changeHash)
if err != nil {
return Commit{}, err
}
for _, commit := range info.changeCommits {
commitCred.Credential.CommitHashes = append(
commitCred.Credential.CommitHashes,
commit.GitCommit.Hash.String(),
)
}
return commitCred, nil
}
// MessageHead implements the method for the CommitInterface interface.
func (cc CommitCredential) MessageHead(common CommitCommon) (string, error) {
hash64 := base64.StdEncoding.EncodeToString(cc.CredentialedHash)
if len(hash64) > 6 {
hash64 = hash64[:6] + "..."
}
credAccounts := strings.Join(common.credAccountIDs(), ", ")
return fmt.Sprintf("Credential of hash %s by %s", hash64, credAccounts), nil
}
// Hash implements the method for the CommitInterface.
func (cc CommitCredential) Hash(_, _ *object.Tree) ([]byte, error) {
return cc.CredentialedHash, nil
}
// GetHash implements the method for the CommitInterface.
func (cc CommitCredential) GetHash() []byte {
return cc.CredentialedHash
}