Skip to content

Commit 8f13b88

Browse files
qmuntalwing328
authored andcommitted
Support custom git repository (#3757)
* add gitHost param to GeneratorSettings and related * parameterize gitHost in READMEs * parameterize gitHost in go.mod * parameterize gitHost in git_push * update petstore samples * run ./bin/utils/export_docs_generators.sh * run meta-codehen.sh * Revert "run meta-codehen.sh" This reverts commit d6d579f. * Revert "run ./bin/utils/export_docs_generators.sh" This reverts commit 1b81538. * Revert "update petstore samples" This reverts commit f513add. * run ensure-up-to-date
1 parent 3be1196 commit 8f13b88

File tree

148 files changed

+1448
-618
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+1448
-618
lines changed

modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ public class Generate implements Runnable {
178178
@Option(name = {"--library"}, title = "library", description = CodegenConstants.LIBRARY_DESC)
179179
private String library;
180180

181+
@Option(name = {"--git-host"}, title = "git host",
182+
description = CodegenConstants.GIT_HOST_DESC)
183+
private String gitHost;
184+
181185
@Option(name = {"--git-user-id"}, title = "git user id",
182186
description = CodegenConstants.GIT_USER_ID_DESC)
183187
private String gitUserId;
@@ -343,6 +347,10 @@ public void run() {
343347
configurator.setLibrary(library);
344348
}
345349

350+
if (isNotEmpty(gitHost)) {
351+
configurator.setGitHost(gitHost);
352+
}
353+
346354
if (isNotEmpty(gitUserId)) {
347355
configurator.setGitUserId(gitUserId);
348356
}

modules/openapi-generator-core/src/main/java/org/openapitools/codegen/config/GeneratorSettings.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
public final class GeneratorSettings implements Serializable {
3131

3232
private static final Logger LOGGER = LoggerFactory.getLogger(GeneratorSettings.class);
33+
private static String DEFAULT_GIT_HOST = "github.com";
3334
private static String DEFAULT_GIT_USER_ID = "GIT_USER_ID";
3435
private static String DEFAULT_GIT_REPO_ID = "GIT_REPO_ID";
3536
private static String DEFAULT_RELEASE_NOTE = "Minor update";
@@ -54,6 +55,7 @@ public final class GeneratorSettings implements Serializable {
5455
private ImmutableMap<String, String> reservedWordMappings;
5556
private ImmutableMap<String, String> serverVariables;
5657

58+
private String gitHost;
5759
private String gitUserId;
5860
private String gitRepoId;
5961
private String releaseNote;
@@ -256,6 +258,17 @@ public Map<String, String> getServerVariables() {
256258
return serverVariables;
257259
}
258260

261+
/**
262+
* Gets git host. e.g. <strong>gitlab.com</strong>.
263+
* <p>
264+
* Generally used by git_push.sh in generated sources which support it.
265+
* This value may also be used by templates in maven style references, READMEs, or other documentation.
266+
*
267+
* @return the git host
268+
*/
269+
public String getGitHost() {
270+
return gitHost;
271+
}
259272

260273
/**
261274
* Gets git user id. e.g. <strong>openapitools</strong>.
@@ -324,6 +337,7 @@ private GeneratorSettings(Builder builder) {
324337
languageSpecificPrimitives = ImmutableSet.copyOf(builder.languageSpecificPrimitives);
325338
reservedWordMappings = ImmutableMap.copyOf(builder.reservedWordMappings);
326339
serverVariables = ImmutableMap.copyOf(builder.serverVariables);
340+
gitHost = builder.gitHost;
327341
gitUserId = builder.gitUserId;
328342
gitRepoId = builder.gitRepoId;
329343
releaseNote = builder.releaseNote;
@@ -358,6 +372,9 @@ private GeneratorSettings(Builder builder) {
358372
if (isNotEmpty(modelNameSuffix)) {
359373
additional.put("modelNameSuffix", modelNameSuffix);
360374
}
375+
if (isNotEmpty(gitHost)) {
376+
additional.put("gitHost", gitHost);
377+
}
361378
if (isNotEmpty(gitUserId)) {
362379
additional.put("gitUserId", gitUserId);
363380
}
@@ -390,6 +407,7 @@ public GeneratorSettings() {
390407
}
391408

392409
private void setDefaults() {
410+
gitHost = DEFAULT_GIT_HOST;
393411
gitUserId = DEFAULT_GIT_USER_ID;
394412
gitRepoId = DEFAULT_GIT_REPO_ID;
395413
releaseNote = DEFAULT_RELEASE_NOTE;
@@ -442,6 +460,7 @@ public static Builder newBuilder(GeneratorSettings copy) {
442460
if (copy.getServerVariables() != null) {
443461
builder.serverVariables.putAll(copy.getServerVariables());
444462
}
463+
builder.gitHost = copy.getGitHost();
445464
builder.gitUserId = copy.getGitUserId();
446465
builder.gitRepoId = copy.getGitRepoId();
447466
builder.releaseNote = copy.getReleaseNote();
@@ -473,6 +492,7 @@ public static final class Builder {
473492
private Set<String> languageSpecificPrimitives;
474493
private Map<String, String> reservedWordMappings;
475494
private Map<String, String> serverVariables;
495+
private String gitHost;
476496
private String gitUserId;
477497
private String gitRepoId;
478498
private String releaseNote;
@@ -490,6 +510,7 @@ public Builder() {
490510
reservedWordMappings = new HashMap<>();
491511
serverVariables = new HashMap<>();
492512

513+
gitHost = DEFAULT_GIT_HOST;
493514
gitUserId = DEFAULT_GIT_USER_ID;
494515
gitRepoId = DEFAULT_GIT_REPO_ID;
495516
releaseNote = DEFAULT_RELEASE_NOTE;
@@ -783,6 +804,17 @@ public Builder withServerVariable(String key, String value) {
783804
return this;
784805
}
785806

807+
/**
808+
* Sets the {@code gitHost} and returns a reference to this Builder so that the methods can be chained together.
809+
*
810+
* @param gitHost the {@code gitHost} to set
811+
* @return a reference to this Builder
812+
*/
813+
public Builder withGitHost(String gitHost) {
814+
this.gitHost = gitHost;
815+
return this;
816+
}
817+
786818
/**
787819
* Sets the {@code gitUserId} and returns a reference to this Builder so that the methods can be chained together.
788820
*
@@ -860,6 +892,7 @@ public String toString() {
860892
", importMappings=" + importMappings +
861893
", languageSpecificPrimitives=" + languageSpecificPrimitives +
862894
", reservedWordMappings=" + reservedWordMappings +
895+
", gitHost='" + gitHost + '\'' +
863896
", gitUserId='" + gitUserId + '\'' +
864897
", gitRepoId='" + gitRepoId + '\'' +
865898
", releaseNote='" + releaseNote + '\'' +
@@ -889,6 +922,7 @@ public boolean equals(Object o) {
889922
Objects.equals(getImportMappings(), that.getImportMappings()) &&
890923
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
891924
Objects.equals(getReservedWordMappings(), that.getReservedWordMappings()) &&
925+
Objects.equals(getGitHost(), that.getGitHost()) &&
892926
Objects.equals(getGitUserId(), that.getGitUserId()) &&
893927
Objects.equals(getGitRepoId(), that.getGitRepoId()) &&
894928
Objects.equals(getReleaseNote(), that.getReleaseNote()) &&
@@ -915,6 +949,7 @@ public int hashCode() {
915949
getImportMappings(),
916950
getLanguageSpecificPrimitives(),
917951
getReservedWordMappings(),
952+
getGitHost(),
918953
getGitUserId(),
919954
getGitRepoId(),
920955
getReleaseNote(),

modules/openapi-generator-gradle-plugin/README.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ apply plugin: 'org.openapi.generator'
202202
|None
203203
|Reference the library template (sub-template) of a generator.
204204

205+
|gitHost
206+
|String
207+
|github.com
208+
|Git user ID, e.g. gitlab.com.
209+
205210
|gitUserId
206211
|String
207212
|None

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
116116
id.set(generate.id)
117117
version.set(generate.version)
118118
library.set(generate.library)
119+
gitHost.set(generate.gitHost)
119120
gitUserId.set(generate.gitUserId)
120121
gitRepoId.set(generate.gitRepoId)
121122
releaseNote.set(generate.releaseNote)

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
160160
*/
161161
val library = project.objects.property<String?>()
162162

163+
/**
164+
* Git host, e.g. gitlab.com.
165+
*/
166+
val gitHost = project.objects.property<String?>()
167+
163168
/**
164169
* Git user ID, e.g. openapitools.
165170
*/

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ open class GenerateTask : DefaultTask() {
204204
@get:Internal
205205
val library = project.objects.property<String?>()
206206

207+
/**
208+
* Git host, e.g. gitlab.com.
209+
*/
210+
@get:Internal
211+
val gitHost = project.objects.property<String?>()
212+
207213
/**
208214
* Git user ID, e.g. openapitools.
209215
*/
@@ -510,6 +516,10 @@ open class GenerateTask : DefaultTask() {
510516
configurator.setLibrary(value)
511517
}
512518

519+
gitHost.ifNotEmpty { value ->
520+
configurator.setGitHost(value)
521+
}
522+
513523
gitUserId.ifNotEmpty { value ->
514524
configurator.setGitUserId(value)
515525
}

modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public class CodeGenMojo extends AbstractMojo {
9797
@Parameter(name = "inputSpec", property = "openapi.generator.maven.plugin.inputSpec", required = true)
9898
private String inputSpec;
9999

100+
/**
101+
* Git host, e.g. gitlab.com.
102+
*/
103+
@Parameter(name = "gitHost", property = "openapi.generator.maven.plugin.gitHost", required = false)
104+
private String gitHost;
105+
100106
/**
101107
* Git user ID, e.g. swagger-api.
102108
*/
@@ -456,6 +462,10 @@ public void execute() throws MojoExecutionException {
456462
configurator.setInputSpec(inputSpec);
457463
}
458464

465+
if (isNotEmpty(gitHost)) {
466+
configurator.setGitHost(gitHost);
467+
}
468+
459469
if (isNotEmpty(gitUserId)) {
460470
configurator.setGitUserId(gitUserId);
461471
}

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,10 @@ public interface CodegenConfig {
222222
*/
223223
String getLibrary();
224224

225+
void setGitHost(String gitHost);
226+
227+
String getGitHost();
228+
225229
void setGitUserId(String gitUserId);
226230

227231
String getGitUserId();

modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
212212
public static final String MODEL_NAME_SUFFIX = "modelNameSuffix";
213213
public static final String MODEL_NAME_SUFFIX_DESC = "Suffix that will be appended to all model names.";
214214

215+
public static final String GIT_HOST = "gitHost";
216+
public static final String GIT_HOST_DESC = "Git host, e.g. gitlab.com.";
217+
215218
public static final String GIT_USER_ID = "gitUserId";
216219
public static final String GIT_USER_ID_DESC = "Git user ID, e.g. openapitools.";
217220

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public class DefaultCodegen implements CodegenConfig {
111111
protected Boolean sortParamsByRequiredFlag = true;
112112
protected Boolean ensureUniqueParams = true;
113113
protected Boolean allowUnicodeIdentifiers = false;
114-
protected String gitUserId, gitRepoId, releaseNote;
114+
protected String gitHost, gitUserId, gitRepoId, releaseNote;
115115
protected String httpUserAgent;
116116
protected Boolean hideGenerationTimestamp = true;
117117
// How to encode special characters like $
@@ -3906,6 +3906,24 @@ public String getLibrary() {
39063906
return library;
39073907
}
39083908

3909+
/**
3910+
* Set Git host.
3911+
*
3912+
* @param gitHost Git host
3913+
*/
3914+
public void setGitHost(String gitHost) {
3915+
this.gitHost = gitHost;
3916+
}
3917+
3918+
/**
3919+
* Git host.
3920+
*
3921+
* @return Git host
3922+
*/
3923+
public String getGitHost() {
3924+
return gitHost;
3925+
}
3926+
39093927
/**
39103928
* Set Git user ID.
39113929
*

modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ public CodegenConfigurator setGitRepoId(String gitRepoId) {
222222
return this;
223223
}
224224

225+
public CodegenConfigurator setGitHost(String gitHost) {
226+
generatorSettingsBuilder.withGitHost(gitHost);
227+
return this;
228+
}
229+
225230
public CodegenConfigurator setGitUserId(String gitUserId) {
226231
generatorSettingsBuilder.withGitUserId(gitUserId);
227232
return this;

modules/openapi-generator/src/main/resources/Java/git_push.sh.mustache

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/bin/sh
22
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
33
#
4-
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update"
4+
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" "gitlab.com"
55

66
git_user_id=$1
77
git_repo_id=$2
88
release_note=$3
9+
git_host=$4
10+
11+
if [ "$git_host" = "" ]; then
12+
git_host="{{{gitHost}}}"
13+
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
14+
fi
915

1016
if [ "$git_user_id" = "" ]; then
1117
git_user_id="{{{gitUserId}}}"
@@ -28,7 +34,7 @@ git init
2834
# Adds the files in the local repository and stages them for commit.
2935
git add .
3036

31-
# Commits the tracked changes and prepares them to be pushed to a remote repository.
37+
# Commits the tracked changes and prepares them to be pushed to a remote repository.
3238
git commit -m "$release_note"
3339

3440
# Sets the new remote
@@ -37,16 +43,16 @@ if [ "$git_remote" = "" ]; then # git remote not defined
3743

3844
if [ "$GIT_TOKEN" = "" ]; then
3945
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
40-
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
46+
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
4147
else
42-
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
48+
git remote add origin https://${git_user_id}:${GIT_TOKEN}@${git_host}/${git_user_id}/${git_repo_id}.git
4349
fi
4450

4551
fi
4652

4753
git pull origin master
4854

4955
# Pushes (Forces) the changes in the local repository up to the remote repository
50-
echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git"
56+
echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
5157
git push origin master 2>&1 | grep -v 'To https'
5258

modules/openapi-generator/src/main/resources/Javascript/es6/git_push.sh.mustache

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#!/bin/sh
22
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
33
#
4-
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update"
4+
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" "gitlab.com"
55

66
git_user_id=$1
77
git_repo_id=$2
88
release_note=$3
9+
git_host=$4
10+
11+
if [ "$git_host" = "" ]; then
12+
git_host="{{{gitHost}}}"
13+
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
14+
fi
915

1016
if [ "$git_user_id" = "" ]; then
1117
git_user_id="{{{gitUserId}}}"
@@ -28,25 +34,25 @@ git init
2834
# Adds the files in the local repository and stages them for commit.
2935
git add .
3036

31-
# Commits the tracked changes and prepares them to be pushed to a remote repository.
37+
# Commits the tracked changes and prepares them to be pushed to a remote repository.
3238
git commit -m "$release_note"
3339

3440
# Sets the new remote
3541
git_remote=`git remote`
3642
if [ "$git_remote" = "" ]; then # git remote not defined
3743

3844
if [ "$GIT_TOKEN" = "" ]; then
39-
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the Git credential in your environment."
40-
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
45+
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
46+
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
4147
else
42-
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
48+
git remote add origin https://${git_user_id}:${GIT_TOKEN}@${git_host}/${git_user_id}/${git_repo_id}.git
4349
fi
4450

4551
fi
4652

4753
git pull origin master
4854

4955
# Pushes (Forces) the changes in the local repository up to the remote repository
50-
echo "Git pushing to https://github.com/${git_user_id}/${git_repo_id}.git"
56+
echo "Git pushing to https://${git_host}/${git_user_id}/${git_repo_id}.git"
5157
git push origin master 2>&1 | grep -v 'To https'
5258

0 commit comments

Comments
 (0)