Skip to content

Commit

Permalink
Support custom git repository (OpenAPITools#3757)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
qmuntal authored and hokamoto committed Sep 12, 2019
1 parent fc74ece commit 7b4a201
Show file tree
Hide file tree
Showing 148 changed files with 1,448 additions and 618 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ public class Generate implements Runnable {
@Option(name = {"--library"}, title = "library", description = CodegenConstants.LIBRARY_DESC)
private String library;

@Option(name = {"--git-host"}, title = "git host",
description = CodegenConstants.GIT_HOST_DESC)
private String gitHost;

@Option(name = {"--git-user-id"}, title = "git user id",
description = CodegenConstants.GIT_USER_ID_DESC)
private String gitUserId;
Expand Down Expand Up @@ -343,6 +347,10 @@ public void run() {
configurator.setLibrary(library);
}

if (isNotEmpty(gitHost)) {
configurator.setGitHost(gitHost);
}

if (isNotEmpty(gitUserId)) {
configurator.setGitUserId(gitUserId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
public final class GeneratorSettings implements Serializable {

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

private String gitHost;
private String gitUserId;
private String gitRepoId;
private String releaseNote;
Expand Down Expand Up @@ -256,6 +258,17 @@ public Map<String, String> getServerVariables() {
return serverVariables;
}

/**
* Gets git host. e.g. <strong>gitlab.com</strong>.
* <p>
* Generally used by git_push.sh in generated sources which support it.
* This value may also be used by templates in maven style references, READMEs, or other documentation.
*
* @return the git host
*/
public String getGitHost() {
return gitHost;
}

/**
* Gets git user id. e.g. <strong>openapitools</strong>.
Expand Down Expand Up @@ -324,6 +337,7 @@ private GeneratorSettings(Builder builder) {
languageSpecificPrimitives = ImmutableSet.copyOf(builder.languageSpecificPrimitives);
reservedWordMappings = ImmutableMap.copyOf(builder.reservedWordMappings);
serverVariables = ImmutableMap.copyOf(builder.serverVariables);
gitHost = builder.gitHost;
gitUserId = builder.gitUserId;
gitRepoId = builder.gitRepoId;
releaseNote = builder.releaseNote;
Expand Down Expand Up @@ -358,6 +372,9 @@ private GeneratorSettings(Builder builder) {
if (isNotEmpty(modelNameSuffix)) {
additional.put("modelNameSuffix", modelNameSuffix);
}
if (isNotEmpty(gitHost)) {
additional.put("gitHost", gitHost);
}
if (isNotEmpty(gitUserId)) {
additional.put("gitUserId", gitUserId);
}
Expand Down Expand Up @@ -390,6 +407,7 @@ public GeneratorSettings() {
}

private void setDefaults() {
gitHost = DEFAULT_GIT_HOST;
gitUserId = DEFAULT_GIT_USER_ID;
gitRepoId = DEFAULT_GIT_REPO_ID;
releaseNote = DEFAULT_RELEASE_NOTE;
Expand Down Expand Up @@ -442,6 +460,7 @@ public static Builder newBuilder(GeneratorSettings copy) {
if (copy.getServerVariables() != null) {
builder.serverVariables.putAll(copy.getServerVariables());
}
builder.gitHost = copy.getGitHost();
builder.gitUserId = copy.getGitUserId();
builder.gitRepoId = copy.getGitRepoId();
builder.releaseNote = copy.getReleaseNote();
Expand Down Expand Up @@ -473,6 +492,7 @@ public static final class Builder {
private Set<String> languageSpecificPrimitives;
private Map<String, String> reservedWordMappings;
private Map<String, String> serverVariables;
private String gitHost;
private String gitUserId;
private String gitRepoId;
private String releaseNote;
Expand All @@ -490,6 +510,7 @@ public Builder() {
reservedWordMappings = new HashMap<>();
serverVariables = new HashMap<>();

gitHost = DEFAULT_GIT_HOST;
gitUserId = DEFAULT_GIT_USER_ID;
gitRepoId = DEFAULT_GIT_REPO_ID;
releaseNote = DEFAULT_RELEASE_NOTE;
Expand Down Expand Up @@ -783,6 +804,17 @@ public Builder withServerVariable(String key, String value) {
return this;
}

/**
* Sets the {@code gitHost} and returns a reference to this Builder so that the methods can be chained together.
*
* @param gitHost the {@code gitHost} to set
* @return a reference to this Builder
*/
public Builder withGitHost(String gitHost) {
this.gitHost = gitHost;
return this;
}

/**
* Sets the {@code gitUserId} and returns a reference to this Builder so that the methods can be chained together.
*
Expand Down Expand Up @@ -860,6 +892,7 @@ public String toString() {
", importMappings=" + importMappings +
", languageSpecificPrimitives=" + languageSpecificPrimitives +
", reservedWordMappings=" + reservedWordMappings +
", gitHost='" + gitHost + '\'' +
", gitUserId='" + gitUserId + '\'' +
", gitRepoId='" + gitRepoId + '\'' +
", releaseNote='" + releaseNote + '\'' +
Expand Down Expand Up @@ -889,6 +922,7 @@ public boolean equals(Object o) {
Objects.equals(getImportMappings(), that.getImportMappings()) &&
Objects.equals(getLanguageSpecificPrimitives(), that.getLanguageSpecificPrimitives()) &&
Objects.equals(getReservedWordMappings(), that.getReservedWordMappings()) &&
Objects.equals(getGitHost(), that.getGitHost()) &&
Objects.equals(getGitUserId(), that.getGitUserId()) &&
Objects.equals(getGitRepoId(), that.getGitRepoId()) &&
Objects.equals(getReleaseNote(), that.getReleaseNote()) &&
Expand All @@ -915,6 +949,7 @@ public int hashCode() {
getImportMappings(),
getLanguageSpecificPrimitives(),
getReservedWordMappings(),
getGitHost(),
getGitUserId(),
getGitRepoId(),
getReleaseNote(),
Expand Down
5 changes: 5 additions & 0 deletions modules/openapi-generator-gradle-plugin/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ apply plugin: 'org.openapi.generator'
|None
|Reference the library template (sub-template) of a generator.

|gitHost
|String
|github.com
|Git user ID, e.g. gitlab.com.

|gitUserId
|String
|None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
id.set(generate.id)
version.set(generate.version)
library.set(generate.library)
gitHost.set(generate.gitHost)
gitUserId.set(generate.gitUserId)
gitRepoId.set(generate.gitRepoId)
releaseNote.set(generate.releaseNote)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
val library = project.objects.property<String?>()

/**
* Git host, e.g. gitlab.com.
*/
val gitHost = project.objects.property<String?>()

/**
* Git user ID, e.g. openapitools.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ open class GenerateTask : DefaultTask() {
@get:Internal
val library = project.objects.property<String?>()

/**
* Git host, e.g. gitlab.com.
*/
@get:Internal
val gitHost = project.objects.property<String?>()

/**
* Git user ID, e.g. openapitools.
*/
Expand Down Expand Up @@ -510,6 +516,10 @@ open class GenerateTask : DefaultTask() {
configurator.setLibrary(value)
}

gitHost.ifNotEmpty { value ->
configurator.setGitHost(value)
}

gitUserId.ifNotEmpty { value ->
configurator.setGitUserId(value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "inputSpec", property = "openapi.generator.maven.plugin.inputSpec", required = true)
private String inputSpec;

/**
* Git host, e.g. gitlab.com.
*/
@Parameter(name = "gitHost", property = "openapi.generator.maven.plugin.gitHost", required = false)
private String gitHost;

/**
* Git user ID, e.g. swagger-api.
*/
Expand Down Expand Up @@ -456,6 +462,10 @@ public void execute() throws MojoExecutionException {
configurator.setInputSpec(inputSpec);
}

if (isNotEmpty(gitHost)) {
configurator.setGitHost(gitHost);
}

if (isNotEmpty(gitUserId)) {
configurator.setGitUserId(gitUserId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ public interface CodegenConfig {
*/
String getLibrary();

void setGitHost(String gitHost);

String getGitHost();

void setGitUserId(String gitUserId);

String getGitUserId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case,
public static final String MODEL_NAME_SUFFIX = "modelNameSuffix";
public static final String MODEL_NAME_SUFFIX_DESC = "Suffix that will be appended to all model names.";

public static final String GIT_HOST = "gitHost";
public static final String GIT_HOST_DESC = "Git host, e.g. gitlab.com.";

public static final String GIT_USER_ID = "gitUserId";
public static final String GIT_USER_ID_DESC = "Git user ID, e.g. openapitools.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public class DefaultCodegen implements CodegenConfig {
protected Boolean sortParamsByRequiredFlag = true;
protected Boolean ensureUniqueParams = true;
protected Boolean allowUnicodeIdentifiers = false;
protected String gitUserId, gitRepoId, releaseNote;
protected String gitHost, gitUserId, gitRepoId, releaseNote;
protected String httpUserAgent;
protected Boolean hideGenerationTimestamp = true;
// How to encode special characters like $
Expand Down Expand Up @@ -3906,6 +3906,24 @@ public String getLibrary() {
return library;
}

/**
* Set Git host.
*
* @param gitHost Git host
*/
public void setGitHost(String gitHost) {
this.gitHost = gitHost;
}

/**
* Git host.
*
* @return Git host
*/
public String getGitHost() {
return gitHost;
}

/**
* Set Git user ID.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ public CodegenConfigurator setGitRepoId(String gitRepoId) {
return this;
}

public CodegenConfigurator setGitHost(String gitHost) {
generatorSettingsBuilder.withGitHost(gitHost);
return this;
}

public CodegenConfigurator setGitUserId(String gitUserId) {
generatorSettingsBuilder.withGitUserId(gitUserId);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update"
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" "gitlab.com"

git_user_id=$1
git_repo_id=$2
release_note=$3
git_host=$4

if [ "$git_host" = "" ]; then
git_host="{{{gitHost}}}"
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
fi

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

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

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

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
git remote add origin https://${git_user_id}:${GIT_TOKEN}@${git_host}/${git_user_id}/${git_repo_id}.git
fi

fi

git pull origin master

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

Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/bin/sh
# ref: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
#
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update"
# Usage example: /bin/sh ./git_push.sh wing328 openapi-pestore-perl "minor update" "gitlab.com"

git_user_id=$1
git_repo_id=$2
release_note=$3
git_host=$4

if [ "$git_host" = "" ]; then
git_host="{{{gitHost}}}"
echo "[INFO] No command line input provided. Set \$git_host to $git_host"
fi

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

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

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

if [ "$GIT_TOKEN" = "" ]; then
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the Git credential in your environment."
git remote add origin https://github.com/${git_user_id}/${git_repo_id}.git
echo "[INFO] \$GIT_TOKEN (environment variable) is not set. Using the git credential in your environment."
git remote add origin https://${git_host}/${git_user_id}/${git_repo_id}.git
else
git remote add origin https://${git_user_id}:${GIT_TOKEN}@github.com/${git_user_id}/${git_repo_id}.git
git remote add origin https://${git_user_id}:${GIT_TOKEN}@${git_host}/${git_user_id}/${git_repo_id}.git
fi

fi

git pull origin master

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

Loading

0 comments on commit 7b4a201

Please sign in to comment.