You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
2.4 KiB
74 lines
2.4 KiB
4 years ago
|
package dehub
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// PayloadCredential describes the structure of a credential payload.
|
||
|
type PayloadCredential struct {
|
||
|
// 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 _ Payload = PayloadCredential{}
|
||
|
|
||
|
// NewPayloadCredential constructs and returns a PayloadUnion populated with a
|
||
|
// PayloadCredential for the given fingerprint. The Credentials of the returned
|
||
|
// PayloadUnion will _not_ be filled in.
|
||
|
func (proj *Project) NewPayloadCredential(fingerprint []byte) (PayloadUnion, error) {
|
||
|
return PayloadUnion{
|
||
|
Credential: &PayloadCredential{},
|
||
|
Common: PayloadCommon{Fingerprint: fingerprint},
|
||
|
}, nil
|
||
|
}
|
||
|
|
||
|
// NewPayloadCredentialFromChanges constructs and returns a PayloadUnion
|
||
|
// populated with a PayloadCredential. The fingerprint of the payload will be a
|
||
|
// change fingerprint encompassing all changes in the given range of Commits.
|
||
|
// The description of the last change payload in the range is used when
|
||
|
// generating the fingerprint.
|
||
|
func (proj *Project) NewPayloadCredentialFromChanges(commits []Commit) (PayloadUnion, error) {
|
||
|
info, err := proj.changeRangeInfo(commits)
|
||
|
if err != nil {
|
||
|
return PayloadUnion{}, err
|
||
|
}
|
||
|
|
||
|
payCred, err := proj.NewPayloadCredential(info.changeFingerprint)
|
||
|
if err != nil {
|
||
|
return PayloadUnion{}, err
|
||
|
}
|
||
|
|
||
|
for _, commit := range info.changeCommits {
|
||
|
payCred.Credential.CommitHashes = append(
|
||
|
payCred.Credential.CommitHashes,
|
||
|
commit.Hash.String(),
|
||
|
)
|
||
|
}
|
||
|
return payCred, nil
|
||
|
}
|
||
|
|
||
|
// MessageHead implements the method for the Payload interface.
|
||
|
func (payCred PayloadCredential) MessageHead(common PayloadCommon) (string, error) {
|
||
|
fingerprintStr := common.Fingerprint.String()
|
||
|
if len(fingerprintStr) > 9 {
|
||
|
fingerprintStr = fingerprintStr[:6] + "..."
|
||
|
}
|
||
|
credIDs := strings.Join(common.credIDs(), ", ")
|
||
|
fullMsgHead := fmt.Sprintf("Credential of hash %s by %s", fingerprintStr, credIDs)
|
||
|
return abbrevCommitMessage(fullMsgHead), nil
|
||
|
}
|
||
|
|
||
|
// Fingerprint implements the method for the Payload interface.
|
||
|
func (payCred PayloadCredential) Fingerprint(changes []ChangedFile) ([]byte, error) {
|
||
|
if len(changes) > 0 {
|
||
|
return nil, errors.New("PayloadCredential cannot have any changed files")
|
||
|
}
|
||
|
// a PayloadCredential can't compute its own fingerprint, it's stored in the
|
||
|
// common.
|
||
|
return nil, nil
|
||
|
}
|