2020-04-26 20:23:03 +00:00
|
|
|
package dehub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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"`
|
2020-05-14 04:19:04 +00:00
|
|
|
|
|
|
|
// ChangeDescription represents the description which has been credentialed.
|
|
|
|
// This field is only relevant if the Credential in the payload is for a
|
|
|
|
// change set.
|
|
|
|
ChangeDescription string `yaml:"change_description"`
|
2020-04-26 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-05-14 04:19:04 +00:00
|
|
|
// change fingerprint generated from the given description and all changes in
|
|
|
|
// the given range of Commits.
|
|
|
|
//
|
|
|
|
// If an empty description is given then the description of the last change
|
|
|
|
// payload in the range is used when generating the fingerprint.
|
|
|
|
func (proj *Project) NewPayloadCredentialFromChanges(descr string, commits []Commit) (PayloadUnion, error) {
|
2020-04-26 20:23:03 +00:00
|
|
|
info, err := proj.changeRangeInfo(commits)
|
|
|
|
if err != nil {
|
|
|
|
return PayloadUnion{}, err
|
|
|
|
}
|
|
|
|
|
2020-05-14 04:19:04 +00:00
|
|
|
if descr == "" {
|
|
|
|
descr = info.changeDescription
|
|
|
|
}
|
|
|
|
fingerprint, err := info.changeFingerprint(descr)
|
2020-04-26 20:23:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return PayloadUnion{}, err
|
|
|
|
}
|
|
|
|
|
2020-05-14 04:19:04 +00:00
|
|
|
payCred, err := proj.NewPayloadCredential(fingerprint)
|
|
|
|
if err != nil {
|
|
|
|
return PayloadUnion{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
payCred.Credential.ChangeDescription = descr
|
2020-04-26 20:23:03 +00:00
|
|
|
for _, commit := range info.changeCommits {
|
|
|
|
payCred.Credential.CommitHashes = append(
|
|
|
|
payCred.Credential.CommitHashes,
|
|
|
|
commit.Hash.String(),
|
|
|
|
)
|
|
|
|
}
|
2020-05-14 04:19:04 +00:00
|
|
|
|
2020-04-26 20:23:03 +00:00
|
|
|
return payCred, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageHead implements the method for the Payload interface.
|
2020-05-12 04:09:01 +00:00
|
|
|
func (payCred PayloadCredential) MessageHead(common PayloadCommon) string {
|
|
|
|
return "Credential of " + common.Fingerprint.String()
|
2020-04-26 20:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|