Skip to content

Commit

Permalink
feat: Improve URL for connectors - MEED-2408 - Meeds-io/MIPs#64 (#100)
Browse files Browse the repository at this point in the history
This PR will add GitHub connector URLs
  • Loading branch information
AzmiTouil committed Sep 18, 2023
1 parent 8c07a11 commit a1ce35a
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,34 @@ public Response getWebHooks(@QueryParam("offset") int offset,
}
}

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{webHookId}")
@RolesAllowed("users")
@Operation(summary = "Retrieves a webHook by its technical identifier", method = "GET")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Request fulfilled"),
@ApiResponse(responseCode = "401", description = "Unauthorized operation"),
@ApiResponse(responseCode = "400", description = "Invalid query input"),
@ApiResponse(responseCode = "404", description = "Not found"),
@ApiResponse(responseCode = "500", description = "Internal server error"), })
public Response getWebHookById(@Parameter(description = "WebHook technical identifier", required = true) @PathParam("webHookId") long webHookId) {
if (webHookId == 0) {
return Response.status(Response.Status.BAD_REQUEST).entity("WebHook Id must be not null").build();
}
String currentUser = getCurrentUser();
try {
WebHook webHook = webhookService.getWebhookId(webHookId, currentUser);
return Response.ok(WebHookBuilder.toRestEntity(webhookService, githubConsumerService, webHook)).build();
} catch (IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
} catch (IllegalAccessException e) {
return Response.status(Response.Status.UNAUTHORIZED).entity(e.getMessage()).build();
} catch (ObjectNotFoundException e) {
return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
}
}

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@RolesAllowed("users")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ public interface WebhookService {
*/
List<WebHook> getWebhooks(String currentUser, int offset, int limit, boolean forceUpdate) throws IllegalAccessException;

/**
* Retrieves a webHook identified by its technical identifier.
*
* @param webhookId WebHook technical identifier
* @return found {@link WebHook}
*/
WebHook getWebhookId(long webhookId);

/**
* Retrieves a webHook identified by its technical identifier accessed by a user
*
* @param webhookId WebHook technical identifier
* @param username user name attempting to access connector webhook
* @return found {@link WebHook}
* @throws IllegalAccessException when user is not authorized to access webhook
* @throws ObjectNotFoundException webhook not found
*/
WebHook getWebhookId(long webhookId, String username) throws IllegalAccessException, ObjectNotFoundException;

/**
* Get available github hooks using offset and limit.
*
Expand Down Expand Up @@ -197,8 +216,9 @@ int countOrganizationRepos(long organizationRemoteId, String currentUser) throws
* @throws IllegalAccessException when user is not authorized enables/disables
* organization event
*/
void setEventEnabledForOrganization(long eventId, long organizationId, boolean enabled, String currentUser) throws IllegalAccessException,
ObjectNotFoundException;

void setEventEnabledForOrganization(long eventId,
long organizationId,
boolean enabled,
String currentUser) throws IllegalAccessException, ObjectNotFoundException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ public List<WebHook> getWebhooks(String currentUser, int offset, int limit, bool
return getWebhooks(offset, limit, forceUpdate);
}

public WebHook getWebhookId(long webhookId, String username) throws IllegalAccessException, ObjectNotFoundException {
if (!Utils.isRewardingManager(username)) {
throw new IllegalAccessException(AUTHORIZED_TO_ACCESS_GIT_HUB_HOOKS);
}
WebHook webHook = getWebhookId(webhookId);
if (webHook == null) {
throw new ObjectNotFoundException("Webhook doesn't exist");
}
return webHook;
}

@Override
public WebHook getWebhookId(long webhookId) {
if (webhookId <= 0) {
throw new IllegalArgumentException("Webhook id is mandatory");
}
return webHookStorage.getWebHookById(webhookId);
}

public List<WebHook> getWebhooks(int offset, int limit, boolean forceUpdate) {
if (forceUpdate) {
forceUpdateWebhooks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ export default {
editing: false,
displayHookDetail: false,
selectedHook: null,
webhooks: []
webhooks: [],
githubConnectorLinkBasePath: '/portal/g/:platform:rewarding/gamificationConnectorsAdministration#github',
};
},
computed: {
Expand All @@ -182,6 +183,15 @@ export default {
return this.connectionSettingStored && !this.enabled && this.webhooksLength > 0;
},
},
watch: {
displayHookDetail() {
if (this.displayHookDetail && this.selectedHook?.id) {
window.history.replaceState('gamification connectors', this.$t('gamification.connectors.label.connectors'), `${this.githubConnectorLinkBasePath}-${this.selectedHook?.id}`);
} else {
window.history.replaceState('gamification connectors', this.$t('gamification.connectors.label.connectors'), `${this.githubConnectorLinkBasePath}-configuration`);
}
},
},
created() {
this.$root.$on('github-hook-detail', this.openHookDetail);
if (!this.apiKey && !this.secretKey && !this.redirectUrl) {
Expand All @@ -196,6 +206,12 @@ export default {
this.redirectUrl = redirectUrl;
this.saveConnectorSetting(this.enabled);
});
const fragment = document.location.hash.substring(1);
const match = fragment.split('-');
const hookId = Number(match[1]);
if (hookId) {
this.openHookDetailById(hookId);
}
},
methods: {
saveConnectorSetting(status) {
Expand Down Expand Up @@ -237,6 +253,14 @@ export default {
webhooksUpdated(webhooks){
this.webhooks = webhooks;
},
openHookDetailById(id) {
this.$githubConnectorService.getGithubWebHookById(id)
.then(hook => {
if (hook?.id) {
this.openHookDetail(hook);
}
});
},
}
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ export function getGithubWebHooks(offset, limit) {
});
}

export function getGithubWebHookById(hookId) {
return fetch(`${eXo.env.portal.context}/${eXo.env.portal.rest}/gamification/connectors/github/hooks/${hookId}`, {
method: 'GET',
credentials: 'include',
}).then((resp) => {
if (resp?.ok) {
return resp.json();
} else {
throw new Error('Error when getting github webhook');
}
});
}

export function saveGithubWebHook(organizationName, accessToken) {
const formData = new FormData();
formData.append('organizationName', organizationName);
Expand Down

0 comments on commit a1ce35a

Please sign in to comment.