Skip to content

Commit

Permalink
WIP: Review creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunous committed Sep 16, 2017
1 parent 20feacf commit 973b5f1
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
android:exported="false"
android:theme="@style/BottomSheetLightTheme"
android:windowSoftInputMode="stateHidden" />
<activity
android:name=".activities.ReviewChangesActivity"
android:exported="false"
android:theme="@style/BottomSheetLightTheme"
android:windowSoftInputMode="stateHidden" />
<activity android:name=".activities.FileViewerActivity"
android:exported="false"
android:configChanges="orientation|screenSize" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.gh4a.Gh4Application;
import com.gh4a.R;

import org.eclipse.egit.github.core.CommitComment;
import org.eclipse.egit.github.core.DraftPullRequestReviewComment;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.Review;
import org.eclipse.egit.github.core.client.RequestException;
import org.eclipse.egit.github.core.service.PullRequestService;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

Review draftReview = getPendingReview(pullRequestService, repoId, prNumber);
List<DraftPullRequestReviewComment> draftComments =
getPendingComments(pullRequestService, repoId, prNumber, draftReview);

if (replyToCommentId != 0L) {
pullRequestService.replyToComment(repoId, prNumber,
(int) replyToCommentId, comment.getBody());
Expand All @@ -64,8 +77,63 @@ protected void createComment(RepositoryId repoId, CommitComment comment,
comment.setCommitId(extras.getString("commit_id"));
comment.setPath(extras.getString("path"));

pullRequestService.createComment(repoId, prNumber, comment);
draftComments.add(new DraftPullRequestReviewComment()
.setPath(extras.getString("path"))
.setPosition(extras.getInt("position"))
.setBody(comment.getBody()));

Map<String, Object> map = new HashMap<>();
map.put("commitId", extras.getString("commit_id"));
map.put("comments", draftComments);

if (draftReview != null) {
if (draftReview.getBody() != null) {
map.put("body", draftReview.getBody());
}

try {
pullRequestService.deleteReview(repoId, prNumber, draftReview.getId());
} catch (RequestException e) {
if (e.getStatus() != HttpURLConnection.HTTP_OK) {
throw e;
}
}
}

pullRequestService.createReview(repoId, prNumber, map);
}
}

@Nullable
private Review getPendingReview(PullRequestService pullRequestService, RepositoryId repoId,
int prNumber) throws IOException {
Review pendingReview = null;
List<Review> reviews = pullRequestService.getReviews(repoId, prNumber);
for (Review review : reviews) {
if (Review.STATE_PENDING.equals(review.getState())) {
pendingReview = review;
break;
}
}
return pendingReview;
}

private List<DraftPullRequestReviewComment> getPendingComments(
PullRequestService pullRequestService, RepositoryId repoId, int prNumber,
Review draftReview) throws IOException {
List<DraftPullRequestReviewComment> draftComments = new ArrayList<>();
if (draftReview != null) {
List<CommitComment> pendingComments =
pullRequestService.getReviewComments(repoId, prNumber, draftReview.getId());

for (CommitComment pendingComment : pendingComments) {
draftComments.add(new DraftPullRequestReviewComment()
.setPath(pendingComment.getPath())
.setPosition(pendingComment.getPosition())
.setBody(pendingComment.getBody()));
}
}
return draftComments;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.delete_branch:
showDeleteRestoreBranchConfirmDialog(mHeadReference == null);
break;
case R.id.review_changes: {
startActivity(ReviewChangesActivity.makeIntent(this, mRepoOwner, mRepoName,
mPullRequestNumber));
break;
}
}
return super.onOptionsItemSelected(item);
}
Expand Down
93 changes: 93 additions & 0 deletions app/src/main/java/com/gh4a/activities/ReviewChangesActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.gh4a.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.AttrRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;

import com.gh4a.Gh4Application;
import com.gh4a.R;
import com.gh4a.widget.EditorBottomSheet;

import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.service.PullRequestService;

import java.io.IOException;

public class ReviewChangesActivity extends AppCompatActivity implements EditorBottomSheet.Callback {

public static Intent makeEditIntent(Context context, String repoOwner, String repoName,
int prNumber, String body) {
return makeIntent(context, repoOwner, repoName, prNumber)
.putExtra("body", body);
}


public static Intent makeIntent(Context context, String repoOwner, String repoName,
int prNumber) {
return new Intent(context, ReviewChangesActivity.class)
.putExtra("owner", repoOwner)
.putExtra("repo", repoName)
.putExtra("pr", prNumber);
}

private CoordinatorLayout mRootLayout;
protected EditorBottomSheet mEditorSheet;

@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(Gh4Application.THEME == R.style.DarkTheme
? R.style.BottomSheetDarkTheme : R.style.BottomSheetLightTheme);
super.onCreate(savedInstanceState);

setContentView(R.layout.comment_editor);

mRootLayout = findViewById(R.id.coordinator_layout);
mEditorSheet = findViewById(R.id.bottom_sheet);

mEditorSheet.setCallback(this);
mEditorSheet.setCommentText(getIntent().getStringExtra("body"), false);

@AttrRes int highlightColorAttr = getIntent().getIntExtra("highlight_color_attr", 0);
if (highlightColorAttr != 0) {
mEditorSheet.setHighlightColor(highlightColorAttr);
}

setResult(RESULT_CANCELED);
}

@Override
public int getCommentEditorHintResId() {
return 0;
}

@Override
public void onSendCommentInBackground(String body) throws IOException {
Bundle extras = getIntent().getExtras();
RepositoryId repoId = new RepositoryId(extras.getString("owner"), extras.getString("repo"));

PullRequestService service =
(PullRequestService) Gh4Application.get().getService(Gh4Application.PULL_SERVICE);

service.createReview(repoId, extras.getInt("pr"), body);
}

@Override
public void onCommentSent() {
setResult(RESULT_OK);
finish();
}

@Override
public FragmentActivity getActivity() {
return this;
}

@Override
public CoordinatorLayout getRootLayout() {
return mRootLayout;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ public void bind(TimelineItem.TimelineReview item) {
mDetailsHeader.setVisibility(View.VISIBLE);
} else {
mDetailsContainer.setVisibility(View.GONE);
mShowDetailsButton.setVisibility(View.GONE);
boolean isPending = Review.STATE_PENDING.equals(review.getState());
mShowDetailsButton.setVisibility(mDisplayReviewDetails && isPending
? View.VISIBLE : View.GONE);
mDetailsHeader.setVisibility(View.GONE);
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/menu/pullrequest_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
android:title="@string/pull_request_reopen"
app:showAsAction="ifRoom|withText" />

<item
android:id="@+id/review_changes"
android:title="@string/review_changes"
app:showAsAction="ifRoom" />

<item
android:id="@+id/pull_close"
android:title="@string/pull_request_close"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@
<string name="close_pull_request_confirm">Do you really want to close this pull request?</string>
<string name="reopen_pull_request_confirm">Do you really want to reopen this pull request?</string>
<string name="pull_request_edit_title">Edit Pull Request #%1$d</string>
<string name="review_changes">Review changes</string>

<!-- Object -->
<string name="object_view_file_at">View file @ %1$s</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.eclipse.egit.github.core;

import java.io.Serializable;

public class DraftPullRequestReviewComment implements Serializable {
private static final long serialVersionUID = -2754992759480082133L;

private String path;
private int position;
private String body;

public String getPath() {
return path;
}

public DraftPullRequestReviewComment setPath(String path) {
this.path = path;
return this;
}

public int getPosition() {
return position;
}

public DraftPullRequestReviewComment setPosition(int position) {
this.position = position;
return this;
}

public String getBody() {
return body;
}

public DraftPullRequestReviewComment setBody(String body) {
this.body = body;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_COMMENTS;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_COMMITS;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_EVENTS;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_FILES;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_MERGE;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_PULLS;
Expand Down Expand Up @@ -770,4 +771,62 @@ public Reaction addCommentReaction(IRepositoryIdProvider repository,
return client.post(uri.toString(), Collections.singletonMap("content", content),
Reaction.class);
}

public Review createReview(IRepositoryIdProvider repository, int pullRequestNumber,
String body) throws IOException {
String id = getId(repository);

StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
.append('/').append(id)
.append(SEGMENT_PULLS)
.append('/').append(pullRequestNumber)
.append(SEGMENT_REVIEWS);

Map<String, Object> params = new HashMap<>();
params.put("body", body); //$NON-NLS-1$

return client.post(uri.toString(), params, Review.class);
}

public Review submitReview(IRepositoryIdProvider repository, int pullRequestNumber,
long reviewId, String body, String event) throws IOException {
String id = getId(repository);

StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
.append('/').append(id)
.append(SEGMENT_PULLS)
.append('/').append(pullRequestNumber)
.append(SEGMENT_REVIEWS)
.append('/').append(reviewId)
.append(SEGMENT_EVENTS);

Map<String, Object> params = new HashMap<>();
params.put("body", body); //$NON-NLS-1$
params.put("event", event); //$NON-NLS-1$

return client.post(uri.toString(), params, Review.class);
}

public void deleteReview(IRepositoryIdProvider repository, int pullRequestNumber,
long reviewId) throws IOException {
String repoId = getId(repository);
StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
.append('/').append(repoId)
.append(SEGMENT_PULLS)
.append('/').append(pullRequestNumber)
.append(SEGMENT_REVIEWS)
.append('/').append(reviewId);
client.delete(uri.toString());
}

public Review createReview(IRepositoryIdProvider repository, int pullRequestNumber,
Map<String, Object> params) throws IOException {
String repoId = getId(repository);
StringBuilder uri = new StringBuilder(SEGMENT_REPOS)
.append('/').append(repoId)
.append(SEGMENT_PULLS)
.append('/').append(pullRequestNumber)
.append(SEGMENT_REVIEWS);
return client.post(uri.toString(), params, Review.class);
}
}

0 comments on commit 973b5f1

Please sign in to comment.