Skip to content

Commit

Permalink
Use users own repo to retrive repos for that user. also only show rep…
Browse files Browse the repository at this point in the history
…os that belong to the tab
  • Loading branch information
suryagaddipati committed Feb 16, 2017
1 parent 3deb6f3 commit 64c865e
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 128 deletions.
8 changes: 5 additions & 3 deletions pom.xml
Expand Up @@ -23,13 +23,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.5</version>
<relativePath />
<relativePath/>
</parent>
<groupId>com.groupon.jenkins-ci.plugins</groupId>
<artifactId>DotCi</artifactId>
Expand Down Expand Up @@ -188,7 +190,7 @@ THE SOFTWARE.
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>github-api</artifactId>
<version>1.77</version>
<version>1.84</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down
Expand Up @@ -38,18 +38,25 @@ of this software and associated documentation files (the "Software"), to deal
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Iterator;
import java.util.stream.StreamSupport;

@Extension
public class GithubReposController implements RootAction, StaplerProxy {

public static final String URL = "mygithubprojects";
private final String currentOrg;

public GithubReposController() {
this(null);
}

public GithubReposController(final String currentOrg) {
this.currentOrg = currentOrg;
}

@Override
public String getIconFileName() {
Expand All @@ -70,39 +77,31 @@ public Iterable<String> getOrgs() throws IOException {
return getCurrentUser().getOrgs();
}

public Iterable<ProjectConfigInfo> getRepositories() throws IOException {
List<ProjectConfigInfo> projectInfos = new LinkedList<ProjectConfigInfo>();
Map<String, GHRepository> ghRepos = getCurrentUser().getRepositories(getCurrentOrg());
for (Map.Entry<String, GHRepository> entry : ghRepos.entrySet()) {
if (entry.getValue().hasAdminAccess()) {
projectInfos.add(new ProjectConfigInfo(entry.getKey(), entry.getValue()));
}
}
return projectInfos;
public Iterator<ProjectConfigInfo> getRepositories() throws IOException {
final String currentOrg = getCurrentOrg();
final Iterable<GHRepository> ghRepos = getCurrentUser().getRepositories(currentOrg);
return StreamSupport.stream(ghRepos.spliterator(), false).
filter(r -> r.hasAdminAccess() && currentOrg.equals(r.getOwnerName())).map(r -> new ProjectConfigInfo(r.getName(), r)).iterator();
}

protected GithubCurrentUserService getCurrentUser() throws IOException {
return new GithubCurrentUserService(getGitHub(Stapler.getCurrentRequest()));
}

public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, InterruptedException, InvocationTargetException, IllegalAccessException {
String[] tokens = req.getRestOfPath().split("/");
String leadToken = tokens.length > 0 ? tokens[1] : null;
GithubRepoAction repoAction = getRepoAction(leadToken);
public Object getDynamic(final String token, final StaplerRequest req, final StaplerResponse rsp) throws InvocationTargetException, IllegalAccessException {
final GithubRepoAction repoAction = getRepoAction(token);
if (repoAction != null) {
String methodToken = tokens.length > 1 ? tokens[2] : "index";
String methodName = "do" + StringUtils.capitalize(methodToken);
Method method = ReflectionUtils.getPublicMethodNamed(repoAction.getClass(), methodName);
final String[] tokens = req.getRestOfPath().split("/");
final String methodToken = tokens.length > 1 ? tokens[1] : "index";
final String methodName = "do" + StringUtils.capitalize(methodToken);
final Method method = ReflectionUtils.getPublicMethodNamed(repoAction.getClass(), methodName);
method.invoke(repoAction, req, rsp);
} else {
String orgToken = req.getRestOfPath().replace("/", "");
req.getSession().setAttribute("setupOrg" + this.getCurrentGithubLogin(), orgToken);
rsp.forwardToPreviousPage(req);
}
return new GithubReposController(token);
}

private GithubRepoAction getRepoAction(String leadToken) {
for (GithubRepoAction repoAction : getRepoActions()) {
private GithubRepoAction getRepoAction(final String leadToken) {
for (final GithubRepoAction repoAction : getRepoActions()) {
if (repoAction.getName().equals(leadToken)) return repoAction;
}
return null;
Expand All @@ -113,12 +112,11 @@ private String getCurrentGithubLogin() throws IOException {
}

public String getCurrentOrg() throws IOException {
String currentOrg = (String) Stapler.getCurrentRequest().getSession().getAttribute("setupOrg" + getCurrentGithubLogin());
return StringUtils.isEmpty(currentOrg) ? Iterables.get(getOrgs(), 0) : currentOrg;
return StringUtils.isEmpty(this.currentOrg) ? getCurrentGithubLogin() : this.currentOrg;
}

public int getSelectedOrgIndex() throws IOException {
Iterable<String> orgs = getOrgs();
final Iterable<String> orgs = getOrgs();
for (int i = 0; i < Iterables.size(orgs); i++) {
if (Iterables.get(orgs, i).equals(getCurrentOrg())) return i;
}
Expand All @@ -129,7 +127,7 @@ public Iterable<GithubRepoAction> getRepoActions() {
return GithubRepoAction.getGithubRepoActions();
}

private GitHub getGitHub(StaplerRequest request) throws IOException {
private GitHub getGitHub(final StaplerRequest request) throws IOException {
return GitHub.connectUsingOAuth(getSetupConfig().getGithubApiUrl(), getAccessToken(request));
}

Expand All @@ -138,14 +136,14 @@ private SetupConfig getSetupConfig() {
}


private String getAccessToken(StaplerRequest request) {
private String getAccessToken(final StaplerRequest request) {
return (String) request.getSession().getAttribute("access_token");
}


@Override
public Object getTarget() {
StaplerRequest currentRequest = Stapler.getCurrentRequest();
final StaplerRequest currentRequest = Stapler.getCurrentRequest();
if (getAccessToken(currentRequest) == null)
return new GithubOauthLoginAction();
return this;
Expand Down
Expand Up @@ -30,46 +30,45 @@ of this software and associated documentation files (the "Software"), to deal
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GithubCurrentUserService {

private final GHMyself user;
private final GitHub gh;

public GithubCurrentUserService(GitHub gh) {
public GithubCurrentUserService(final GitHub gh) {
this.gh = gh;
try {
this.user = gh.getMyself();
} catch (IOException e) {
} catch (final IOException e) {
throw new RuntimeException(e);
}
}


public Map<String, GHRepository> getRepositories(String orgName) {
public Iterable<GHRepository> getRepositories(final String orgName) {
try {
if (orgName.equals(user.getLogin())) {
return user.getRepositories();
if (orgName.equals(this.user.getLogin())) {
return this.gh.getMyself().listRepositories();
} else {
return gh.getOrganization(orgName).getRepositories();
return this.gh.getOrganization(orgName).listRepositories();
}
} catch (IOException e) {
} catch (final IOException e) {
throw new RuntimeException(e);
}
}

public String getCurrentLogin() {
return user.getLogin();
return this.user.getLogin();
}

public Iterable<String> getOrgs() {
try {
List<String> allOrgs = new ArrayList<String>();
final List<String> allOrgs = new ArrayList<>();
allOrgs.add(getCurrentLogin());
allOrgs.addAll(gh.getMyOrganizations().keySet());
allOrgs.addAll(this.gh.getMyOrganizations().keySet());
return allOrgs;
} catch (IOException e) {
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
Expand Down
Expand Up @@ -25,87 +25,99 @@

<!DOCTYPE html>
<html lang="en">
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">
<head>
<st:setHeader name="Expires" value="0" />
<st:contentType value="text/html;charset=UTF-8" />
<j:invokeStatic var="developmentMode" className="java.lang.Boolean" method="getBoolean">
<j:arg type="java.lang.String" value="hudson.hpi.run"/>
</j:invokeStatic>
<j:new var="h" className="hudson.Functions" />
${h.initPageVariables(context)}
<title>New DotCi Project</title>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes"/>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<head>
<st:setHeader name="Expires" value="0"/>
<st:contentType value="text/html;charset=UTF-8"/>
<j:invokeStatic var="developmentMode" className="java.lang.Boolean"
method="getBoolean">
<j:arg type="java.lang.String" value="hudson.hpi.run"/>
</j:invokeStatic>
<j:new var="h" className="hudson.Functions"/>
${h.initPageVariables(context)}
<title>New DotCi Project</title>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta name="viewport"
content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=yes"/>

<script src="${resURL}/plugin/DotCi/js/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="icon" href="${resURL}/plugin/DotCi/favicons/default.ico" type="image/vnd.microsoft.icon" />
<script
src="${resURL}/plugin/DotCi/js/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="icon" href="${resURL}/plugin/DotCi/favicons/default.ico"
type="image/vnd.microsoft.icon"/>

<j:if test='${developmentMode}'>
<link rel="import" href="${resURL}/plugin/DotCi/webcomponent-imports.html"/>
</j:if>
<j:if test='${!developmentMode}'>
<link rel="import" href="${resURL}/plugin/DotCi/webcomponent-imports-vulcanized.html"/>
</j:if>
<style is="custom-style">
.list {
padding-top: 12px;
background-color: white;
display: inline-block;
margin: 12px;
@apply(--shadow-elevation-2dp);
}
paper-tabs, paper-toolbar {
background-color: #3f51b5;
color: #fff;
box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.2);
}
paper-toolbar paper-tabs {
box-shadow: none;
}
paper-tabs[noink][no-bar] paper-tab.iron-selected {
color: #ffff8d;
}
paper-tabs[align-bottom] {
box-shadow: 0px -2px 6px rgba(0, 0, 0, 0.15);
}
.org-tab{
color: #FFFFFF;
text-decoration: none;
}
</style>
</head>
<j:if test='${developmentMode}'>
<link rel="import"
href="${resURL}/plugin/DotCi/webcomponent-imports.html"/>
</j:if>
<j:if test='${!developmentMode}'>
<link rel="import"
href="${resURL}/plugin/DotCi/webcomponent-imports-vulcanized.html"/>
</j:if>
<style is="custom-style">
.list {
padding-top: 12px;
background-color: white;
display: inline-block;
margin: 12px;
@apply(--shadow-elevation-2dp);
}
paper-tabs, paper-toolbar {
background-color: #3f51b5;
color: #fff;
box-shadow: 0px 3px 6px rgba(0, 0, 0, 0.2);
}
paper-toolbar paper-tabs {
box-shadow: none;
}
paper-tabs[noink][no-bar] paper-tab.iron-selected {
color: #ffff8d;
}
paper-tabs[align-bottom] {
box-shadow: 0px -2px 6px rgba(0, 0, 0, 0.15);
}
.org-tab{
color: #FFFFFF;
text-decoration: none;
}
</style>
</head>


<body>
<body>

<paper-toolbar>
<paper-icon-button src='${resURL}/plugin/DotCi/logo.png' onclick="window.location='${rootURL}'" />
<div class="title"> New DotCi Project</div>
</paper-toolbar>
<paper-tabs selected="${it.selectedOrgIndex}">
<j:forEach items="${it.orgs}" var="i">
<paper-tab link="true"> <a class=" org-tab horizontal center-center layout" href="${i}"><h3>${i}</h3></a> </paper-tab>
</j:forEach>
</paper-tabs>
<div class="list">
<paper-toolbar>
<paper-icon-button src='${resURL}/plugin/DotCi/logo.png'
onclick="window.location='${rootURL}'"/>
<div class="title">New DotCi Project</div>
</paper-toolbar>
<paper-tabs selected="${it.selectedOrgIndex}">
<j:forEach items="${it.orgs}" var="i">
<paper-tab link="true">
<a class=" org-tab horizontal center-center layout"
href="${rootURL}/${it.urlName}/${i}">
<h3>${i}</h3>
</a>
</paper-tab>
</j:forEach>
</paper-tabs>
<div class="list">

<j:forEach items="${it.repositories}" var="j">
<j:forEach items="${it.repositories}" var="j">

<paper-material>
<paper-item>
<paper-item-body two-line="true">
<div> ${j.name} </div>
<div secondary="true">
<st:include page="repo_actions.jelly" />
</div>
</paper-item-body>
</paper-item>
</paper-material>
<paper-material>
<paper-item>
<paper-item-body two-line="true">
<div>${j.name}</div>
<div secondary="true">
<st:include page="repo_actions.jelly"/>
</div>
</paper-item-body>
</paper-item>
</paper-material>

</j:forEach>
</div>
</body>
</j:jelly>
</j:forEach>
</div>
</body>
</j:jelly>
</html>

0 comments on commit 64c865e

Please sign in to comment.