Skip to content

Commit

Permalink
Merge pull request #16 from jenkinsci/master
Browse files Browse the repository at this point in the history
Fork Sync: Update from parent repository
  • Loading branch information
ppiorunski authored Oct 8, 2024
2 parents c4a9ef1 + 6b1b549 commit 5f25618
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.jenkinsci.plugins.p4.publish;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.model.EnvironmentContributingAction;
import hudson.model.Run;

public class P4PublishEnvironmentContributingAction implements EnvironmentContributingAction {
private final String publishedChangeId;

public P4PublishEnvironmentContributingAction(String publishedChangeID) {
this.publishedChangeId = publishedChangeID;
}

@Override
public void buildEnvironment(@NonNull Run<?, ?> run, @NonNull EnvVars env) {
env.put("P4_PUBLISH_CHANGELIST", publishedChangeId);
}

@Override
public String getIconFileName() {
return "";
}

@Override
public String getDisplayName() {
return "";
}

@Override
public String getUrlName() {
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import hudson.tasks.Publisher;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.p4.credentials.P4CredentialsImpl;
import org.jenkinsci.plugins.p4.tasks.PublishTask;
Expand Down Expand Up @@ -86,11 +87,11 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
desc = ws.getExpand().format(desc, false);
getPublish().setExpandedDesc(desc);

boolean success = buildWorkspace.act(task);
String publishedChangeId = buildWorkspace.act(task);

cleanupPerforceClient(build, buildWorkspace, listener);

return success;
return StringUtils.isNotEmpty(publishedChangeId);
}

protected void cleanupPerforceClient(Run<?, ?> run, FilePath buildWorkspace, TaskListener listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hudson.model.TaskListener;
import hudson.tasks.BuildStepMonitor;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.p4.tasks.PublishTask;
import org.jenkinsci.plugins.p4.workspace.Workspace;
import org.kohsuke.stapler.DataBoundConstructor;
Expand All @@ -17,7 +18,7 @@ public class PublishNotifierStep extends PublishNotifier implements SimpleBuildS

@DataBoundConstructor
public PublishNotifierStep(String credential, Workspace workspace,
Publish publish) {
Publish publish) {
super(credential, workspace, publish);
}

Expand All @@ -28,7 +29,7 @@ public BuildStepMonitor getRequiredMonitorService() {

@Override
public void perform(Run<?, ?> run, FilePath buildWorkspace,
Launcher launcher, TaskListener listener)
Launcher launcher, TaskListener listener)
throws InterruptedException, IOException {

// return early if publish not required
Expand All @@ -48,7 +49,10 @@ public void perform(Run<?, ?> run, FilePath buildWorkspace,
desc = ws.getExpand().format(desc, false);
getPublish().setExpandedDesc(desc);

buildWorkspace.act(task);
String publishedChangeID = buildWorkspace.act(task);
if (StringUtils.isNotEmpty(publishedChangeID)) {
run.addAction(new P4PublishEnvironmentContributingAction(publishedChangeID));
}

cleanupPerforceClient(run, buildWorkspace, listener);
}
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/jenkinsci/plugins/p4/tasks/PublishTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.model.TaskListener;
import hudson.remoting.VirtualChannel;
import jenkins.security.Roles;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.p4.client.ClientHelper;
import org.jenkinsci.plugins.p4.publish.Publish;
import org.jenkinsci.remoting.RoleChecker;
Expand All @@ -15,7 +16,7 @@
import java.io.Serializable;
import java.util.logging.Logger;

public class PublishTask extends AbstractTask implements FileCallable<Boolean>, Serializable {
public class PublishTask extends AbstractTask implements FileCallable<String>, Serializable {

private static final long serialVersionUID = 1L;

Expand All @@ -28,22 +29,23 @@ public PublishTask(String credential, Run<?, ?> run, TaskListener listener, Publ
this.publish = publish;
}

public Boolean invoke(File workspace, VirtualChannel channel) throws IOException {
return (Boolean) tryTask();
public String invoke(File workspace, VirtualChannel channel) throws IOException {
return (String) tryTask();
}

@Override
public Object task(ClientHelper p4) throws Exception {
String publishedChangeID = StringUtils.EMPTY;
try {
// Check connection (might be on remote slave)
if (!checkConnection(p4)) {
return false;
return StringUtils.EMPTY;
}

// Look for changes and add to change-list, then publish
boolean open = p4.buildChange(publish);
if (open) {
p4.publishChange(publish);
publishedChangeID = p4.publishChange(publish);
}
} catch (Exception e) {
p4.log("(p4):stop:exception\n");
Expand All @@ -52,10 +54,11 @@ public Object task(ClientHelper p4) throws Exception {
logger.warning(err);
throw new AbortException(err);
}
return true;
return publishedChangeID;
}

public void checkRoles(RoleChecker checker) throws SecurityException {
checker.check(this, Roles.SLAVE);
}

}

0 comments on commit 5f25618

Please sign in to comment.