Java源码示例:com.jcabi.github.Github
示例1
@Test
public void getSpringCloudVersionBomRangesMissingTest() {
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
when(rest.getForObject(anyString(), eq(Map.class))).thenReturn(new HashMap());
InitializrSpringCloudInfoService service = new InitializrSpringCloudInfoService(
rest, github, githubPomReader);
try {
service.getSpringCloudVersion("2.1.0");
fail("Exception should have been thrown");
}
catch (SpringCloudVersionNotFoundException e) {
assertThat(e.getCause().getMessage(), Matchers.startsWith("bom-ranges"));
}
}
示例2
@Test
public void getSpringCloudVersionSpringCloudMissingTest() {
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
Map<String, Map<String, String>> info = new HashMap<>();
info.put("bom-ranges", new HashMap<>());
when(rest.getForObject(anyString(), eq(Map.class))).thenReturn(info);
InitializrSpringCloudInfoService service = new InitializrSpringCloudInfoService(
rest, github, githubPomReader);
try {
service.getSpringCloudVersion("2.1.0");
fail("Exception should have been thrown");
}
catch (SpringCloudVersionNotFoundException e) {
assertThat(e.getCause().getMessage(), Matchers.startsWith("spring-cloud"));
}
}
示例3
@Test
public void getSpringCloudReleaseVersionTest() throws Exception {
String bomVersion = "vHoxton.BUILD-SNAPSHOT";
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
when(githubPomReader.readPomFromUrl(eq(String
.format(SpringCloudRelease.SPRING_CLOUD_STARTER_PARENT_RAW, bomVersion))))
.thenReturn(new MavenXpp3Reader()
.read(new FileReader(new ClassPathResource(
"spring-cloud-starter-parent-pom.xml")
.getFile())));
when(githubPomReader.readPomFromUrl(eq(String.format(
SpringCloudRelease.SPRING_CLOUD_RELEASE_DEPENDENCIES_RAW, bomVersion))))
.thenReturn(new MavenXpp3Reader().read(new FileReader(
new ClassPathResource("spring-cloud-dependencies-pom.xml")
.getFile())));
InitializrSpringCloudInfoService service = spy(
new InitializrSpringCloudInfoService(rest, github, githubPomReader));
doReturn(Arrays.asList(new String[] { bomVersion })).when(service)
.getSpringCloudVersions();
Map<String, String> releaseVersionsResult = service
.getReleaseVersions(bomVersion);
assertThat(releaseVersionsResult,
Matchers.equalTo(SpringCloudInfoTestData.releaseVersions));
}
示例4
@Test(expected = SpringCloudVersionNotFoundException.class)
public void getSpringCloudReleaseVersionNotFoundTest() throws Exception {
String bomVersion = "vFooBar.BUILD-SNAPSHOT";
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
when(githubPomReader.readPomFromUrl(eq(String
.format(SpringCloudRelease.SPRING_CLOUD_STARTER_PARENT_RAW, bomVersion))))
.thenReturn(new MavenXpp3Reader()
.read(new FileReader(new ClassPathResource(
"spring-cloud-starter-parent-pom.xml")
.getFile())));
when(githubPomReader.readPomFromUrl(eq(String.format(
SpringCloudRelease.SPRING_CLOUD_RELEASE_DEPENDENCIES_RAW, bomVersion))))
.thenReturn(new MavenXpp3Reader().read(new FileReader(
new ClassPathResource("spring-cloud-dependencies-pom.xml")
.getFile())));
InitializrSpringCloudInfoService service = spy(
new InitializrSpringCloudInfoService(rest, github, githubPomReader));
doReturn(new ArrayList()).when(service).getSpringCloudVersions();
service.getReleaseVersions(bomVersion);
}
示例5
@Test
public void getSpringCloudVersionsTest() throws Exception {
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
Response response = mock(Response.class);
Request request = mock(Request.class);
RequestURI requestURI = mock(RequestURI.class);
JsonResponse jsonResponse = new JsonResponse(new DefaultResponse(request, 200, "",
new Array<>(),
IOUtils.toByteArray(new ClassPathResource("spring-cloud-versions.json")
.getInputStream())));
doReturn(request).when(requestURI).back();
doReturn(requestURI).when(requestURI).path(eq(SPRING_CLOUD_RELEASE_TAGS_PATH));
doReturn(requestURI).when(request).uri();
doReturn(jsonResponse).when(response).as(eq(JsonResponse.class));
doReturn(response).when(request).fetch();
doReturn(request).when(github).entry();
InitializrSpringCloudInfoService service = spy(
new InitializrSpringCloudInfoService(rest, github, githubPomReader));
assertThat(service.getSpringCloudVersions(),
Matchers.equalTo(SpringCloudInfoTestData.springCloudVersions.stream()
.map(v -> v.replaceFirst("v", "")).collect(Collectors.toList())));
}
示例6
@Test
public void getMilestoneDueDateTest() throws Exception {
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
Repos repos = mock(Repos.class);
Repo repo = mock(Repo.class);
Milestones milestones = mock(Milestones.class);
Iterable iterable = buildMilestonesIterable();
doReturn(iterable).when(milestones).iterate(any(Map.class));
doReturn(milestones).when(repo).milestones();
doReturn(repo).when(repos).get(any(Coordinates.class));
doReturn(repos).when(github).repos();
InitializrSpringCloudInfoService service = spy(
new InitializrSpringCloudInfoService(rest, github, githubPomReader));
assertThat(service.getMilestoneDueDate("Finchley.SR4"),
Matchers.equalTo(new SpringCloudInfoService.Milestone("No Due Date")));
assertThat(service.getMilestoneDueDate("Hoxton.RELEASE"),
Matchers.equalTo(new SpringCloudInfoService.Milestone("2019-07-31")));
}
示例7
@Test
public void getMilestonesTest() throws Exception {
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
Repos repos = mock(Repos.class);
Repo repo = mock(Repo.class);
Milestones milestones = mock(Milestones.class);
Iterable iterable = buildMilestonesIterable();
doReturn(iterable).when(milestones).iterate(any(Map.class));
doReturn(milestones).when(repo).milestones();
doReturn(repo).when(repos).get(any(Coordinates.class));
doReturn(repos).when(github).repos();
InitializrSpringCloudInfoService service = spy(
new InitializrSpringCloudInfoService(rest, github, githubPomReader));
assertThat(service.getMilestones(), Matchers.equalTo(milestoneStrings.keySet()));
}
示例8
@Test
public void getSpringCloudVersionTest() throws Exception {
RestTemplate rest = mock(RestTemplate.class);
Github github = mock(Github.class);
GithubPomReader githubPomReader = mock(GithubPomReader.class);
Map<String, String> springCloudVersions = generateSpringCloudData();
Map<String, Map<String, String>> springCloud = new HashMap<>();
springCloud.put("spring-cloud", springCloudVersions);
Map<String, Map<String, Map<String, String>>> info = new HashMap<>();
info.put("bom-ranges", springCloud);
when(rest.getForObject(anyString(), eq(Map.class))).thenReturn(info);
InitializrSpringCloudInfoService service = new InitializrSpringCloudInfoService(
rest, github, githubPomReader);
String version = service.getSpringCloudVersion("2.1.0.RELEASE").getVersion();
assertThat(version, Matchers.equalTo("Greenwich.SR1"));
version = service.getSpringCloudVersion("2.1.4.RELEASE").getVersion();
assertThat(version, Matchers.equalTo("Greenwich.SR1"));
version = service.getSpringCloudVersion("2.1.5.RELEASE").getVersion();
assertThat(version, Matchers.equalTo("Greenwich.BUILD-SNAPSHOT"));
version = service.getSpringCloudVersion("1.5.5.RELEASE").getVersion();
assertThat(version, Matchers.equalTo("Edgware.SR5"));
version = service.getSpringCloudVersion("1.5.21.BUILD-SNAPSHOT").getVersion();
assertThat(version, Matchers.equalTo("Edgware.BUILD-SNAPSHOT"));
}
示例9
@Override
public void perform(Command command, Logger logger) throws IOException {
final String author = command.authorLogin();
final Github github = command.issue().repo().github();
final Request follow = github.entry()
.uri().path("/user/following/").path(author).back()
.method("PUT");
logger.info("Following Github user " + author + " ...");
try {
final int status = follow.fetch().status();
if(status != HttpURLConnection.HTTP_NO_CONTENT) {
logger.error("User follow status response is " + status + " . Should have been 204 (NO CONTENT)");
} else {
logger.info("Followed user " + author + " .");
}
} catch (final IOException ex) {//don't rethrow, this is just a cosmetic step, not critical.
logger.error("IOException while trying to follow the user.");
}
this.next().perform(command, logger);
}
示例10
@Test
public void getsAuthorEmail() throws Exception {
MkStorage storage = new MkStorage.Synced(new MkStorage.InFile());
MkGithub authorGh = new MkGithub(storage, "amihaiemil");
authorGh.users().self().emails().add(Arrays.asList("[email protected]"));
Repo authorRepo = authorGh.randomRepo();
Comment com = authorRepo.issues().create("", "").comments().post("@charlesmike do something");
Github agentGh = new MkGithub(storage, "charlesmike");
Issue issue = agentGh.repos().get(authorRepo.coordinates()).issues().get(com.issue().number());
Command comm = Mockito.mock(Command.class);
JsonObject authorInfo = Json.createObjectBuilder().add("login", "amihaiemil").build();
JsonObject json = Json.createObjectBuilder()
.add("user", authorInfo)
.add("body", com.json().getString("body"))
.add("id", 2)
.build();
Mockito.when(comm.json()).thenReturn(json);
Mockito.when(comm.issue()).thenReturn(issue);
ValidCommand vc = new ValidCommand(comm);
assertTrue(vc.authorEmail().equals("[email protected]"));
}
示例11
/**
* StarRepo can successfully star a given repository.
* @throws Exception If something goes wrong.
*/
@Test
public void starsRepo() throws Exception {
Logger logger = Mockito.mock(Logger.class);
Mockito.doNothing().when(logger).info(Mockito.anyString());
Mockito.doThrow(new IllegalStateException("Unexpected error; test failed")).when(logger).error(Mockito.anyString());
Github gh = new MkGithub("amihaiemil");
Repo repo = gh.repos().create(
new RepoCreate("amihaiemil.github.io", false)
);
Command com = Mockito.mock(Command.class);
Issue issue = Mockito.mock(Issue.class);
Mockito.when(issue.repo()).thenReturn(repo);
Mockito.when(com.issue()).thenReturn(issue);
Step sr = new StarRepo(Mockito.mock(Step.class));
assertFalse(com.issue().repo().stars().starred());
sr.perform(com, logger);
assertTrue(com.issue().repo().stars().starred());
}
示例12
/**
* StarRepo tries to star a repo twice.
* @throws Exception If something goes wrong.
*/
@Test
public void starsRepoTwice() throws Exception {
Logger logger = Mockito.mock(Logger.class);
Mockito.doNothing().when(logger).info(Mockito.anyString());
Mockito.doThrow(new IllegalStateException("Unexpected error; test failed")).when(logger).error(Mockito.anyString());
Github gh = new MkGithub("amihaiemil");
Repo repo = gh.repos().create(
new RepoCreate("amihaiemil.github.io", false)
);
Command com = Mockito.mock(Command.class);
Issue issue = Mockito.mock(Issue.class);
Mockito.when(issue.repo()).thenReturn(repo);
Mockito.when(com.issue()).thenReturn(issue);
Step sr = new StarRepo(Mockito.mock(Step.class));
assertFalse(com.issue().repo().stars().starred());
sr.perform(com, logger);
sr.perform(com, logger);
assertTrue(com.issue().repo().stars().starred());
}
示例13
/**
* Mock a Github command where the agent is mentioned.
* @return The created Command.
* @throws IOException If something goes wrong.
*/
public Command mockCommand(String msg) throws IOException {
Github gh = new MkGithub("amihaiemil");
RepoCreate repoCreate = new RepoCreate("amihaiemil.github.io", false);
gh.repos().create(repoCreate);
Issue issue = gh.repos().get(
new Coordinates.Simple("amihaiemil", "amihaiemil.github.io")
).issues().create("Test issue for commands", "test body");
Comment c = issue.comments().post(msg);
Command com = Mockito.mock(Command.class);
Mockito.when(com.json()).thenReturn(c.json());
Mockito.when(com.issue()).thenReturn(issue);
return com;
}
示例14
/**
* Agent already replied once to the last comment.
* @throws Exception if something goes wrong.
*/
@Test
public void agentRepliedAlreadyToTheLastComment() throws Exception {
final MkStorage storage = new MkStorage.Synced(new MkStorage.InFile());
final Repo repoMihai = new MkGithub(storage, "amihaiemil").repos().create( new RepoCreate("amihaiemil.github.io", false));
final Issue issue = repoMihai.issues().create("test issue", "body");
issue.comments().post("@charlesmike hello!");
final Github charlesmike = new MkGithub(storage, "charlesmike");
Issue issueCharlesmike = charlesmike.repos().get(repoMihai.coordinates()).issues().get(issue.number());
issueCharlesmike.comments().post("@amihaiemil hi there, I can help you index... ");
issue.comments().post("@someoneelse, please check that...");
LastComment lastComment = new LastComment(issueCharlesmike);
JsonObject jsonComment = lastComment.json();
JsonObject emptyMentionComment = Json.createObjectBuilder().add("id", "-1").add("body", "").build();
assertTrue(emptyMentionComment.equals(jsonComment));
}
示例15
/**
* There is more than 1 mention of the agent in the issue and it has already
* replied to others, but the last one is not replied to yet.
* @throws Exception if something goes wrong.
*/
@Test
public void agentRepliedToPreviousMention() throws Exception {
final MkStorage storage = new MkStorage.Synced(new MkStorage.InFile());
final Repo repoMihai = new MkGithub(storage, "amihaiemil").repos().create( new RepoCreate("amihaiemil.github.io", false));
final Issue issue = repoMihai.issues().create("test issue", "body");
issue.comments().post("@charlesmike hello!");//first mention
final Github charlesmike = new MkGithub(storage, "charlesmike");
Issue issueCharlesmike = charlesmike.repos().get(repoMihai.coordinates()).issues().get(issue.number());
issueCharlesmike.comments().post("@amihaiemil hi there, I can help you index... "); //first reply
Comment lastMention = issue.comments().post("@charlesmike hello again!!");//second mention
issue.comments().post("@someoneelse, please check that..."); //some other comment that is the last on the ticket.
LastComment lastComment = new LastComment(issueCharlesmike);
JsonObject jsonComment = lastComment.json();
assertTrue(lastMention.json().equals(jsonComment));
}
示例16
/**
* Mock an issue on Github.
* @return 2 Issues: 1 from the commander's Github (where the comments
* are posted) and 1 from the agent's Github (where comments are checked)
* @throws IOException If something goes wrong.
*/
private Issue[] mockIssue() throws IOException {
MkStorage storage = new MkStorage.InFile();
Github commanderGithub = new MkGithub(storage, "amihaiemil");
commanderGithub.users().self().emails().add(Arrays.asList("[email protected]"));
Github agentGithub = new MkGithub(storage, "charlesmike");
RepoCreate repoCreate = new RepoCreate("amihaiemil.github.io", false);
commanderGithub.repos().create(repoCreate);
Issue[] issues = new Issue[2];
Coordinates repoCoordinates = new Coordinates.Simple("amihaiemil", "amihaiemil.github.io");
Issue authorsIssue = commanderGithub.repos().get(repoCoordinates).issues().create("Test issue for commands", "test body");
Issue agentsIssue = agentGithub.repos().get(repoCoordinates).issues().get(authorsIssue.number());
issues[0] = authorsIssue;
issues[1] = agentsIssue;
return issues;
}
示例17
@Test
public void should_not_do_anything_for_non_release_train_version() {
Github github = BDDMockito.mock(Github.class);
CustomGithubIssues githubIssues = new SpringCloudGithubIssues(github, properties);
githubIssues
.fileIssueInSpringGuides(
new Projects(new ProjectVersion("foo", "1.0.0.BUILD-SNAPSHOT"),
new ProjectVersion("spring-cloud-build",
"2.0.0.BUILD-SNAPSHOT")),
new ProjectVersion("sc-release", "Edgware.BUILD-SNAPSHOT"));
BDDMockito.then(github).shouldHaveZeroInteractions();
}
示例18
@Test
public void should_not_do_anything_for_non_release_train_version_when_updating_startspringio()
throws IOException {
setupStartSpringIo();
Github github = BDDMockito.mock(Github.class);
CustomGithubIssues issues = new SpringCloudGithubIssues(github, properties);
issues.fileIssueInStartSpringIo(
new Projects(new ProjectVersion("foo", "1.0.0.BUILD-SNAPSHOT"),
new ProjectVersion("spring-cloud-build", "2.0.0.RELEASE")),
new ProjectVersion("sc-release", "Edgware.BUILD-SNAPSHOT"));
BDDMockito.then(github).shouldHaveZeroInteractions();
}
示例19
@Bean
Github githubClient(ReleaserProperties properties) {
if (!StringUtils.hasText(properties.getGit().getOauthToken())) {
throw new BeanInitializationException(
"You must set the value of the OAuth token. You can do it "
+ "either via the command line [--releaser.git.oauth-token=...] "
+ "or put it as an env variable in [~/.bashrc] or "
+ "[~/.zshrc] e.g. [export RELEASER_GIT_OAUTH_TOKEN=...]");
}
return new RtGithub(new RtGithub(properties.getGit().getOauthToken()).entry()
.through(RetryWire.class));
}
示例20
@Bean
BeanPostProcessor cachingGithubBeanPostProcessor() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof RtGithub) {
return new CachingGithub((Github) bean);
}
return bean;
}
};
}
示例21
@Test
void should_call_repos_only_once_for_same_github() {
Github github = mock(Github.class);
Repos repos = mock(Repos.class);
given(github.repos()).willReturn(repos);
CachingGithub cachingGithub = new CachingGithub(github);
cachingGithub.repos();
cachingGithub.repos();
cachingGithub.repos();
verify(github, only()).repos();
}
示例22
@Test
void should_not_cache_repos_calls_for_different_githubs() {
Github github1 = mock(Github.class);
Github github2 = mock(Github.class);
Github github3 = mock(Github.class);
new CachingGithub(github1).repos();
new CachingGithub(github2).repos();
new CachingGithub(github3).repos();
verify(github1).repos();
verify(github2).repos();
verify(github3).repos();
}
示例23
@Test
public void should_not_do_anything_for_non_release_train_version() {
Github github = BDDMockito.mock(Github.class);
GithubIssues issues = new GithubIssues(Collections.emptyList());
issues.fileIssueInSpringGuides(
new Projects(new ProjectVersion("foo", "1.0.0.BUILD-SNAPSHOT"),
new ProjectVersion("spring-cloud-build", "2.0.0.BUILD-SNAPSHOT")),
new ProjectVersion("sc-release", "Edgware.BUILD-SNAPSHOT"));
BDDMockito.then(github).shouldHaveZeroInteractions();
}
示例24
@Test
public void should_not_do_anything_if_not_applicable() {
Github github = BDDMockito.mock(Github.class);
GithubIssues issues = new GithubIssues(Collections.emptyList());
issues.fileIssueInSpringGuides(
new Projects(new ProjectVersion("foo", "1.0.0.RELEASE"),
new ProjectVersion("bar", "2.0.0.RELEASE"),
new ProjectVersion("baz", "3.0.0.RELEASE")),
new ProjectVersion("sc-release", "Edgware.RELEASE"));
BDDMockito.then(github).shouldHaveZeroInteractions();
}
示例25
@Test
public void should_not_do_anything_for_non_release_train_version_when_updating_startspringio()
throws IOException {
setupStartSpringIo();
Github github = BDDMockito.mock(Github.class);
GithubIssues issues = new GithubIssues(Collections.emptyList());
issues.fileIssueInStartSpringIo(
new Projects(new ProjectVersion("foo", "1.0.0.BUILD-SNAPSHOT"),
new ProjectVersion("spring-cloud-build", "2.0.0.RELEASE")),
new ProjectVersion("sc-release", "Edgware.BUILD-SNAPSHOT"));
BDDMockito.then(github).shouldHaveZeroInteractions();
}
示例26
@Test
public void should_not_do_anything_if_not_applicable_when_updating_startspringio()
throws IOException {
setupStartSpringIo();
Github github = BDDMockito.mock(Github.class);
GithubIssues issues = new GithubIssues(Collections.emptyList());
issues.fileIssueInStartSpringIo(
new Projects(new ProjectVersion("foo", "1.0.0.RELEASE"),
new ProjectVersion("bar", "2.0.0.RELEASE"),
new ProjectVersion("baz", "3.0.0.RELEASE")),
new ProjectVersion("sc-release", "Edgware.RELEASE"));
BDDMockito.then(github).shouldHaveZeroInteractions();
}
示例27
@Bean
public SpringCloudInfoService initializrSpringCloudVersionService(
SpringCloudInfoConfigurationProperties properties) {
Github github = new RtGithub(properties.getGit().getOauthToken());
RestTemplate rest = new RestTemplateBuilder().build();
return new InitializrSpringCloudInfoService(rest, github,
new GithubPomReader(new MavenXpp3Reader(), rest));
}
示例28
/**
* Handles notifications, starts one action thread for each of them.
* @param notifications List of notifications.
* @return true if actions were started successfully; false otherwise.
*/
private boolean handleNotifications(final Notifications notifications) {
String authToken = System.getProperty("github.auth.token");
if(authToken == null || authToken.isEmpty()) {
LOG.error("Missing github.auth.token. Please specify a Github api access token!");
return false;
} else {
Github gh = new RtGithub(
new RtGithub(
authToken
).entry().through(RetryWire.class)
);
try {
for(final Notification notification : notifications) {
this.actions.take(
new Action(
gh.repos().get(
new Coordinates.Simple(notification.repoFullName())
).issues().get(notification.issueNumber())
)
);
}
return true;
} catch (IOException ex) {
LOG.error("IOException while getting the Issue from Github API");
return false;
}
}
}
示例29
/**
* Mock a Github issue.
* @return The created Issue.
* @throws IOException If something goes wrong.
*/
public Issue mockIssue() throws IOException {
Github gh = new MkGithub("amihaiemil");
RepoCreate repoCreate = new RepoCreate("amihaiemil.github.io", false);
gh.repos().create(repoCreate);
return gh.repos().get(
new Coordinates.Simple("amihaiemil", "amihaiemil.github.io")
).issues().create("Test issue for commands", "test body");
}
示例30
/**
* Mock a command.
* @return The created Command.
* @throws IOException If something goes wrong.
*/
private Command mockCommand() throws IOException {
Github gh = new MkGithub("amihaiemil");
RepoCreate repoCreate = new RepoCreate("amihaiemil.github.io", false);
gh.repos().create(repoCreate);
Issue issue = gh.repos().get(
new Coordinates.Simple("amihaiemil", "amihaiemil.github.io")
).issues().create("Test issue for commands", "test body");
Command com = Mockito.mock(Command.class);
Mockito.when(com.language()).thenReturn(new English());
Mockito.when(com.authorLogin()).thenReturn("amihaiemil");
Mockito.when(com.issue()).thenReturn(issue);
Mockito.when(com.json()).thenReturn(Json.createObjectBuilder().add("body", "@charlesmike mock command").build());
return com;
}