3
3
import android .content .Context ;
4
4
import android .content .Intent ;
5
5
import android .os .Bundle ;
6
+ import android .support .annotation .Nullable ;
6
7
import android .view .View ;
7
8
import android .widget .TextView ;
8
9
9
10
import com .gh4a .Gh4Application ;
10
11
import com .gh4a .R ;
11
12
12
13
import org .eclipse .egit .github .core .CommitComment ;
14
+ import org .eclipse .egit .github .core .DraftPullRequestReviewComment ;
13
15
import org .eclipse .egit .github .core .RepositoryId ;
16
+ import org .eclipse .egit .github .core .Review ;
17
+ import org .eclipse .egit .github .core .client .RequestException ;
14
18
import org .eclipse .egit .github .core .service .PullRequestService ;
15
19
16
20
import java .io .IOException ;
21
+ import java .net .HttpURLConnection ;
22
+ import java .util .ArrayList ;
23
+ import java .util .HashMap ;
24
+ import java .util .List ;
25
+ import java .util .Map ;
17
26
18
27
public class EditPullRequestDiffCommentActivity extends EditCommentActivity {
19
28
public static Intent makeIntent (Context context , String repoOwner , String repoName ,
@@ -55,6 +64,10 @@ protected void createComment(RepositoryId repoId, CommitComment comment,
55
64
PullRequestService pullRequestService = (PullRequestService )
56
65
app .getService (Gh4Application .PULL_SERVICE );
57
66
67
+ Review draftReview = getPendingReview (pullRequestService , repoId , prNumber );
68
+ List <DraftPullRequestReviewComment > draftComments =
69
+ getPendingComments (pullRequestService , repoId , prNumber , draftReview );
70
+
58
71
if (replyToCommentId != 0L ) {
59
72
pullRequestService .replyToComment (repoId , prNumber ,
60
73
(int ) replyToCommentId , comment .getBody ());
@@ -64,8 +77,63 @@ protected void createComment(RepositoryId repoId, CommitComment comment,
64
77
comment .setCommitId (extras .getString ("commit_id" ));
65
78
comment .setPath (extras .getString ("path" ));
66
79
67
- pullRequestService .createComment (repoId , prNumber , comment );
80
+ draftComments .add (new DraftPullRequestReviewComment ()
81
+ .setPath (extras .getString ("path" ))
82
+ .setPosition (extras .getInt ("position" ))
83
+ .setBody (comment .getBody ()));
84
+
85
+ Map <String , Object > map = new HashMap <>();
86
+ map .put ("commitId" , extras .getString ("commit_id" ));
87
+ map .put ("comments" , draftComments );
88
+
89
+ if (draftReview != null ) {
90
+ if (draftReview .getBody () != null ) {
91
+ map .put ("body" , draftReview .getBody ());
92
+ }
93
+
94
+ try {
95
+ pullRequestService .deleteReview (repoId , prNumber , draftReview .getId ());
96
+ } catch (RequestException e ) {
97
+ if (e .getStatus () != HttpURLConnection .HTTP_OK ) {
98
+ throw e ;
99
+ }
100
+ }
101
+ }
102
+
103
+ pullRequestService .createReview (repoId , prNumber , map );
104
+ }
105
+ }
106
+
107
+ @ Nullable
108
+ private Review getPendingReview (PullRequestService pullRequestService , RepositoryId repoId ,
109
+ int prNumber ) throws IOException {
110
+ Review pendingReview = null ;
111
+ List <Review > reviews = pullRequestService .getReviews (repoId , prNumber );
112
+ for (Review review : reviews ) {
113
+ if (Review .STATE_PENDING .equals (review .getState ())) {
114
+ pendingReview = review ;
115
+ break ;
116
+ }
117
+ }
118
+ return pendingReview ;
119
+ }
120
+
121
+ private List <DraftPullRequestReviewComment > getPendingComments (
122
+ PullRequestService pullRequestService , RepositoryId repoId , int prNumber ,
123
+ Review draftReview ) throws IOException {
124
+ List <DraftPullRequestReviewComment > draftComments = new ArrayList <>();
125
+ if (draftReview != null ) {
126
+ List <CommitComment > pendingComments =
127
+ pullRequestService .getReviewComments (repoId , prNumber , draftReview .getId ());
128
+
129
+ for (CommitComment pendingComment : pendingComments ) {
130
+ draftComments .add (new DraftPullRequestReviewComment ()
131
+ .setPath (pendingComment .getPath ())
132
+ .setPosition (pendingComment .getPosition ())
133
+ .setBody (pendingComment .getBody ()));
134
+ }
68
135
}
136
+ return draftComments ;
69
137
}
70
138
71
139
@ Override
0 commit comments