Skip to content

Commit 1346004

Browse files
*.java
- Serialization fixes for the reported issue jenkinsci#55
1 parent 93053f7 commit 1346004

10 files changed

+192
-114
lines changed

src/main/java/org/jenkinsci/plugins/pipeline/github/CommitFileGroovyObject.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ public class CommitFileGroovyObject extends GroovyObjectSupport implements Seria
1919
private final CommitFile file;
2020

2121
CommitFileGroovyObject(final CommitFile file) {
22-
Objects.requireNonNull(file, "file cannot be null");
23-
24-
this.file = file;
22+
this.file = Objects.requireNonNull(file, "file cannot be null");
2523
}
2624

2725
@Whitelisted

src/main/java/org/jenkinsci/plugins/pipeline/github/CommitGroovyObject.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,27 @@
4343
public class CommitGroovyObject extends GroovyObjectSupport implements Serializable {
4444
private static final long serialVersionUID = 1L;
4545

46+
private final String jobId;
4647
private final RepositoryCommit commit;
47-
private final ExtendedCommitService commitService;
4848
private final RepositoryId base;
4949

50-
CommitGroovyObject(final RepositoryCommit commit,
50+
private transient ExtendedCommitService commitService;
51+
52+
CommitGroovyObject(final String jobId,
53+
final RepositoryCommit commit,
5154
final ExtendedCommitService commitService,
5255
final RepositoryId base) {
53-
Objects.requireNonNull(commit, "commit cannot be null");
54-
Objects.requireNonNull(commitService, "commitService cannot be null");
55-
Objects.requireNonNull(base, "base cannot be null");
56+
this.jobId = Objects.requireNonNull(jobId, "jobId cannot be null");
57+
this.commit = Objects.requireNonNull(commit, "commit cannot be null");
58+
this.commitService = Objects.requireNonNull(commitService, "commitService cannot be null");
59+
this.base = Objects.requireNonNull(base, "base cannot be null");
60+
}
5661

57-
this.commit = commit;
58-
this.commitService = commitService;
59-
this.base = base;
62+
private ExtendedCommitService getCommitService() {
63+
if (commitService == null) {
64+
commitService = new ExtendedCommitService(GitHubHelper.getGitHubClient(GitHubHelper.getJob(jobId)));
65+
}
66+
return commitService;
6067
}
6168

6269
@Whitelisted
@@ -111,16 +118,16 @@ public int getTotalChanges() {
111118
@Whitelisted
112119
public Iterable<ReviewCommentGroovyObject> getComments() {
113120
Stream<ReviewCommentGroovyObject> stream = StreamSupport.stream(
114-
commitService.pageComments2(base, commit.getSha()).spliterator(), false)
121+
getCommitService().pageComments2(base, commit.getSha()).spliterator(), false)
115122
.flatMap(Collection::stream)
116-
.map(c -> new ReviewCommentGroovyObject(c, base, commitService));
123+
.map(c -> new ReviewCommentGroovyObject(jobId, base, c, commitService));
117124
return stream::iterator;
118125
}
119126

120127
@Whitelisted
121128
public Iterable<CommitStatusGroovyObject> getStatuses() {
122129
try {
123-
return commitService.getStatuses(base, commit.getSha())
130+
return getCommitService().getStatuses(base, commit.getSha())
124131
.stream()
125132
.map(CommitStatusGroovyObject::new)
126133
.collect(toList());
@@ -167,9 +174,10 @@ public ReviewCommentGroovyObject comment(final String body,
167174
comment.setPosition(position);
168175
try {
169176
return new ReviewCommentGroovyObject(
170-
commitService.addComment(base, commit.getSha(), comment),
177+
jobId,
171178
base,
172-
commitService);
179+
getCommitService().addComment(base, commit.getSha(), comment),
180+
getCommitService());
173181
} catch (final IOException e) {
174182
throw new UncheckedIOException(e);
175183
}
@@ -199,7 +207,7 @@ public CommitStatusGroovyObject createStatus(final String status,
199207
commitStatus.setTargetUrl(targetUrl);
200208
try {
201209
return new CommitStatusGroovyObject(
202-
commitService.createStatus(base, commit.getSha(), commitStatus));
210+
getCommitService().createStatus(base, commit.getSha(), commitStatus));
203211
} catch (final IOException e) {
204212
throw new UncheckedIOException(e);
205213
}

src/main/java/org/jenkinsci/plugins/pipeline/github/CommitStatusGroovyObject.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ public class CommitStatusGroovyObject extends GroovyObjectSupport implements Ser
2020
private final CommitStatus commitStatus;
2121

2222
CommitStatusGroovyObject(final CommitStatus commitStatus) {
23-
Objects.requireNonNull(commitStatus, "commitStatus cannot be null");
24-
25-
this.commitStatus = commitStatus;
23+
this.commitStatus = Objects.requireNonNull(commitStatus, "commitStatus cannot be null");
2624
}
2725

2826
@Whitelisted

src/main/java/org/jenkinsci/plugins/pipeline/github/GitHubHelper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cloudbees.plugins.credentials.common.StandardCredentials;
44
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
55
import hudson.model.Job;
6+
import jenkins.model.Jenkins;
67
import jenkins.scm.api.SCMHead;
78
import jenkins.scm.api.SCMSource;
89
import org.eclipse.egit.github.core.RepositoryId;
@@ -31,7 +32,7 @@ public class GitHubHelper {
3132

3233
private final static Logger LOG = LoggerFactory.getLogger(GitHubHelper.class);
3334

34-
private GitHubHelper(){
35+
private GitHubHelper() {
3536
// go away
3637
}
3738

@@ -105,4 +106,12 @@ public static String userToLogin(final User user) {
105106
return user == null ? null : user.getLogin();
106107
}
107108

109+
public static Job getJob(final String jobId) {
110+
final Job job = Jenkins.get().getItemByFullName(jobId, Job.class);
111+
if (job == null) {
112+
throw new IllegalStateException("Unable to find Job: " + jobId);
113+
}
114+
return job;
115+
}
116+
108117
}

src/main/java/org/jenkinsci/plugins/pipeline/github/IssueCommentGroovyObject.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,27 @@
2525
public class IssueCommentGroovyObject extends GroovyObjectSupport implements Serializable {
2626
private static final long serialVersionUID = 1L;
2727

28+
private final String jobId;
2829
private final RepositoryId base;
29-
private final IssueService issueService;
3030
private Comment comment;
3131

32-
IssueCommentGroovyObject(final Comment comment, final RepositoryId base, final IssueService issueService) {
33-
Objects.requireNonNull(comment, "comment cannot be null");
34-
Objects.requireNonNull(base, "base cannot be null");
35-
Objects.requireNonNull(issueService, "issueService cannot be null");
32+
private transient IssueService issueService;
3633

37-
this.comment = comment;
38-
this.base = base;
39-
this.issueService = issueService;
34+
IssueCommentGroovyObject(final String jobId,
35+
final Comment comment,
36+
final RepositoryId base,
37+
final IssueService issueService) {
38+
this.jobId = Objects.requireNonNull(jobId, "jobId cannot be null");
39+
this.comment = Objects.requireNonNull(comment, "comment cannot be null");
40+
this.base = Objects.requireNonNull(base, "base cannot be null");
41+
this.issueService = Objects.requireNonNull(issueService, "issueService cannot be null");
42+
}
43+
44+
private IssueService getIssueService() {
45+
if (issueService == null) {
46+
issueService = new IssueService(GitHubHelper.getGitHubClient(GitHubHelper.getJob(jobId)));
47+
}
48+
return issueService;
4049
}
4150

4251
@Whitelisted
@@ -85,7 +94,7 @@ public void setBody(final String body) {
8594
edit.setId(comment.getId());
8695
edit.setBody(body);
8796
try {
88-
comment = issueService.editComment(base, edit);
97+
comment = getIssueService().editComment(base, edit);
8998
} catch (final IOException e) {
9099
throw new UncheckedIOException(e);
91100
}
@@ -94,7 +103,7 @@ public void setBody(final String body) {
94103
@Whitelisted
95104
public void delete() {
96105
try {
97-
issueService.deleteComment(base, comment.getId());
106+
getIssueService().deleteComment(base, comment.getId());
98107
} catch (final IOException e) {
99108
throw new UncheckedIOException(e);
100109
}

src/main/java/org/jenkinsci/plugins/pipeline/github/MilestoneGroovyObject.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.Serializable;
99
import java.util.Date;
10+
import java.util.Objects;
1011

1112
/**
1213
* Groovy wrapper over {@link Milestone}
@@ -17,10 +18,12 @@
1718
public class MilestoneGroovyObject extends GroovyObjectSupport implements Serializable {
1819
private static final long serialVersionUID = 1L;
1920

21+
private final String jobId;
2022
private final ExtendedMilestone milestone;
2123

22-
MilestoneGroovyObject(final ExtendedMilestone milestone) {
23-
this.milestone = milestone;
24+
MilestoneGroovyObject(final String jobId, final ExtendedMilestone milestone) {
25+
this.jobId = Objects.requireNonNull(jobId, "jobId cannot be null");
26+
this.milestone = Objects.requireNonNull(milestone, "milestone cannot be null");
2427
}
2528

2629
@Whitelisted

src/main/java/org/jenkinsci/plugins/pipeline/github/PullRequestGlobalVariable.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.jenkinsci.plugins.pipeline.github;
22

3+
import hudson.model.Run;
34
import org.jenkinsci.plugins.workflow.cps.CpsScript;
45
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;
56

@@ -22,7 +23,11 @@ public String getName() {
2223
@Nonnull
2324
@Override
2425
public Object getValue(@Nonnull final CpsScript script) throws Exception {
25-
return new PullRequestGroovyObject(script);
26+
final Run<?, ?> build = script.$build();
27+
if (build == null) {
28+
throw new IllegalStateException("No associated build");
29+
}
30+
return new PullRequestGroovyObject(build.getParent());
2631
}
2732

2833
}

0 commit comments

Comments
 (0)