Skip to content

Commit

Permalink
fix issue #96: Able to access ref being updated in build job
Browse files Browse the repository at this point in the history
  • Loading branch information
robinshine committed Apr 14, 2020
1 parent 854f2ff commit f75cb84
Show file tree
Hide file tree
Showing 27 changed files with 172 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
import io.onedev.server.entitymanager.ProjectManager;
import io.onedev.server.entitymanager.SettingManager;
import io.onedev.server.entitymanager.UserManager;
import io.onedev.server.event.Event;
import io.onedev.server.event.ProjectEvent;
import io.onedev.server.event.RefUpdated;
import io.onedev.server.event.build.BuildFinished;
import io.onedev.server.event.build.BuildPending;
import io.onedev.server.event.build.BuildRetrying;
Expand Down Expand Up @@ -174,7 +174,8 @@ private void validate(Project project, ObjectId commitId) {

@Transactional
@Override
public Build submit(Project project, ObjectId commitId, String jobName, Map<String, List<String>> paramMap) {
public Build submit(Project project, ObjectId commitId, String jobName,
Map<String, List<String>> paramMap, @Nullable String updatedRef) {
Lock lock = LockUtils.getLock("job-manager: " + project.getId() + "-" + commitId.name());
transactionManager.mustRunAfterTransaction(new Runnable() {

Expand All @@ -191,14 +192,14 @@ public void run() {

validate(project, commitId);

return submit(project, commitId, jobName, paramMap, new LinkedHashSet<>());
return submit(project, commitId, jobName, paramMap, updatedRef, new LinkedHashSet<>());
} catch (Throwable e) {
throw ExceptionUtils.unchecked(e);
}
}

private Build submit(Project project, ObjectId commitId, String jobName,
Map<String, List<String>> paramMap, Set<String> checkedJobNames) {
Map<String, List<String>> paramMap, String updatedRef, Set<String> checkedJobNames) {

ScriptIdentity.push(new JobIdentity(project, commitId));
try {
Expand All @@ -209,6 +210,7 @@ private Build submit(Project project, ObjectId commitId, String jobName,
build.setSubmitDate(new Date());
build.setStatus(Build.Status.WAITING);
build.setSubmitter(SecurityUtils.getUser());
build.setUpdatedRef(updatedRef);

ParamSupply.validateParamMap(build.getJob().getParamSpecMap(), paramMap);

Expand Down Expand Up @@ -254,7 +256,7 @@ private Build submit(Project project, ObjectId commitId, String jobName,
@Override
public void run(Map<String, List<String>> params) {
Build dependencyBuild = submit(project, commitId, dependency.getJobName(),
params, new LinkedHashSet<>(checkedJobNames));
params, updatedRef, new LinkedHashSet<>(checkedJobNames));
BuildDependence dependence = new BuildDependence();
dependence.setDependency(dependencyBuild);
dependence.setDependent(build);
Expand Down Expand Up @@ -607,65 +609,66 @@ private void markBuildError(Build build, String errorMessage) {
@Sessional
@Listen
public void on(ProjectEvent event) {
Event.push(event);
try {
if (event instanceof CommitAware) {
ObjectId commitId = ((CommitAware) event).getCommit().getCommitId();
if (!commitId.equals(ObjectId.zeroId())) {
ScriptIdentity.push(new JobIdentity(event.getProject(), commitId));
try {
BuildSpec buildSpec = event.getProject().getBuildSpec(commitId);
if (buildSpec != null) {
for (Job job: buildSpec.getJobs()) {
JobTrigger trigger = job.getMatchedTrigger(event);
if (trigger != null) {
Map<String, List<List<String>>> paramMatrix = ParamSupply.getParamMatrix(trigger.getParams());
Long projectId = event.getProject().getId();

// run asynchrously as session may get closed due to exception
transactionManager.runAfterCommit(new Runnable() {
if (event instanceof CommitAware) {
ObjectId commitId = ((CommitAware) event).getCommit().getCommitId();
if (!commitId.equals(ObjectId.zeroId())) {
ScriptIdentity.push(new JobIdentity(event.getProject(), commitId));
try {
BuildSpec buildSpec = event.getProject().getBuildSpec(commitId);
if (buildSpec != null) {
for (Job job: buildSpec.getJobs()) {
JobTrigger trigger = job.getMatchedTrigger(event);
if (trigger != null) {
Map<String, List<List<String>>> paramMatrix = ParamSupply.getParamMatrix(trigger.getParams());
Long projectId = event.getProject().getId();

String updatedRef;
if (event instanceof RefUpdated)
updatedRef = ((RefUpdated)event).getRefName();
else
updatedRef = null;

// run asynchrously as session may get closed due to exception
transactionManager.runAfterCommit(new Runnable() {

@Override
public void run() {
sessionManager.runAsync(new Runnable() {

@Override
public void run() {
Project project = projectManager.load(projectId);
try {
new MatrixRunner<List<String>>(paramMatrix) {

@Override
public void run(Map<String, List<String>> paramMap) {
submit(project, commitId, job.getName(), paramMap);
}

}.run();
} catch (Throwable e) {
String message = String.format("Error submitting build (project: %s, commit: %s, job: %s)",
project.getName(), commitId.name(), job.getName());
logger.error(message, e);
}
@Override
public void run() {
sessionManager.runAsync(new Runnable() {

@Override
public void run() {
Project project = projectManager.load(projectId);
try {
new MatrixRunner<List<String>>(paramMatrix) {

@Override
public void run(Map<String, List<String>> paramMap) {
submit(project, commitId, job.getName(), paramMap, updatedRef);
}

}.run();
} catch (Throwable e) {
String message = String.format("Error submitting build (project: %s, commit: %s, job: %s)",
project.getName(), commitId.name(), job.getName());
logger.error(message, e);
}
});
}
});
}
}

});
}

});
}
}
} catch (Throwable e) {
String message = String.format("Error checking job triggers (project: %s, commit: %s)",
event.getProject().getName(), commitId.name());
logger.error(message, e);
} finally {
ScriptIdentity.pop();
}
} catch (Throwable e) {
String message = String.format("Error checking job triggers (project: %s, commit: %s)",
event.getProject().getName(), commitId.name());
logger.error(message, e);
} finally {
ScriptIdentity.pop();
}
}
} finally {
Event.pop();
}
}

Expand All @@ -684,6 +687,8 @@ public void resubmit(Build build, Map<String, List<String>> paramMap) {
build.setSubmitter(SecurityUtils.getUser());
build.setCanceller(null);
build.setCancellerName(null);
build.setUpdatedRef(null);

buildParamManager.deleteParams(build);
for (Map.Entry<String, List<String>> entry: paramMap.entrySet()) {
ParamSpec paramSpec = build.getJob().getParamSpecMap().get(entry.getKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public interface JobManager {

public static final String JOB_TOKEN_HTTP_HEADER = "X-ONEDEV-JOB-TOKEN";

Build submit(Project project, ObjectId commitId, String jobName, Map<String, List<String>> paramMap);
Build submit(Project project, ObjectId commitId, String jobName,
Map<String, List<String>> paramMap, String updatedRef);

void resubmit(Build build, Map<String, List<String>> paramMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public String getValue(Build build) {
}

},
UPDATED_REF {

@Override
public String getValue(Build build) {
return build.getUpdatedRef();
}

},
COMMIT_HASH {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ public VariableInterpolator(Build build) {
@Override
public String apply(String t) {
for (JobVariable var: JobVariable.values()) {
if (var.name().toLowerCase().equals(t))
return var.getValue(build);
if (var.name().toLowerCase().equals(t)) {
String value = var.getValue(build);
return value!=null?value:"";
}
}
if (t.startsWith(PREFIX_PARAMS)) {
String paramName = t.substring(PREFIX_PARAMS.length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void execute(Build build) {
@Override
public void run(Map<String, List<String>> paramMap) {
OneDev.getInstance(JobManager.class).submit(build.getProject(),
build.getCommitId(), getJobName(), paramMap);
build.getCommitId(), getJobName(), paramMap, null);
}

}.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.validator.constraints.NotEmpty;

import io.onedev.server.event.Event;
import io.onedev.server.model.Build;
import io.onedev.server.util.GroovyUtils;
import io.onedev.server.web.editable.annotation.Editable;
Expand Down Expand Up @@ -62,7 +61,6 @@ public int hashCode() {
public List<List<String>> getValues() {
Map<String, Object> variables = new HashMap<>();
variables.put("build", Build.get());
variables.put("event", Event.get());
List<List<String>> values = new ArrayList<>();
Object result = GroovyUtils.evalScriptByName(scriptName, variables);
if (result instanceof List) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
import io.onedev.server.search.entity.issue.StateCriteria;
import io.onedev.server.util.Input;
import io.onedev.server.util.IssueUtils;
import io.onedev.server.util.ProjectAwareCommit;
import io.onedev.server.util.ProjectScopedCommit;
import io.onedev.server.util.SecurityUtils;
import io.onedev.server.util.match.Matcher;
import io.onedev.server.util.match.PathMatcher;
Expand Down Expand Up @@ -474,7 +474,7 @@ public void run() {
criterias.add(IssueCriteria.or(fromStateCriterias));
criterias.add(query.getCriteria());
query = new IssueQuery(IssueCriteria.and(criterias), new ArrayList<>());
ProjectAwareCommit.push(new ProjectAwareCommit(project, newCommitId) {
ProjectScopedCommit.push(new ProjectScopedCommit(project, newCommitId) {

private static final long serialVersionUID = 1L;

Expand All @@ -490,7 +490,7 @@ public Collection<Long> getFixedIssueNumbers() {
changeState(issue, transition.getToState(), new HashMap<>(), null);
}
} finally {
ProjectAwareCommit.pop();
ProjectScopedCommit.pop();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import io.onedev.server.entitymanager.PullRequestReviewManager;
import io.onedev.server.entitymanager.PullRequestUpdateManager;
import io.onedev.server.entitymanager.UserManager;
import io.onedev.server.event.Event;
import io.onedev.server.event.RefUpdated;
import io.onedev.server.event.build.BuildEvent;
import io.onedev.server.event.entity.EntityRemoved;
Expand Down Expand Up @@ -798,37 +797,32 @@ private void checkBuilds(PullRequest request) {
if (trigger instanceof PullRequestTrigger) {
PullRequestTrigger pullRequestTrigger = (PullRequestTrigger) trigger;
PullRequestMergePreviewCalculated pullRequestMergePreviewCalculated = new PullRequestMergePreviewCalculated(request);
Event.push(pullRequestMergePreviewCalculated);
try {
if (pullRequestTrigger.matches(pullRequestMergePreviewCalculated, job)) {
boolean required = requiredJobNames.contains(job.getName());
new MatrixRunner<List<String>>(ParamSupply.getParamMatrix(trigger.getParams())) {

@Override
public void run(Map<String, List<String>> paramMap) {
Build build = jobManager.submit(request.getTargetProject(),
commitId, job.getName(), paramMap);
PullRequestBuild pullRequestBuild = null;
for (PullRequestBuild prevPullRequestBuild: prevPullRequestBuilds) {
if (prevPullRequestBuild.getBuild().equals(build)) {
pullRequestBuild = prevPullRequestBuild;
break;
}
}
if (pullRequestBuild == null) {
pullRequestBuild = new PullRequestBuild();
pullRequestBuild.setRequest(request);
pullRequestBuild.setBuild(build);
if (pullRequestTrigger.matches(pullRequestMergePreviewCalculated, job)) {
boolean required = requiredJobNames.contains(job.getName());
new MatrixRunner<List<String>>(ParamSupply.getParamMatrix(trigger.getParams())) {

@Override
public void run(Map<String, List<String>> paramMap) {
Build build = jobManager.submit(request.getTargetProject(),
commitId, job.getName(), paramMap, null);
PullRequestBuild pullRequestBuild = null;
for (PullRequestBuild prevPullRequestBuild: prevPullRequestBuilds) {
if (prevPullRequestBuild.getBuild().equals(build)) {
pullRequestBuild = prevPullRequestBuild;
break;
}
pullRequestBuild.setRequired(required);
request.getPullRequestBuilds().add(pullRequestBuild);
}

}.run();
requiredJobNames.remove(job.getName());
}
} finally {
Event.pop();
if (pullRequestBuild == null) {
pullRequestBuild = new PullRequestBuild();
pullRequestBuild.setRequest(request);
pullRequestBuild.setBuild(build);
}
pullRequestBuild.setRequired(required);
request.getPullRequestBuilds().add(pullRequestBuild);
}

}.run();
requiredJobNames.remove(job.getName());
}
}
}
Expand Down
26 changes: 0 additions & 26 deletions server-core/src/main/java/io/onedev/server/event/Event.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.onedev.server.event;

import java.util.Date;
import java.util.Stack;

import javax.annotation.Nullable;

Expand All @@ -13,15 +12,6 @@
@JsonTypeInfo(property="@class", use = Id.CLASS)
public abstract class Event {

private static ThreadLocal<Stack<Event>> stack = new ThreadLocal<Stack<Event>>() {

@Override
protected Stack<Event> initialValue() {
return new Stack<Event>();
}

};

private final User user;

private final Date date;
Expand All @@ -40,20 +30,4 @@ public Date getDate() {
return date;
}

public static void push(ProjectEvent event) {
stack.get().push(event);
}

public static void pop() {
stack.get().pop();
}

@Nullable
public static Event get() {
if (!stack.get().isEmpty())
return stack.get().peek();
else
return null;
}

}
Loading

0 comments on commit f75cb84

Please sign in to comment.