-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(posts): add deletePost GraphQL endpoint #6813
Merged
jjoyce0510
merged 1 commit into
datahub-project:master
from
aditya-radhakrishnan:ar--12-19-delete-post
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ql-core/src/main/java/com/linkedin/datahub/graphql/resolvers/post/DeletePostResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.linkedin.datahub.graphql.resolvers.post; | ||
|
||
import com.datahub.authentication.Authentication; | ||
import com.datahub.authentication.post.PostService; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.common.urn.UrnUtils; | ||
import com.linkedin.datahub.graphql.QueryContext; | ||
import com.linkedin.datahub.graphql.authorization.AuthorizationUtils; | ||
import com.linkedin.datahub.graphql.exception.AuthorizationException; | ||
import graphql.schema.DataFetcher; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import java.util.concurrent.CompletableFuture; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class DeletePostResolver implements DataFetcher<CompletableFuture<Boolean>> { | ||
private final PostService _postService; | ||
|
||
@Override | ||
public CompletableFuture<Boolean> get(final DataFetchingEnvironment environment) throws Exception { | ||
final QueryContext context = environment.getContext(); | ||
|
||
if (!AuthorizationUtils.canCreateGlobalAnnouncements(context)) { | ||
throw new AuthorizationException( | ||
"Unauthorized to delete posts. Please contact your DataHub administrator if this needs corrective action."); | ||
} | ||
|
||
final Urn postUrn = UrnUtils.getUrn(environment.getArgument("urn")); | ||
final Authentication authentication = context.getAuthentication(); | ||
|
||
return CompletableFuture.supplyAsync(() -> { | ||
try { | ||
return _postService.deletePost(postUrn, authentication); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Failed to create a new post", e); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...ore/src/test/java/com/linkedin/datahub/graphql/resolvers/post/DeletePostResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.linkedin.datahub.graphql.resolvers.post; | ||
|
||
import com.datahub.authentication.Authentication; | ||
import com.datahub.authentication.post.PostService; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.common.urn.UrnUtils; | ||
import com.linkedin.datahub.graphql.QueryContext; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import static com.linkedin.datahub.graphql.TestUtils.*; | ||
import static org.mockito.Mockito.*; | ||
import static org.testng.Assert.*; | ||
|
||
|
||
public class DeletePostResolverTest { | ||
private static final String POST_URN_STRING = "urn:li:post:123"; | ||
private PostService _postService; | ||
private DeletePostResolver _resolver; | ||
private DataFetchingEnvironment _dataFetchingEnvironment; | ||
private Authentication _authentication; | ||
|
||
@BeforeMethod | ||
public void setupTest() throws Exception { | ||
_postService = mock(PostService.class); | ||
_dataFetchingEnvironment = mock(DataFetchingEnvironment.class); | ||
_authentication = mock(Authentication.class); | ||
|
||
_resolver = new DeletePostResolver(_postService); | ||
} | ||
|
||
@Test | ||
public void testNotAuthorizedFails() { | ||
QueryContext mockContext = getMockDenyContext(); | ||
when(_dataFetchingEnvironment.getContext()).thenReturn(mockContext); | ||
|
||
assertThrows(() -> _resolver.get(_dataFetchingEnvironment).join()); | ||
} | ||
|
||
@Test | ||
public void testDeletePost() throws Exception { | ||
Urn postUrn = UrnUtils.getUrn(POST_URN_STRING); | ||
|
||
QueryContext mockContext = getMockAllowContext(); | ||
when(_dataFetchingEnvironment.getContext()).thenReturn(mockContext); | ||
when(mockContext.getAuthentication()).thenReturn(_authentication); | ||
when(_dataFetchingEnvironment.getArgument(eq("urn"))).thenReturn(POST_URN_STRING); | ||
when(_postService.deletePost(eq(postUrn), eq(_authentication))).thenReturn(true); | ||
|
||
assertTrue(_resolver.get(_dataFetchingEnvironment).join()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import com.linkedin.common.Media; | ||
import com.linkedin.common.MediaType; | ||
import com.linkedin.common.url.Url; | ||
import com.linkedin.common.urn.Urn; | ||
import com.linkedin.entity.client.EntityClient; | ||
import com.linkedin.metadata.key.PostKey; | ||
import com.linkedin.mxe.MetadataChangeProposal; | ||
|
@@ -68,4 +69,13 @@ public boolean createPost(@Nonnull String postType, @Nonnull PostContent postCon | |
|
||
return true; | ||
} | ||
|
||
public boolean deletePost(@Nonnull Urn postUrn, @Nonnull Authentication authentication) | ||
throws RemoteInvocationException { | ||
if (!_entityClient.exists(postUrn, authentication)) { | ||
throw new RuntimeException("Post does not exist"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: I'd prefer to see an DataHubGraphQLException with a NOT_FOUND code (404) |
||
} | ||
_entityClient.deleteEntity(postUrn, authentication); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future, once the scope of Posts expands, we will need to change this permission. Maybe it's better to check the owner of the post.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This addresses the use case for now, and we can get it in immediately. But I'd like to discuss this one offline a bit more.