Skip to content

Commit 973b5f1

Browse files
committed
WIP: Review creation
1 parent 20feacf commit 973b5f1

File tree

9 files changed

+278
-2
lines changed

9 files changed

+278
-2
lines changed

app/src/main/AndroidManifest.xml

+5
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@
8282
android:exported="false"
8383
android:theme="@style/BottomSheetLightTheme"
8484
android:windowSoftInputMode="stateHidden" />
85+
<activity
86+
android:name=".activities.ReviewChangesActivity"
87+
android:exported="false"
88+
android:theme="@style/BottomSheetLightTheme"
89+
android:windowSoftInputMode="stateHidden" />
8590
<activity android:name=".activities.FileViewerActivity"
8691
android:exported="false"
8792
android:configChanges="orientation|screenSize" />

app/src/main/java/com/gh4a/activities/EditPullRequestDiffCommentActivity.java

+69-1
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
import android.content.Context;
44
import android.content.Intent;
55
import android.os.Bundle;
6+
import android.support.annotation.Nullable;
67
import android.view.View;
78
import android.widget.TextView;
89

910
import com.gh4a.Gh4Application;
1011
import com.gh4a.R;
1112

1213
import org.eclipse.egit.github.core.CommitComment;
14+
import org.eclipse.egit.github.core.DraftPullRequestReviewComment;
1315
import org.eclipse.egit.github.core.RepositoryId;
16+
import org.eclipse.egit.github.core.Review;
17+
import org.eclipse.egit.github.core.client.RequestException;
1418
import org.eclipse.egit.github.core.service.PullRequestService;
1519

1620
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;
1726

1827
public class EditPullRequestDiffCommentActivity extends EditCommentActivity {
1928
public static Intent makeIntent(Context context, String repoOwner, String repoName,
@@ -55,6 +64,10 @@ protected void createComment(RepositoryId repoId, CommitComment comment,
5564
PullRequestService pullRequestService = (PullRequestService)
5665
app.getService(Gh4Application.PULL_SERVICE);
5766

67+
Review draftReview = getPendingReview(pullRequestService, repoId, prNumber);
68+
List<DraftPullRequestReviewComment> draftComments =
69+
getPendingComments(pullRequestService, repoId, prNumber, draftReview);
70+
5871
if (replyToCommentId != 0L) {
5972
pullRequestService.replyToComment(repoId, prNumber,
6073
(int) replyToCommentId, comment.getBody());
@@ -64,8 +77,63 @@ protected void createComment(RepositoryId repoId, CommitComment comment,
6477
comment.setCommitId(extras.getString("commit_id"));
6578
comment.setPath(extras.getString("path"));
6679

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+
}
68135
}
136+
return draftComments;
69137
}
70138

71139
@Override

app/src/main/java/com/gh4a/activities/PullRequestActivity.java

+5
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
289289
case R.id.delete_branch:
290290
showDeleteRestoreBranchConfirmDialog(mHeadReference == null);
291291
break;
292+
case R.id.review_changes: {
293+
startActivity(ReviewChangesActivity.makeIntent(this, mRepoOwner, mRepoName,
294+
mPullRequestNumber));
295+
break;
296+
}
292297
}
293298
return super.onOptionsItemSelected(item);
294299
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.gh4a.activities;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.os.Bundle;
6+
import android.support.annotation.AttrRes;
7+
import android.support.design.widget.CoordinatorLayout;
8+
import android.support.v4.app.FragmentActivity;
9+
import android.support.v7.app.AppCompatActivity;
10+
11+
import com.gh4a.Gh4Application;
12+
import com.gh4a.R;
13+
import com.gh4a.widget.EditorBottomSheet;
14+
15+
import org.eclipse.egit.github.core.RepositoryId;
16+
import org.eclipse.egit.github.core.service.PullRequestService;
17+
18+
import java.io.IOException;
19+
20+
public class ReviewChangesActivity extends AppCompatActivity implements EditorBottomSheet.Callback {
21+
22+
public static Intent makeEditIntent(Context context, String repoOwner, String repoName,
23+
int prNumber, String body) {
24+
return makeIntent(context, repoOwner, repoName, prNumber)
25+
.putExtra("body", body);
26+
}
27+
28+
29+
public static Intent makeIntent(Context context, String repoOwner, String repoName,
30+
int prNumber) {
31+
return new Intent(context, ReviewChangesActivity.class)
32+
.putExtra("owner", repoOwner)
33+
.putExtra("repo", repoName)
34+
.putExtra("pr", prNumber);
35+
}
36+
37+
private CoordinatorLayout mRootLayout;
38+
protected EditorBottomSheet mEditorSheet;
39+
40+
@Override
41+
public void onCreate(Bundle savedInstanceState) {
42+
setTheme(Gh4Application.THEME == R.style.DarkTheme
43+
? R.style.BottomSheetDarkTheme : R.style.BottomSheetLightTheme);
44+
super.onCreate(savedInstanceState);
45+
46+
setContentView(R.layout.comment_editor);
47+
48+
mRootLayout = findViewById(R.id.coordinator_layout);
49+
mEditorSheet = findViewById(R.id.bottom_sheet);
50+
51+
mEditorSheet.setCallback(this);
52+
mEditorSheet.setCommentText(getIntent().getStringExtra("body"), false);
53+
54+
@AttrRes int highlightColorAttr = getIntent().getIntExtra("highlight_color_attr", 0);
55+
if (highlightColorAttr != 0) {
56+
mEditorSheet.setHighlightColor(highlightColorAttr);
57+
}
58+
59+
setResult(RESULT_CANCELED);
60+
}
61+
62+
@Override
63+
public int getCommentEditorHintResId() {
64+
return 0;
65+
}
66+
67+
@Override
68+
public void onSendCommentInBackground(String body) throws IOException {
69+
Bundle extras = getIntent().getExtras();
70+
RepositoryId repoId = new RepositoryId(extras.getString("owner"), extras.getString("repo"));
71+
72+
PullRequestService service =
73+
(PullRequestService) Gh4Application.get().getService(Gh4Application.PULL_SERVICE);
74+
75+
service.createReview(repoId, extras.getInt("pr"), body);
76+
}
77+
78+
@Override
79+
public void onCommentSent() {
80+
setResult(RESULT_OK);
81+
finish();
82+
}
83+
84+
@Override
85+
public FragmentActivity getActivity() {
86+
return this;
87+
}
88+
89+
@Override
90+
public CoordinatorLayout getRootLayout() {
91+
return mRootLayout;
92+
}
93+
}

app/src/main/java/com/gh4a/adapter/timeline/ReviewViewHolder.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,9 @@ public void bind(TimelineItem.TimelineReview item) {
191191
mDetailsHeader.setVisibility(View.VISIBLE);
192192
} else {
193193
mDetailsContainer.setVisibility(View.GONE);
194-
mShowDetailsButton.setVisibility(View.GONE);
194+
boolean isPending = Review.STATE_PENDING.equals(review.getState());
195+
mShowDetailsButton.setVisibility(mDisplayReviewDetails && isPending
196+
? View.VISIBLE : View.GONE);
195197
mDetailsHeader.setVisibility(View.GONE);
196198
}
197199

app/src/main/res/menu/pullrequest_menu.xml

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
android:title="@string/pull_request_reopen"
1212
app:showAsAction="ifRoom|withText" />
1313

14+
<item
15+
android:id="@+id/review_changes"
16+
android:title="@string/review_changes"
17+
app:showAsAction="ifRoom" />
18+
1419
<item
1520
android:id="@+id/pull_close"
1621
android:title="@string/pull_request_close"

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@
448448
<string name="close_pull_request_confirm">Do you really want to close this pull request?</string>
449449
<string name="reopen_pull_request_confirm">Do you really want to reopen this pull request?</string>
450450
<string name="pull_request_edit_title">Edit Pull Request #%1$d</string>
451+
<string name="review_changes">Review changes</string>
451452

452453
<!-- Object -->
453454
<string name="object_view_file_at">View file @ %1$s</string>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.eclipse.egit.github.core;
2+
3+
import java.io.Serializable;
4+
5+
public class DraftPullRequestReviewComment implements Serializable {
6+
private static final long serialVersionUID = -2754992759480082133L;
7+
8+
private String path;
9+
private int position;
10+
private String body;
11+
12+
public String getPath() {
13+
return path;
14+
}
15+
16+
public DraftPullRequestReviewComment setPath(String path) {
17+
this.path = path;
18+
return this;
19+
}
20+
21+
public int getPosition() {
22+
return position;
23+
}
24+
25+
public DraftPullRequestReviewComment setPosition(int position) {
26+
this.position = position;
27+
return this;
28+
}
29+
30+
public String getBody() {
31+
return body;
32+
}
33+
34+
public DraftPullRequestReviewComment setBody(String body) {
35+
this.body = body;
36+
return this;
37+
}
38+
}

github-api/src/main/java/org/eclipse/egit/github/core/service/PullRequestService.java

+59
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_COMMENTS;
3636
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_COMMITS;
37+
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_EVENTS;
3738
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_FILES;
3839
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_MERGE;
3940
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_PULLS;
@@ -770,4 +771,62 @@ public Reaction addCommentReaction(IRepositoryIdProvider repository,
770771
return client.post(uri.toString(), Collections.singletonMap("content", content),
771772
Reaction.class);
772773
}
774+
775+
public Review createReview(IRepositoryIdProvider repository, int pullRequestNumber,
776+
String body) throws IOException {
777+
String id = getId(repository);
778+
779+
StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
780+
.append('/').append(id)
781+
.append(SEGMENT_PULLS)
782+
.append('/').append(pullRequestNumber)
783+
.append(SEGMENT_REVIEWS);
784+
785+
Map<String, Object> params = new HashMap<>();
786+
params.put("body", body); //$NON-NLS-1$
787+
788+
return client.post(uri.toString(), params, Review.class);
789+
}
790+
791+
public Review submitReview(IRepositoryIdProvider repository, int pullRequestNumber,
792+
long reviewId, String body, String event) throws IOException {
793+
String id = getId(repository);
794+
795+
StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
796+
.append('/').append(id)
797+
.append(SEGMENT_PULLS)
798+
.append('/').append(pullRequestNumber)
799+
.append(SEGMENT_REVIEWS)
800+
.append('/').append(reviewId)
801+
.append(SEGMENT_EVENTS);
802+
803+
Map<String, Object> params = new HashMap<>();
804+
params.put("body", body); //$NON-NLS-1$
805+
params.put("event", event); //$NON-NLS-1$
806+
807+
return client.post(uri.toString(), params, Review.class);
808+
}
809+
810+
public void deleteReview(IRepositoryIdProvider repository, int pullRequestNumber,
811+
long reviewId) throws IOException {
812+
String repoId = getId(repository);
813+
StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
814+
.append('/').append(repoId)
815+
.append(SEGMENT_PULLS)
816+
.append('/').append(pullRequestNumber)
817+
.append(SEGMENT_REVIEWS)
818+
.append('/').append(reviewId);
819+
client.delete(uri.toString());
820+
}
821+
822+
public Review createReview(IRepositoryIdProvider repository, int pullRequestNumber,
823+
Map<String, Object> params) throws IOException {
824+
String repoId = getId(repository);
825+
StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
826+
.append('/').append(repoId)
827+
.append(SEGMENT_PULLS)
828+
.append('/').append(pullRequestNumber)
829+
.append(SEGMENT_REVIEWS);
830+
return client.post(uri.toString(), params, Review.class);
831+
}
773832
}

0 commit comments

Comments
 (0)