Merge "Add 'declare' to all API interfaces"
diff --git a/Documentation/rest-api-changes.txt b/Documentation/rest-api-changes.txt
index d3635b3..cf01be9 100644
--- a/Documentation/rest-api-changes.txt
+++ b/Documentation/rest-api-changes.txt
@@ -7058,9 +7058,6 @@
 |`web_links`   |optional|
 Links to the file in external sites as a list of
 link:rest-api-changes.html#web-link-info[WebLinkInfo] entries.
-|`edit_web_links`   |optional|
-Links to edit the file in external sites as a list of
-link:rest-api-changes.html#web-link-info[WebLinkInfo] entries.
 |==========================
 
 [[diff-info]]
@@ -7090,6 +7087,9 @@
 |`web_links`       |optional|
 Links to the file diff in external sites as a list of
 link:rest-api-changes.html#diff-web-link-info[DiffWebLinkInfo] entries.
+|`edit_web_links`   |optional|
+Links to edit the file in external sites as a list of
+link:rest-api-changes.html#web-link-info[WebLinkInfo] entries.
 |`binary`          |not set if `false`|Whether the file is binary.
 |==========================
 
diff --git a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
index 003df28..3e8cf3b 100644
--- a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
+++ b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
@@ -1268,6 +1268,7 @@
     assertThat(diff.diffHeader).isNotNull();
     assertThat(diff.intralineStatus).isNull();
     assertThat(diff.webLinks).isNull();
+    assertThat(diff.editWebLinks).isNull();
 
     assertThat(diff.metaA).isNull();
     assertThat(diff.metaB).isNotNull();
@@ -1284,7 +1285,6 @@
     assertThat(diff.metaB.lines).isEqualTo(expectedLines.size());
     assertThat(diff.metaB.name).isEqualTo(path);
     assertThat(diff.metaB.webLinks).isNull();
-    assertThat(diff.metaB.editWebLinks).isNull();
 
     assertThat(diff.content).hasSize(1);
     DiffInfo.ContentEntry contentEntry = diff.content.get(0);
diff --git a/java/com/google/gerrit/extensions/common/DiffInfo.java b/java/com/google/gerrit/extensions/common/DiffInfo.java
index 5a59613..5a9b82b 100644
--- a/java/com/google/gerrit/extensions/common/DiffInfo.java
+++ b/java/com/google/gerrit/extensions/common/DiffInfo.java
@@ -32,6 +32,8 @@
   public List<ContentEntry> content;
   // Links to the file diff in external sites
   public List<DiffWebLinkInfo> webLinks;
+  // Links to edit the file in external sites
+  public List<WebLinkInfo> editWebLinks;
   // Binary file
   public Boolean binary;
 
@@ -52,8 +54,6 @@
     public Integer lines;
     // Links to the file in external sites
     public List<WebLinkInfo> webLinks;
-    // Links to edit the file in external sites
-    public List<WebLinkInfo> editWebLinks;
   }
 
   public static final class ContentEntry {
diff --git a/java/com/google/gerrit/extensions/common/testing/DiffInfoSubject.java b/java/com/google/gerrit/extensions/common/testing/DiffInfoSubject.java
index e258134..b800d17 100644
--- a/java/com/google/gerrit/extensions/common/testing/DiffInfoSubject.java
+++ b/java/com/google/gerrit/extensions/common/testing/DiffInfoSubject.java
@@ -74,6 +74,11 @@
     return check("webLinks").that(diffInfo.webLinks);
   }
 
+  public IterableSubject editWebLinks() {
+    isNotNull();
+    return check("editWebLinks").that(diffInfo.editWebLinks);
+  }
+
   public BooleanSubject binary() {
     isNotNull();
     return check("binary").that(diffInfo.binary);
diff --git a/java/com/google/gerrit/extensions/common/testing/FileMetaSubject.java b/java/com/google/gerrit/extensions/common/testing/FileMetaSubject.java
index d0212f3..0953bfe 100644
--- a/java/com/google/gerrit/extensions/common/testing/FileMetaSubject.java
+++ b/java/com/google/gerrit/extensions/common/testing/FileMetaSubject.java
@@ -64,9 +64,4 @@
     isNotNull();
     return check("webLinks").that(fileMeta.webLinks);
   }
-
-  public IterableSubject editWebLinks() {
-    isNotNull();
-    return check("editWebLinks").that(fileMeta.editWebLinks);
-  }
 }
diff --git a/java/com/google/gerrit/server/diff/DiffInfoCreator.java b/java/com/google/gerrit/server/diff/DiffInfoCreator.java
index 53f0019..606e42b 100644
--- a/java/com/google/gerrit/server/diff/DiffInfoCreator.java
+++ b/java/com/google/gerrit/server/diff/DiffInfoCreator.java
@@ -73,6 +73,8 @@
 
     ImmutableList<DiffWebLinkInfo> links = webLinksProvider.getDiffLinks();
     result.webLinks = links.isEmpty() ? null : links;
+    ImmutableList<WebLinkInfo> editLinks = webLinksProvider.getEditWebLinks();
+    result.editWebLinks = editLinks.isEmpty() ? null : editLinks;
 
     if (ps.isBinary()) {
       result.binary = true;
@@ -158,8 +160,6 @@
     result.lines = fileInfo.content.getSize();
     ImmutableList<WebLinkInfo> fileLinks = webLinksProvider.getFileWebLinks(side.type());
     result.webLinks = fileLinks.isEmpty() ? null : fileLinks;
-    ImmutableList<WebLinkInfo> editLinks = webLinksProvider.getEditWebLinks(side.type());
-    result.editWebLinks = editLinks.isEmpty() ? null : editLinks;
     result.commitId = fileInfo.commitId;
     return Optional.of(result);
   }
diff --git a/java/com/google/gerrit/server/diff/DiffWebLinksProvider.java b/java/com/google/gerrit/server/diff/DiffWebLinksProvider.java
index d4c7f5b..2590ebc 100644
--- a/java/com/google/gerrit/server/diff/DiffWebLinksProvider.java
+++ b/java/com/google/gerrit/server/diff/DiffWebLinksProvider.java
@@ -24,9 +24,9 @@
   /** Returns links associated with the diff view */
   ImmutableList<DiffWebLinkInfo> getDiffLinks();
 
+  /** Returns edit links associated with the diff view */
+  ImmutableList<WebLinkInfo> getEditWebLinks();
+
   /** Returns file links associated with the diff side */
   ImmutableList<WebLinkInfo> getFileWebLinks(DiffSide.Type fileInfoType);
-
-  /** Returns edit links associated with the diff side */
-  ImmutableList<WebLinkInfo> getEditWebLinks(DiffSide.Type fileInfoType);
 }
diff --git a/java/com/google/gerrit/server/restapi/change/GetDiff.java b/java/com/google/gerrit/server/restapi/change/GetDiff.java
index d48d76a..2169d57 100644
--- a/java/com/google/gerrit/server/restapi/change/GetDiff.java
+++ b/java/com/google/gerrit/server/restapi/change/GetDiff.java
@@ -226,10 +226,8 @@
     }
 
     @Override
-    public ImmutableList<WebLinkInfo> getEditWebLinks(DiffSide.Type type) {
-      String rev = getSideRev(type);
-      DiffSide side = getDiffSide(type);
-      return webLinks.getEditLinks(projectName.get(), rev, side.fileName());
+    public ImmutableList<WebLinkInfo> getEditWebLinks() {
+      return webLinks.getEditLinks(projectName.get(), revB, sideB.fileName());
     }
 
     @Override
diff --git a/java/com/google/gerrit/server/restapi/change/GetFixPreview.java b/java/com/google/gerrit/server/restapi/change/GetFixPreview.java
index 5191fc8..99c8a0a 100644
--- a/java/com/google/gerrit/server/restapi/change/GetFixPreview.java
+++ b/java/com/google/gerrit/server/restapi/change/GetFixPreview.java
@@ -137,12 +137,12 @@
     }
 
     @Override
-    public ImmutableList<WebLinkInfo> getFileWebLinks(Type fileInfoType) {
+    public ImmutableList<WebLinkInfo> getEditWebLinks() {
       return ImmutableList.of();
     }
 
     @Override
-    public ImmutableList<WebLinkInfo> getEditWebLinks(Type fileInfoType) {
+    public ImmutableList<WebLinkInfo> getFileWebLinks(Type fileInfoType) {
       return ImmutableList.of();
     }
   }
diff --git a/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java b/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java
index 37b4a1c..9bdc420 100644
--- a/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java
+++ b/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java
@@ -160,9 +160,8 @@
               .revision(result.getCommit().name())
               .file(fileName)
               .diff();
-      assertThat(info.metaB.editWebLinks).hasSize(1);
-      assertThat(info.metaB.editWebLinks.get(0).url)
-          .isEqualTo("http://edit/" + project + "/" + fileName);
+      assertThat(info.editWebLinks).hasSize(1);
+      assertThat(info.editWebLinks.get(0).url).isEqualTo("http://edit/" + project + "/" + fileName);
     }
   }
 
diff --git a/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog.ts b/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog.ts
index 39a5d54..738abc0 100644
--- a/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog.ts
+++ b/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog.ts
@@ -30,7 +30,6 @@
   $: {
     name: HTMLInputElement;
     username: HTMLInputElement;
-    email: HTMLSelectElement;
   };
 }
 
@@ -73,11 +72,17 @@
   _serverConfig?: ServerInfo;
 
   @property({
-    computed: '_computeUsernameMutable(_serverConfig,_account.username)',
+    computed: '_computeUsernameMutable(_serverConfig, _account.username)',
     type: Boolean,
   })
   _usernameMutable = false;
 
+  @property({type: Boolean})
+  _hasUsernameChange?: boolean;
+
+  @property({type: String, observer: '_usernameChanged'})
+  _username?: string;
+
   private readonly restApiService = appContext.restApiService;
 
   /** @override */
@@ -86,26 +91,17 @@
     this._ensureAttribute('role', 'dialog');
   }
 
-  _computeUsernameMutable(config?: ServerInfo, username?: string) {
-    // Polymer 2: check for undefined
-    // username is not being checked for undefined as we want to avoid
-    // setting it null explicitly to trigger the computation
-    if (config === undefined) {
-      return false;
-    }
-
-    return (
-      config.auth.editable_account_fields.includes(
-        EditableAccountField.USER_NAME
-      ) && !username
-    );
-  }
-
   loadData() {
     this._loading = true;
 
     const loadAccount = this.restApiService.getAccount().then(account => {
-      this._account = {...this._account, ...account};
+      if (!account) return;
+      this._hasUsernameChange = false;
+      // Provide predefined value for username to trigger computation of
+      // username mutability.
+      account.username = account.username || '';
+      this._account = account;
+      this._username = account.username;
     });
 
     const loadConfig = this.restApiService.getConfig().then(config => {
@@ -117,17 +113,37 @@
     });
   }
 
+  _usernameChanged() {
+    if (this._loading || !this._account) {
+      return;
+    }
+    this._hasUsernameChange =
+      (this._account.username || '') !== (this._username || '');
+  }
+
+  _computeUsernameMutable(config?: ServerInfo, username?: string) {
+    // Polymer 2: check for undefined
+    if (config === undefined) {
+      return false;
+    }
+
+    // Username may not be changed once it is set.
+    return (
+      config.auth.editable_account_fields.includes(
+        EditableAccountField.USER_NAME
+      ) && !username
+    );
+  }
+
   _save() {
     this._saving = true;
-    const promises = [
-      this.restApiService.setAccountName(this.$.name.value),
-      this.restApiService.setPreferredAccountEmail(this.$.email.value || ''),
-    ];
 
-    if (this._usernameMutable) {
-      promises.push(
-        this.restApiService.setAccountUsername(this.$.username.value)
-      );
+    const promises = [this.restApiService.setAccountName(this.$.name.value)];
+
+    // Note that we are intentionally not acting on this._username being the
+    // empty string (which is falsy).
+    if (this._hasUsernameChange && this._usernameMutable && this._username) {
+      promises.push(this.restApiService.setAccountUsername(this._username));
     }
 
     return Promise.all(promises).then(() => {
@@ -151,12 +167,8 @@
     fireEvent(this, 'close');
   }
 
-  _computeSaveDisabled(name?: string, email?: string, saving?: boolean) {
-    return !name || !email || saving;
-  }
-
-  _computeUsernameClass(usernameMutable: boolean) {
-    return usernameMutable ? '' : 'hide';
+  _computeSaveDisabled(name?: string, username?: string, saving?: boolean) {
+    return saving || (!name && !username);
   }
 
   @observe('_loading')
diff --git a/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_html.ts b/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_html.ts
index a1d6a5c..0e31bc9 100644
--- a/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_html.ts
+++ b/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_html.ts
@@ -59,9 +59,6 @@
     input {
       width: 20em;
     }
-    section.hide {
-      display: none;
-    }
   </style>
   <div class="container gr-form-styles">
     <header>Please confirm your contact information</header>
@@ -75,35 +72,31 @@
       </p>
       <hr />
       <section>
-        <div class="title">Full Name</div>
-        <iron-input bind-value="{{_account.name}}">
-          <input
-            is="iron-input"
-            id="name"
-            bind-value="{{_account.name}}"
-            disabled="[[_saving]]"
-          />
-        </iron-input>
-      </section>
-      <section class$="[[_computeUsernameClass(_usernameMutable)]]">
-        <div class="title">Username</div>
-        <iron-input bind-value="{{_account.username}}">
-          <input
-            is="iron-input"
-            id="username"
-            bind-value="{{_account.username}}"
-            disabled="[[_saving]]"
-          />
-        </iron-input>
+        <span class="title">Full Name</span>
+        <span class="value">
+          <iron-input bind-value="{{_account.name}}">
+            <input
+              is="iron-input"
+              id="name"
+              bind-value="{{_account.name}}"
+              disabled="[[_saving]]"
+            />
+          </iron-input>
+        </span>
       </section>
       <section>
-        <div class="title">Preferred Email</div>
-        <select id="email" disabled="[[_saving]]">
-          <option value="[[_account.email]]">[[_account.email]]</option>
-          <template is="dom-repeat" items="[[_account.secondary_emails]]">
-            <option value="[[item]]">[[item]]</option>
-          </template>
-        </select>
+        <span class="title">Username</span>
+        <span hidden$="[[_usernameMutable]]" class="value">[[_username]]</span>
+        <span hidden$="[[!_usernameMutable]]" class="value">
+          <iron-input bind-value="{{_username}}">
+            <input
+              is="iron-input"
+              id="username"
+              bind-value="{{_username}}"
+              disabled="[[_saving]]"
+            />
+          </iron-input>
+        </span>
       </section>
       <hr />
       <p>
@@ -123,7 +116,7 @@
         id="saveButton"
         primary=""
         link=""
-        disabled="[[_computeSaveDisabled(_account.name, _account.email, _saving)]]"
+        disabled="[[_computeSaveDisabled(_account.name, _username, _saving)]]"
         on-click="_handleSave"
         >Save</gr-button
       >
diff --git a/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_test.ts b/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_test.ts
index ccb7404..22f21e1 100644
--- a/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_test.ts
+++ b/polygerrit-ui/app/elements/settings/gr-registration-dialog/gr-registration-dialog_test.ts
@@ -17,11 +17,7 @@
 import '../../../test/common-test-setup-karma';
 import {GrRegistrationDialog} from './gr-registration-dialog';
 import {queryAndAssert, stubRestApi} from '../../../test/test-utils';
-import {
-  AccountDetailInfo,
-  EmailAddress,
-  Timestamp,
-} from '../../../types/common';
+import {AccountDetailInfo, Timestamp} from '../../../types/common';
 import * as MockInteractions from '@polymer/iron-test-helpers/mock-interactions';
 import {AuthType, EditableAccountField} from '../../../constants/constants';
 import {createServerInfo} from '../../../test/test-data-generators';
@@ -39,8 +35,6 @@
 
     account = {
       name: 'name',
-      email: 'email' as EmailAddress,
-      secondary_emails: ['email2', 'email3'],
       registered_on: '2018-02-08 18:49:18.000000000' as Timestamp,
     };
 
@@ -53,10 +47,6 @@
       account.username = username;
       return Promise.resolve();
     });
-    stubRestApi('setPreferredAccountEmail').callsFake(email => {
-      account.email = email as EmailAddress;
-      return Promise.resolve();
-    });
     stubRestApi('getConfig').returns(
       Promise.resolve({
         ...createServerInfo(),
@@ -116,41 +106,32 @@
   test('saves account details', done => {
     flush(() => {
       element.$.name.value = 'new name';
-      element.$.username.value = 'new username';
-      element.$.email.value = 'email3';
+
+      element.set('_account.username', '');
+      element._hasUsernameChange = false;
+      assert.isTrue(element._usernameMutable);
+
+      element.set('_username', 'new username');
 
       // Nothing should be committed yet.
       assert.equal(account.name, 'name');
       assert.isNotOk(account.username);
-      assert.equal(account.email, 'email' as EmailAddress);
 
       // Save and verify new values are committed.
       save()
         .then(() => {
           assert.equal(account.name, 'new name');
           assert.equal(account.username, 'new username');
-          assert.equal(account.email, 'email3' as EmailAddress);
         })
         .then(done);
     });
   });
 
-  test('email select properly populated', done => {
-    element._account = {
-      email: 'foo' as EmailAddress,
-      secondary_emails: ['bar', 'baz'],
-    };
-    flush(() => {
-      assert.equal(element.$.email.value, 'foo');
-      done();
-    });
-  });
-
   test('save btn disabled', () => {
     const compute = element._computeSaveDisabled;
     assert.isTrue(compute('', '', false));
-    assert.isTrue(compute('', 'test', false));
-    assert.isTrue(compute('test', '', false));
+    assert.isFalse(compute('', 'test', false));
+    assert.isFalse(compute('test', '', false));
     assert.isTrue(compute('test', 'test', true));
     assert.isFalse(compute('test', 'test', false));
   });
diff --git a/polygerrit-ui/app/types/diff.ts b/polygerrit-ui/app/types/diff.ts
index d30917a0..35c5726 100644
--- a/polygerrit-ui/app/types/diff.ts
+++ b/polygerrit-ui/app/types/diff.ts
@@ -51,6 +51,12 @@
    * entries.
    */
   web_links?: DiffWebLinkInfo[];
+
+  /**
+   * Links to edit the file in external sites as a list of WebLinkInfo
+   * entries.
+   */
+  edit_web_links?: WebLinkInfo[];
 }
 
 /**
@@ -74,11 +80,6 @@
 export interface DiffFileMetaInfo extends DiffFileMetaInfoApi {
   /** Links to the file in external sites as a list of WebLinkInfo entries. */
   web_links?: WebLinkInfo[];
-  /**
-   * Links to edit the file in external sites as a list of WebLinkInfo
-   * entries.
-   */
-  edit_web_links?: WebLinkInfo[];
 }
 
 /**