2020-03-05 23:43:40 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2020-04-18 19:26:32 +00:00
|
|
|
"errors"
|
2020-03-05 23:43:40 +00:00
|
|
|
"fmt"
|
2020-03-21 17:22:15 +00:00
|
|
|
"strings"
|
2020-03-05 23:43:40 +00:00
|
|
|
|
2020-04-12 17:59:23 +00:00
|
|
|
"dehub.dev/src/dehub.git/yamlutil"
|
2020-03-05 23:43:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CommitCredential describes the structure of a credential commit message.
|
|
|
|
type CommitCredential struct {
|
|
|
|
CredentialedHash yamlutil.Blob `yaml:"credentialed_hash"`
|
2020-03-26 00:01:32 +00:00
|
|
|
|
|
|
|
// 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"`
|
2020-03-05 23:43:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:26:32 +00:00
|
|
|
// NewCommitCredentialFromChanges constructs and returns a Commit populated with
|
2020-03-23 23:15:44 +00:00
|
|
|
// 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
|
|
|
|
}
|
2020-03-26 00:01:32 +00:00
|
|
|
|
|
|
|
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
|
2020-03-23 23:15:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 23:43:40 +00:00
|
|
|
// MessageHead implements the method for the CommitInterface interface.
|
2020-03-21 17:22:15 +00:00
|
|
|
func (cc CommitCredential) MessageHead(common CommitCommon) (string, error) {
|
2020-03-05 23:43:40 +00:00
|
|
|
hash64 := base64.StdEncoding.EncodeToString(cc.CredentialedHash)
|
|
|
|
if len(hash64) > 6 {
|
|
|
|
hash64 = hash64[:6] + "..."
|
|
|
|
}
|
2020-03-21 17:22:15 +00:00
|
|
|
|
2020-04-12 17:59:23 +00:00
|
|
|
credIDs := strings.Join(common.credIDs(), ", ")
|
|
|
|
return fmt.Sprintf("Credential of hash %s by %s", hash64, credIDs), nil
|
2020-03-05 23:43:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 19:26:32 +00:00
|
|
|
// ExpectedHash implements the method for the CommitInterface.
|
|
|
|
func (cc CommitCredential) ExpectedHash(changes []ChangedFile) ([]byte, error) {
|
|
|
|
if len(changes) > 0 {
|
|
|
|
return nil, errors.New("CommitCredential cannot have any changed files")
|
|
|
|
}
|
2020-03-05 23:43:40 +00:00
|
|
|
return cc.CredentialedHash, nil
|
|
|
|
}
|
|
|
|
|
2020-04-18 19:26:32 +00:00
|
|
|
// StoredHash implements the method for the CommitInterface.
|
|
|
|
func (cc CommitCredential) StoredHash() []byte {
|
2020-03-05 23:43:40 +00:00
|
|
|
return cc.CredentialedHash
|
|
|
|
}
|