Skip to content

Commit d9e7d35

Browse files
committed
Merge branch 'stable-3.8'
* stable-3.8: Add support for virtual host based wizard flow Change-Id: I260557af0d3bd4ee35d3373ae010f6026f72ed07
2 parents 9e45c9b + 18a22aa commit d9e7d35

File tree

6 files changed

+202
-19
lines changed

6 files changed

+202
-19
lines changed

github-plugin/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,13 @@ limitations under the License.
157157
<artifactId>velocity-engine-core</artifactId>
158158
<version>2.3</version>
159159
</dependency>
160+
<dependency>
161+
<groupId>junit</groupId>
162+
<artifactId>junit</artifactId>
163+
</dependency>
164+
<dependency>
165+
<groupId>com.google.truth</groupId>
166+
<artifactId>truth</artifactId>
167+
</dependency>
160168
</dependencies>
161169
</project>

github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/GitHubConfig.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@
1414
package com.googlesource.gerrit.plugins.github;
1515

1616
import com.google.common.base.MoreObjects;
17-
import com.google.common.collect.Maps;
17+
import com.google.common.collect.HashBasedTable;
18+
import com.google.common.collect.Table;
1819
import com.google.gerrit.entities.Account;
1920
import com.google.gerrit.httpd.CanonicalWebUrl;
20-
import com.google.gerrit.server.config.AllProjectsNameProvider;
21+
import com.google.gerrit.server.config.AllProjectsName;
2122
import com.google.gerrit.server.config.GerritServerConfig;
2223
import com.google.gerrit.server.config.SitePaths;
2324
import com.google.inject.Inject;
25+
import com.google.inject.Provider;
2426
import com.google.inject.Singleton;
2527
import com.googlesource.gerrit.plugins.github.oauth.GitHubOAuthConfig;
2628
import java.net.MalformedURLException;
2729
import java.nio.file.Path;
28-
import java.util.HashMap;
2930
import org.eclipse.jgit.lib.Config;
3031

3132
@Singleton
3233
public class GitHubConfig extends GitHubOAuthConfig {
3334

3435
private static final String CONF_WIZARD_FLOW = "wizardFlow";
35-
private HashMap<String, NextPage> wizardFromTo = Maps.newHashMap();
3636
private static final String FROM_TO_SEPARATOR = "=>";
3737
private static final String FROM_TO_REDIRECT_SEPARATOR = "R>";
3838
private static final String CONF_JOB_POOL_LIMIT = "jobPoolLimit";
@@ -45,6 +45,7 @@ public class GitHubConfig extends GitHubOAuthConfig {
4545
private static final String CONF_WEBHOOK_SECRET = "webhookSecret";
4646
private static final String CONF_WEBHOOK_USER = "webhookUser";
4747
private static final String CONF_IMPORT_ACCOUNT_ID = "importAccountId";
48+
private static final String DEFAULT_SERVER = "default";
4849

4950
public final Path gitDir;
5051
public final int jobPoolLimit;
@@ -58,6 +59,7 @@ public class GitHubConfig extends GitHubOAuthConfig {
5859
public final String webhookSecret;
5960
public final String webhookUser;
6061
public final Account.Id importAccountId;
62+
private final Table<String, String, NextPage> wizardFromTo = HashBasedTable.create();
6163

6264
public static class NextPage {
6365
public final String uri;
@@ -73,19 +75,15 @@ public NextPage(final String pageUri, final boolean redirect) {
7375
public GitHubConfig(
7476
@GerritServerConfig Config config,
7577
final SitePaths site,
76-
AllProjectsNameProvider allProjectsNameProvider,
78+
Provider<AllProjectsName> allProjectsNameProvider,
7779
CanonicalWebUrl canonicalWebUrl)
7880
throws MalformedURLException {
7981
super(config, canonicalWebUrl);
80-
String[] wizardFlows = config.getStringList(CONF_SECTION, null, CONF_WIZARD_FLOW);
81-
for (String fromTo : wizardFlows) {
82-
boolean redirect = fromTo.indexOf(FROM_TO_REDIRECT_SEPARATOR) > 0;
83-
int sepPos = getSepPos(fromTo, redirect);
84-
String fromPage = fromTo.substring(0, sepPos).trim();
85-
NextPage toPage =
86-
new NextPage(
87-
fromTo.substring(sepPos + getSeparator(redirect).length() + 1).trim(), redirect);
88-
wizardFromTo.put(fromPage, toPage);
82+
parseWizardFlow(config.getStringList(CONF_SECTION, null, CONF_WIZARD_FLOW), DEFAULT_SERVER);
83+
84+
// Virtual host specific sections
85+
for (String server : config.getSubsections(CONF_SECTION)) {
86+
parseWizardFlow(config.getStringList(CONF_SECTION, server, CONF_WIZARD_FLOW), server);
8987
}
9088

9189
jobPoolLimit = config.getInt(CONF_SECTION, CONF_JOB_POOL_LIMIT, 5);
@@ -107,21 +105,36 @@ public GitHubConfig(
107105
importAccountId = Account.id(config.getInt(CONF_SECTION, CONF_IMPORT_ACCOUNT_ID, 1000000));
108106
}
109107

110-
private String getSeparator(boolean redirect) {
108+
private void parseWizardFlow(String[] wizardFlows, String server) {
109+
for (String fromTo : wizardFlows) {
110+
boolean redirect = fromTo.indexOf(FROM_TO_REDIRECT_SEPARATOR) > 0;
111+
int sepPos = getSepPos(fromTo, redirect);
112+
String fromPage = fromTo.substring(0, sepPos).trim();
113+
NextPage toPage =
114+
new NextPage(
115+
fromTo.substring(sepPos + getSeparator(redirect).length() + 1).trim(), redirect);
116+
wizardFromTo.put(server, fromPage, toPage);
117+
}
118+
}
119+
120+
private static String getSeparator(boolean redirect) {
111121
String separator = redirect ? FROM_TO_REDIRECT_SEPARATOR : FROM_TO_SEPARATOR;
112122
return separator;
113123
}
114124

115-
private int getSepPos(String fromTo, boolean redirect) {
125+
private static int getSepPos(String fromTo, boolean redirect) {
116126
int sepPos = fromTo.indexOf(getSeparator(redirect));
117127
if (sepPos < 0) {
118128
throw new InvalidGitHubConfigException(fromTo);
119129
}
120130
return sepPos;
121131
}
122132

123-
public NextPage getNextPage(String sourcePage) {
124-
return wizardFromTo.get(sourcePage);
133+
public NextPage getNextPage(String serverName, String sourcePage) {
134+
if (!wizardFromTo.containsRow(serverName)) {
135+
return wizardFromTo.get(DEFAULT_SERVER, sourcePage);
136+
}
137+
return wizardFromTo.get(serverName, sourcePage);
125138
}
126139

127140
public String getBaseProject(boolean isPrivateProject) {

github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/wizard/VelocityControllerServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ private void redirectToNextStep(HttpServletRequest req, HttpServletResponse resp
125125
if (queryStringStart > 0) {
126126
sourcePage = sourcePage.substring(0, queryStringStart);
127127
}
128-
NextPage nextPage = githubConfig.getNextPage(sourcePage);
128+
NextPage nextPage = githubConfig.getNextPage(req.getServerName(), sourcePage);
129129
if (nextPage != null) {
130130
if (nextPage.redirect) {
131131
resp.sendRedirect(nextPageURL(sourcePath, nextPage));

github-plugin/src/main/resources/Documentation/config.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,40 @@ github.httpReadTimeout
9595
* h, hr, hour, hours
9696
Default value: 30 seconds
9797

98+
github.wizardFlow
99+
: Define the transition from one page to another during the initial
100+
user setup wizard flow. The format of the value is the following:
101+
`page => next page` or `page R> next page` for redirections.
102+
103+
The example below shows an initial wizard that guides through
104+
the import of repositories, pull-requests and then redirects
105+
to the Gerrit projects admin page.
106+
107+
**Example:**
108+
```
109+
wizardFlow = account.gh => repositories.html
110+
wizardFlow = repositories-next.gh => pullrequests.html
111+
wizardFlow = pullrequests-next.gh R> / #/admin/projects/
112+
```
113+
114+
github.<domain>.wizardFlow
115+
: Allow to customise the GitHub wizard flow for the domain `<domain>`.
116+
This setting is useful for multi-site setups where the GitHub
117+
import Wizard can be different between sites.
118+
119+
The example below shows an initial wizard that guides through
120+
the import of repositories for all sites, but redirects to
121+
the Eclipse ECA sign page for the `eclipse.gerrithub.io` site.
122+
123+
**Example:**
124+
```
125+
[github]
126+
wizardFlow = account.gh => repositories.html
127+
128+
[github "eclipse.gerrithub.io"]
129+
wizardFlow = account.gh R> eclipse-eca.html
130+
```
131+
98132
Key Configuration
99133
-------------
100134

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Copyright (C) 2023 The Android Open Source Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.googlesource.gerrit.plugins.github;
15+
16+
import static com.google.common.truth.Truth.assertThat;
17+
18+
import com.google.gerrit.server.config.AllProjectsName;
19+
import com.google.gerrit.server.config.SitePaths;
20+
import com.google.inject.Provider;
21+
import com.google.inject.util.Providers;
22+
import java.nio.file.Path;
23+
import org.eclipse.jgit.lib.Config;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class GitHubConfigTest {
28+
private static final Provider<AllProjectsName> ALL_PROJECTS_NAME_PROVIDER =
29+
Providers.of(new AllProjectsName("All-Projects"));
30+
public static final String TEST_DOMAIN = "anydomain.com";
31+
public static final String SOURCE_PAGE = "sourcePage";
32+
public static final String NEXT_PAGE = "nextPage";
33+
public static final String CUSTOM_NEXT_PAGE = "customNextPage";
34+
private SitePaths site;
35+
36+
@Before
37+
public void setup() throws Exception {
38+
site = new SitePaths(Path.of("/tmp"));
39+
}
40+
41+
@Test
42+
public void getNextPageDefault() throws Exception {
43+
GitHubConfig.NextPage nextPage =
44+
newGitHubConfig("wizardFlow = " + SOURCE_PAGE + " => " + NEXT_PAGE)
45+
.getNextPage(TEST_DOMAIN, SOURCE_PAGE);
46+
47+
assertThat(nextPage.redirect).isFalse();
48+
assertThat(nextPage.uri).isEqualTo(NEXT_PAGE);
49+
}
50+
51+
@Test
52+
public void getNextPageRedirectDefault() throws Exception {
53+
GitHubConfig.NextPage nextPage =
54+
newGitHubConfig("wizardFlow = " + SOURCE_PAGE + " R> " + NEXT_PAGE)
55+
.getNextPage(TEST_DOMAIN, SOURCE_PAGE);
56+
57+
assertThat(nextPage.redirect).isTrue();
58+
assertThat(nextPage.uri).isEqualTo(NEXT_PAGE);
59+
}
60+
61+
@Test
62+
public void getNextPageByDomain() throws Exception {
63+
GitHubConfig.NextPage nextPage =
64+
newGitHubConfig(
65+
"wizardFlow = "
66+
+ SOURCE_PAGE
67+
+ " => "
68+
+ NEXT_PAGE
69+
+ "\n"
70+
+ "[github \""
71+
+ TEST_DOMAIN
72+
+ "\"]\n"
73+
+ "wizardFlow = "
74+
+ SOURCE_PAGE
75+
+ " => "
76+
+ CUSTOM_NEXT_PAGE)
77+
.getNextPage(TEST_DOMAIN, SOURCE_PAGE);
78+
79+
assertThat(nextPage.redirect).isFalse();
80+
assertThat(nextPage.uri).isEqualTo(CUSTOM_NEXT_PAGE);
81+
}
82+
83+
@Test
84+
public void getNextPageRedirectByDomain() throws Exception {
85+
GitHubConfig.NextPage nextPage =
86+
newGitHubConfig(
87+
"wizardFlow = "
88+
+ SOURCE_PAGE
89+
+ " R> "
90+
+ CUSTOM_NEXT_PAGE
91+
+ "\n"
92+
+ "[github \""
93+
+ TEST_DOMAIN
94+
+ "\"]\n"
95+
+ "wizardFlow = \""
96+
+ SOURCE_PAGE
97+
+ "\" R> "
98+
+ CUSTOM_NEXT_PAGE)
99+
.getNextPage(TEST_DOMAIN, SOURCE_PAGE);
100+
101+
assertThat(nextPage.redirect).isTrue();
102+
assertThat(nextPage.uri).isEqualTo(CUSTOM_NEXT_PAGE);
103+
}
104+
105+
private GitHubConfig newGitHubConfig(String configText) throws Exception {
106+
Config gerritConfig = new Config();
107+
gerritConfig.fromText(
108+
"[auth]\n"
109+
+ "httpHeader = GITHUB\n"
110+
+ "type = HTTP\n"
111+
+ "[gerrit]\n"
112+
+ "basePath = /tmp\n"
113+
+ "[github-key \"default\"]\n"
114+
+ "current = true\n"
115+
+ "passwordDevice = /dev/zero\n"
116+
+ "[github]\n"
117+
+ "clientId = myclientid\n"
118+
+ "clientSecret = mysecret\n"
119+
+ configText);
120+
return new GitHubConfig(gerritConfig, site, ALL_PROJECTS_NAME_PROVIDER, null);
121+
}
122+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ limitations under the License.
310310
<version>4.13.2</version>
311311
<scope>test</scope>
312312
</dependency>
313+
<dependency>
314+
<groupId>com.google.truth</groupId>
315+
<artifactId>truth</artifactId>
316+
<version>1.1.4</version>
317+
<scope>test</scope>
318+
</dependency>
313319
</dependencies>
314320
</dependencyManagement>
315321
</project>

0 commit comments

Comments
 (0)