Skip to content
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.5.1.201910021850-r</version>
<version>5.10.0.202012080955-r</version>
</dependency>
<!-- end git stuff -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.FetchResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -96,12 +97,14 @@ static class ExtendedBuildState {
public final String commitIDAfterBuild;
public final List<String> tagsAdded;
public final File workDir;
ExtendedBuildState(BuildState buildState, String commitIDBeforeBuild, String commitIDAfterBuild, List<String> tagsAdded, File workDir) {
public final String branch;
ExtendedBuildState(BuildState buildState, String commitIDBeforeBuild, String commitIDAfterBuild, List<String> tagsAdded, File workDir, String branch) {
this.buildState = buildState;
this.commitIDBeforeBuild = commitIDBeforeBuild;
this.commitIDAfterBuild = commitIDAfterBuild;
this.tagsAdded = tagsAdded;
this.workDir = workDir;
this.branch = branch;
}
}

Expand All @@ -110,10 +113,11 @@ public ExtendedBuildState build(Writer outputHandler, String branch, String buil
final File workDir;
final ExtendedBuildState extendedBuildState;
try (Git git = pullFromGitAndCopyWorkingCopyToNewDir(outputHandler, branch)) {
branch = git.getRepository().getBranch();
workDir = git.getRepository().getWorkTree();
doubleLog(outputHandler, "Created new instance in " + dirPath(workDir));

Ref headBefore = git.getRepository().exactRef("HEAD");
Ref headBefore = git.getRepository().exactRef(Constants.HEAD);
Ref headAfter = headBefore;
ObjectId beforeCommitID = headBefore.getObjectId();
List<String> newTags = new ArrayList<>();
Expand All @@ -140,7 +144,7 @@ public ExtendedBuildState build(Writer outputHandler, String branch, String buil
ProcessStarter processStarter = new ProcessStarter(outputHandler);
result = processStarter.run(outputHandler, command, workDir, TimeUnit.MINUTES.toMillis(buildTimeout), environment);

headAfter = git.getRepository().exactRef("HEAD");
headAfter = git.getRepository().exactRef(Constants.HEAD);

try (RevWalk walk = new RevWalk(git.getRepository())) {
walk.markStart(walk.parseCommit(headAfter.getObjectId()));
Expand All @@ -154,7 +158,7 @@ public ExtendedBuildState build(Writer outputHandler, String branch, String buil

newTags.removeAll(tagsBefore);

extendedBuildState = new ExtendedBuildState(result, beforeCommitID.name(), headAfter.getObjectId().name(), Collections.unmodifiableList(newTags), workDir);
extendedBuildState = new ExtendedBuildState(result, beforeCommitID.name(), headAfter.getObjectId().name(), Collections.unmodifiableList(newTags), workDir, branch);
}

deleteDirectoryQuietly(workDir, StandardDeleteOption.OVERRIDE_READ_ONLY);
Expand All @@ -169,10 +173,53 @@ public void close() {
}

private Git pullFromGitAndCopyWorkingCopyToNewDir(Writer writer, String branch) throws GitAPIException, IOException {
git.fetch().setRemote("origin").setProgressMonitor(new TextProgressMonitor(writer)).call();
FetchResult fetchResult = git.fetch().setRemote("origin").setProgressMonitor(new TextProgressMonitor(writer)).call();
if (branch == null) {
Ref ref = findBranchToBuild(fetchResult);
if (ref == null) {
throw new RuntimeException("Could not determine default branch to build");
} else {
branch = StringUtils.removeStart(ref.getName(), Constants.R_HEADS);
}
doubleLog(writer, "Building default branch: "+branch);
} else {
doubleLog(writer, "Building branch: "+branch);
}
return copyToNewInstanceDirAndSwitchBranch(branch);
}

private Ref findBranchToBuild(FetchResult result) {
final Ref headRef = result.getAdvertisedRef(Constants.HEAD);
if (headRef == null) {
return null;
}

if (headRef.isSymbolic()) {
return headRef.getTarget();
}

final ObjectId headObjectId = headRef.getObjectId();
if (headObjectId == null) {
return null;
}

final Ref masterRef = result.getAdvertisedRef(Constants.R_HEADS + Constants.MASTER);
if (masterRef != null && headObjectId.equals(masterRef.getObjectId())) {
return masterRef;
}

for (Ref adRef : result.getAdvertisedRefs()) {
if (!adRef.getName().startsWith(Constants.R_HEADS)) {
continue;
}
if (headObjectId.equals(adRef.getObjectId())) {
return adRef;
}
}

return null;
}

private Git copyToNewInstanceDirAndSwitchBranch(String branch) throws GitAPIException, IOException {
File dest = new File(instanceDir, String.valueOf(System.currentTimeMillis()));
if (!dest.mkdir()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.muserver.rest.ApiResponse;
import io.muserver.rest.Description;
import io.muserver.rest.ResponseHeader;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -51,7 +52,7 @@ public BuildResource(FileSandbox fileSandbox, BuildQueue buildQueue, BuildDataba
@ApiResponse(code = "400", message = "No gitUrl form parameter was specified.", contentType = "text/plain")
public Response create(@FormParam("gitUrl") @Description(value = "The URL of a git repo that includes a `build.sh` or `build.bat` file. " +
"It can be any type of Git URL (e.g. SSH or HTTPS) that the server has permission for.", example = "https://github.com/3redronin/mu-server-sample.git") String gitUrl,
@DefaultValue("master") @FormParam("branch") @Description(value = "The value of the git branch. This parameter is optional.") String branch,
@FormParam("branch") @Description(value = "The value of the git branch. This parameter is optional.") String branch,
@FormParam("buildParam") @Description(value = "The parameter for the `build.sh` or `build.bat` file. This parameter is optional.") String buildParam,
@Context UriInfo uriInfo) {
BuildResult result = createInternal(gitUrl, branch, buildParam, uriInfo);
Expand All @@ -69,11 +70,7 @@ private BuildResult createInternal(String gitUrl, String branch, String buildPar
throw new BadRequestException("A form parameter named gitUrl must point to a valid git repo");
}

String gitBranch = branch;
if(null == branch || branch.trim().isEmpty()) {
gitBranch = "master";
}

String gitBranch = StringUtils.trimToNull(branch);
GitRepo gitRepo = new GitRepo(gitUrl, gitBranch);
String id = UUID.randomUUID().toString().replace("-", "");
Map<String, String> environment = getEnrichedEnvironment(id, uriInfo);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h2>Submit a build</h2>
</label>
<label>
Git Branch:
<input type="text" id="branchBox" name="branch" placeholder="master">
<input type="text" id="branchBox" name="branch" placeholder="<default>">
</label>
<label>
Build Parameters:
Expand Down
9 changes: 7 additions & 2 deletions src/main/resources/web/restabuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ document.addEventListener('DOMContentLoaded', function () {
var value = urlBox.value;
var branch = branchBox.value;
var buildParam = buildParamBox.value;
cc.textContent = 'curl -LNs -F \'gitUrl=' + (value || 'git-url') + '\' ' + '-F \'branch=' + (branch || 'master') + '\' ' + '-F \'param=' + (buildParam || '') + '\' ' + form.action;
history.replaceState(null, null, value ? encodeURI(path) + '?url=' + encodeURIComponent(value) : encodeURI(path));
cc.textContent = 'curl -LNs -F \'gitUrl=' + (value || 'git-url') + '\' ' + (branch ? ('-F \'branch=' + branch + '\' ') : '') + (buildParam ? ('-F \'param=' + buildParam + '\' ') : '') + form.action;
var encodedQS = '';
if (value) encodedQS += "&url="+encodeURIComponent(value);
if (branch) encodedQS += "&branch="+encodeURIComponent(branch);
if (buildParam) encodedQS += "&param="+encodeURIComponent(buildParam);
if (encodedQS) encodedQS = "?"+encodedQS.slice(1);
history.replaceState(null, null, encodeURI(path) + encodedQS);
};
urlBox.addEventListener('input', update);
branchBox.addEventListener('input', update);
Expand Down
Loading