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"` // 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"` } 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 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) { info, err := proj.changeRangeInfo(commits) if err != nil { return PayloadUnion{}, err } if descr == "" { descr = info.changeDescription } fingerprint, err := info.changeFingerprint(descr) if err != nil { return PayloadUnion{}, err } payCred, err := proj.NewPayloadCredential(fingerprint) if err != nil { return PayloadUnion{}, err } payCred.Credential.ChangeDescription = descr 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 { return "Credential of " + common.Fingerprint.String() } // 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 }