Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Retrieving Template Repository Information for a Repo… #1817

Merged
merged 3 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/org/kohsuke/github/GHCreateRepositoryBuilder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kohsuke.github;

import java.io.IOException;
import java.util.Objects;

import static org.kohsuke.github.internal.Previews.BAPTISTE;

Expand Down Expand Up @@ -131,6 +132,20 @@ public GHCreateRepositoryBuilder fromTemplateRepository(String templateOwner, St
return this;
}

/**
* Create repository from template repository.
*
* @param templateRepository
* the template repository as a GHRepository
* @return a builder to continue with building
* @see <a href="https://developer.github.com/v3/previews/">GitHub API Previews</a>
*/
@Preview(BAPTISTE)
public GHCreateRepositoryBuilder fromTemplateRepository(GHRepository templateRepository) {
Objects.requireNonNull(templateRepository, "templateRepository cannot be null");
jbenaventem marked this conversation as resolved.
Show resolved Hide resolved
return fromTemplateRepository(templateRepository.getOwnerName(), templateRepository.getName());
}

/**
* Creates a repository with all the parameters.
*
Expand Down
27 changes: 26 additions & 1 deletion src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
@SuppressWarnings({ "UnusedDeclaration" })
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_PUBLIC_OR_PROTECTED_FIELD", "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD" },
justification = "JSON API")
public class GHRepository extends GHObject {
public class GHRepository extends GHObject implements Cloneable {

private String nodeId, description, homepage, name, full_name;

Expand Down Expand Up @@ -120,6 +120,8 @@

private String default_branch, language;

private GHRepository template_repository;

private Map<String, GHCommit> commits = Collections.synchronizedMap(new WeakHashMap<>());

@SkipFromToString
Expand Down Expand Up @@ -948,6 +950,17 @@
return default_branch;
}

/**
* Get Repository template was the repository created from.
*
* @throws CloneNotSupportedException
* if the template repository is not cloneable
* @return the repository template
*/
public GHRepository getTemplateRepository() throws CloneNotSupportedException {
return (GHRepository) template_repository.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you clone() the repo? This looks odd to me (and I usually refrain from using clone()).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I suspect you will have to inject the root in the repository, unless it's done automatically by Jackson.

In any case, adding a test to test that you can actually execute an operation on the repository after getting it would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clone method was because I've found an spotbug error "EI_EXPOSE_REP: May expose internal representation by returning reference to mutable object", it's for that reason I implements Clonable in this object, and return an object copy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to add the following annotation to the method instead:

@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, It's another posibility, but I unknow the project security rules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I suspect you will have to inject the root in the repository, unless it's done automatically by Jackson.

In any case, adding a test to test that you can actually execute an operation on the repository after getting it would be better.

Sorry, my English is terrible, and I'm not sure about that comment.
Is the provided test sufficient?

    /**
     * Test get repository created from a template repository
     *
     * @throws Exception
     *             the exception
     */
    @Test
    public void testGetRepositoryWithTemplateRepositoryInfo() throws Exception {
        GHRepository testRepo = gitHub.getRepositoryById(repo.getId());
        assertThat(testRepo.getTemplateRepository(), notNullValue());
        assertThat(testRepo.getTemplateRepository().getOwnerName(), equalTo("octocat"));
        assertThat(testRepo.getTemplateRepository().isTemplate(), equalTo(true));
    }

}

/**
* Gets size.
*
Expand Down Expand Up @@ -1417,6 +1430,18 @@
set().private_(value);
}

/**
* Sets repository template
*
* @param value
* the GitHub repository used as a template
* @throws IOException
* the io exception
*/
public void setTemplateRepository(GHRepository value) throws IOException {
set().templateRepository(value);
}

Check warning on line 1443 in src/main/java/org/kohsuke/github/GHRepository.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kohsuke/github/GHRepository.java#L1442-L1443

Added lines #L1442 - L1443 were not covered by tests

jbenaventem marked this conversation as resolved.
Show resolved Hide resolved
/**
* Sets visibility.
*
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepositoryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@
return with("private", enabled);
}

/**
* Set template repository as GHRepository.
*
* @param repositoryTemplate
* GHRepository
* @throws IOException
* In case of any networking error or error from the server.
* @return a builder to continue with building
*
*/
public S templateRepository(GHRepository repositoryTemplate) throws IOException {
return with("template_repository", repositoryTemplate);

Check warning on line 183 in src/main/java/org/kohsuke/github/GHRepositoryBuilder.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/kohsuke/github/GHRepositoryBuilder.java#L183

Added line #L183 was not covered by tests
}

jbenaventem marked this conversation as resolved.
Show resolved Hide resolved
/**
* Sets the repository visibility.
*
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/kohsuke/github/GHContentIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ public void testGetRepository() throws Exception {
assertThat(testRepo.getName(), equalTo(repo.getName()));
}

/**
* Test get repository created from a template repository
*
* @throws Exception
* the exception
*/
@Test
public void testGetRepositoryWithTemplateRepositoryInfo() throws Exception {
GHRepository testRepo = gitHub.getRepositoryById(repo.getId());
assertThat(testRepo.getTemplateRepository(), notNullValue());
}

/**
* Test get file content.
*
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/kohsuke/github/GHOrganizationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,29 @@ public void testCreateRepositoryWithTemplate() throws IOException {

}

/**
* Test create repository with template.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void testCreateRepositoryWithTemplateAndGHRepository() throws IOException {
cleanupRepository(GITHUB_API_TEST_ORG + '/' + GITHUB_API_TEST);

GHOrganization org = gitHub.getOrganization(GITHUB_API_TEST_ORG);
GHRepository templateRepository = org.getRepository(GITHUB_API_TEMPLATE_TEST);

GHRepository repository = org.createRepository(GITHUB_API_TEST)
.fromTemplateRepository(templateRepository)
.owner(GITHUB_API_TEST_ORG)
.create();

assertThat(repository, notNullValue());
assertThat(repository.getReadme(), notNullValue());

}

/**
* Test invite user.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"login": "bitwiseman",
"id": 1958953,
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
"avatar_url": "https://avatars.githubusercontent.com/u/1958953?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/bitwiseman",
"html_url": "https://github.com/bitwiseman",
"followers_url": "https://api.github.com/users/bitwiseman/followers",
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
"repos_url": "https://api.github.com/users/bitwiseman/repos",
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
"type": "User",
"site_admin": false,
"name": "Liam Newman",
"company": "Cloudbees, Inc.",
"blog": "",
"location": "Seattle, WA, USA",
"email": "[email protected]",
"hireable": null,
"bio": null,
"twitter_username": "bitwiseman",
"public_repos": 202,
"public_gists": 8,
"followers": 179,
"following": 11,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2021-02-25T18:01:06Z",
"private_gists": 19,
"total_private_repos": 18,
"owned_private_repos": 0,
"disk_usage": 33700,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 10000
}
}
Loading