Java源码示例:com.jcabi.github.Repo

示例1
private void fileAGithubIssue(String user, String repo, String issueTitle,
		String issueText) {
	Repo ghRepo = this.github.repos().get(new Coordinates.Simple(user, repo));
	// check if the issue is not already there
	boolean issueAlreadyFiled = issueAlreadyFiled(ghRepo, issueTitle);
	if (issueAlreadyFiled) {
		log.info("Issue already filed, will not do that again");
		return;
	}
	try {
		int number = ghRepo.issues().create(issueTitle, issueText).number();
		log.info(
				"Successfully created an issue with "
						+ "title [{}] for the [{}/{}] GitHub repository" + number,
				issueTitle, user, repo);
	}
	catch (IOException e) {
		log.error("Exception occurred while trying to create the issue in guides", e);
	}
}
 
示例2
private boolean issueAlreadyFiled(Repo springGuides, String issueTitle) {
	Map<String, String> map = new HashMap<>();
	map.put("state", "open");
	int counter = 0;
	int maxIssues = 10;
	for (Issue issue : springGuides.issues().iterate(map)) {
		if (counter >= maxIssues) {
			return false;
		}
		Issue.Smart smartIssue = new Issue.Smart(issue);
		try {
			if (issueTitle.equals(smartIssue.title())) {
				return true;
			}
		}
		catch (IOException e) {
			return false;
		}
		counter = counter + 1;
	}
	return false;
}
 
示例3
@Test
void should_not_cache_calls_for_different_coordinates_for_repo() {
	Repo repo1 = mock(Repo.class);
	given(repo1.coordinates()).willReturn(new Coordinates.Simple("foo", "bar"));
	Repo repo2 = mock(Repo.class);
	given(repo2.coordinates()).willReturn(new Coordinates.Simple("foo", "bar"));
	Repo repo3 = mock(Repo.class);
	given(repo3.coordinates()).willReturn(new Coordinates.Simple("foo", "bar"));

	new CachingRepo(repo1).coordinates();
	new CachingRepo(repo2).coordinates();
	new CachingRepo(repo3).coordinates();

	verify(repo1).coordinates();
	verify(repo2).coordinates();
	verify(repo3).coordinates();
}
 
示例4
@Test
void should_not_cache_calls_for_different_issues_for_repo() {
	Repo repo1 = mock(Repo.class);
	given(repo1.issues()).willReturn(BDDMockito.mock(Issues.class));
	Repo repo2 = mock(Repo.class);
	given(repo2.issues()).willReturn(BDDMockito.mock(Issues.class));
	Repo repo3 = mock(Repo.class);
	given(repo3.issues()).willReturn(BDDMockito.mock(Issues.class));

	new CachingRepo(repo1).issues();
	new CachingRepo(repo2).issues();
	new CachingRepo(repo3).issues();

	verify(repo1).issues();
	verify(repo2).issues();
	verify(repo3).issues();
}
 
示例5
@Test
void should_not_cache_calls_for_different_milestones_for_repo() {
	Repo repo1 = mock(Repo.class);
	given(repo1.milestones()).willReturn(BDDMockito.mock(Milestones.class));
	Repo repo2 = mock(Repo.class);
	given(repo2.milestones()).willReturn(BDDMockito.mock(Milestones.class));
	Repo repo3 = mock(Repo.class);
	given(repo3.milestones()).willReturn(BDDMockito.mock(Milestones.class));

	new CachingRepo(repo1).milestones();
	new CachingRepo(repo2).milestones();
	new CachingRepo(repo3).milestones();

	verify(repo1).milestones();
	verify(repo2).milestones();
	verify(repo3).milestones();
}
 
示例6
@Test
void should_not_cache_calls_for_different_releases_for_repo() {
	Repo repo1 = mock(Repo.class);
	given(repo1.releases()).willReturn(BDDMockito.mock(Releases.class));
	Repo repo2 = mock(Repo.class);
	given(repo2.releases()).willReturn(BDDMockito.mock(Releases.class));
	Repo repo3 = mock(Repo.class);
	given(repo3.releases()).willReturn(BDDMockito.mock(Releases.class));

	new CachingRepo(repo1).releases();
	new CachingRepo(repo2).releases();
	new CachingRepo(repo3).releases();

	verify(repo1).releases();
	verify(repo2).releases();
	verify(repo3).releases();
}
 
示例7
@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")));
}
 
示例8
@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()));

}
 
示例9
@Override
public void perform(Command command, Logger logger) throws IOException {
    try {
        logger.info("Starring repository...");
        Repo repo = command.issue().repo();
        if(!repo.stars().starred()) {
            repo.stars().star();
        }
        logger.info("Repository starred!");
    } catch (IOException e) {
        logger.error("Error when starring repository: " + e.getMessage(), e);
        //We do not rethrow it here since starring the repo is not
        //a critical matter
    }
    this.next().perform(command, logger);
}
 
示例10
/**
 * CommandedRepo can tell when the repo has a gh-pages branch.
 * @throws Exception If something goes wrong.
 */
@Test
public void repoHasGhPagesBranch() throws Exception {
    int port = this.port();
    MkContainer server = new MkGrizzlyContainer()
        .next(new MkAnswer.Simple(HttpURLConnection.HTTP_OK))
        .start(port);
    try {
        CachedRepo crepo = Mockito.spy(
            new CachedRepo(Mockito.mock(Repo.class))
        );
        Mockito.when(crepo.json()).thenReturn(Json
            .createObjectBuilder()
            .add(
                "branches_url",
                "http://localhost:" + port + "/branches{/branch}"
            ).build()
        );
        assertTrue(
            "Expected a gh-pages branch!",
            crepo.hasGhPagesBranch()
        );
    } finally {
        server.stop();
    }
}
 
示例11
/**
 * CommandedRepo can tell when the repo doesn't
 * have a gh-pages branch.
 * @throws Exception If something goes wrong.
 */
@Test
public void repoHasNoGhPagesBranch() throws Exception {
    int port = this.port();
    MkContainer server = new MkGrizzlyContainer()
        .next(new MkAnswer.Simple(HttpURLConnection.HTTP_NOT_FOUND))
        .start(port);
    try {
        CachedRepo crepo = Mockito.spy(
            new CachedRepo(Mockito.mock(Repo.class))
        );
        Mockito.when(crepo.json()).thenReturn(Json
            .createObjectBuilder()
            .add(
                "branches_url",
                "http://localhost:" + port + "/branches{/branch}"
            ).build()
        );
        assertFalse(
            "Unexpected gh-pages branch!",
            crepo.hasGhPagesBranch()
        );
    } finally {
        server.stop();
    }
}
 
示例12
@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]"));
}
 
示例13
/**
 * 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());
}
 
示例14
/**
 * 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());
}
 
示例15
/**
 * StarRepo did not star the repository due to an IOException. 
 * StarRepo.perform() should return true anyway because it's not a critical operation and we
 * shouldn't fail the whole process just because of this.
 * 
 * This test expects an RuntimeException (we mock the logger in such a way) because it's the easiest way
 * to find out if the flow entered the catch block.
 * @throws IOException If something goes wrong.
 */
@Test(expected = RuntimeException.class)
public void repoStarringFails() throws IOException {
    Logger logger = Mockito.mock(Logger.class);
    Mockito.doNothing().when(logger).info(Mockito.anyString());
    Mockito.doThrow(new RuntimeException("Excpected excetion; all is ok!")).when(logger).error(
        Mockito.anyString(), Mockito.any(IOException.class)
    );
    
    Repo repo = Mockito.mock(Repo.class);
    Stars stars = Mockito.mock(Stars.class);
    Mockito.when(stars.starred()).thenReturn(false);
    Mockito.doThrow(new IOException()).when(stars).star();
    Mockito.when(repo.stars()).thenReturn(stars);
    Command com = Mockito.mock(Command.class);
    Issue issue = Mockito.mock(Issue.class);
    Mockito.when(issue.repo()).thenReturn(repo);
    Mockito.when(com.issue()).thenReturn(issue);
    
    StarRepo sr = new StarRepo(Mockito.mock(Step.class));
    sr.perform(com, logger);
}
 
示例16
/**
 * 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)); 
}
 
示例17
/**
 * 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)); 
}
 
示例18
/**
 * Extract at-mentions of contributors, given a list of commits (will fetch
 * contributor login from github). The list is deduplicated and sorted in
 * case-insensitive alphabetical order.
 */
List<String> extractContributorMentions(Repo repo, List<SimpleCommit> revCommits) {
	RepoCommits commitsClient = repo.commits();
	return revCommits.stream().map(c -> commitToGithubMention(commitsClient, c))
			.filter(Objects::nonNull).distinct().sorted(String.CASE_INSENSITIVE_ORDER)
			.collect(Collectors.toList());
}
 
示例19
@Test
void should_coordinates_only_once_for_same_repo() {
	Repo repo = mock(Repo.class);
	given(repo.coordinates()).willReturn(new Coordinates.Simple("foo", "bar"));
	CachingRepo cachingRepo = new CachingRepo(repo);

	cachingRepo.coordinates();
	cachingRepo.coordinates();
	cachingRepo.coordinates();

	verify(repo, only()).coordinates();
}
 
示例20
@Test
void should_issues_only_once_for_same_repo() {
	Repo repo = mock(Repo.class);
	given(repo.issues()).willReturn(BDDMockito.mock(Issues.class));
	CachingRepo cachingRepo = new CachingRepo(repo);

	cachingRepo.issues();
	cachingRepo.issues();
	cachingRepo.issues();

	verify(repo, only()).issues();
}
 
示例21
@Test
void should_call_milestones_only_once_for_same_repo() {
	Repo repo = mock(Repo.class);
	given(repo.milestones()).willReturn(BDDMockito.mock(Milestones.class));
	CachingRepo cachingRepo = new CachingRepo(repo);

	cachingRepo.milestones();
	cachingRepo.milestones();
	cachingRepo.milestones();

	verify(repo, only()).milestones();
}
 
示例22
@Test
void should_call_releases_only_once_for_same_repo() {
	Repo repo = mock(Repo.class);
	given(repo.releases()).willReturn(BDDMockito.mock(Releases.class));
	CachingRepo cachingRepo = new CachingRepo(repo);

	cachingRepo.releases();
	cachingRepo.releases();
	cachingRepo.releases();

	verify(repo, only()).releases();
}
 
示例23
/**
 * Mock a command, add issue, repo and github mocks into it.
 * @return Command.
 */
private Command mockCommand() {
    final Command com = Mockito.mock(Command.class);
    Mockito.when(com.authorLogin()).thenReturn("amihaiemil");
    
    final Issue issue = Mockito.mock(Issue.class);
    final Repo repo = Mockito.mock(Repo.class);
    Mockito.when(repo.github()).thenReturn(Mockito.mock(Github.class));
    Mockito.when(issue.repo()).thenReturn(repo);
    
    Mockito.when(com.issue()).thenReturn(issue);
    
    return com;
}
 
示例24
/**
 * CommandedRepo throws IOException if the http response
 * status is not appropriate.
 * @throws Exception If something goes wrong.
 */
@Test
public void unexpectedHttpStatusFromBranchesAPI() throws Exception {
    int port = this.port();
    MkContainer server = new MkGrizzlyContainer()
        .next(new MkAnswer.Simple(HttpURLConnection.HTTP_BAD_REQUEST))
        .start(port);
    try {
        CachedRepo crepo = Mockito.spy(
            new CachedRepo(Mockito.mock(Repo.class))
        );
        Mockito.when(crepo.json()).thenReturn(Json
            .createObjectBuilder()
            .add(
                "branches_url",
                "http://localhost:" + port + "/branches{/branch}"
            ).build()
        );
        crepo.hasGhPagesBranch();
        fail("Expected an IOException here");
    } catch (IOException ex) {
        assertTrue(
            ex.getMessage()
                .equals("Unexpected HTTP status response.")
        );
    } finally {
        server.stop();
    }
}
 
示例25
/**
 * CommandedRepo can represent itself in json format.
 * @throws Exception If something goes wrong.
 */
@Test
public void getsJson() throws Exception {
    MkGithub gh = new MkGithub("amihaiemil");
    Repo rep = gh.repos().create(new RepoCreate("charlesrepo", false));
    CachedRepo crepo = new CachedRepo(rep);
    JsonObject repoJson = crepo.json();
    assertTrue(crepo.name().equals("charlesrepo"));
    assertTrue(repoJson.getString("private").equals("false"));

    JsonObject repoFromCache = crepo.json();
    assertTrue(repoJson == repoFromCache);
}
 
示例26
/**
 * CachedRepo can return the .charles.yml which is in the repo.
 * @throws Exception If something goes wrong.
 */
@Test
public void getsExistingCharlesYml() throws Exception {
    final MkGithub gh = new MkGithub("amihaiemil");
    final Repo repo = gh.repos().create(new RepoCreate("charlesrepo", false));
    repo.contents()
        .create(
            Json.createObjectBuilder()
                .add("path", ".charles.yml")
                .add("message", "just a test")
                .add(
                    "content",
                    Base64.encodeBase64String(
                        Yaml.createYamlMappingBuilder()
                            .add("tweet", "true")
                            .add(
                                "commanders",
                                Yaml.createYamlSequenceBuilder()
                                    .add("johndoe")
                                    .add("amihaiemil")
                                    .add("queeney")
                                    .build()
                            ).build().toString().getBytes()
                    )
                ).build()
        );
    final CharlesYml yml = new CachedRepo(repo).charlesYml();
    MatcherAssert.assertThat(yml.commanders(), Matchers.hasSize(3));
    MatcherAssert.assertThat(yml.commanders().get(0), Matchers.equalTo("amihaiemil"));//YAML orders them alphabetically
    MatcherAssert.assertThat(yml.commanders().get(1), Matchers.equalTo("johndoe"));
    MatcherAssert.assertThat(yml.commanders().get(2), Matchers.equalTo("queeney"));
    MatcherAssert.assertThat(yml.tweet(), Matchers.is(true));
}
 
示例27
/**
 * CachedRepo can return a default ChalesYml if the repo does not have it.
 * @throws Exception If something goes worng.
 */
@Test
public void getsDefaultCharlesYml() throws Exception {
    MkGithub gh = new MkGithub("amihaiemil");
    Repo rep = gh.repos().create(new RepoCreate("charlesrepo", false));
    CharlesYml yml = new CachedRepo(rep).charlesYml();
    MatcherAssert.assertThat(yml.commanders(), Matchers.emptyIterable());
    MatcherAssert.assertThat(yml.tweet(), Matchers.is(false));
}
 
示例28
@NotNull
public GithubFacade build(Configuration configuration) {

    Patchset patchset = PatchsetBuilder.build(configuration);

    String oAuthKey = configuration.getProperty(GeneralOption.GITHUB_API_KEY);
    Github github = new RtGithub(
            new RtGithub(oAuthKey)
                    .entry()
                    .through(RetryWire.class)
    );

    Repo repo = github.repos().get(new Coordinates.Simple(patchset.getProjectPath()));
    return new GithubFacade(repo, patchset);
}
 
示例29
private String createIssueLink(Optional<Integer> issueId) {
    final Repo repo = pull.repo();
    if (issueId.isPresent()) {
        return String.format("https://github.com/%s/%s/issues/%d",
                repo.coordinates().user(), repo.coordinates().repo(), issueId.get());
    } else {
        return String.format("https://github.com/%s/%s/issues",
                repo.coordinates().user(), repo.coordinates().repo());
    }
}
 
示例30
private Repo createGettingStartedGuides(MkGithub github) throws IOException {
	return github.repos()
			.create(new Repos.RepoCreate("getting-started-guides", false));
}