-
Notifications
You must be signed in to change notification settings - Fork 370
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
Extension: RepositorySizeGithubAPI to get size of a repo #316
Open
rishabhBudhouliya
wants to merge
23
commits into
jenkinsci:master
Choose a base branch
from
rishabhBudhouliya:add-gitplugin-extension
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
5018984
RepositoryAPI
rishabhBudhouliya b77c89e
Change import statement from GitRepoSizeEstimator to GitToolChooser
rishabhBudhouliya 63e3e1d
Rename extension from RepositoryAPI to RepositorySizeAPI
rishabhBudhouliya f41e6aa
Rename extension
rishabhBudhouliya e3fe52b
Remove the extension class from GitSCMSource
rishabhBudhouliya 4d067df
This class contains an extension which provides the size of a remote …
rishabhBudhouliya e1f6dc8
Merge branch 'master' into add-gitplugin-extension
rishabhBudhouliya b760cb8
Add automated test cases for the estimator class
rishabhBudhouliya 8b4b2fd
Merge branch 'add-gitplugin-extension' of https://github.com/rishabhB…
rishabhBudhouliya 98deda5
Remove unused GitToolChooser import
rishabhBudhouliya 36954d1
Add an automated test case which expects isApplicableTo to fail due t…
rishabhBudhouliya 37e2afd
remove unused imports from the test class
rishabhBudhouliya 0eaf780
Point to local wiremock
bitwiseman a8ba6cc
Catch exceptions raised by GitHubRepositoryInfo incase of malformed URLs
rishabhBudhouliya ee7a8a1
Merge branch 'add-gitplugin-extension' of https://github.com/rishabhB…
rishabhBudhouliya 03efd94
Merge branch 'master' into add-gitplugin-extension
rishabhBudhouliya 5ce9fcb
Fix upper bound dependencies requirement
rishabhBudhouliya f752531
Merge branch 'add-gitplugin-extension' of https://github.com/rishabhB…
rishabhBudhouliya a8f522c
Depend on git plugin 4.4.0 and Jenkins 2.204.1
rishabhBudhouliya 801806c
Base commit for auto formatting for open PRs
bitwiseman 1b1d76d
Merge branch 'task/formatting-base' into add-gitplugin-extension
bitwiseman fe70bc1
Apply autoformatting
bitwiseman c40ee6e
Merge remote-tracking branch 'upstream/master' into add-gitplugin-ext…
bitwiseman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubRepoSizeEstimator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.jenkinsci.plugins.github_branch_source; | ||
|
||
import com.cloudbees.plugins.credentials.common.StandardCredentials; | ||
import hudson.Extension; | ||
import hudson.model.Item; | ||
import org.kohsuke.github.GHRepository; | ||
import org.kohsuke.github.GitHub; | ||
import jenkins.plugins.git.GitToolChooser; | ||
|
||
import java.io.IOException; | ||
|
||
public class GitHubRepoSizeEstimator { | ||
/** | ||
* This extension intends to perform a GET request without any credentials on the provided repository URL | ||
* to return the size of repository. | ||
*/ | ||
@Extension | ||
public static class RepositorySizeGithubAPI extends GitToolChooser.RepositorySizeAPI { | ||
|
||
@Override | ||
public boolean isApplicableTo(String repoUrl, Item context, String credentialsId) { | ||
StandardCredentials credentials = Connector.lookupScanCredentials(context, repoUrl, credentialsId); | ||
GitHubRepositoryInfo info = GitHubRepositoryInfo.forRepositoryUrl(repoUrl); | ||
try { | ||
GitHub gitHub = Connector.connect(info.getApiUri(), credentials); | ||
gitHub.checkApiUrlValidity(); | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
public Long getSizeOfRepository(String repoUrl, Item context, String credentialsId) throws Exception { | ||
StandardCredentials credentials = Connector.lookupScanCredentials(context, repoUrl, credentialsId); | ||
GitHubRepositoryInfo info = GitHubRepositoryInfo.forRepositoryUrl(repoUrl); | ||
GitHub github = Connector.connect(info.getApiUri(), credentials); | ||
GHRepository ghRepository = github.getRepository(info.getRepoOwner() + '/' + info.getRepository()); | ||
return (long) ghRepository.getSize(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubRepoSizeEstimatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.jenkinsci.plugins.github_branch_source; | ||
|
||
import com.cloudbees.plugins.credentials.*; | ||
import com.cloudbees.plugins.credentials.common.StandardCredentials; | ||
import com.cloudbees.plugins.credentials.domains.Domain; | ||
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; | ||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.TopLevelItem; | ||
import hudson.plugins.git.BranchSpec; | ||
import hudson.plugins.git.GitSCM; | ||
import hudson.plugins.git.SubmoduleConfig; | ||
import hudson.plugins.git.UserRemoteConfig; | ||
import hudson.plugins.git.extensions.GitSCMExtension; | ||
import hudson.plugins.git.extensions.impl.DisableRemotePoll; | ||
import hudson.triggers.SCMTrigger; | ||
import jenkins.model.Jenkins; | ||
import org.jenkinsci.plugins.gitclient.JGitTool; | ||
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowJob; | ||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
|
||
public class GitHubRepoSizeEstimatorTest { | ||
|
||
@Rule | ||
public final JenkinsRule j = new JenkinsRule(); | ||
|
||
private CredentialsStore store = null; | ||
|
||
@Before | ||
public void enableSystemCredentialsProvider() { | ||
SystemCredentialsProvider.getInstance().setDomainCredentialsMap( | ||
Collections.singletonMap(Domain.global(), Collections.<Credentials>emptyList())); | ||
for (CredentialsStore s : CredentialsProvider.lookupStores(Jenkins.get())) { | ||
if (s.getProvider() instanceof SystemCredentialsProvider.ProviderImpl) { | ||
store = s; | ||
break; | ||
} | ||
} | ||
assertThat("The system credentials provider is enabled", store, notNullValue()); | ||
} | ||
|
||
@Test | ||
public void testExtensionApplicability() throws Exception { | ||
GitHubRepoSizeEstimator.RepositorySizeGithubAPI repositorySizeGithubAPI = new GitHubRepoSizeEstimator.RepositorySizeGithubAPI(); | ||
|
||
String url = "https://github.com/jenkinsci/github-branch-source-plugin.git"; | ||
|
||
store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "github")); | ||
store.save(); | ||
|
||
// WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); | ||
// p.setDefinition(new CpsFlowDefinition( | ||
// "node {\n" | ||
// + " checkout(\n" | ||
// + " [$class: 'GitSCM', \n" | ||
// + " userRemoteConfigs: [[credentialsId: 'github', url: $/" + url + "/$]]]\n" | ||
// + " )" | ||
// + "}", true)); | ||
// WorkflowRun b = j.assertBuildStatusSuccess(p.scheduleBuild2(0)); | ||
// j.waitForMessage("using credential github", b); | ||
List<UserRemoteConfig> repos = new ArrayList<>(); | ||
repos.add(new UserRemoteConfig(url, "origin", null, "github")); | ||
FreeStyleProject projectWithMaster = setupProject(repos, Collections.singletonList(new BranchSpec("master")), null, false); | ||
projectWithMaster.scheduleBuild2(0); | ||
|
||
List<TopLevelItem> items = j.jenkins.getItems(); | ||
|
||
assertThat(repositorySizeGithubAPI.isApplicableTo(url, items.get(0), "github"), is(true)); | ||
} | ||
|
||
private StandardCredentials createCredential(CredentialsScope scope, String id) { | ||
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "rishabhBudhouliya", "rishabh2020"); | ||
} | ||
|
||
protected FreeStyleProject setupProject(List<UserRemoteConfig> repos, List<BranchSpec> branchSpecs, | ||
String scmTriggerSpec, boolean disableRemotePoll) throws Exception { | ||
FreeStyleProject project = j.createFreeStyleProject(); | ||
GitSCM scm = new GitSCM( | ||
repos, | ||
branchSpecs, | ||
false, Collections.<SubmoduleConfig>emptyList(), | ||
null, JGitTool.MAGIC_EXENAME, | ||
Collections.<GitSCMExtension>emptyList()); | ||
if(disableRemotePoll) scm.getExtensions().add(new DisableRemotePoll()); | ||
project.setScm(scm); | ||
if(scmTriggerSpec != null) { | ||
SCMTrigger trigger = new SCMTrigger(scmTriggerSpec); | ||
project.addTrigger(trigger); | ||
trigger.start(project, true); | ||
} | ||
//project.getBuildersList().add(new CaptureEnvironmentBuilder()); | ||
project.save(); | ||
return project; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a couple of ways to do this: use Mockito to directly mock the calls to GitHub and/or use Wiremock to create a local web server that simulates. the responses from GitHub.
Example: adddd68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for providing an example,
I've been using Mockito to mock the calls to Github but I'm stuck at mocking static methods which is not possible with Mockito.
Reference:
Connector.connect
method is a static method provided to return a GitHub object.