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

Extension: RepositorySizeGithubAPI to get size of a repo #316

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5018984
RepositoryAPI
rishabhBudhouliya Jul 28, 2020
b77c89e
Change import statement from GitRepoSizeEstimator to GitToolChooser
rishabhBudhouliya Jul 28, 2020
63e3e1d
Rename extension from RepositoryAPI to RepositorySizeAPI
rishabhBudhouliya Jul 28, 2020
f41e6aa
Rename extension
rishabhBudhouliya Jul 28, 2020
e3fe52b
Remove the extension class from GitSCMSource
rishabhBudhouliya Aug 10, 2020
4d067df
This class contains an extension which provides the size of a remote …
rishabhBudhouliya Aug 10, 2020
e1f6dc8
Merge branch 'master' into add-gitplugin-extension
rishabhBudhouliya Aug 10, 2020
b760cb8
Add automated test cases for the estimator class
rishabhBudhouliya Aug 10, 2020
8b4b2fd
Merge branch 'add-gitplugin-extension' of https://github.com/rishabhB…
rishabhBudhouliya Aug 10, 2020
98deda5
Remove unused GitToolChooser import
rishabhBudhouliya Aug 11, 2020
36954d1
Add an automated test case which expects isApplicableTo to fail due t…
rishabhBudhouliya Aug 11, 2020
37e2afd
remove unused imports from the test class
rishabhBudhouliya Aug 11, 2020
0eaf780
Point to local wiremock
bitwiseman Sep 1, 2020
a8ba6cc
Catch exceptions raised by GitHubRepositoryInfo incase of malformed URLs
rishabhBudhouliya Sep 5, 2020
ee7a8a1
Merge branch 'add-gitplugin-extension' of https://github.com/rishabhB…
rishabhBudhouliya Sep 5, 2020
03efd94
Merge branch 'master' into add-gitplugin-extension
rishabhBudhouliya Sep 5, 2020
5ce9fcb
Fix upper bound dependencies requirement
rishabhBudhouliya Sep 5, 2020
f752531
Merge branch 'add-gitplugin-extension' of https://github.com/rishabhB…
rishabhBudhouliya Sep 5, 2020
a8f522c
Depend on git plugin 4.4.0 and Jenkins 2.204.1
rishabhBudhouliya Sep 5, 2020
801806c
Base commit for auto formatting for open PRs
bitwiseman Mar 4, 2021
1b1d76d
Merge branch 'task/formatting-base' into add-gitplugin-extension
bitwiseman Mar 4, 2021
fe70bc1
Apply autoformatting
bitwiseman Mar 4, 2021
c40ee6e
Merge remote-tracking branch 'upstream/master' into add-gitplugin-ext…
bitwiseman Mar 4, 2021
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
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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.Item;
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 jenkins.plugins.git.GitToolChooser;
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.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.jvnet.hudson.test.JenkinsRule;
import org.kohsuke.github.GitHub;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;

@RunWith(Parameterized.class)
public class GitHubRepoSizeEstimatorTest extends GitSCMSourceBase {

public GitHubRepoSizeEstimatorTest(GitHubSCMSource source) {
this.source = source;
}

@Parameterized.Parameters(name = "{index}: revision={0}")
public static GitHubSCMSource[] revisions() {
return new GitHubSCMSource[]{
new GitHubSCMSource("cloudbeers", "yolo", null, false),
new GitHubSCMSource("", "", "https://github.com/cloudbeers/yolo", true)
};
}

@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 isApplicableToTest() throws Exception {
GitHubRepoSizeEstimator.RepositorySizeGithubAPI api = j.jenkins.getExtensionList(GitToolChooser.RepositorySizeAPI.class).get(GitHubRepoSizeEstimator.RepositorySizeGithubAPI.class);
Item context = Mockito.mock(Item.class);
store.addCredentials(Domain.global(), createCredential(CredentialsScope.GLOBAL, "github"));
store.save();

githubApi.stubFor(
get(urlEqualTo("/"))
.willReturn(
aResponse()
.withHeader("Content-Type", "application/json; charset=utf-8")
.withBodyFile("../__files/body-(root)-XwEI7.json")));

assertThat(api.isApplicableTo("https://github.com/rishabhBudhouliya/zoom-suspender.git", context, "github"), is(false));
Copy link
Author

Choose a reason for hiding this comment

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

@bitwiseman The GitHub.checkApiUrlValidity() is reaching for https://api.github.com/ and since the credentials are not valid, it returns a bad credentials error.

To write the test cases, I would need to wiremock this and I have tried doing that looking at the test case examples provided by you but I have been unsuccessful in doing so.

}

private StandardCredentials createCredential(CredentialsScope scope, String id) {
return new UsernamePasswordCredentialsImpl(scope, id, "desc: " + id, "username", "password");
}
}