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

JENKINS-39389 Implementation of jenkins.scm.api.SCMSource #20

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Thu Feb 17 14:25:30 CST 2011
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.source=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jenkinsPlugin {
// source
gitHubUrl = 'https://github.com/jenkinsci/multiple-scms-plugin'

dependencies {
jenkinsPlugins 'org.jenkins-ci.plugins:scm-api:2.0.3@jar'
}
// the developers section is optional, and corresponds to the POM developers section
developers {
developer {
Expand Down
95 changes: 49 additions & 46 deletions src/main/java/org/jenkinsci/plugins/multiplescms/MultiSCM.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,5 @@
package org.jenkinsci.plugins.multiplescms;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Saveable;
import hudson.model.TaskListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Run;
import hudson.model.Hudson;
import hudson.scm.ChangeLogParser;
import hudson.scm.PollingResult;
import hudson.scm.PollingResult.Change;
import hudson.scm.SCMDescriptor;
import hudson.scm.SCMRevisionState;
import hudson.scm.NullSCM;
import hudson.scm.SCM;
import hudson.util.DescribableList;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -32,19 +10,43 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;

import net.sf.json.JSONObject;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.Saveable;
import hudson.model.TaskListener;
import hudson.scm.ChangeLogParser;
import hudson.scm.NullSCM;
import hudson.scm.PollingResult;
import hudson.scm.PollingResult.Change;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.scm.SCMRevisionState;
import hudson.util.DescribableList;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;

public class MultiSCM extends SCM implements Saveable {
private DescribableList<SCM,Descriptor<SCM>> scms =
new DescribableList<SCM,Descriptor<SCM>>(this);
new DescribableList<>(this);

@DataBoundConstructor
public MultiSCM(List<SCM> scmList) throws IOException {
Expand All @@ -57,16 +59,16 @@ public List<SCM> getConfiguredSCMs() {
}

@Override
public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> build,
Launcher launcher, TaskListener listener) throws IOException,
InterruptedException {
public @CheckForNull SCMRevisionState calcRevisionsFromBuild(
@Nonnull Run<?,?> build, @Nullable FilePath workspace,
@Nullable Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException {
MultiSCMRevisionState revisionStates = new MultiSCMRevisionState();

MultiSCMRevisionState revisionStates = new MultiSCMRevisionState();

for(SCM scm : scms) {
SCMRevisionState scmState = scm.calcRevisionsFromBuild(build, launcher, listener);
revisionStates.add(scm, build.getWorkspace(), build, scmState);
}
for(SCM scm : scms) {
SCMRevisionState scmState = scm.calcRevisionsFromBuild
(build, workspace, launcher, listener);
revisionStates.add(scm, workspace, build, scmState);
}

return revisionStates;
}
Expand All @@ -90,7 +92,7 @@ public void buildEnvVars(AbstractBuild<?,?> build, Map<String, String> env) {
}
catch(NullPointerException npe)
{}

}
}

Expand Down Expand Up @@ -142,7 +144,7 @@ public void checkout(Run<?, ?> build, Launcher launcher,
}
scm.checkout(build, launcher, workspace, listener, subChangeLog, workspaceRevision);

List<Action> actions = build.getActions();
List<? extends Action> actions = build.getAllActions();
for(Action a : actions) {
if(!scmActions.contains(a) && a instanceof SCMRevisionState && !(a instanceof MultiSCMRevisionState)) {
scmActions.add(a);
Expand Down Expand Up @@ -214,17 +216,17 @@ public DescriptorImpl() {
// TODO Auto-generated constructor stub
}

public List<SCMDescriptor<?>> getApplicableSCMs(AbstractProject<?, ?> project) {
public List<SCMDescriptor<?>> getApplicableSCMs(Object item) {
List<SCMDescriptor<?>> scms = new ArrayList<SCMDescriptor<?>>();

for(SCMDescriptor<?> scm : SCM._for(project)) {
// Filter MultiSCM itself from the list of choices.
// Theoretically it might work, but I see no practical reason to allow
// nested MultiSCM configurations.
if(!(scm instanceof DescriptorImpl) && !(scm instanceof NullSCM.DescriptorImpl))
scms.add(scm);
if (item instanceof Job) {
for(SCMDescriptor<?> scm : SCM._for((Job<?, ?>)item)) {
// Filter MultiSCM itself from the list of choices.
// Theoretically it might work, but I see no practical reason to allow
// nested MultiSCM configurations.
if(!(scm instanceof DescriptorImpl) && !(scm instanceof NullSCM.DescriptorImpl))
scms.add(scm);
}
}

return scms;
}

Expand Down Expand Up @@ -267,7 +269,8 @@ public SCM newInstance(StaplerRequest req, JSONObject formData)

private static void readItem(StaplerRequest req, JSONObject obj, List<SCM> dest) throws FormException {
String staplerClass = obj.getString("stapler-class");
Descriptor<SCM> d = (Descriptor<SCM>) Hudson.getInstance().getDescriptor(staplerClass);
@SuppressWarnings("unchecked")
Descriptor<SCM> d = Jenkins.getInstance().getDescriptor(staplerClass);
dest.add(d.newInstance(req, obj));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.multiplescms;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.scm.ChangeLogParser;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
Expand Down Expand Up @@ -55,15 +56,17 @@ public MultiSCMChangeLogParser(List<SCM> scms) {
private class LogSplitter extends DefaultHandler {

private final MultiSCMChangeLogSet changeLogs;
private final AbstractBuild build;
private final Run<?,?> build;
private RepositoryBrowser<?> browser;
private final File tempFile;
private String scmClass;
private StringBuffer buffer;

public LogSplitter(AbstractBuild build, String tempFilePath) {
changeLogs = new MultiSCMChangeLogSet(build);
public LogSplitter(Run<?,?> build, RepositoryBrowser<?> browser, String tempFilePath) {
changeLogs = new MultiSCMChangeLogSet(build, browser);
this.tempFile= new File(tempFilePath);
this.build = build;
this.browser = browser;
}

@Override
Expand Down Expand Up @@ -107,16 +110,16 @@ public void endElement(String uri, String localName, String qName)
/*
* Due to XSTREAM serialization scmRepositoryBrowsers may be null.
*/
RepositoryBrowser<?> browser = null;
if (scmRepositoryBrowsers != null) {
browser = scmRepositoryBrowsers.get(scmClass);
}
// RepositoryBrowser<?> browser = null;
// if (scmRepositoryBrowsers != null) {
// browser = scmRepositoryBrowsers.get(scmClass);
// }
if(parser != null) {
ChangeLogSet<? extends ChangeLogSet.Entry> cls;
if (browser != null) {
cls = parser.parse(build, browser, tempFile);
} else {
cls = parser.parse(build, tempFile);
cls = parser.parse(build, browser, tempFile);
}
changeLogs.add(scmClass, scmDisplayNames.get(scmClass), cls);

Expand All @@ -139,8 +142,7 @@ public ChangeLogSet<? extends Entry> getChangeLogSets() {
}

@Override
public ChangeLogSet<? extends Entry> parse(AbstractBuild build, File changelogFile)
throws IOException, SAXException {
public ChangeLogSet<? extends Entry> parse(Run build, RepositoryBrowser<?> browser, File changelogFile) throws IOException, SAXException {

if(scmLogParsers == null)
return ChangeLogSet.createEmpty(build);
Expand All @@ -154,7 +156,7 @@ public ChangeLogSet<? extends Entry> parse(AbstractBuild build, File changelogFi
throw new SAXException("Could not create parser", e);
}

LogSplitter splitter = new LogSplitter(build, changelogFile.getPath() + ".temp2");
LogSplitter splitter = new LogSplitter(build, browser, changelogFile.getPath() + ".temp2");
parser.parse(changelogFile, splitter);
return splitter.getChangeLogSets();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;

import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.scm.RepositoryBrowser;
import hudson.scm.ChangeLogSet;
import hudson.scm.ChangeLogSet.Entry;
Expand All @@ -17,8 +18,8 @@ public class MultiSCMChangeLogSet extends ChangeLogSet<Entry> {
private final HashMap<String, ChangeLogSetWrapper> changes;
private final Set<String> kinds;

protected MultiSCMChangeLogSet(AbstractBuild<?, ?> build) {
super(build);
protected MultiSCMChangeLogSet(Run<?, ?> build, RepositoryBrowser<?> browser) {
super(build, browser);
changes = new HashMap<String, ChangeLogSetWrapper>();
kinds = new HashSet<String>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* The MIT License
*
* Copyright (c) 2016 Martin Weber
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.multiplescms;

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

import edu.umd.cs.findbugs.annotations.NonNull;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadObserver;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;

/**
* @author weber
*
*/
class MultiSCMHeadObserver extends SCMHeadObserver {

private final List<Collector> result;
private Collector currentObserver;
private SCMSource currentSource;

/**
*
*/
public MultiSCMHeadObserver(int numSources) {
result = new ArrayList<SCMHeadObserver.Collector>(numSources);
}

/**
* Notified before a new SCM source will be observed. Must be invoked prior
* to {@link #observe}.
*
* @param newSource
*/
public void beginObserving(SCMSource newSource) {
if (newSource != currentSource) {
currentObserver = new SCMHeadObserver.Collector();
currentSource = newSource;
}
}

/**
* Notified after a SCM source has been successfully observed. Must be
* invoked after to {@link #observe}. May be not invoked, if an error
* occurred in the calling code.
*/
public void endObserving() {
result.add(currentObserver);
currentObserver = null;
currentSource = null;
}

/*
* (non-Javadoc)
*
* @see jenkins.scm.api.SCMHeadObserver#observe(jenkins.scm.api.SCMHead,
* jenkins.scm.api.SCMRevision)
*/
@Override
public void observe(SCMHead head, SCMRevision revision) {
currentObserver.observe(head, revision);
}

/**
* Returns the collected results. For each invocation sequence of
* {@link #beginObserving(SCMSource)}/{@link #endObserving()}, the returned
* list will contain a corresponding {@code Collector} object.
*
*
* @return the collected results.
*/
@NonNull
public List<Collector> result() {
return result;
}
}
Loading