wip

Michael Muré created

Change summary

bridge/github/config.go |  6 ++++--
cache/identity_cache.go |  2 +-
identity/bare.go        | 37 ++++++-------------------------------
identity/bare_test.go   |  1 -
identity/common.go      |  2 +-
identity/identity.go    | 40 +++++++++++++++++-----------------------
identity/interface.go   |  3 ---
identity/version.go     | 42 ++++++++++++++++++++----------------------
8 files changed, 49 insertions(+), 84 deletions(-)

Detailed changes

bridge/github/config.go 🔗

@@ -149,7 +149,6 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
 		}
 
 		// tag the default user with the github login, if any
-		// if not,
 		user, err := repo.GetUserIdentity()
 		if err == identity.ErrNoIdentitySet {
 			return nil
@@ -158,7 +157,10 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
 			return err
 		}
 
-		repo.GetUserIdentity()
+		userLogin, ok := user.ImmutableMetadata()[metaKeyGithubLogin]
+		if !ok {
+			user.SetMetadata()
+		}
 
 	}(login)
 

cache/identity_cache.go 🔗

@@ -21,7 +21,7 @@ func (i *IdentityCache) notifyUpdated() error {
 	return i.repoCache.identityUpdated(i.Identity.Id())
 }
 
-func (i *IdentityCache) Mutate(f func(identity.VersionMutator) identity.VersionMutator) error {
+func (i *IdentityCache) Mutate(f func(identity.IdentityMutator) identity.IdentityMutator) error {
 	i.Identity.Mutate(f)
 	return i.notifyUpdated()
 }

identity/bare.go 🔗

@@ -25,7 +25,6 @@ type Bare struct {
 	id        entity.Id
 	name      string
 	email     string
-	login     string
 	avatarUrl string
 }
 
@@ -33,8 +32,8 @@ func NewBare(name string, email string) *Bare {
 	return &Bare{id: entity.UnsetId, name: name, email: email}
 }
 
-func NewBareFull(name string, email string, login string, avatarUrl string) *Bare {
-	return &Bare{id: entity.UnsetId, name: name, email: email, login: login, avatarUrl: avatarUrl}
+func NewBareFull(name string, email string, avatarUrl string) *Bare {
+	return &Bare{id: entity.UnsetId, name: name, email: email, avatarUrl: avatarUrl}
 }
 
 func deriveId(data []byte) entity.Id {
@@ -45,7 +44,7 @@ func deriveId(data []byte) entity.Id {
 type bareIdentityJSON struct {
 	Name      string `json:"name,omitempty"`
 	Email     string `json:"email,omitempty"`
-	Login     string `json:"login,omitempty"`
+	Login     string `json:"login,omitempty"` // Deprecated, only kept to have the same ID when reading an old value
 	AvatarUrl string `json:"avatar_url,omitempty"`
 }
 
@@ -53,7 +52,6 @@ func (i *Bare) MarshalJSON() ([]byte, error) {
 	return json.Marshal(bareIdentityJSON{
 		Name:      i.name,
 		Email:     i.email,
-		Login:     i.login,
 		AvatarUrl: i.avatarUrl,
 	})
 }
@@ -70,7 +68,6 @@ func (i *Bare) UnmarshalJSON(data []byte) error {
 
 	i.name = aux.Name
 	i.email = aux.Email
-	i.login = aux.Login
 	i.avatarUrl = aux.AvatarUrl
 
 	return nil
@@ -109,11 +106,6 @@ func (i *Bare) Email() string {
 	return i.email
 }
 
-// Login return the last version of the login
-func (i *Bare) Login() string {
-	return i.login
-}
-
 // AvatarUrl return the last version of the Avatar URL
 func (i *Bare) AvatarUrl() string {
 	return i.avatarUrl
@@ -132,22 +124,13 @@ func (i *Bare) ValidKeysAtTime(time lamport.Time) []Key {
 // DisplayName return a non-empty string to display, representing the
 // identity, based on the non-empty values.
 func (i *Bare) DisplayName() string {
-	switch {
-	case i.name == "" && i.login != "":
-		return i.login
-	case i.name != "" && i.login == "":
-		return i.name
-	case i.name != "" && i.login != "":
-		return fmt.Sprintf("%s (%s)", i.name, i.login)
-	}
-
-	panic("invalid person data")
+	return i.name
 }
 
 // Validate check if the Identity data is valid
 func (i *Bare) Validate() error {
-	if text.Empty(i.name) && text.Empty(i.login) {
-		return fmt.Errorf("either name or login should be set")
+	if text.Empty(i.name) {
+		return fmt.Errorf("name is not set")
 	}
 
 	if strings.Contains(i.name, "\n") {
@@ -158,14 +141,6 @@ func (i *Bare) Validate() error {
 		return fmt.Errorf("name is not fully printable")
 	}
 
-	if strings.Contains(i.login, "\n") {
-		return fmt.Errorf("login should be a single line")
-	}
-
-	if !text.Safe(i.login) {
-		return fmt.Errorf("login is not fully printable")
-	}
-
 	if strings.Contains(i.email, "\n") {
 		return fmt.Errorf("email should be a single line")
 	}

identity/bare_test.go 🔗

@@ -18,7 +18,6 @@ func TestBare_Id(t *testing.T) {
 
 func TestBareSerialize(t *testing.T) {
 	before := &Bare{
-		login:     "login",
 		email:     "email",
 		name:      "name",
 		avatarUrl: "avatar",

identity/common.go 🔗

@@ -37,7 +37,7 @@ func UnmarshalJSON(raw json.RawMessage) (Interface, error) {
 	b := &Bare{}
 
 	err = json.Unmarshal(raw, b)
-	if err == nil && (b.name != "" || b.login != "") {
+	if err == nil && b.name != "" {
 		return b, nil
 	}
 

identity/identity.go 🔗

@@ -56,14 +56,13 @@ func NewIdentity(name string, email string) *Identity {
 	}
 }
 
-func NewIdentityFull(name string, email string, login string, avatarUrl string) *Identity {
+func NewIdentityFull(name string, email string, avatarUrl string) *Identity {
 	return &Identity{
 		id: entity.UnsetId,
 		versions: []*Version{
 			{
 				name:      name,
 				email:     email,
-				login:     login,
 				avatarURL: avatarUrl,
 				nonce:     makeNonce(20),
 			},
@@ -272,12 +271,18 @@ func IsUserIdentitySet(repo repository.Repo) (bool, error) {
 	return len(configs) == 1, nil
 }
 
+type Mutator struct {
+	Name      string
+	Email     string
+	AvatarUrl string
+	Keys      []Key
+}
+
 // Mutate allow to create a new version of the Identity
-func (i *Identity) Mutate(f func(orig VersionMutator) VersionMutator) {
-	orig := VersionMutator{
+func (i *Identity) Mutate(f func(orig Mutator) Mutator) {
+	orig := Mutator{
 		Name:      i.Name(),
 		Email:     i.Email(),
-		Login:     i.Login(),
 		AvatarUrl: i.AvatarUrl(),
 		Keys:      i.Keys(),
 	}
@@ -288,7 +293,6 @@ func (i *Identity) Mutate(f func(orig VersionMutator) VersionMutator) {
 	i.versions = append(i.versions, &Version{
 		name:      mutated.Name,
 		email:     mutated.Email,
-		login:     mutated.Login,
 		avatarURL: mutated.AvatarUrl,
 		keys:      mutated.Keys,
 	})
@@ -497,11 +501,6 @@ func (i *Identity) Email() string {
 	return i.lastVersion().email
 }
 
-// Login return the last version of the login
-func (i *Identity) Login() string {
-	return i.lastVersion().login
-}
-
 // AvatarUrl return the last version of the Avatar URL
 func (i *Identity) AvatarUrl() string {
 	return i.lastVersion().avatarURL
@@ -530,16 +529,7 @@ func (i *Identity) ValidKeysAtTime(time lamport.Time) []Key {
 // DisplayName return a non-empty string to display, representing the
 // identity, based on the non-empty values.
 func (i *Identity) DisplayName() string {
-	switch {
-	case i.Name() == "" && i.Login() != "":
-		return i.Login()
-	case i.Name() != "" && i.Login() == "":
-		return i.Name()
-	case i.Name() != "" && i.Login() != "":
-		return fmt.Sprintf("%s (%s)", i.Name(), i.Login())
-	}
-
-	panic("invalid person data")
+	return i.Name()
 }
 
 // IsProtected return true if the chain of git commits started to be signed.
@@ -559,9 +549,13 @@ func (i *Identity) LastModification() timestamp.Timestamp {
 	return timestamp.Timestamp(i.lastVersion().unixTime)
 }
 
-// SetMetadata store arbitrary metadata along the last defined Version.
-// If the Version has been commit to git already, it won't be overwritten.
+// SetMetadata store arbitrary metadata along the last not-commit Version.
+// If the Version has been commit to git already, a new version is added and will need to be
+// commit.
 func (i *Identity) SetMetadata(key string, value string) {
+	if i.lastVersion().commitHash != "" {
+
+	}
 	i.lastVersion().SetMetadata(key, value)
 }
 

identity/interface.go 🔗

@@ -17,9 +17,6 @@ type Interface interface {
 	// Email return the last version of the email
 	Email() string
 
-	// Login return the last version of the login
-	Login() string
-
 	// AvatarUrl return the last version of the Avatar URL
 	AvatarUrl() string
 

identity/version.go 🔗

@@ -24,8 +24,7 @@ type Version struct {
 	unixTime int64
 
 	name      string
-	email     string // TODO: remove ? keep and use metadata for bridges ?
-	login     string // TODO: remove
+	email     string // as defined in git, not for bridges
 	avatarURL string
 
 	// The set of keys valid at that time, from this version onward, until they get removed
@@ -53,19 +52,28 @@ type VersionJSON struct {
 	UnixTime  int64             `json:"unix_time"`
 	Name      string            `json:"name,omitempty"`
 	Email     string            `json:"email,omitempty"`
-	Login     string            `json:"login,omitempty"`
 	AvatarUrl string            `json:"avatar_url,omitempty"`
 	Keys      []Key             `json:"pub_keys,omitempty"`
 	Nonce     []byte            `json:"nonce,omitempty"`
 	Metadata  map[string]string `json:"metadata,omitempty"`
 }
 
-type VersionMutator struct {
-	Name      string
-	Email     string
-	Login     string
-	AvatarUrl string
-	Keys      []Key
+// Make a deep copy
+func (v *Version) Clone() *Version {
+
+	clone := &Version{
+		name:      v.name,
+		email:     v.email,
+		avatarURL: v.avatarURL,
+		keys:      make([]Key, len(v.keys)),
+		metadata:  make(map[string]string),
+	}
+
+	for i, op := range opp.Operations {
+		clone.Operations[i] = op
+	}
+
+	return clone
 }
 
 func (v *Version) MarshalJSON() ([]byte, error) {
@@ -75,7 +83,6 @@ func (v *Version) MarshalJSON() ([]byte, error) {
 		UnixTime:      v.unixTime,
 		Name:          v.name,
 		Email:         v.email,
-		Login:         v.login,
 		AvatarUrl:     v.avatarURL,
 		Keys:          v.keys,
 		Nonce:         v.nonce,
@@ -98,7 +105,6 @@ func (v *Version) UnmarshalJSON(data []byte) error {
 	v.unixTime = aux.UnixTime
 	v.name = aux.Name
 	v.email = aux.Email
-	v.login = aux.Login
 	v.avatarURL = aux.AvatarUrl
 	v.keys = aux.Keys
 	v.nonce = aux.Nonce
@@ -116,8 +122,8 @@ func (v *Version) Validate() error {
 		return fmt.Errorf("lamport time not set")
 	}
 
-	if text.Empty(v.name) && text.Empty(v.login) {
-		return fmt.Errorf("either name or login should be set")
+	if text.Empty(v.name) {
+		return fmt.Errorf("name not set")
 	}
 
 	if strings.Contains(v.name, "\n") {
@@ -128,14 +134,6 @@ func (v *Version) Validate() error {
 		return fmt.Errorf("name is not fully printable")
 	}
 
-	if strings.Contains(v.login, "\n") {
-		return fmt.Errorf("login should be a single line")
-	}
-
-	if !text.Safe(v.login) {
-		return fmt.Errorf("login is not fully printable")
-	}
-
 	if strings.Contains(v.email, "\n") {
 		return fmt.Errorf("email should be a single line")
 	}
@@ -210,7 +208,7 @@ func (v *Version) GetMetadata(key string) (string, bool) {
 	return val, ok
 }
 
-// AllMetadata return all metadata for this Identity
+// AllMetadata return all metadata for this Version
 func (v *Version) AllMetadata() map[string]string {
 	return v.metadata
 }