Skip to content

Commit

Permalink
🐛 retrieve correct data from response
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Jun 11, 2024
1 parent c4d03a7 commit d0adbe1
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@
@TemplateData
public class DataCommonItem extends DataCommonObject {

static final String ISSUE_FIELDS = COMMON_OBJECT_FIELDS + """
public static final String ISSUE_FIELDS = COMMON_OBJECT_FIELDS + """
number
title
body
closed
closedAt
""";

static final String PR_FIELDS = ISSUE_FIELDS + """
public static final String PR_FIELDS = ISSUE_FIELDS + """
reviewDecision
""";

static final String ISSUE_FIELDS_MIN = COMMON_OBJECT_MIN + """
public static final String ISSUE_FIELDS_MIN = COMMON_OBJECT_MIN + """
number
title
""";

static final String PR_FIELDS_MIN = ISSUE_FIELDS_MIN + """
public static final String PR_FIELDS_MIN = ISSUE_FIELDS_MIN + """
reviewDecision
""";

Expand Down Expand Up @@ -91,7 +91,10 @@ public static DataCommonItem createIssue(QueryContext qc, String title, String b
}

public static DataCommonItem editIssueDescription(QueryContext qc,
String nodeId, String bodyString) {
String nodeId, String bodyString, String issueFields) {

issueFields = issueFields == null ? ISSUE_FIELDS_MIN : issueFields;

Map<String, Object> variables = new HashMap<>();
variables.put("id", nodeId);
variables.put("body", bodyString);
Expand All @@ -104,7 +107,7 @@ public static DataCommonItem editIssueDescription(QueryContext qc,
}) {
clientMutationId
issue {
""" + ISSUE_FIELDS_MIN + """
""" + issueFields + """
}
}
}
Expand All @@ -114,11 +117,14 @@ public static DataCommonItem editIssueDescription(QueryContext qc,
return null;
}
JsonObject result = JsonAttribute.updateIssue.jsonObjectFrom(response.getData());
return JsonAttribute.pullRequest.commonItemFrom(result);
return JsonAttribute.issue.commonItemFrom(result);
}

public static DataCommonItem editPullRequestDescription(QueryContext qc,
String nodeId, String bodyString) {
String nodeId, String bodyString, String prFields) {

prFields = prFields == null ? PR_FIELDS_MIN : prFields;

Map<String, Object> variables = new HashMap<>();
variables.put("id", nodeId);
variables.put("body", bodyString);
Expand All @@ -131,7 +137,7 @@ public static DataCommonItem editPullRequestDescription(QueryContext qc,
}) {
clientMutationId
pullRequest {
""" + PR_FIELDS_MIN + """
""" + prFields + """
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ static DataDiscussion queryDiscussion(QueryContext qc, String nodeId) {
* package private. See QueryHelper / QueryContext
*/
static DataDiscussion editDiscussion(QueryContext qc, String nodeId,
String modifiedText) {
String modifiedText, String fields) {

fields = fields == null ? DISCUSSION_FIELDS : fields;

Map<String, Object> variables = new HashMap<>();
variables.put("id", nodeId);
variables.put("body", modifiedText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ public DataCommonItem createItem(EventType eventType, String title, String descr
}

public DataCommonItem updateItemDescription(EventType eventType, String nodeId, String bodyString) {
return updateItemDescription(eventType, nodeId, bodyString, null);
}

public DataCommonItem updateItemDescription(EventType eventType, String nodeId, String bodyString, String fields) {
if (isDryRun()) {
Log.debugf("[%s] updateItemDescription would set body to: %s", getLogId(), bodyString);
return null;
Expand All @@ -486,11 +490,11 @@ public DataCommonItem updateItemDescription(EventType eventType, String nodeId,

return switch (eventType) {
case discussion, discussion_comment ->
DataDiscussion.editDiscussion(this, nodeId, bodyString);
DataDiscussion.editDiscussion(this, nodeId, bodyString, fields);
case issue, issue_comment ->
DataCommonItem.editIssueDescription(this, nodeId, bodyString);
DataCommonItem.editIssueDescription(this, nodeId, bodyString, fields);
case pull_request, pull_request_review ->
DataCommonItem.editPullRequestDescription(this, nodeId, bodyString);
DataCommonItem.editPullRequestDescription(this, nodeId, bodyString, fields);
default -> {
logAndSendEmail(getLogId(), "updateItemDescription: Unknown event type " + eventType, null);
yield null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.commonhaus.automation.admin.api;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

import org.commonhaus.automation.admin.api.ApplicationData.ApplicationPost;
import org.commonhaus.automation.admin.api.ApplicationData.Feedback;
import org.commonhaus.automation.admin.api.CommonhausUser.MemberStatus;
import org.commonhaus.automation.admin.api.CommonhausUser.MembershipApplication;
import org.commonhaus.automation.admin.github.AppContextService;
import org.commonhaus.automation.admin.github.CommonhausDatastore;
import org.commonhaus.automation.admin.github.CommonhausDatastore.UpdateEvent;
Expand Down Expand Up @@ -82,11 +85,33 @@ public void handleApplicationEvent(ScopedQueryContext qc, GHIssue issue, DataCom
}
}

ApplicationData findUserApplication(MemberSession session, String applicationId) {
public ApplicationData userUpdateApplicationIssue(
MemberSession session,
ScopedQueryContext qc,
ApplicationData applicationData,
ApplicationPost applicationPost) {

String content = ApplicationData.issueContent(session, applicationPost);
Collection<DataLabel> labels = qc.findLabels(List.of(ApplicationData.NEW));
MembershipApplication application = applicationData == null ? null : applicationData.application;

DataCommonItem item = application == null
? qc.createItem(EventType.issue,
ApplicationData.createTitle(session),
content,
labels)
: qc.updateItemDescription(EventType.issue, application.nodeId(), content, DataCommonItem.ISSUE_FIELDS);

return item == null
? null
: new ApplicationData(session.login(), item);
}

ApplicationData findUserApplication(MemberSession session, String applicationId, boolean withComments) {
ScopedQueryContext qc = ctx.getDatastoreContext();
DataCommonItem issue = qc.getItem(EventType.issue, applicationId);
ApplicationData application = new ApplicationData(session.login(), issue);
if (application.isValid()) {
if (application.isValid() && withComments) {
Feedback feedback = getFeedback(qc, applicationId, issue.mostRecentEdit());
if (feedback != null) {
application.setFeedback(feedback);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.commonhaus.automation.admin.api;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.enterprise.context.ApplicationScoped;
Expand All @@ -21,9 +19,6 @@
import org.commonhaus.automation.admin.github.CommonhausDatastore;
import org.commonhaus.automation.admin.github.CommonhausDatastore.UpdateEvent;
import org.commonhaus.automation.admin.github.ScopedQueryContext;
import org.commonhaus.automation.github.context.DataCommonItem;
import org.commonhaus.automation.github.context.DataLabel;
import org.commonhaus.automation.github.context.EventType;

import io.quarkus.logging.Log;
import io.quarkus.security.Authenticated;
Expand Down Expand Up @@ -57,7 +52,7 @@ public Response getApplication() {
MembershipApplication application = user.application();
ApplicationData applicationData = application == null
? null
: memberApplicationProcess.findUserApplication(session, application.nodeId());
: memberApplicationProcess.findUserApplication(session, application.nodeId(), true);

if (application == null && applicationData == null) {
return Response.status(Response.Status.NOT_FOUND).build();
Expand All @@ -79,6 +74,7 @@ public Response getApplication() {
public Response setApplication(ApplicationPost applicationPost) {
try {
if (applicationPost == null) {
Log.errorf("setApplication|%s: No application data", session.login());
return Response.status(Response.Status.BAD_REQUEST).build();
}
CommonhausUser user = datastore.getCommonhausUser(session, false, false);
Expand All @@ -88,7 +84,7 @@ public Response setApplication(ApplicationPost applicationPost) {
MembershipApplication application = user.application();
ApplicationData applicationData = application == null
? null
: memberApplicationProcess.findUserApplication(session, application.nodeId());
: memberApplicationProcess.findUserApplication(session, application.nodeId(), false);

if (applicationData != null && !applicationData.isValid()) {
applicationData = null;
Expand Down Expand Up @@ -143,7 +139,8 @@ private Response doUserApplicationUpdate(CommonhausUser user, ApplicationData ap
}

// UPDATE APPLICATION ISSUE
ApplicationData updated = userUpdateApplicationIssue(qc, applicationData, post);
ApplicationData updated = memberApplicationProcess.userUpdateApplicationIssue(session, qc, applicationData,
post);

if (qc.hasErrors()) {
Throwable e = qc.bundleExceptions();
Expand All @@ -152,6 +149,7 @@ private Response doUserApplicationUpdate(CommonhausUser user, ApplicationData ap
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
if (updated == null) {
Log.errorf("doUserApplicationUpdate|%s: Updated data was not returned", session.login());
return Response.status(Response.Status.BAD_REQUEST).build();
}
final MembershipApplication application = updated.application;
Expand All @@ -177,24 +175,4 @@ private Response doUserApplicationUpdate(CommonhausUser user, ApplicationData ap
}
}

public ApplicationData userUpdateApplicationIssue(ScopedQueryContext qc,
ApplicationData applicationData,
ApplicationPost applicationPost) {

String content = ApplicationData.issueContent(session, applicationPost);
Collection<DataLabel> labels = qc.findLabels(List.of(ApplicationData.NEW));
MembershipApplication application = applicationData == null ? null : applicationData.application;

DataCommonItem item = application == null
? qc.createItem(EventType.issue,
ApplicationData.createTitle(session),
content,
labels)
: qc.updateItemDescription(EventType.issue, application.nodeId(), content);

return item == null
? null
: new ApplicationData(session.login(), item);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.contains;
Expand All @@ -25,6 +26,7 @@
import jakarta.inject.Inject;

import org.commonhaus.automation.admin.AdminDataCache;
import org.commonhaus.automation.admin.api.ApplicationData.ApplicationPost;
import org.commonhaus.automation.admin.api.CommonhausUser.Attestation;
import org.commonhaus.automation.admin.api.CommonhausUser.AttestationPost;
import org.commonhaus.automation.admin.api.CommonhausUser.MemberStatus;
Expand Down Expand Up @@ -507,6 +509,63 @@ void testGetApplicationWithFeedback() throws Exception {
assertThat(result.data.status).isEqualTo(MemberStatus.PENDING);
}

@Test
@TestSecurity(user = botLogin)
@OidcSecurity(userinfo = {
@UserInfo(key = "login", value = botLogin),
@UserInfo(key = "id", value = botId + ""),
@UserInfo(key = "node_id", value = botNodeId),
@UserInfo(key = "avatar_url", value = "https://avatars.githubusercontent.com/u/156364140?v=4")
})
void testSubmitApplication() throws Exception {
GHUser botUser = botGithub.getUser(botLogin);
appendMockTeam(organizationName + "/team-quorum-default", botUser);

mockExistingCommonhausData("src/test/resources/haus-member-application.yaml");

Response queryIssue = mockResponse("src/test/resources/github/queryIssue-ApplicationMatch.json");
when(mockContext.mocks.installationGraphQLClient(installationId)
.executeSync(contains("query($id: ID!) {"), anyMap()))
.thenReturn(queryIssue);

Response updateIssue = mockResponse("src/test/resources/github/mutableUpdateIssue.json");
when(mockContext.mocks.installationGraphQLClient(installationId)
.executeSync(contains("updateIssue(input: {"), anyMap()))
.thenReturn(updateIssue);

ApplicationPost application = new ApplicationPost(
"unknown",
"draft");
String applyJson = mapper.writeValueAsString(application);

// update to fix member status
GHContentBuilder builder = mockUpdateCommonhausData();

given()
.log().all()
.when()
.contentType(ContentType.JSON)
.body(applyJson)
.post("/apply")
.then()
.log().all()
.statusCode(200)
.body("HAUS.goodUntil.attestation.test.withStatus", equalTo("UNKNOWN")) // mock response
.body("APPLY.created", notNullValue()) // from mutableUpdateIssue.json
.body("APPLY.updated", notNullValue()) // from mutableUpdateIssue.json
.body("APPLY.contributions", equalTo("testContrib")) // from mutableUpdateIssue.json
.body("APPLY.additionalNotes", equalTo("testNotes")); // from mutableUpdateIssue.json

// Look at data passed to mutable operation to update user data
final ArgumentCaptor<String> contentCaptor = ArgumentCaptor.forClass(String.class);
verify(builder).content(contentCaptor.capture());

var result = AppContextService.yamlMapper().readValue(contentCaptor.getValue(), CommonhausUser.class);

assertThat(result.application).isNotNull();
assertThat(result.data.status).isEqualTo(MemberStatus.PENDING); // changed from UNKNOWN -> PENDING
}

void mockExistingCommonhausData() throws IOException {
mockExistingCommonhausData("src/test/resources/commonhaus-user.yaml");
}
Expand Down
19 changes: 19 additions & 0 deletions cf-admin-bot/src/test/resources/github/mutableUpdateIssue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"updateIssue": {
"clientMutationId": null,
"issue": {
"id": "I_kwDOL8tG0s6LSQkK",
"author": {
"login": "commonhaus-bot",
"url": "https://api.github.com/users/commonhaus-bot"
},
"url": "https://github.com/commonhaus-test/ops-test/issues/1",
"number": 1,
"title": "Membership application: Any Name (commonhaus-bot)",
"body": "[commonhaus-bot](null)\n\n> [!TIP]\n> - Include \"::response::\" in your comment to send feedback to the applicant.\n> - Add the 'application/accepted' label to accept the application.\n> - Add the 'application/declined' label to decline or reject the application.\n## Contribution Details\n<!--CONTRIBUTION::-->\ntestContrib\n<!--::CONTRIBUTION-->\n\n## Additional Notes\n<!--NOTES::-->\ntestNotes\n<!--::NOTES-->",
"createdAt": "2024-06-05T20:56:05Z",
"lastEditedAt": "2024-06-05T21:20:22Z",
"updatedAt": "2024-06-05T21:20:23Z"
}
}
}

0 comments on commit d0adbe1

Please sign in to comment.