From 1302e8f3694debd31b6d55406ebfdf56cc08b0e7 Mon Sep 17 00:00:00 2001 From: David Russell Date: Mon, 15 May 2017 17:32:26 +0700 Subject: [PATCH] Added support for Gitea and Gogs services. --- app/Module.java | 2 + app/com/gitpitch/git/GRSManager.java | 18 +++ app/com/gitpitch/git/vendors/Gitea.java | 78 +++++++++++ app/com/gitpitch/git/vendors/Gogs.java | 78 +++++++++++ app/com/gitpitch/models/GiteaRepoModel.java | 145 ++++++++++++++++++++ app/com/gitpitch/models/GogsRepoModel.java | 145 ++++++++++++++++++++ conf/application.conf | 23 +++- 7 files changed, 488 insertions(+), 1 deletion(-) create mode 100644 app/com/gitpitch/git/vendors/Gitea.java create mode 100644 app/com/gitpitch/git/vendors/Gogs.java create mode 100644 app/com/gitpitch/models/GiteaRepoModel.java create mode 100644 app/com/gitpitch/models/GogsRepoModel.java diff --git a/app/Module.java b/app/Module.java index d901ee9..43da8f2 100644 --- a/app/Module.java +++ b/app/Module.java @@ -58,6 +58,8 @@ public class Module extends AbstractModule { bind(GitHub.class).asEagerSingleton(); bind(GitLab.class).asEagerSingleton(); bind(BitBucket.class).asEagerSingleton(); + bind(Gitea.class).asEagerSingleton(); + bind(Gogs.class).asEagerSingleton(); bind(FrontEndThreads.class).asEagerSingleton(); bind(BackEndThreads.class).asEagerSingleton(); bind(Dependencies.class).asEagerSingleton(); diff --git a/app/com/gitpitch/git/GRSManager.java b/app/com/gitpitch/git/GRSManager.java index 091a446..ca812b1 100644 --- a/app/com/gitpitch/git/GRSManager.java +++ b/app/com/gitpitch/git/GRSManager.java @@ -48,6 +48,8 @@ public class GRSManager { private final GitHub gitHubService; private final GitLab gitLabService; private final BitBucket bitBucketService; + private final Gitea giteaService; + private final Gogs gogsService; private final Configuration cfg; private final Map grsStore = new HashMap(); private GRS grsDefault; @@ -57,6 +59,8 @@ public class GRSManager { GitHub gitHubService, GitLab gitLabService, BitBucket bitBucketService, + Gitea giteaService, + Gogs gogsService, Configuration cfg) { this.diskService = diskService; @@ -70,6 +74,12 @@ public class GRSManager { this.bitBucketService = bitBucketService; this.bitBucketService.init(this, diskService); + this.giteaService = giteaService; + this.giteaService.init(this, diskService); + + this.gogsService = gogsService; + this.gogsService.init(this, diskService); + this.cfg = cfg; List grsCfg = cfg.getList("gitpitch.git.repo.services"); @@ -140,6 +150,14 @@ public class GRSManager { log.debug("getService: matching BitBucket"); service = bitBucketService; break; + case Gitea.TYPE: + log.debug("getService: matching Gitea"); + service = giteaService; + break; + case Gogs.TYPE: + log.debug("getService: matching Gogs"); + service = gogsService; + break; default: log.debug("getService: defaulting GitHub"); service = gitHubService; diff --git a/app/com/gitpitch/git/vendors/Gitea.java b/app/com/gitpitch/git/vendors/Gitea.java new file mode 100644 index 0000000..1bc8110 --- /dev/null +++ b/app/com/gitpitch/git/vendors/Gitea.java @@ -0,0 +1,78 @@ +/* + * MIT License + * + * Copyright (c) 2016 David Russell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.gitpitch.git.vendors; + +import com.gitpitch.git.*; +import com.gitpitch.models.*; +import com.gitpitch.services.DiskService; +import com.gitpitch.utils.PitchParams; +import com.fasterxml.jackson.databind.JsonNode; +import java.nio.file.Path; +import java.util.*; +import javax.inject.*; +import play.Logger; +import play.Logger.ALogger; + +/* + * Gitea API Service. + */ +@Singleton +public class Gitea extends GRSService { + + private final Logger.ALogger log = Logger.of(this.getClass()); + + public GitRepoModel model(PitchParams pp, JsonNode json) { + return GiteaRepoModel.build(pp, json); + } + + public String raw(PitchParams pp) { + + GRS grs = grsManager.get(pp); + + return new StringBuffer(grs.getRawBase()) + .append(pp.user) + .append(SLASH) + .append(pp.repo) + .append(GITEA_RAW) + .append(pp.branch) + .append(SLASH) + .toString(); + } + + public String repo(PitchParams pp) { + + GRS grs = grsManager.get(pp); + + return new StringBuffer(grs.getApiBase()) + .append(GITEA_REPO_API) + .append(pp.user) + .append(SLASH) + .append(pp.repo) + .toString(); + } + + public static final String TYPE = "gitea"; + private static final String GITEA_REPO_API = "repos/"; + private static final String GITEA_RAW = "/raw/"; +} diff --git a/app/com/gitpitch/git/vendors/Gogs.java b/app/com/gitpitch/git/vendors/Gogs.java new file mode 100644 index 0000000..8ac85bd --- /dev/null +++ b/app/com/gitpitch/git/vendors/Gogs.java @@ -0,0 +1,78 @@ +/* + * MIT License + * + * Copyright (c) 2016 David Russell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.gitpitch.git.vendors; + +import com.gitpitch.git.*; +import com.gitpitch.models.*; +import com.gitpitch.services.DiskService; +import com.gitpitch.utils.PitchParams; +import com.fasterxml.jackson.databind.JsonNode; +import java.nio.file.Path; +import java.util.*; +import javax.inject.*; +import play.Logger; +import play.Logger.ALogger; + +/* + * Gogs API Service. + */ +@Singleton +public class Gogs extends GRSService { + + private final Logger.ALogger log = Logger.of(this.getClass()); + + public GitRepoModel model(PitchParams pp, JsonNode json) { + return GogsRepoModel.build(pp, json); + } + + public String raw(PitchParams pp) { + + GRS grs = grsManager.get(pp); + + return new StringBuffer(grs.getRawBase()) + .append(pp.user) + .append(SLASH) + .append(pp.repo) + .append(GOGS_RAW) + .append(pp.branch) + .append(SLASH) + .toString(); + } + + public String repo(PitchParams pp) { + + GRS grs = grsManager.get(pp); + + return new StringBuffer(grs.getApiBase()) + .append(GOGS_REPO_API) + .append(pp.user) + .append(SLASH) + .append(pp.repo) + .toString(); + } + + public static final String TYPE = "gogs"; + private static final String GOGS_REPO_API = "repos/"; + private static final String GOGS_RAW = "/raw/"; +} diff --git a/app/com/gitpitch/models/GiteaRepoModel.java b/app/com/gitpitch/models/GiteaRepoModel.java new file mode 100644 index 0000000..e27e6de --- /dev/null +++ b/app/com/gitpitch/models/GiteaRepoModel.java @@ -0,0 +1,145 @@ +/* + * MIT License + * + * Copyright (c) 2016 David Russell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.gitpitch.models; + +import com.fasterxml.jackson.databind.JsonNode; +import com.gitpitch.utils.PitchParams; +import play.Logger; +import play.Logger.ALogger; + +import java.util.*; + +/* + * Rendering model for views.Landing.scala.html. + */ +public class GiteaRepoModel extends GitRepoModel { + + private final Logger.ALogger log = Logger.of(this.getClass()); + + /* + * Initialize instance of GiteaRepoModel. + */ + private GiteaRepoModel(PitchParams pp) { + this(pp, null); + } + + /* + * Initialize instance of GiteaRepoModel. + */ + private GiteaRepoModel(PitchParams pp, JsonNode json) { + + this._pp = pp; + + this._pretty = new StringBuffer(SLASH) + .append(this._pp.user) + .append(SLASH) + .append(this._pp.repo) + .toString(); + + this._cacheKey = genKey(pp); + + /* + * Generate derived data on instance only if the GitHub + * API JSON is available for processing. + */ + if (json != null) { + + JsonNode ownerNode = json.findPath("owner"); + this._type = null; + + this._desc = json.findPath("description").textValue(); + this._created = json.findPath("created_at").textValue(); + this._updated = json.findPath("updated_at").textValue(); + this._lang = null; + + this._stars = json.findPath("stars_count").asInt(); + this._forks = json.findPath("forks_count").asInt(); + this._issues = json.findPath("open_issues.count").asInt(); + + this._hasWiki = false; + this._hasPages = false; + + } else { + + this._type = null; + + this._desc = null; + this._created = null; + this._updated = null; + this._lang = null; + + this._stars = 0; + this._forks = 0; + this._issues = 0; + + this._hasWiki = false; + this._hasPages = false; + } + + } + + public static GitRepoModel build(PitchParams pp) { + return build(pp, null); + } + + public static GitRepoModel build(PitchParams pp, JsonNode json) { + return new GiteaRepoModel(pp, json); + } + + public String owner() { + return _pp.user; + } + + public String name() { + return _pp.repo; + } + + public String description() { + return _desc; + } + + public String lang() { + return _lang; + } + + public boolean byOrg() { + return false; + } + + public int stargazers() { + return _stars; + } + + public int forks() { + return _forks; + } + + public String toString() { + return _pretty; + } + + public String key() { + return _cacheKey; + } +} diff --git a/app/com/gitpitch/models/GogsRepoModel.java b/app/com/gitpitch/models/GogsRepoModel.java new file mode 100644 index 0000000..8c8d454 --- /dev/null +++ b/app/com/gitpitch/models/GogsRepoModel.java @@ -0,0 +1,145 @@ +/* + * MIT License + * + * Copyright (c) 2016 David Russell + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.gitpitch.models; + +import com.fasterxml.jackson.databind.JsonNode; +import com.gitpitch.utils.PitchParams; +import play.Logger; +import play.Logger.ALogger; + +import java.util.*; + +/* + * Rendering model for views.Landing.scala.html. + */ +public class GogsRepoModel extends GitRepoModel { + + private final Logger.ALogger log = Logger.of(this.getClass()); + + /* + * Initialize instance of GogsRepoModel. + */ + private GogsRepoModel(PitchParams pp) { + this(pp, null); + } + + /* + * Initialize instance of GogsRepoModel. + */ + private GogsRepoModel(PitchParams pp, JsonNode json) { + + this._pp = pp; + + this._pretty = new StringBuffer(SLASH) + .append(this._pp.user) + .append(SLASH) + .append(this._pp.repo) + .toString(); + + this._cacheKey = genKey(pp); + + /* + * Generate derived data on instance only if the GitHub + * API JSON is available for processing. + */ + if (json != null) { + + JsonNode ownerNode = json.findPath("owner"); + this._type = null; + + this._desc = json.findPath("description").textValue(); + this._created = json.findPath("created_at").textValue(); + this._updated = json.findPath("updated_at").textValue(); + this._lang = null; + + this._stars = json.findPath("stars_count").asInt(); + this._forks = json.findPath("forks_count").asInt(); + this._issues = json.findPath("open_issues.count").asInt(); + + this._hasWiki = false; + this._hasPages = false; + + } else { + + this._type = null; + + this._desc = null; + this._created = null; + this._updated = null; + this._lang = null; + + this._stars = 0; + this._forks = 0; + this._issues = 0; + + this._hasWiki = false; + this._hasPages = false; + } + + } + + public static GitRepoModel build(PitchParams pp) { + return build(pp, null); + } + + public static GitRepoModel build(PitchParams pp, JsonNode json) { + return new GogsRepoModel(pp, json); + } + + public String owner() { + return _pp.user; + } + + public String name() { + return _pp.repo; + } + + public String description() { + return _desc; + } + + public String lang() { + return _lang; + } + + public boolean byOrg() { + return false; + } + + public int stargazers() { + return _stars; + } + + public int forks() { + return _forks; + } + + public String toString() { + return _pretty; + } + + public String key() { + return _cacheKey; + } +} diff --git a/conf/application.conf b/conf/application.conf index e55b89f..76fdb17 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -451,7 +451,28 @@ gitpitch { rawbase = "https://bitbucket.org/" default = "false" } - + { + name = "Gitea" + type = "gitea" + site = "https://gitea.io" + apibase = "http://localhost:3000/api/v1/" + apibase = "https://try.gitea.io/api/v1/" + // apitoken = "token your-gitea-app-token-here" + apitokenheader = "Authorization" + rawbase = "http://localhost:3000/" + default = "false" + } + { + name = "Gogs" + type = "gogs" + site = "https://gogs.io" + apibase = "http://localhost:3000/api/v1/" + apitoken = "token your-gitea-app-token-here" + apitokenheader = "Authorization" + rawbase = "http://localhost:3000/" + rawbase = "https://try.gogs.io/" + default = "false" + } ] } }