-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e63eec8
commit 0130343
Showing
5 changed files
with
236 additions
and
49 deletions.
There are no files selected for viewing
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
62 changes: 62 additions & 0 deletions
62
src/main/java/io/jenkins/plugins/git_push/GitPushCommand.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,62 @@ | ||
package io.jenkins.plugins.git_push; | ||
|
||
import hudson.AbortException; | ||
import hudson.EnvVars; | ||
import hudson.FilePath; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.plugins.git.GitException; | ||
import hudson.plugins.git.GitSCM; | ||
import java.io.IOException; | ||
import org.eclipse.jgit.lib.ObjectId; | ||
import org.eclipse.jgit.transport.RemoteConfig; | ||
import org.eclipse.jgit.transport.URIish; | ||
import org.jenkinsci.plugins.gitclient.GitClient; | ||
|
||
/** @author Réda Housni Alaoui */ | ||
public class GitPushCommand { | ||
|
||
private final GitSCM scm; | ||
private final Run<?, ?> run; | ||
private final TaskListener listener; | ||
private final FilePath workspace; | ||
|
||
public GitPushCommand(GitSCM scm, Run<?, ?> run, TaskListener listener, FilePath workspace) { | ||
this.scm = scm; | ||
this.run = run; | ||
this.listener = listener; | ||
this.workspace = workspace; | ||
} | ||
|
||
public void call(String targetBranch, String targetRepo) | ||
throws IOException, InterruptedException, Failure { | ||
EnvVars environment = run.getEnvironment(listener); | ||
|
||
GitClient git = | ||
scm.createClient(listener, environment, run, workspace, new GitPushUnsupportedCommand()); | ||
|
||
RemoteConfig remote = scm.getRepositoryByName(targetRepo); | ||
if (remote == null) { | ||
throw new AbortException("No repository found for target repo name '" + targetRepo + "'"); | ||
} | ||
|
||
remote = scm.getParamExpandedRepo(environment, remote); | ||
URIish remoteURI = remote.getURIs().get(0); | ||
|
||
try { | ||
git.fetch_().from(remoteURI, remote.getFetchRefSpecs()).execute(); | ||
ObjectId remoteRev = git.revParse(targetRepo + "/" + targetBranch); | ||
git.merge().setRevisionToMerge(remoteRev).execute(); | ||
git.push().to(remoteURI).ref("HEAD:" + targetBranch).tags(true).execute(); | ||
git.fetch_().from(remoteURI, remote.getFetchRefSpecs()).execute(); | ||
} catch (GitException e) { | ||
throw new Failure("Failed to push to " + targetRepo, e); | ||
} | ||
} | ||
|
||
public static class Failure extends Exception { | ||
public Failure(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
src/main/java/io/jenkins/plugins/git_push/GitPushStep.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,133 @@ | ||
package io.jenkins.plugins.git_push; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import hudson.AbortException; | ||
import hudson.Extension; | ||
import hudson.FilePath; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.plugins.git.GitSCM; | ||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.util.Set; | ||
import javax.annotation.Nonnull; | ||
import org.jenkinsci.plugins.workflow.steps.Step; | ||
import org.jenkinsci.plugins.workflow.steps.StepContext; | ||
import org.jenkinsci.plugins.workflow.steps.StepDescriptor; | ||
import org.jenkinsci.plugins.workflow.steps.StepExecution; | ||
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
/** @author Réda Housni Alaoui */ | ||
public class GitPushStep extends Step { | ||
|
||
private GitSCM gitScm; | ||
private String targetBranch; | ||
private String targetRepo; | ||
|
||
@DataBoundConstructor | ||
public GitPushStep() { | ||
// Only needed to mark the constructor with @DataBoundConstructor | ||
} | ||
|
||
@DataBoundSetter | ||
public void setGitScm(GitSCM gitScm) { | ||
this.gitScm = gitScm; | ||
} | ||
|
||
public GitSCM getGitScm() { | ||
return gitScm; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setTargetBranch(String targetBranch) { | ||
this.targetBranch = targetBranch; | ||
} | ||
|
||
public String getTargetBranch() { | ||
return targetBranch; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setTargetRepo(String targetRepo) { | ||
this.targetRepo = targetRepo; | ||
} | ||
|
||
public String getTargetRepo() { | ||
return targetRepo; | ||
} | ||
|
||
@Override | ||
public StepExecution start(StepContext context) { | ||
return new Execution(context, gitScm, targetBranch, targetRepo); | ||
} | ||
|
||
private static class Execution extends SynchronousNonBlockingStepExecution<Void> { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private transient GitSCM gitScm; | ||
private String targetBranch; | ||
private String targetRepo; | ||
|
||
protected Execution( | ||
@Nonnull StepContext context, | ||
@Nonnull GitSCM gitScm, | ||
@Nonnull String targetBranch, | ||
@Nonnull String targetRepo) { | ||
super(context); | ||
this.gitScm = gitScm; | ||
this.targetBranch = targetBranch; | ||
this.targetRepo = targetRepo; | ||
} | ||
|
||
@Override | ||
protected Void run() throws Exception { | ||
if (gitScm == null) { | ||
throw new AbortException("gitScm is missing"); | ||
} | ||
|
||
new GitPushCommand( | ||
gitScm, | ||
getContext().get(Run.class), | ||
getContext().get(TaskListener.class), | ||
getContext().get(FilePath.class)) | ||
.call(targetBranch, targetRepo); | ||
|
||
return null; | ||
} | ||
|
||
private void writeObject(ObjectOutputStream outputStream) throws IOException { | ||
outputStream.writeObject(targetBranch); | ||
outputStream.writeObject(targetRepo); | ||
} | ||
|
||
private void readObject(ObjectInputStream inputStream) | ||
throws IOException, ClassNotFoundException { | ||
gitScm = null; | ||
targetBranch = (String) inputStream.readObject(); | ||
targetRepo = (String) inputStream.readObject(); | ||
} | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends StepDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Git Push"; | ||
} | ||
|
||
@Override | ||
public Set<? extends Class<?>> getRequiredContext() { | ||
return ImmutableSet.of(Run.class, TaskListener.class, FilePath.class); | ||
} | ||
|
||
@Override | ||
public String getFunctionName() { | ||
return "gitPush"; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/io/jenkins/plugins/git_push/GitPushUnsupportedCommand.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,13 @@ | ||
package io.jenkins.plugins.git_push; | ||
|
||
import org.jenkinsci.plugins.gitclient.UnsupportedCommand; | ||
|
||
/** @author Réda Housni Alaoui */ | ||
class GitPushUnsupportedCommand extends UnsupportedCommand { | ||
@Override | ||
public boolean determineSupportForJGit() { | ||
// Do not know why we exactly need that. Inspired by | ||
// https://github.com/jenkinsci/git-plugin/blob/b95bffa7579c91cb79616b5a1e45feea52e4f70b/src/main/java/hudson/plugins/git/GitPublisher.java#L189 | ||
return false; | ||
} | ||
} |
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