From bdcec7601dcaac691acd4e1e11edaa637232dafb Mon Sep 17 00:00:00 2001 From: Jesse Glick Date: Fri, 9 Aug 2024 16:49:35 -0400 Subject: [PATCH] Handle `GitException` (#1628) * Handle `GitException` * Three test cases were actually asserting the broken behavior! --- .../java/hudson/plugins/git/GitPublisher.java | 9 +++- src/main/java/hudson/plugins/git/GitSCM.java | 49 +++++++++++-------- .../plugins/git/RevisionParameterAction.java | 4 +- .../git/extensions/impl/PreBuildMerge.java | 2 +- .../hudson/plugins/git/util/GitUtils.java | 2 +- .../plugins/git/AbstractGitSCMSource.java | 46 +++++++++++------ .../plugins/git/GitHooksConfiguration.java | 7 +-- .../jenkins/plugins/git/GitSCMBuilder.java | 16 ++++-- .../jenkins/plugins/git/GitSCMFileSystem.java | 10 +++- .../java/jenkins/plugins/git/GitStep.java | 8 ++- .../jenkins/plugins/git/GitToolChooser.java | 9 +++- .../git/CheckoutStepSnippetizerTest.java | 2 + .../git/GitChangeSetPluginHistoryTest.java | 4 +- .../hudson/plugins/git/GitChangeSetUtil.java | 2 +- .../hudson/plugins/git/GitPublisherTest.java | 43 +++++++++++----- .../hudson/plugins/git/GitSCMBrowserTest.java | 2 +- .../hudson/plugins/git/GitSCMUnitTest.java | 18 +++---- .../hudson/plugins/git/GitTagActionTest.java | 8 ++- .../java/hudson/plugins/git/TestGitRepo.java | 2 +- .../git/browser/GitRepositoryBrowserTest.java | 5 +- .../plugins/git/browser/GithubWebTest.java | 4 +- .../TFS2013GitRepositoryBrowserTest.java | 2 +- .../impl/CloneOptionNoTagsTest.java | 3 +- .../CloneOptionShallowDefaultTagsTest.java | 3 +- .../extensions/impl/SubmoduleOptionTest.java | 6 +-- .../git/util/AncestryBuildChooserTest.java | 5 +- .../git/util/DefaultBuildChooserTest.java | 2 +- .../plugins/git/AbstractGitSCMSourceTest.java | 5 +- .../jenkins/plugins/git/CliGitCommand.java | 4 +- .../plugins/git/FIPSModeSCMSourceTest.java | 4 +- .../git/GitHooksConfigurationTest.java | 9 ++-- .../plugins/git/GitSCMTelescopeTest.java | 4 +- .../AbstractGitTagMessageExtensionTest.java | 4 +- 33 files changed, 190 insertions(+), 113 deletions(-) diff --git a/src/main/java/hudson/plugins/git/GitPublisher.java b/src/main/java/hudson/plugins/git/GitPublisher.java index c0d3f6ea9b..26728dd889 100644 --- a/src/main/java/hudson/plugins/git/GitPublisher.java +++ b/src/main/java/hudson/plugins/git/GitPublisher.java @@ -153,7 +153,7 @@ protected GitClient getGitClient( EnvVars environment, AbstractBuild build, UnsupportedCommand cmd) - throws IOException, InterruptedException { + throws GitException, IOException, InterruptedException { return gitSCM.createClient(listener, environment, build, build.getWorkspace(), cmd); } @@ -190,7 +190,12 @@ public boolean perform(AbstractBuild build, UnsupportedCommand cmd = new UnsupportedCommand(); cmd.gitPublisher(true); - final GitClient git = getGitClient(gitSCM, listener, environment, build, cmd); + final GitClient git; + try { + git = getGitClient(gitSCM, listener, environment, build, cmd); + } catch (GitException x) { + throw new IOException(x); + } URIish remoteURI; diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java index 6c06095ad5..ceab12095a 100644 --- a/src/main/java/hudson/plugins/git/GitSCM.java +++ b/src/main/java/hudson/plugins/git/GitSCM.java @@ -182,7 +182,7 @@ public static List createRepoList(String url, String credentia * @param repositoryUrl git repository URL * Repository URL to clone from. */ - public GitSCM(String repositoryUrl) { + public GitSCM(String repositoryUrl) throws GitException { this( createRepoList(repositoryUrl, null), Collections.singletonList(new BranchSpec("")), @@ -197,7 +197,7 @@ public GitSCM( Collection submoduleCfg, @CheckForNull GitRepositoryBrowser browser, @CheckForNull String gitTool, - List extensions) { + List extensions) throws GitException { this(userRemoteConfigs, branches, browser, gitTool, extensions); } @@ -207,7 +207,7 @@ public GitSCM( List branches, @CheckForNull GitRepositoryBrowser browser, @CheckForNull String gitTool, - List extensions) { + List extensions) throws GitException { // moved from createBranches if (branches == null || branches.isEmpty()) { @@ -265,7 +265,7 @@ private void updateFromUserData() throws GitException { } @SuppressWarnings("deprecation") // `source` field is deprecated but required - public Object readResolve() throws IOException { + public Object readResolve() throws IOException, GitException { // Migrate data // Default unspecified to v0 @@ -491,7 +491,7 @@ public String getParamLocalBranch(Run build, TaskListener listener) throws } @Deprecated - public List getParamExpandedRepos(Run build) throws IOException, InterruptedException { + public List getParamExpandedRepos(Run build) throws GitException, IOException, InterruptedException { return getParamExpandedRepos(build, new LogTaskListener(LOGGER, Level.INFO)); } @@ -505,7 +505,7 @@ public List getParamExpandedRepos(Run build) throws IOExcept * @throws InterruptedException when interrupted * @return can be empty but never null. */ - public List getParamExpandedRepos(Run build, TaskListener listener) throws IOException, InterruptedException { + public List getParamExpandedRepos(Run build, TaskListener listener) throws GitException, IOException, InterruptedException { List expandedRepos = new ArrayList<>(); EnvVars env = build.getEnvironment(listener); @@ -523,7 +523,7 @@ public List getParamExpandedRepos(Run build, TaskListener li * @param remoteRepository Remote repository with parameters * @return remote repository with expanded parameters */ - public RemoteConfig getParamExpandedRepo(EnvVars env, RemoteConfig remoteRepository) { + public RemoteConfig getParamExpandedRepo(EnvVars env, RemoteConfig remoteRepository) throws GitException { List refSpecs = getRefSpecs(remoteRepository, env); return newRemoteConfig( getParameterString(remoteRepository.getName(), env), @@ -693,7 +693,7 @@ public PollingResult compareRemoteRevisionWith(Job project, Launcher launc public static final Pattern GIT_REF = Pattern.compile("^(refs/[^/]+)/(.+)"); - private PollingResult compareRemoteRevisionWithImpl(Job project, Launcher launcher, FilePath workspace, final @NonNull TaskListener listener) throws IOException, InterruptedException { + private PollingResult compareRemoteRevisionWithImpl(Job project, Launcher launcher, FilePath workspace, final @NonNull TaskListener listener) throws GitException, IOException, InterruptedException { // Poll for changes. Are there any unbuilt revisions that Jenkins ought to build ? listener.getLogger().println("Using strategy: " + getBuildChooser().getDisplayName()); @@ -836,7 +836,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job project, Launcher * @throws InterruptedException when interrupted */ @NonNull - public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, FilePath workspace) throws IOException, InterruptedException { + public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, FilePath workspace) throws GitException, IOException, InterruptedException { FilePath ws = workingDirectory(build.getParent(), workspace, environment, listener); /* ws will be null if the node which ran the build is offline */ if (ws != null) { @@ -859,7 +859,7 @@ public GitClient createClient(TaskListener listener, EnvVars environment, @NonNu * @throws InterruptedException when interrupted */ @NonNull - public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, FilePath workspace, UnsupportedCommand postBuildUnsupportedCommand) throws IOException, InterruptedException { + public GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, FilePath workspace, UnsupportedCommand postBuildUnsupportedCommand) throws GitException, IOException, InterruptedException { FilePath ws = workingDirectory(build.getParent(), workspace, environment, listener); /* ws will be null if the node which ran the build is offline */ if (ws != null) { @@ -870,12 +870,12 @@ public GitClient createClient(TaskListener listener, EnvVars environment, @NonNu } @NonNull - private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, Node n, FilePath ws) throws IOException, InterruptedException { + private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, Node n, FilePath ws) throws GitException, IOException, InterruptedException { return createClient(listener, environment, build, n, ws, null); } @NonNull - private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, Node n, FilePath ws, UnsupportedCommand postBuildUnsupportedCommand) throws IOException, InterruptedException { + private GitClient createClient(TaskListener listener, EnvVars environment, @NonNull Run build, Node n, FilePath ws, UnsupportedCommand postBuildUnsupportedCommand) throws GitException, IOException, InterruptedException { if (postBuildUnsupportedCommand == null) { /* UnsupportedCommand supports JGit by default */ @@ -976,7 +976,7 @@ private BuildData fixNull(BuildData bd) { private void fetchFrom(GitClient git, @CheckForNull Run run, TaskListener listener, - RemoteConfig remoteRepository) throws InterruptedException, IOException { + RemoteConfig remoteRepository) throws GitException, InterruptedException, IOException { boolean first = true; for (URIish url : remoteRepository.getURIs()) { @@ -999,7 +999,7 @@ private void fetchFrom(GitClient git, } } - private RemoteConfig newRemoteConfig(String name, String refUrl, RefSpec... refSpec) { + private RemoteConfig newRemoteConfig(String name, String refUrl, RefSpec... refSpec) throws GitException { try { Config repoConfig = new Config(); @@ -1115,7 +1115,7 @@ public EnvVars getEnvironment() { final @NonNull BuildData buildData, final EnvVars environment, final @NonNull GitClient git, - final @NonNull TaskListener listener) throws IOException, InterruptedException { + final @NonNull TaskListener listener) throws GitException, IOException, InterruptedException { PrintStream log = listener.getLogger(); Collection candidates = Collections.emptyList(); final BuildChooserContext context = new BuildChooserContextImpl(build.getParent(), build, environment); @@ -1195,7 +1195,7 @@ public EnvVars getEnvironment() { * * By the end of this method, remote refs are updated to include all the commits found in the remote servers. */ - private void retrieveChanges(Run build, GitClient git, TaskListener listener) throws IOException, InterruptedException { + private void retrieveChanges(Run build, GitClient git, TaskListener listener) throws GitException, IOException, InterruptedException { final PrintStream log = listener.getLogger(); boolean removeSecondFetch = false; @@ -1273,7 +1273,14 @@ private boolean determineSecondFetch(CloneOption option, @NonNull RemoteConfig r @Override public void checkout(Run build, Launcher launcher, FilePath workspace, TaskListener listener, File changelogFile, SCMRevisionState baseline) throws IOException, InterruptedException { + try { + _checkout(build, launcher, workspace, listener, changelogFile, baseline); + } catch (GitException x) { + throw new IOException(x); + } + } + private void _checkout(Run build, Launcher launcher, FilePath workspace, TaskListener listener, File changelogFile, SCMRevisionState baseline) throws GitException, IOException, InterruptedException { if (!ALLOW_LOCAL_CHECKOUT && !workspace.isRemote()) { abortIfSourceIsLocal(); } @@ -1359,7 +1366,7 @@ public void checkout(Run build, Launcher launcher, FilePath workspace, Tas // Needs to be after the checkout so that revToBuild is in the workspace try { printCommitMessageToLog(listener, git, revToBuild); - } catch (IOException | ArithmeticException | GitException ge) { + } catch (IOException | ArithmeticException ge) { // JENKINS-45729 reports a git exception when revToBuild cannot be found in the workspace. // JENKINS-46628 reports a git exception when revToBuild cannot be found in the workspace. // JENKINS-62710 reports a JGit arithmetic exception on an older Java 8 system. @@ -1416,7 +1423,7 @@ private static boolean isRemoteUrlValid(String remoteUrl) { } private void printCommitMessageToLog(TaskListener listener, GitClient git, final Build revToBuild) - throws IOException { + throws GitException, IOException { try { RevCommit commit = git.withRepository(new RevCommitRepositoryCallback(revToBuild)); listener.getLogger().println("Commit message: \"" + commit.getShortMessage() + "\""); @@ -1469,7 +1476,7 @@ private void printCommitMessageToLog(TaskListener listener, GitClient git, final * Information that captures what we did during the last build. We need this for changelog, * or else we won't know where to stop. */ - private void computeChangeLog(GitClient git, Revision revToBuild, TaskListener listener, BuildData previousBuildData, FilePath changelogFile, BuildChooserContext context) throws IOException, InterruptedException { + private void computeChangeLog(GitClient git, Revision revToBuild, TaskListener listener, BuildData previousBuildData, FilePath changelogFile, BuildChooserContext context) throws GitException, IOException, InterruptedException { boolean executed = false; ChangelogCommand changelog = git.changelog(); changelog.includes(revToBuild.getSha1()); @@ -1814,7 +1821,7 @@ public String getOldGitExe() { public static List createRepositoryConfigurations(String[] urls, String[] repoNames, - String[] refs) throws IOException { + String[] refs) throws GitException, IOException { List remoteRepositories; Config repoConfig = new Config(); @@ -2069,7 +2076,7 @@ public int size() { * @throws IOException on input or output error * @throws InterruptedException when interrupted */ - protected FilePath workingDirectory(Job context, FilePath workspace, EnvVars environment, TaskListener listener) throws IOException, InterruptedException { + protected FilePath workingDirectory(Job context, FilePath workspace, EnvVars environment, TaskListener listener) throws GitException, IOException, InterruptedException { // JENKINS-10880: workspace can be null if (workspace == null) { return null; diff --git a/src/main/java/hudson/plugins/git/RevisionParameterAction.java b/src/main/java/hudson/plugins/git/RevisionParameterAction.java index 7af792adec..f3b6b64791 100644 --- a/src/main/java/hudson/plugins/git/RevisionParameterAction.java +++ b/src/main/java/hudson/plugins/git/RevisionParameterAction.java @@ -87,11 +87,11 @@ public RevisionParameterAction(Revision revision, boolean combineCommits) { } @Deprecated - public Revision toRevision(IGitAPI git) throws InterruptedException { + public Revision toRevision(IGitAPI git) throws GitException, InterruptedException { return toRevision((GitClient) git); } - public Revision toRevision(GitClient git) throws InterruptedException { + public Revision toRevision(GitClient git) throws GitException, InterruptedException { if (revision != null) { return revision; } diff --git a/src/main/java/hudson/plugins/git/extensions/impl/PreBuildMerge.java b/src/main/java/hudson/plugins/git/extensions/impl/PreBuildMerge.java index 859c7dce14..da8e082fb5 100644 --- a/src/main/java/hudson/plugins/git/extensions/impl/PreBuildMerge.java +++ b/src/main/java/hudson/plugins/git/extensions/impl/PreBuildMerge.java @@ -54,7 +54,7 @@ public UserMergeOptions getOptions() { } @Override - public Revision decorateRevisionToBuild(GitSCM scm, Run build, GitClient git, TaskListener listener, Revision marked, Revision rev) throws IOException, InterruptedException { + public Revision decorateRevisionToBuild(GitSCM scm, Run build, GitClient git, TaskListener listener, Revision marked, Revision rev) throws GitException, IOException, InterruptedException { String remoteBranchRef = GitSCM.getParameterString(options.getRef(), build.getEnvironment(listener)); // if the branch we are merging is already at the commit being built, the entire merge becomes no-op diff --git a/src/main/java/hudson/plugins/git/util/GitUtils.java b/src/main/java/hudson/plugins/git/util/GitUtils.java index 1142142f63..3ee5e46bda 100644 --- a/src/main/java/hudson/plugins/git/util/GitUtils.java +++ b/src/main/java/hudson/plugins/git/util/GitUtils.java @@ -199,7 +199,7 @@ public Revision sortBranchesForRevision(Revision revision, List bran * @throws InterruptedException when interrupted */ @WithBridgeMethods(Collection.class) - public List filterTipBranches(final Collection revisions) throws InterruptedException { + public List filterTipBranches(final Collection revisions) throws GitException, InterruptedException { // If we have 3 branches that we might want to build // ----A--.---.--- B // \-----C diff --git a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java index c4f4f626a7..334f292f48 100644 --- a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java +++ b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java @@ -314,13 +314,13 @@ protected GitTool resolveGitTool(String gitTool, TaskListener listener) { } private interface Retriever { - default T run(GitClient client, String remoteName) throws IOException, InterruptedException { + default T run(GitClient client, String remoteName) throws GitException, IOException, InterruptedException { throw new AbstractMethodError("Not implemented"); } } private interface Retriever2 extends Retriever { - T run(GitClient client, String remoteName, FetchCommand fetch) throws IOException, InterruptedException; + T run(GitClient client, String remoteName, FetchCommand fetch) throws GitException, IOException, InterruptedException; } @NonNull @@ -415,6 +415,8 @@ private , R extends GitSCMSourceRequest> return ((Retriever2)retriever).run(client, remoteName, fetchCommand); } return retriever.run(client, remoteName); + } catch (GitException x) { + throw new IOException(x); } finally { cacheLock.unlock(); } @@ -438,7 +440,7 @@ protected SCMRevision retrieve(@NonNull final SCMHead head, @NonNull final TaskL //TODO write test using GitRefSCMHead return doRetrieve(new Retriever() { @Override - public SCMRevision run(GitClient client, String remoteName) throws IOException, InterruptedException { + public SCMRevision run(GitClient client, String remoteName) throws GitException, IOException, InterruptedException { if (head instanceof GitTagSCMHead) { try { ObjectId objectId = client.revParse(Constants.R_TAGS + head.getName()); @@ -608,7 +610,7 @@ public void record(@NonNull SCMHead head, SCMRevision revision, } doRetrieve(new Retriever2() { @Override - public Void run(GitClient client, String remoteName, FetchCommand fetch) throws IOException, InterruptedException { + public Void run(GitClient client, String remoteName, FetchCommand fetch) throws GitException, IOException, InterruptedException { final Map remoteReferences; if (context.wantBranches() || context.wantTags() || context.wantOtherRefs()) { listener.getLogger().println("Listing remote references..."); @@ -878,9 +880,12 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta listener.getLogger().printf("Attempting to resolve %s from remote references...%n", revision); boolean headsOnly = !context.wantOtherRefs() && context.wantBranches(); boolean tagsOnly = !context.wantOtherRefs() && context.wantTags(); - Map remoteReferences = client.getRemoteReferences( - getRemote(), null, headsOnly, tagsOnly - ); + Map remoteReferences; + try { + remoteReferences = client.getRemoteReferences(getRemote(), null, headsOnly, tagsOnly); + } catch (GitException x) { + throw new IOException(x); + } String tagName = null; Set shortNameMatches = new TreeSet<>(); String shortHashMatch = null; @@ -998,7 +1003,7 @@ protected SCMRevision retrieve(@NonNull final String revision, @NonNull final Ta final String tagRef = Constants.R_TAGS+tagName; return doRetrieve(new Retriever() { @Override - public SCMRevision run(GitClient client, String remoteName) throws IOException, + public SCMRevision run(GitClient client, String remoteName) throws GitException, IOException, InterruptedException { try (@SuppressWarnings("deprecation") // Local repo reference final Repository repository = client.getRepository(); @@ -1024,7 +1029,7 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException, return doRetrieve(new Retriever() { @Override - public SCMRevision run(GitClient client, String remoteName) throws IOException, InterruptedException { + public SCMRevision run(GitClient client, String remoteName) throws GitException, IOException, InterruptedException { ObjectId objectId; String hash; try { @@ -1106,9 +1111,12 @@ protected Set retrieveRevisions(@NonNull final TaskListener listener, @C listener.getLogger().println("Listing remote references..."); boolean headsOnly = !context.wantOtherRefs() && context.wantBranches(); boolean tagsOnly = !context.wantOtherRefs() && context.wantTags(); - Map remoteReferences = client.getRemoteReferences( - getRemote(), null, headsOnly, tagsOnly - ); + Map remoteReferences; + try { + remoteReferences = client.getRemoteReferences(getRemote(), null, headsOnly, tagsOnly); + } catch (GitException x) { + throw new IOException(x); + } for (String name : remoteReferences.keySet()) { if (context.wantBranches()) { if (name.startsWith(Constants.R_HEADS)) { @@ -1173,7 +1181,12 @@ protected List retrieveActions(@CheckForNull SCMSourceEvent event, @NonN } GitClient client = git.getClient(); client.addDefaultCredentials(getCredentials()); - Map symrefs = client.getRemoteSymbolicReferences(getRemote(), null); + Map symrefs; + try { + symrefs = client.getRemoteSymbolicReferences(getRemote(), null); + } catch (GitException x) { + throw new IOException(x); + } if (symrefs.containsKey(Constants.HEAD)) { // Hurrah! The Server is Git 1.8.5 or newer and our client has symref reporting String target = symrefs.get(Constants.HEAD); @@ -1193,7 +1206,12 @@ protected List retrieveActions(@CheckForNull SCMSourceEvent event, @NonN // the remote server is Git 1.8.4 or earlier, or that the local CLI git implementation is // older than git 2.8.0 (CentOS 6, CentOS 7, Debian 7, Debian 8, Ubuntu 14, and // Ubuntu 16) - Map remoteReferences = client.getRemoteReferences(getRemote(), null, false, false); + Map remoteReferences; + try { + remoteReferences = client.getRemoteReferences(getRemote(), null, false, false); + } catch (GitException x) { + throw new IOException(x); + } if (remoteReferences.containsKey(Constants.HEAD)) { ObjectId head = remoteReferences.get(Constants.HEAD); Set names = new TreeSet<>(); diff --git a/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java b/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java index e170e4225e..1e8c8a849b 100644 --- a/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java +++ b/src/main/java/jenkins/plugins/git/GitHooksConfiguration.java @@ -28,6 +28,7 @@ import hudson.Extension; import hudson.Functions; import hudson.model.PersistentDescriptor; +import hudson.plugins.git.GitException; import hudson.remoting.Channel; import jenkins.model.GlobalConfiguration; import jenkins.model.GlobalConfigurationCategory; @@ -85,12 +86,12 @@ public GlobalConfigurationCategory getCategory() { return GlobalConfigurationCategory.get(GlobalConfigurationCategory.Security.class); } - public static void configure(GitClient client) throws IOException, InterruptedException { + public static void configure(GitClient client) throws GitException, IOException, InterruptedException { final GitHooksConfiguration configuration = GitHooksConfiguration.get(); configure(client, configuration.isAllowedOnController(), configuration.isAllowedOnAgents()); } - public static void configure(GitClient client, final boolean allowedOnController, final boolean allowedOnAgents) throws IOException, InterruptedException { + public static void configure(GitClient client, final boolean allowedOnController, final boolean allowedOnAgents) throws GitException, IOException, InterruptedException { if (Channel.current() == null) { //Running on controller try (Repository ignored = client.getRepository()){ @@ -106,7 +107,7 @@ public static void configure(GitClient client, final boolean allowedOnController } } - public static void configure(GitClient client, final boolean allowed) throws IOException, InterruptedException { + public static void configure(GitClient client, final boolean allowed) throws GitException, IOException, InterruptedException { if (!allowed) { client.withRepository((repo, channel) -> { disable(repo); diff --git a/src/main/java/jenkins/plugins/git/GitSCMBuilder.java b/src/main/java/jenkins/plugins/git/GitSCMBuilder.java index fbfa2f8d47..886e528585 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMBuilder.java +++ b/src/main/java/jenkins/plugins/git/GitSCMBuilder.java @@ -30,6 +30,7 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.plugins.git.BranchSpec; +import hudson.plugins.git.GitException; import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitTool; import hudson.plugins.git.UserRemoteConfig; @@ -509,11 +510,16 @@ public GitSCM build() { GitRefSCMHead gitHead = (GitRefSCMHead) scmHead; withRefSpec(gitHead.getRef()); } - return new GitSCM( - asRemoteConfigs(), - Collections.singletonList(new BranchSpec(head().getName())), - browser(), gitTool(), - extensions); + try { + return new GitSCM( + asRemoteConfigs(), + Collections.singletonList(new BranchSpec(head().getName())), + browser(), gitTool(), + extensions); + } catch (GitException x) { + // TODO interface defines no checked exception + throw new IllegalStateException(x); + } } /** diff --git a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java index 7e146bc563..6d2599c184 100644 --- a/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java +++ b/src/main/java/jenkins/plugins/git/GitSCMFileSystem.java @@ -183,6 +183,8 @@ public V invoke(final FSFunction function) throws IOException, Interrupte throw new IOException("Closed"); } return client.withRepository((Repository repository, VirtualChannel virtualChannel) -> function.invoke(repository)); + } catch (GitException x) { + throw new IOException(x); } finally { cacheLock.unlock(); } @@ -219,14 +221,14 @@ public boolean changesSince(@CheckForNull SCMRevision revision, @NonNull OutputS changelog.to(out).max(GitSCM.MAX_CHANGELOG).execute(); executed = true; return !commitId.equals(fromCommitId); - } catch (GitException ge) { - throw new IOException("Unable to retrieve changes", ge); } finally { if (!executed) { changelog.abort(); } changeLogStream.close(); } + } catch (GitException ge) { + throw new IOException("Unable to retrieve changes", ge); } finally { cacheLock.unlock(); } @@ -407,6 +409,8 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull listener.getLogger().println("Done."); return new GitSCMFileSystem(client, remote, Constants.R_REMOTES + remoteName + "/" + headNameResult.headName, (AbstractGitSCMSource.SCMRevisionImpl) rev); + } catch (GitException x) { + throw new IOException(x); } finally { cacheLock.unlock(); } @@ -452,6 +456,8 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch listener.getLogger().println("Done."); return new GitSCMFileSystem(client, gitSCMSource.getRemote(), Constants.R_REMOTES+remoteName+"/"+head.getName(), (AbstractGitSCMSource.SCMRevisionImpl) rev); + } catch (GitException x) { + throw new IOException(x); } finally { cacheLock.unlock(); } diff --git a/src/main/java/jenkins/plugins/git/GitStep.java b/src/main/java/jenkins/plugins/git/GitStep.java index 67cf84c371..4b80e52b1d 100644 --- a/src/main/java/jenkins/plugins/git/GitStep.java +++ b/src/main/java/jenkins/plugins/git/GitStep.java @@ -29,6 +29,7 @@ import hudson.Util; import hudson.model.Item; import hudson.plugins.git.BranchSpec; +import hudson.plugins.git.GitException; import hudson.plugins.git.GitSCM; import hudson.plugins.git.UserRemoteConfig; import hudson.plugins.git.extensions.impl.LocalBranch; @@ -82,7 +83,12 @@ public void setCredentialsId(String credentialsId) { @Override public SCM createSCM() { - return new GitSCM(GitSCM.createRepoList(url, credentialsId), Collections.singletonList(new BranchSpec("*/" + branch)), null, null, Collections.singletonList(new LocalBranch(branch))); + try { + return new GitSCM(GitSCM.createRepoList(url, credentialsId), Collections.singletonList(new BranchSpec("*/" + branch)), null, null, Collections.singletonList(new LocalBranch(branch))); + } catch (GitException x) { + // TODO interface defines no checked exception + throw new IllegalStateException(x); + } } @Extension diff --git a/src/main/java/jenkins/plugins/git/GitToolChooser.java b/src/main/java/jenkins/plugins/git/GitToolChooser.java index faf8c043fb..9c13266265 100644 --- a/src/main/java/jenkins/plugins/git/GitToolChooser.java +++ b/src/main/java/jenkins/plugins/git/GitToolChooser.java @@ -7,6 +7,7 @@ import hudson.model.Item; import hudson.model.Node; import hudson.model.TaskListener; +import hudson.plugins.git.GitException; import hudson.plugins.git.GitTool; import hudson.plugins.git.util.GitUtils; import jenkins.model.Jenkins; @@ -102,7 +103,13 @@ private boolean decideAndUseCache(String remoteName) throws IOException, Interru if (cacheDir != null) { Git git = Git.with(TaskListener.NULL, new EnvVars(EnvVars.masterEnvVars)).in(cacheDir).using("git"); GitClient client = git.getClient(); - if (client.hasGitRepo(false)) { + boolean hasGitRepo; + try { + hasGitRepo = client.hasGitRepo(false); + } catch (GitException x) { + throw new IOException(x); + } + if (hasGitRepo) { long clientRepoSize = FileUtils.sizeOfDirectory(cacheDir) / 1024; // Conversion from Bytes to Kilo Bytes if (clientRepoSize > sizeOfRepo) { if (sizeOfRepo > 0) { diff --git a/src/test/java/hudson/plugins/git/CheckoutStepSnippetizerTest.java b/src/test/java/hudson/plugins/git/CheckoutStepSnippetizerTest.java index e854d10232..2ba8e0154b 100644 --- a/src/test/java/hudson/plugins/git/CheckoutStepSnippetizerTest.java +++ b/src/test/java/hudson/plugins/git/CheckoutStepSnippetizerTest.java @@ -75,6 +75,8 @@ public class CheckoutStepSnippetizerTest { /* Tested values common to many tests */ private final String remoteConfig = "userRemoteConfigs: [[url: '" + url + "']]"; + public CheckoutStepSnippetizerTest() throws Exception {} + @Test public void checkoutSimplest() throws Exception { tester.assertRoundTrip(checkoutStep, "checkout scmGit(" diff --git a/src/test/java/hudson/plugins/git/GitChangeSetPluginHistoryTest.java b/src/test/java/hudson/plugins/git/GitChangeSetPluginHistoryTest.java index 53bf18397a..b82197c45a 100644 --- a/src/test/java/hudson/plugins/git/GitChangeSetPluginHistoryTest.java +++ b/src/test/java/hudson/plugins/git/GitChangeSetPluginHistoryTest.java @@ -65,7 +65,7 @@ public class GitChangeSetPluginHistoryTest { "edf066f3", }; - public GitChangeSetPluginHistoryTest(GitClient git, boolean authorOrCommitter, String sha1String) throws IOException, InterruptedException { + public GitChangeSetPluginHistoryTest(GitClient git, boolean authorOrCommitter, String sha1String) throws Exception { this.sha1 = ObjectId.fromString(sha1String); StringWriter stringWriter = new StringWriter(); git.changelog().includes(sha1).max(1).to(stringWriter).execute(); @@ -95,7 +95,7 @@ private static List getNonMergeChanges() throws IOException { } @Parameterized.Parameters(name = "{2}-{1}") - public static Collection generateData() throws IOException, InterruptedException { + public static Collection generateData() throws Exception { List args = new ArrayList<>(); String[] implementations = new String[]{"git", "jgit"}; boolean[] choices = {true, false}; diff --git a/src/test/java/hudson/plugins/git/GitChangeSetUtil.java b/src/test/java/hudson/plugins/git/GitChangeSetUtil.java index d498e8b278..e72c24c4de 100644 --- a/src/test/java/hudson/plugins/git/GitChangeSetUtil.java +++ b/src/test/java/hudson/plugins/git/GitChangeSetUtil.java @@ -130,7 +130,7 @@ static void assertChangeSet(GitChangeSet changeSet) { } } - public static GitChangeSet genChangeSet(ObjectId sha1, String gitImplementation, boolean authorOrCommitter) throws IOException, InterruptedException { + public static GitChangeSet genChangeSet(ObjectId sha1, String gitImplementation, boolean authorOrCommitter) throws GitException, IOException, InterruptedException { EnvVars envVars = new EnvVars(); TaskListener listener = StreamTaskListener.fromStdout(); GitClient git = Git.with(listener, envVars).in(new FilePath(new File("."))).using(gitImplementation).getClient(); diff --git a/src/test/java/hudson/plugins/git/GitPublisherTest.java b/src/test/java/hudson/plugins/git/GitPublisherTest.java index 7b159bf130..336d1d4a1e 100644 --- a/src/test/java/hudson/plugins/git/GitPublisherTest.java +++ b/src/test/java/hudson/plugins/git/GitPublisherTest.java @@ -116,8 +116,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen return super.perform(build, launcher, listener); } finally { // until the 3rd one (which is the last one), we shouldn't create a tag - if (run.get()<3) - assertFalse(existsTag("foo")); + if (run.get() < 3) { + try { + assertFalse(existsTag("foo")); + } catch (Exception x) { + throw new AssertionError(x); + } + } } } @@ -215,8 +220,13 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListen return super.perform(build, launcher, listener); } finally { // until the 3rd one (which is the last one), we shouldn't create a tag - if (run.get()<3) - assertFalse(existsTag("foo")); + if (run.get() < 3) { + try { + assertFalse(existsTag("foo")); + } catch (Exception x) { + throw new AssertionError(x); + } + } } } @@ -880,23 +890,27 @@ protected GitClient getGitClient( AbstractBuild build, UnsupportedCommand cmd) throws IOException, InterruptedException { - GitClient gitClient = super.getGitClient(gitSCM, listener, environment, build, cmd); - gitClient.config(GitClient.ConfigLevel.LOCAL, "commit.gpgsign", "false"); - gitClient.config(GitClient.ConfigLevel.LOCAL, "tag.gpgSign", "false"); - return gitClient; + try { + GitClient gitClient = super.getGitClient(gitSCM, listener, environment, build, cmd); + gitClient.config(GitClient.ConfigLevel.LOCAL, "commit.gpgsign", "false"); + gitClient.config(GitClient.ConfigLevel.LOCAL, "tag.gpgSign", "false"); + return gitClient; + } catch (GitException x) { + throw new IOException(x); + } } } - private boolean existsTag(String tag) throws InterruptedException { + private boolean existsTag(String tag) throws Exception { return existsTagInRepo(testGitClient, tag); } - private boolean existsTagInRepo(GitClient gitClient, String tag) throws InterruptedException { + private boolean existsTagInRepo(GitClient gitClient, String tag) throws Exception { Set tags = gitClient.getTagNames("*"); return tags.contains(tag); } - private boolean containsTagMessage(String tag, String str) throws InterruptedException { + private boolean containsTagMessage(String tag, String str) throws Exception { String msg = testGitClient.getTagMessage(tag); return msg.contains(str); } @@ -927,7 +941,14 @@ class LongRunningCommit extends Builder { @Override public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException { + try { + return _perform(build, launcher, listener); + } catch (Exception x) { + throw new IOException(x); + } + } + private boolean _perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws Exception { TestGitRepo workspaceGit = new TestGitRepo("workspace", new File(build.getWorkspace().getRemote()), listener); TestGitRepo remoteGit = new TestGitRepo("remote", this.remoteGitDir, listener); diff --git a/src/test/java/hudson/plugins/git/GitSCMBrowserTest.java b/src/test/java/hudson/plugins/git/GitSCMBrowserTest.java index 044c55b2fe..08dd0da57f 100644 --- a/src/test/java/hudson/plugins/git/GitSCMBrowserTest.java +++ b/src/test/java/hudson/plugins/git/GitSCMBrowserTest.java @@ -126,7 +126,7 @@ public static Collection permuteRepositoryURL() { } @Test - public void guessedBrowser() { + public void guessedBrowser() throws Exception { GitSCM gitSCM = new GitSCM(gitURI); GitRepositoryBrowser browser = (GitRepositoryBrowser) gitSCM.guessBrowser(); if (expectedClass == null || expectedURI == null) { diff --git a/src/test/java/hudson/plugins/git/GitSCMUnitTest.java b/src/test/java/hudson/plugins/git/GitSCMUnitTest.java index c94836e3a4..96533247c7 100644 --- a/src/test/java/hudson/plugins/git/GitSCMUnitTest.java +++ b/src/test/java/hudson/plugins/git/GitSCMUnitTest.java @@ -54,7 +54,7 @@ public class GitSCMUnitTest { private final GitSCM gitSCM = new GitSCM(gitDir); private final String repoURL = "https://github.com/jenkinsci/git-plugin"; - public GitSCMUnitTest() { + public GitSCMUnitTest() throws Exception { } @Test @@ -220,7 +220,7 @@ public void testRequiresWorkspaceForPolling() { } @Test - public void testRequiresWorkspaceForPollingSingleBranch() { + public void testRequiresWorkspaceForPollingSingleBranch() throws Exception { /* Force single-branch use case */ GitSCM bigGitSCM = new GitSCM(createRepoList(repoURL, null), Collections.singletonList(new BranchSpec("master")), @@ -229,7 +229,7 @@ public void testRequiresWorkspaceForPollingSingleBranch() { } @Test - public void testRequiresWorkspaceForPollingSingleBranchWithRemoteName() { + public void testRequiresWorkspaceForPollingSingleBranchWithRemoteName() throws Exception { /* Force single-branch use case */ GitSCM bigGitSCM = new GitSCM(createRepoList(repoURL, null), Collections.singletonList(new BranchSpec("origin/master")), @@ -238,7 +238,7 @@ public void testRequiresWorkspaceForPollingSingleBranchWithRemoteName() { } @Test - public void testRequiresWorkspaceForPollingSingleBranchWithWildcardRemoteName() { + public void testRequiresWorkspaceForPollingSingleBranchWithWildcardRemoteName() throws Exception { /* Force single-branch use case */ GitSCM bigGitSCM = new GitSCM(createRepoList(repoURL, null), Collections.singletonList(new BranchSpec("*/master")), @@ -247,7 +247,7 @@ public void testRequiresWorkspaceForPollingSingleBranchWithWildcardRemoteName() } @Test - public void testRequiresWorkspaceForPollingSingleBranchWithWildcardSuffix() { + public void testRequiresWorkspaceForPollingSingleBranchWithWildcardSuffix() throws Exception { /* Force single-branch use case */ GitSCM bigGitSCM = new GitSCM(createRepoList(repoURL, null), Collections.singletonList(new BranchSpec("master*")), @@ -256,7 +256,7 @@ public void testRequiresWorkspaceForPollingSingleBranchWithWildcardSuffix() { } @Test - public void testRequiresWorkspaceForPollingMultiBranch() { + public void testRequiresWorkspaceForPollingMultiBranch() throws Exception { /* Multi-branch use case */ List branches = new ArrayList<>(); branches.add(new BranchSpec("master")); @@ -268,7 +268,7 @@ public void testRequiresWorkspaceForPollingMultiBranch() { } @Test - public void testRequiresWorkspaceForPollingEmptyBranchName() { + public void testRequiresWorkspaceForPollingEmptyBranchName() throws Exception { /* Multi-branch use case */ EnvVars env = new EnvVars(); env.put("A", ""); @@ -291,7 +291,7 @@ public void testIsDoGenerateSubmoduleConfigurations() { @Test @Deprecated - public void testIsDoGenerateSubmoduleConfigurationsTrue() { + public void testIsDoGenerateSubmoduleConfigurationsTrue() throws Exception { GitSCM bigGitSCM = new GitSCM(createRepoList(repoURL, null), Collections.singletonList(new BranchSpec("master")), null, null, Collections.emptyList()); @@ -343,7 +343,7 @@ public void testSetDoGenerateSubmoduleConfigurations() { @Issue("JENKINS-68562") @Test - public void testAbortIfSourceIsLocal() { + public void testAbortIfSourceIsLocal() throws Exception { GitSCM gitSCM = new GitSCM(createRepoList(repoURL, null), Collections.singletonList(new BranchSpec("master")), null, null, Collections.emptyList()); diff --git a/src/test/java/hudson/plugins/git/GitTagActionTest.java b/src/test/java/hudson/plugins/git/GitTagActionTest.java index 2e70a2c44d..de424fbc8f 100644 --- a/src/test/java/hudson/plugins/git/GitTagActionTest.java +++ b/src/test/java/hudson/plugins/git/GitTagActionTest.java @@ -290,8 +290,12 @@ public TestTagWorkerThread(Map tagSet, String ignoredComment) { @Override protected GitClient getGitClient(TaskListener listener, EnvVars environment, FilePath workspace) throws IOException, InterruptedException { GitClient gitClient = super.getGitClient(listener, environment, workspace); - gitClient.config(GitClient.ConfigLevel.LOCAL, "commit.gpgsign", "false"); - gitClient.config(GitClient.ConfigLevel.LOCAL, "tag.gpgSign", "false"); + try { + gitClient.config(GitClient.ConfigLevel.LOCAL, "commit.gpgsign", "false"); + gitClient.config(GitClient.ConfigLevel.LOCAL, "tag.gpgSign", "false"); + } catch (GitException x) { + throw new IOException(x); + } return gitClient; } } diff --git a/src/test/java/hudson/plugins/git/TestGitRepo.java b/src/test/java/hudson/plugins/git/TestGitRepo.java index b21275fcef..886dc34473 100644 --- a/src/test/java/hudson/plugins/git/TestGitRepo.java +++ b/src/test/java/hudson/plugins/git/TestGitRepo.java @@ -32,7 +32,7 @@ public class TestGitRepo { public final PersonIdent johnDoe = new PersonIdent("John Doe", "john@doe.com"); public final PersonIdent janeDoe = new PersonIdent("Jane Doe", "jane@doe.com"); - public TestGitRepo(String name, File tmpDir, TaskListener listener) throws IOException, InterruptedException { + public TestGitRepo(String name, File tmpDir, TaskListener listener) throws Exception { this.name = name; this.listener = listener; diff --git a/src/test/java/hudson/plugins/git/browser/GitRepositoryBrowserTest.java b/src/test/java/hudson/plugins/git/browser/GitRepositoryBrowserTest.java index 9232ef5b3d..7d33ad2554 100644 --- a/src/test/java/hudson/plugins/git/browser/GitRepositoryBrowserTest.java +++ b/src/test/java/hudson/plugins/git/browser/GitRepositoryBrowserTest.java @@ -4,6 +4,7 @@ import hudson.model.TaskListener; import hudson.plugins.git.GitChangeSet; import hudson.plugins.git.GitChangeSetUtil; +import hudson.plugins.git.GitException; import org.eclipse.jgit.lib.ObjectId; @@ -84,14 +85,14 @@ private static ObjectId getMostRecentCommit() { try { GitClient git = Git.with(TaskListener.NULL, new EnvVars()).getClient(); headCommit = git.revParse("HEAD"); - } catch (IOException | InterruptedException e) { + } catch (GitException | IOException | InterruptedException e) { headCommit = ObjectId.fromString("016407404eeda093385ba2ebe9557068b519b669"); // simple commit } return headCommit; } @Before - public void setUp() throws IOException, InterruptedException { + public void setUp() throws Exception { browser = new GitRepositoryBrowserImpl(null); changeSet = GitChangeSetUtil.genChangeSet(sha1, gitImplementation, useAuthorName); paths = changeSet.getPaths(); diff --git a/src/test/java/hudson/plugins/git/browser/GithubWebTest.java b/src/test/java/hudson/plugins/git/browser/GithubWebTest.java index 7e87701404..6824c5ccfd 100644 --- a/src/test/java/hudson/plugins/git/browser/GithubWebTest.java +++ b/src/test/java/hudson/plugins/git/browser/GithubWebTest.java @@ -136,7 +136,7 @@ private String repoUrl(String baseUrl, boolean add_git_suffix, boolean add_slash } @Test - public void testGuessBrowser() { + public void testGuessBrowser() throws Exception { assertGuessURL("https://github.com/kohsuke/msv.git", "https://github.com/kohsuke/msv/"); assertGuessURL("https://github.com/kohsuke/msv/", "https://github.com/kohsuke/msv/"); assertGuessURL("https://github.com/kohsuke/msv", "https://github.com/kohsuke/msv/"); @@ -153,7 +153,7 @@ public void testGuessBrowser() { } } - private void assertGuessURL(String repo, String web) { + private void assertGuessURL(String repo, String web) throws Exception { RepositoryBrowser guess = new GitSCM(repo).guessBrowser(); String actual = guess instanceof GithubWeb ? ((GithubWeb) guess).getRepoUrl() : null; assertEquals("For repo '" + repo + "':", web, actual); diff --git a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java index ae3806d0e9..aba12999f9 100644 --- a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java +++ b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java @@ -24,7 +24,7 @@ public class TFS2013GitRepositoryBrowserTest { private static final GitChangeSetSample sample = new GitChangeSetSample(false); @BeforeClass - public static void setUp() { + public static void setUp() throws Exception { GitSCM scm = new GitSCM( Collections.singletonList(new UserRemoteConfig(repoUrl, null, null, null)), new ArrayList<>(), diff --git a/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionNoTagsTest.java b/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionNoTagsTest.java index 675a3b0818..d659fda8da 100644 --- a/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionNoTagsTest.java +++ b/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionNoTagsTest.java @@ -3,7 +3,6 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; -import java.io.IOException; import java.util.Set; import hudson.model.Result; @@ -70,7 +69,7 @@ public void detectNoChangeAfterCreatingATag() throws Exception { assertTrue("there should no tags have been fetched from remote", allTagsInProjectWorkspace().isEmpty()); } - private Set allTagsInProjectWorkspace() throws IOException, InterruptedException { + private Set allTagsInProjectWorkspace() throws Exception { GitClient git = Git.with(listener, null).in(project.getSomeWorkspace()).getClient(); return git.getTagNames("*"); } diff --git a/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionShallowDefaultTagsTest.java b/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionShallowDefaultTagsTest.java index 4778c9e8c7..40d783a1b5 100644 --- a/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionShallowDefaultTagsTest.java +++ b/src/test/java/hudson/plugins/git/extensions/impl/CloneOptionShallowDefaultTagsTest.java @@ -8,7 +8,6 @@ import hudson.plugins.git.extensions.GitSCMExtensionTest; import hudson.plugins.git.extensions.GitSCMExtension; -import java.io.IOException; import java.util.Set; import org.jenkinsci.plugins.gitclient.Git; @@ -51,7 +50,7 @@ public void evenShallowCloningFetchesTagsByDefault() throws Exception { assertEquals("tag " + tagName + " should have been cloned from remote", 1, tagsInProjectWorkspaceWithName(tagName).size()); } - private Set tagsInProjectWorkspaceWithName(String tagPattern) throws IOException, InterruptedException { + private Set tagsInProjectWorkspaceWithName(String tagPattern) throws Exception { GitClient git = Git.with(listener, null).in(project.getSomeWorkspace()).getClient(); return git.getTagNames(tagPattern); } diff --git a/src/test/java/hudson/plugins/git/extensions/impl/SubmoduleOptionTest.java b/src/test/java/hudson/plugins/git/extensions/impl/SubmoduleOptionTest.java index dbd659b760..32042c1c5a 100644 --- a/src/test/java/hudson/plugins/git/extensions/impl/SubmoduleOptionTest.java +++ b/src/test/java/hudson/plugins/git/extensions/impl/SubmoduleOptionTest.java @@ -260,7 +260,7 @@ public void testToStringDataBoundConstructor() { @Test @Issue("JENKINS-64382") - public void testDetermineSupportForJGit() { + public void testDetermineSupportForJGit() throws Exception { /* JGit was incorrectly used when submodule option was added with no items checked. */ GitSCM scm = new GitSCM("https://github.com/jenkinsci/git-plugin"); scm.getExtensions().add(submoduleOption); @@ -271,7 +271,7 @@ public void testDetermineSupportForJGit() { @Test @Issue("JENKINS-64382") - public void testDetermineSupportForJGitRecursiveSubmodules() { + public void testDetermineSupportForJGitRecursiveSubmodules() throws Exception { /* JGit was incorrectly used when submodule option was added with only recursive submodule checked. */ GitSCM scm = new GitSCM("https://github.com/jenkinsci/git-plugin"); submoduleOption = new SubmoduleOption(DISABLE_SUBMODULES_FALSE, @@ -287,7 +287,7 @@ public void testDetermineSupportForJGitRecursiveSubmodules() { } @Test - public void testDetermineSupportForJGitThreads() { + public void testDetermineSupportForJGitThreads() throws Exception { GitSCM scm = new GitSCM("https://github.com/jenkinsci/git-plugin"); Integer threads = randomSmallNonNegativeIntegerOrNull(); submoduleOption.setThreads(threads); diff --git a/src/test/java/hudson/plugins/git/util/AncestryBuildChooserTest.java b/src/test/java/hudson/plugins/git/util/AncestryBuildChooserTest.java index 87c2c706c1..4c948eba76 100644 --- a/src/test/java/hudson/plugins/git/util/AncestryBuildChooserTest.java +++ b/src/test/java/hudson/plugins/git/util/AncestryBuildChooserTest.java @@ -18,7 +18,6 @@ import org.eclipse.jgit.api.CommitCommand; import org.eclipse.jgit.api.Git; -import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Repository; import org.jenkinsci.plugins.gitclient.GitClient; @@ -115,7 +114,7 @@ private String getLastCommitSha1(Set prevBranches) throws Exception { // Git Client implementation throws away committer date info so we have to do this manually.. // Copied from JGitAPIImpl.commit(String message) - private void commit(String message, PersonIdent author, PersonIdent committer) { + private void commit(String message, PersonIdent author, PersonIdent committer) throws Exception { try (@SuppressWarnings("deprecation") // Local repository reference Repository repo = testGitClient.getRepository()) { CommitCommand cmd = Git.wrap(repo).commit().setMessage(message); @@ -125,8 +124,6 @@ private void commit(String message, PersonIdent author, PersonIdent committer) { // cmd.setCommitter(new PersonIdent(committer,new Date())); cmd.setCommitter(committer); cmd.call(); - } catch (GitAPIException e) { - throw new GitException(e); } } diff --git a/src/test/java/hudson/plugins/git/util/DefaultBuildChooserTest.java b/src/test/java/hudson/plugins/git/util/DefaultBuildChooserTest.java index e7e42598f4..a1bbf8d65a 100644 --- a/src/test/java/hudson/plugins/git/util/DefaultBuildChooserTest.java +++ b/src/test/java/hudson/plugins/git/util/DefaultBuildChooserTest.java @@ -157,7 +157,7 @@ public void testSingleCandidateRevisionWithLocalAndRemoteRefsOnSameCommitWithRef assertThat(candidateRevisions, hasSize(1)); } - private void createRefsWithPredefinedOrderInHashSet(String ref1, String ref2) throws InterruptedException { + private void createRefsWithPredefinedOrderInHashSet(String ref1, String ref2) throws Exception { ObjectId commit1 = testGitClient.revParse("HEAD"); testGitClient.ref(ref1); testGitClient.commit("Commit"); diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index 5848928140..72c0a6f072 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -25,6 +25,7 @@ import hudson.plugins.git.extensions.impl.LocalBranch; import hudson.util.StreamTaskListener; import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -1034,7 +1035,7 @@ public void refLockEncounteredIfPruneTraitNotPresentOnNotFoundRetrieval() throws createRefLockEnvironment(listener, source); - final GitException e = assertThrows(GitException.class, () -> source.fetch("v1.2", listener, null)); + final IOException e = assertThrows(IOException.class, () -> source.fetch("v1.2", listener, null)); assertFalse(e.getMessage().contains("--prune")); } @@ -1055,7 +1056,7 @@ public void refLockEncounteredIfPruneTraitNotPresentOnTagRetrieval() throws Exce createRefLockEnvironment(listener, source); - final GitException e = assertThrows(GitException.class, () -> source.fetch("v1.2", listener, null)); + final IOException e = assertThrows(IOException.class, () -> source.fetch("v1.2", listener, null)); assertFalse(e.getMessage().contains("--prune")); } diff --git a/src/test/java/jenkins/plugins/git/CliGitCommand.java b/src/test/java/jenkins/plugins/git/CliGitCommand.java index 7406b92c6b..f22e91718b 100644 --- a/src/test/java/jenkins/plugins/git/CliGitCommand.java +++ b/src/test/java/jenkins/plugins/git/CliGitCommand.java @@ -60,11 +60,11 @@ public class CliGitCommand { private String[] output; private ArgumentListBuilder args; - public CliGitCommand(GitClient client, String... arguments) { + public CliGitCommand(GitClient client, String... arguments) throws GitException { this(client, GitUtilsTest.getConfigNoSystemEnvsVars(), arguments); } - public CliGitCommand(GitClient client, EnvVars envVars, String... arguments) { + public CliGitCommand(GitClient client, EnvVars envVars, String... arguments) throws GitException { args = new ArgumentListBuilder("git"); args.add(arguments); listener = StreamTaskListener.NULL; diff --git a/src/test/java/jenkins/plugins/git/FIPSModeSCMSourceTest.java b/src/test/java/jenkins/plugins/git/FIPSModeSCMSourceTest.java index 2d8b38f348..8855101f3a 100644 --- a/src/test/java/jenkins/plugins/git/FIPSModeSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/FIPSModeSCMSourceTest.java @@ -2,8 +2,8 @@ import hudson.logging.LogRecorder; import hudson.model.TaskListener; -import hudson.plugins.git.GitException; import hudson.util.StreamTaskListener; +import java.io.IOException; import org.junit.Rule; import org.junit.Test; import org.jvnet.hudson.test.RealJenkinsRule; @@ -29,7 +29,7 @@ public void remotesAreNotFetchedTest() throws Throwable { rule.then( r -> { GitSCMSource source = new GitSCMSource("http://insecure-repo"); TaskListener listener = StreamTaskListener.fromStderr(); - assertThrows("expected exception as repo doesn't exist", GitException.class, () -> source.fetch(listener)); + assertThrows("expected exception as repo doesn't exist", IOException.class, () -> source.fetch(listener)); LogRecorder logRecorder = new LogRecorder(AbstractGitSCMSource.class.getName()); LogRecorder.Target target = new LogRecorder.Target(AbstractGitSCMSource.class.getName(), Level.SEVERE); diff --git a/src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java b/src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java index d7bea205aa..65cb5f6a7b 100644 --- a/src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java +++ b/src/test/java/jenkins/plugins/git/GitHooksConfigurationTest.java @@ -26,7 +26,6 @@ import hudson.EnvVars; import hudson.model.TaskListener; import java.io.File; -import java.io.IOException; import java.util.Random; import org.eclipse.jgit.lib.StoredConfig; import org.jenkinsci.plugins.gitclient.Git; @@ -58,14 +57,14 @@ public GitHooksConfigurationTest() { } @Before - public void setUp() throws IOException, InterruptedException { + public void setUp() throws Exception { configuration = GitHooksConfiguration.get(); Git git = Git.with(TaskListener.NULL, new EnvVars()); client = git.getClient(); } @After - public void resetHooksPath() throws IOException, InterruptedException { + public void resetHooksPath() throws Exception { client.withRepository((repo, channel) -> { final StoredConfig repoConfig = repo.getConfig(); repoConfig.unset("core", null, "hooksPath"); @@ -118,7 +117,7 @@ public void testGetCategory() { assertThat(GitHooksConfiguration.get().getCategory(), is(configuration.getCategory())); } - private void setCoreHooksPath(String hooksPath) throws IOException, InterruptedException { + private void setCoreHooksPath(String hooksPath) throws Exception { /* Configure a core.hook with path `hooksPath` */ client.withRepository((repo, channel) -> { final StoredConfig repoConfig = repo.getConfig(); @@ -128,7 +127,7 @@ private void setCoreHooksPath(String hooksPath) throws IOException, InterruptedE }); } - private String getCoreHooksPath() throws IOException, InterruptedException { + private String getCoreHooksPath() throws Exception { String hooksPath = client.withRepository((repo, channel) -> { final StoredConfig repoConfig = repo.getConfig(); return repoConfig.getString("core", null, "hooksPath"); diff --git a/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java b/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java index 0f60c015fa..c2da45a352 100644 --- a/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java +++ b/src/test/java/jenkins/plugins/git/GitSCMTelescopeTest.java @@ -95,7 +95,7 @@ public void createTelescopeForRemote() { } @Test - public void testOf_GitSCM() { + public void testOf_GitSCM() throws Exception { /* Testing GitSCMTelescope.of() for non null return needs JenkinsRule */ GitSCM multiBranchSource = new GitSCM(remote); GitSCMTelescope telescopeOfMultiBranchSource = GitSCMTelescope.of(multiBranchSource); @@ -133,7 +133,7 @@ public void testValidate() throws Exception { * @param repoUrl URL to the repository for the returned GitSCM * @return GitSCM with a single branch in its definition */ - private GitSCM getSingleBranchSource(String repoUrl) { + private GitSCM getSingleBranchSource(String repoUrl) throws Exception { UserRemoteConfig remoteConfig = new UserRemoteConfig( repoUrl, "origin", diff --git a/src/test/java/org/jenkinsci/plugins/gittagmessage/AbstractGitTagMessageExtensionTest.java b/src/test/java/org/jenkinsci/plugins/gittagmessage/AbstractGitTagMessageExtensionTest.java index 11caef6f5a..f84dcbba05 100644 --- a/src/test/java/org/jenkinsci/plugins/gittagmessage/AbstractGitTagMessageExtensionTest.java +++ b/src/test/java/org/jenkinsci/plugins/gittagmessage/AbstractGitTagMessageExtensionTest.java @@ -7,7 +7,6 @@ import hudson.plugins.git.util.BuildData; import hudson.plugins.git.util.GitUtilsTest; import jenkins.model.ParameterizedJobMixIn; -import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.util.SystemReader; import org.jenkinsci.plugins.gitclient.Git; import org.jenkinsci.plugins.gitclient.GitClient; @@ -18,7 +17,6 @@ import org.junit.rules.TemporaryFolder; import org.jvnet.hudson.test.JenkinsRule; -import java.io.IOException; import static org.junit.Assert.assertNotNull; @@ -47,7 +45,7 @@ private J configureGitTagMessageJob() throws Exception { protected abstract void assertBuildEnvironment(R run, String expectedName, String expectedMessage) throws Exception; @Before - public void setUp() throws IOException, InterruptedException, ConfigInvalidException { + public void setUp() throws Exception { SystemReader.getInstance().getUserConfig().clear(); // Set up a temporary git repository for each test case repo = Git.with(r.createTaskListener(), GitUtilsTest.getConfigNoSystemEnvsVars()).in(repoDir.getRoot()).getClient();