refactor commit interface to pass more information around

---
type: change
message: refactor commit interface to pass more information around
change_hash: AIZfiSAG5eOrh4UUXaAaHLpEtmhgLwhKj5GrReujp/dO
credentials:
- type: pgp_signature
  pub_key_id: 95C46FA6A41148AC
  body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl52TUIACgkQlcRvpqQRSKwOOA/+IGXZF6fbhmRlnf1o11lF3eXXh4196GdCLaFsnwFj2cbPyv9H6hmOPQGGGqriRVZUIwAn5vpvHH3yxwi+3wSCrxVaOUUiwaN9NuWfc/4Qyhd+qMnjjrb+5WtCeCGO7LwSsK/YbFlKX9Bdvigen1AvyAY9e2jAr8BaRT4qeM+/OV8S+aQZIvO3nec2kzrwHul0aDxK8uRmmmLGZf5PPozVZgPkcl79f2dL6rG9wunCb6PSHnPXgvKpyxyzw9Rr2J2f/gi/Dfudmmux2eb8gfPz29pTR0jVBnVOb1p8HPZ+2+4Napcg+SW8cP0UlIa4pZ8X/Oqsy1DEcIQMTpj9BvjQTUa5D7wiBI0NY9imOuBPXIwFkU9SHca7rFdG7az6mmmMoU07CPQuULKglif7TZLXCXEz06rWJIzn/I3ZfWSXBIFIqne4BMsTIYiHi2EAn4eOxJnCsIXTW7lQ/b9xZjTGR5C6uBc9ChniMDIGm+PAAWF0Bdcls+Hj5wx3CdIBDCusaoCe2gFvX9oIKKbArQdKD5XTnQ8IO6X8lYTE0SWuhb9Szvoszxqn2qAFyKJ72O/X/O4opZxN3ZWDdyvHP/yfV6tQXqmBjM5XnEnKWcd3aqSg2yogoejW6B0lWqjnvG/yvZrZVvSKQWykCEaqcnYupjgpQuT3Mgo5odFUc/nn7kc=
  account: mediocregopher
main
mediocregopher 4 years ago
parent 1f422511d5
commit b74186446e
  1. 37
      commit.go
  2. 2
      commit_change.go
  3. 7
      commit_credential.go

@ -10,6 +10,7 @@ import (
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt" "fmt"
"sort"
"time" "time"
"gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4"
@ -22,7 +23,9 @@ import (
// different commit types. // different commit types.
type CommitInterface interface { type CommitInterface interface {
// MessageHead returns the head of the commit message (i.e. the first line). // MessageHead returns the head of the commit message (i.e. the first line).
MessageHead() (string, error) // The CommitCommon of the outer Commit is passed in for added context, if
// necessary.
MessageHead(CommitCommon) (string, error)
// Hash returns the raw hash which Signifiers can sign to accredit this // Hash returns the raw hash which Signifiers can sign to accredit this
// commit. The tree objects given describe the filesystem state of the // commit. The tree objects given describe the filesystem state of the
@ -36,15 +39,33 @@ type CommitInterface interface {
GetHash() []byte GetHash() []byte
} }
// CommitCommon describes the fields common to all Commit objects.
type CommitCommon struct {
// Credentials represent all created Credentials for this commit, and can be
// set on all Commit objects regardless of other fields being set.
Credentials []sigcred.Credential `yaml:"credentials"`
}
func (cc CommitCommon) credAccountIDs() []string {
m := map[string]struct{}{}
for _, cred := range cc.Credentials {
m[cred.AccountID] = struct{}{}
}
s := make([]string, 0, len(m))
for accountID := range m {
s = append(s, accountID)
}
sort.Strings(s)
return s
}
// Commit represents a single Commit which is being added to a branch. Only one // Commit represents a single Commit which is being added to a branch. Only one
// field should be set on a Commit, unless otherwise noted. // field should be set on a Commit, unless otherwise noted.
type Commit struct { type Commit struct {
Change *CommitChange `type:"change,default"` Change *CommitChange `type:"change,default"`
Credential *CommitCredential `type:"credential"` Credential *CommitCredential `type:"credential"`
// Credentials represent all created Credentials for this commit, and can be Common CommitCommon `yaml:",inline"`
// set on all Commit objects regardless of other fields being set.
Credentials []sigcred.Credential `yaml:"credentials"`
} }
// MarshalYAML implements the yaml.Marshaler interface. // MarshalYAML implements the yaml.Marshaler interface.
@ -84,7 +105,7 @@ func (c Commit) MarshalText() ([]byte, error) {
return nil, fmt.Errorf("could not cast Commit %+v to interface : %w", c, err) return nil, fmt.Errorf("could not cast Commit %+v to interface : %w", c, err)
} }
msgHead, err := commitInt.MessageHead() msgHead, err := commitInt.MessageHead(c.Common)
if err != nil { if err != nil {
return nil, fmt.Errorf("error constructing message head: %w", err) return nil, fmt.Errorf("error constructing message head: %w", err)
} }
@ -133,7 +154,7 @@ func (r *Repo) AccreditCommit(commit Commit, sigInt sigcred.SignifierInterface)
if err != nil { if err != nil {
return commit, fmt.Errorf("could not accredit change commit: %w", err) return commit, fmt.Errorf("could not accredit change commit: %w", err)
} }
commit.Credentials = append(commit.Credentials, cred) commit.Common.Credentials = append(commit.Common.Credentials, cred)
return commit, nil return commit, nil
} }
@ -241,7 +262,7 @@ func (r *Repo) assertAccessControls(
return accessctl.AssertCanCommit(acl, accessctl.CommitRequest{ return accessctl.AssertCanCommit(acl, accessctl.CommitRequest{
Type: commitType, Type: commitType,
Branch: branch.Short(), Branch: branch.Short(),
Credentials: commit.Credentials, Credentials: commit.Common.Credentials,
FilesChanged: pathsChanged, FilesChanged: pathsChanged,
}) })
} }
@ -318,7 +339,7 @@ func (r *Repo) verifyCommit(branch plumbing.ReferenceName, gitCommit GitCommit,
base64.StdEncoding.EncodeToString(changeHash)) base64.StdEncoding.EncodeToString(changeHash))
} }
for _, cred := range gitCommit.Commit.Credentials { for _, cred := range gitCommit.Commit.Common.Credentials {
sig, err := r.signifierForCredential(sigFS, cred) sig, err := r.signifierForCredential(sigFS, cred)
if err != nil { if err != nil {
return fmt.Errorf("finding signifier for credential %+v: %w", cred, err) return fmt.Errorf("finding signifier for credential %+v: %w", cred, err)

@ -45,7 +45,7 @@ func (r *Repo) NewCommitChange(msg string) (Commit, error) {
} }
// MessageHead implements the method for the CommitInterface interface. // MessageHead implements the method for the CommitInterface interface.
func (cc CommitChange) MessageHead() (string, error) { func (cc CommitChange) MessageHead(CommitCommon) (string, error) {
i := strings.Index(cc.Message, "\n") i := strings.Index(cc.Message, "\n")
if i > 0 { if i > 0 {
return cc.Message[:i], nil return cc.Message[:i], nil

@ -4,6 +4,7 @@ import (
"dehub/yamlutil" "dehub/yamlutil"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"strings"
"gopkg.in/src-d/go-git.v4/plumbing/object" "gopkg.in/src-d/go-git.v4/plumbing/object"
) )
@ -27,12 +28,14 @@ func (r *Repo) NewCommitCredential(hash []byte) (Commit, error) {
} }
// MessageHead implements the method for the CommitInterface interface. // MessageHead implements the method for the CommitInterface interface.
func (cc CommitCredential) MessageHead() (string, error) { func (cc CommitCredential) MessageHead(common CommitCommon) (string, error) {
hash64 := base64.StdEncoding.EncodeToString(cc.CredentialedHash) hash64 := base64.StdEncoding.EncodeToString(cc.CredentialedHash)
if len(hash64) > 6 { if len(hash64) > 6 {
hash64 = hash64[:6] + "..." hash64 = hash64[:6] + "..."
} }
return fmt.Sprintf("Credential of hash %s", hash64), nil
credAccounts := strings.Join(common.credAccountIDs(), ", ")
return fmt.Sprintf("Credential of hash %s by %s", hash64, credAccounts), nil
} }
// Hash implements the method for the CommitInterface. // Hash implements the method for the CommitInterface.

Loading…
Cancel
Save