GetServerInfo: Include configured contributor agreements

When contributor agreements are enabled, include the list of configured
contributor agreements in the `auth` section.

Add support to get the server info via the API, and use this in the
existing agreements acceptance test to verify that the agreements are
returned as expected.

Change-Id: Ie6c8d25a4cd1519e3a42eb4cb3e6aeea77e56651
This commit is contained in:
David Pursehouse 2016-08-22 20:08:52 +09:00
parent 0512b9fdcc
commit cc046735b5
6 changed files with 77 additions and 2 deletions

View File

@ -54,6 +54,14 @@ As result a link:#server-info[ServerInfo] entity is returned.
{ {
"auth": { "auth": {
"auth_type": "LDAP", "auth_type": "LDAP",
"use_contributor_agreements": true,
"contributor_agreements": [
{
"name": "Individual",
"description": "If you are going to be contributing code on your own, this is the one you want. You can sign this one online.",
"url": "static/cla_individual.html"
}
],
"editable_account_fields": [ "editable_account_fields": [
"FULL_NAME", "FULL_NAME",
"REGISTER_NEW_EMAIL" "REGISTER_NEW_EMAIL"
@ -1226,6 +1234,9 @@ configured on the server. Can be `OPENID`, `OPENID_SSO`, `OAUTH`,
|`use_contributor_agreements` |not set if `false`| |`use_contributor_agreements` |not set if `false`|
Whether link:config-gerrit.html#auth.contributorAgreements[contributor Whether link:config-gerrit.html#auth.contributorAgreements[contributor
agreements] are required. agreements] are required.
|`contributor_agreements` |not set if `use_contributor_agreements` is `false`|
List of contributor agreements as link:rest-api-accounts.html#contributor-agreement-info[
ContributorAgreementInfo] entities.
|`editable_account_fields` || |`editable_account_fields` ||
List of account fields that are editable. Possible values are List of account fields that are editable. Possible values are
`FULL_NAME`, `USER_NAME` and `REGISTER_NEW_EMAIL`. `FULL_NAME`, `USER_NAME` and `REGISTER_NEW_EMAIL`.

View File

@ -24,6 +24,7 @@ import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.common.data.PermissionRule; import com.google.gerrit.common.data.PermissionRule;
import com.google.gerrit.extensions.api.groups.GroupApi; import com.google.gerrit.extensions.api.groups.GroupApi;
import com.google.gerrit.extensions.common.AgreementInfo; import com.google.gerrit.extensions.common.AgreementInfo;
import com.google.gerrit.extensions.common.ServerInfo;
import com.google.gerrit.extensions.restapi.BadRequestException; import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException; import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException; import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
@ -75,6 +76,25 @@ public class AgreementsIT extends AbstractDaemonTest {
setApiUser(user); setApiUser(user);
} }
@Test
public void getAvailableAgreements() throws Exception {
ServerInfo info = gApi.config().server().getInfo();
if (isContributorAgreementsEnabled()) {
assertThat(info.auth.useContributorAgreements).isTrue();
assertThat(info.auth.contributorAgreements).hasSize(2);
AgreementInfo agreementInfo = info.auth.contributorAgreements.get(0);
assertThat(agreementInfo.name).isEqualTo(ca.getName());
assertThat(agreementInfo.autoVerifyGroup.name)
.isEqualTo(ca.getAutoVerify().getName());
agreementInfo = info.auth.contributorAgreements.get(1);
assertThat(agreementInfo.name).isEqualTo(ca2.getName());
assertThat(agreementInfo.autoVerifyGroup).isNull();
} else {
assertThat(info.auth.useContributorAgreements).isNull();
assertThat(info.auth.contributorAgreements).isNull();
}
}
@Test @Test
public void signNonExistingAgreement() throws Exception { public void signNonExistingAgreement() throws Exception {
assume().that(isContributorAgreementsEnabled()).isTrue(); assume().that(isContributorAgreementsEnabled()).isTrue();

View File

@ -16,6 +16,7 @@ package com.google.gerrit.extensions.api.config;
import com.google.gerrit.extensions.client.DiffPreferencesInfo; import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo; import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gerrit.extensions.common.ServerInfo;
import com.google.gerrit.extensions.restapi.NotImplementedException; import com.google.gerrit.extensions.restapi.NotImplementedException;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
@ -25,6 +26,8 @@ public interface Server {
*/ */
String getVersion() throws RestApiException; String getVersion() throws RestApiException;
ServerInfo getInfo() throws RestApiException;
GeneralPreferencesInfo getDefaultPreferences() throws RestApiException; GeneralPreferencesInfo getDefaultPreferences() throws RestApiException;
GeneralPreferencesInfo setDefaultPreferences(GeneralPreferencesInfo in) GeneralPreferencesInfo setDefaultPreferences(GeneralPreferencesInfo in)
throws RestApiException; throws RestApiException;
@ -42,6 +45,11 @@ public interface Server {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@Override
public ServerInfo getInfo() throws RestApiException {
throw new NotImplementedException();
}
@Override @Override
public GeneralPreferencesInfo getDefaultPreferences() public GeneralPreferencesInfo getDefaultPreferences()
throws RestApiException { throws RestApiException {

View File

@ -22,6 +22,7 @@ import java.util.List;
public class AuthInfo { public class AuthInfo {
public AuthType authType; public AuthType authType;
public Boolean useContributorAgreements; public Boolean useContributorAgreements;
public List<AgreementInfo> contributorAgreements;
public List<AccountFieldName> editableAccountFields; public List<AccountFieldName> editableAccountFields;
public String loginUrl; public String loginUrl;
public String loginText; public String loginText;

View File

@ -18,10 +18,12 @@ import com.google.gerrit.common.Version;
import com.google.gerrit.extensions.api.config.Server; import com.google.gerrit.extensions.api.config.Server;
import com.google.gerrit.extensions.client.DiffPreferencesInfo; import com.google.gerrit.extensions.client.DiffPreferencesInfo;
import com.google.gerrit.extensions.client.GeneralPreferencesInfo; import com.google.gerrit.extensions.client.GeneralPreferencesInfo;
import com.google.gerrit.extensions.common.ServerInfo;
import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.config.ConfigResource; import com.google.gerrit.server.config.ConfigResource;
import com.google.gerrit.server.config.GetDiffPreferences; import com.google.gerrit.server.config.GetDiffPreferences;
import com.google.gerrit.server.config.GetPreferences; import com.google.gerrit.server.config.GetPreferences;
import com.google.gerrit.server.config.GetServerInfo;
import com.google.gerrit.server.config.SetDiffPreferences; import com.google.gerrit.server.config.SetDiffPreferences;
import com.google.gerrit.server.config.SetPreferences; import com.google.gerrit.server.config.SetPreferences;
import com.google.inject.Inject; import com.google.inject.Inject;
@ -37,16 +39,19 @@ public class ServerImpl implements Server {
private final SetPreferences setPreferences; private final SetPreferences setPreferences;
private final GetDiffPreferences getDiffPreferences; private final GetDiffPreferences getDiffPreferences;
private final SetDiffPreferences setDiffPreferences; private final SetDiffPreferences setDiffPreferences;
private final GetServerInfo getServerInfo;
@Inject @Inject
ServerImpl(GetPreferences getPreferences, ServerImpl(GetPreferences getPreferences,
SetPreferences setPreferences, SetPreferences setPreferences,
GetDiffPreferences getDiffPreferences, GetDiffPreferences getDiffPreferences,
SetDiffPreferences setDiffPreferences) { SetDiffPreferences setDiffPreferences,
GetServerInfo getServerInfo) {
this.getPreferences = getPreferences; this.getPreferences = getPreferences;
this.setPreferences = setPreferences; this.setPreferences = setPreferences;
this.getDiffPreferences = getDiffPreferences; this.getDiffPreferences = getDiffPreferences;
this.setDiffPreferences = setDiffPreferences; this.setDiffPreferences = setDiffPreferences;
this.getServerInfo = getServerInfo;
} }
@Override @Override
@ -54,6 +59,15 @@ public class ServerImpl implements Server {
return Version.getVersion(); return Version.getVersion();
} }
@Override
public ServerInfo getInfo() throws RestApiException {
try {
return getServerInfo.apply(new ConfigResource());
} catch (IOException e) {
throw new RestApiException("Cannot get server info", e);
}
}
@Override @Override
public GeneralPreferencesInfo getDefaultPreferences() public GeneralPreferencesInfo getDefaultPreferences()
throws RestApiException { throws RestApiException {

View File

@ -20,6 +20,7 @@ import com.google.common.base.Optional;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gerrit.common.data.ContributorAgreement;
import com.google.gerrit.extensions.common.AuthInfo; import com.google.gerrit.extensions.common.AuthInfo;
import com.google.gerrit.extensions.common.ChangeConfigInfo; import com.google.gerrit.extensions.common.ChangeConfigInfo;
import com.google.gerrit.extensions.common.DownloadInfo; import com.google.gerrit.extensions.common.DownloadInfo;
@ -47,12 +48,14 @@ import com.google.gerrit.server.change.GetArchive;
import com.google.gerrit.server.change.Submit; import com.google.gerrit.server.change.Submit;
import com.google.gerrit.server.documentation.QueryDocumentationExecutor; import com.google.gerrit.server.documentation.QueryDocumentationExecutor;
import com.google.gerrit.server.notedb.NotesMigration; import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.server.project.ProjectCache;
import com.google.inject.Inject; import com.google.inject.Inject;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -77,6 +80,8 @@ public class GetServerInfo implements RestReadView<ConfigResource> {
private final boolean enableSignedPush; private final boolean enableSignedPush;
private final QueryDocumentationExecutor docSearcher; private final QueryDocumentationExecutor docSearcher;
private final NotesMigration migration; private final NotesMigration migration;
private final ProjectCache projectCache;
private final AgreementJson agreementJson;
@Inject @Inject
public GetServerInfo( public GetServerInfo(
@ -94,7 +99,9 @@ public class GetServerInfo implements RestReadView<ConfigResource> {
DynamicItem<AvatarProvider> avatar, DynamicItem<AvatarProvider> avatar,
@EnableSignedPush boolean enableSignedPush, @EnableSignedPush boolean enableSignedPush,
QueryDocumentationExecutor docSearcher, QueryDocumentationExecutor docSearcher,
NotesMigration migration) { NotesMigration migration,
ProjectCache projectCache,
AgreementJson agreementJson) {
this.config = config; this.config = config;
this.authConfig = authConfig; this.authConfig = authConfig;
this.realm = realm; this.realm = realm;
@ -110,6 +117,8 @@ public class GetServerInfo implements RestReadView<ConfigResource> {
this.enableSignedPush = enableSignedPush; this.enableSignedPush = enableSignedPush;
this.docSearcher = docSearcher; this.docSearcher = docSearcher;
this.migration = migration; this.migration = migration;
this.projectCache = projectCache;
this.agreementJson = agreementJson;
} }
@Override @Override
@ -142,6 +151,18 @@ public class GetServerInfo implements RestReadView<ConfigResource> {
info.switchAccountUrl = cfg.getSwitchAccountUrl(); info.switchAccountUrl = cfg.getSwitchAccountUrl();
info.isGitBasicAuth = toBoolean(cfg.isGitBasicAuth()); info.isGitBasicAuth = toBoolean(cfg.isGitBasicAuth());
if (info.useContributorAgreements != null) {
Collection<ContributorAgreement> agreements =
projectCache.getAllProjects().getConfig().getContributorAgreements();
if (!agreements.isEmpty()) {
info.contributorAgreements =
Lists.newArrayListWithCapacity(agreements.size());
for (ContributorAgreement agreement: agreements) {
info.contributorAgreements.add(agreementJson.format(agreement));
}
}
}
switch (info.authType) { switch (info.authType) {
case LDAP: case LDAP:
case LDAP_BIND: case LDAP_BIND: