Skip to content

Commit

Permalink
Merge pull request #445 from amihaiemil/443
Browse files Browse the repository at this point in the history
#443 Backend for Personal Repos
  • Loading branch information
amihaiemil authored Jun 26, 2021
2 parents e0b6182 + 6166d59 commit 03b5a85
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

<properties>
<java.version>11</java.version>
<self.core.version>0.0.81</self.core.version>
<self.storage.version>0.0.70</self.storage.version>
<self.core.version>0.0.88</self.core.version>
<self.storage.version>0.0.71</self.storage.version>
</properties>

<dependencies>
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/com/selfxdsd/selfweb/api/Repositories.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
* @author Mihai Andronache (amihaiemil@gmail.com)
* @version $Id$
* @since 0.0.1
* @todo #33:60min Design the front-end part, similarly to how
* we already read and display the public repos. Don't forget to
* add a tooltip advising the user to go to Github Settings and
* grant Org Access if they do not see all the repos.
*/
@RestController
public class Repositories extends BaseApiController {
Expand All @@ -60,6 +56,35 @@ public Repositories(final User user) {
this.user = user;
}

/**
* Get the user's personal repos (both public and private).
* @return ResponseEntity.
* @todo #443:60min Modify the front-end part of personal repos. We
* should call this endpoint instead of Github directly.
*/
@GetMapping(
value = "/repositories/personal",
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<String> personalRepos() {
JsonArrayBuilder reposBuilder = Json.createArrayBuilder();
final Repos personal = this.user.provider().repos();
for(final Repo repo : personal) {
reposBuilder = reposBuilder.add(Json.createObjectBuilder()
.add("repoFullName", repo.fullName())
.add("provider", repo.provider())
.build());
}
final JsonArray repos = reposBuilder.build();
final ResponseEntity<String> response;
if(repos.isEmpty()){
response = ResponseEntity.noContent().build();
} else {
response = ResponseEntity.ok(repos.toString());
}
return response;
}

/**
* Get the user's organization repos (repos in an organization
* to which the user has admin rights).
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/repositories.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ <h4 class="user_name"></h4>
<img src="/images/loading.svg">
</div>
<div id="personal-repos-info" class="mb-4" style="display: none;">
These are all your personal public repositories (personal private repositories are not yet supported).
These are all your personal repositories, both public and private.
</div>
<table id="repos" class="display">
<thead>
Expand Down
75 changes: 75 additions & 0 deletions src/test/java/com/selfxdsd/selfweb/api/RepositoriesTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,79 @@ public void returnsManagedRepos() {
);
}

/**
* It can return the user's personal repos.
*/
@Test
public void returnsFoundPersonalRepos() {
final Repo first = Mockito.mock(Repo.class);
Mockito.when(first.fullName()).thenReturn("mihai/first");
Mockito.when(first.provider()).thenReturn("github");

final Repo second = Mockito.mock(Repo.class);
Mockito.when(second.fullName()).thenReturn("mihai/second");
Mockito.when(second.provider()).thenReturn("github");


final Repos personal = Mockito.mock(Repos.class);
Mockito.when(personal.iterator()).thenReturn(
List.of(first, second).iterator()
);
final Provider provider = Mockito.mock(Provider.class);
Mockito.when(provider.repos()).thenReturn(personal);
final User user = Mockito.mock(User.class);
Mockito.when(user.provider()).thenReturn(provider);

final Repositories reposApi = new Repositories(user);
final ResponseEntity<String> resp = reposApi.personalRepos();
MatcherAssert.assertThat(
resp.getStatusCode(),
Matchers.equalTo(HttpStatus.OK)
);
final JsonArray array = Json
.createReader(new StringReader(resp.getBody()))
.readArray();
MatcherAssert.assertThat(array, Matchers.iterableWithSize(2));
MatcherAssert.assertThat(array.get(0)
.asJsonObject()
.getString("repoFullName"),
Matchers.equalTo("mihai/first")
);
MatcherAssert.assertThat(array.get(0)
.asJsonObject()
.getString("provider"),
Matchers.equalTo("github")
);
MatcherAssert.assertThat(array.get(1)
.asJsonObject()
.getString("repoFullName"),
Matchers.equalTo("mihai/second")
);
MatcherAssert.assertThat(array.get(1)
.asJsonObject()
.getString("provider"),
Matchers.equalTo("github")
);
}

/**
* If the user has no personal repos, it should return NO CONTENT.
*/
@Test
public void returnsNoContentPersonalRepos() {
final Repos personal = Mockito.mock(Repos.class);
Mockito.when(personal.iterator()).thenReturn(
new ArrayList<Repo>().iterator()
);
final Provider provider = Mockito.mock(Provider.class);
Mockito.when(provider.repos()).thenReturn(personal);
final User user = Mockito.mock(User.class);
Mockito.when(user.provider()).thenReturn(provider);

final Repositories reposApi = new Repositories(user);
MatcherAssert.assertThat(
reposApi.personalRepos().getStatusCode(),
Matchers.equalTo(HttpStatus.NO_CONTENT)
);
}
}

0 comments on commit 03b5a85

Please sign in to comment.