Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/src/content/docs/reference/components/zeplin.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@ Version: 1
## Triggers


### Project Note
Triggers when new note is created, deleted or updated in specified project.

#### Type: DYNAMIC_WEBHOOK
#### Properties

| Name | Type | Control Type | Description |
|:--------------:|:------------:|:--------------------:|:-------------------:|
| Project | STRING | SELECT | |


### Output



Type: OBJECT


#### Properties

| Type | Control Type |
|:------------:|:--------------------:|
| {STRING\(id), STRING\(type), {STRING\(id), STRING\(status), [{STRING\(id), {STRING\(id), STRING\(email), STRING\(username)}\(author), STRING\(content)}]\(comments)}\(data)} | OBJECT_BUILDER |
| STRING | TEXT |
| STRING | TEXT |







<hr />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

package com.bytechef.component.zeplin;

import static com.bytechef.component.zeplin.constant.ZeplinConstants.PROJECT_ID;

import com.bytechef.component.OpenApiComponentHandler;
import com.bytechef.component.definition.ActionDefinition;
import com.bytechef.component.definition.ComponentCategory;
import com.bytechef.component.definition.ComponentDsl.ModifiableComponentDefinition;
import com.bytechef.component.definition.ComponentDsl.ModifiableProperty;
import com.bytechef.component.definition.ComponentDsl.ModifiableStringProperty;
import com.bytechef.component.definition.ComponentDsl.ModifiableTriggerDefinition;
import com.bytechef.component.definition.OptionsDataSource.ActionOptionsFunction;
import com.bytechef.component.zeplin.trigger.ZeplinProjectNoteTrigger;
import com.bytechef.component.zeplin.util.ZeplinUtils;
import com.google.auto.service.AutoService;
import java.util.List;
import java.util.Objects;

/**
Expand All @@ -33,6 +38,11 @@
@AutoService(OpenApiComponentHandler.class)
public class ZeplinComponentHandler extends AbstractZeplinComponentHandler {

@Override
public List<ModifiableTriggerDefinition> getTriggers() {
return List.of(ZeplinProjectNoteTrigger.TRIGGER_DEFINITION);
}

@Override
public ModifiableComponentDefinition modifyComponent(ModifiableComponentDefinition modifiableComponentDefinition) {
return modifiableComponentDefinition
Expand All @@ -45,7 +55,7 @@ public ModifiableComponentDefinition modifyComponent(ModifiableComponentDefiniti
public ModifiableProperty<?> modifyProperty(
ActionDefinition actionDefinition, ModifiableProperty<?> modifiableProperty) {

if (Objects.equals(modifiableProperty.getName(), "project_id")) {
if (Objects.equals(modifiableProperty.getName(), PROJECT_ID)) {
((ModifiableStringProperty) modifiableProperty)
.options((ActionOptionsFunction<String>) ZeplinUtils::getProjectIdOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2023-present ByteChef Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bytechef.component.zeplin.constant;

/**
* @author Monika Kušter
*/
public class ZeplinConstants {

private ZeplinConstants() {
}

public static final String ID = "id";
public static final String PROJECT_ID = "project_id";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2023-present ByteChef Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bytechef.component.zeplin.trigger;

import static com.bytechef.component.definition.ComponentDsl.ModifiableTriggerDefinition;
import static com.bytechef.component.definition.ComponentDsl.array;
import static com.bytechef.component.definition.ComponentDsl.object;
import static com.bytechef.component.definition.ComponentDsl.outputSchema;
import static com.bytechef.component.definition.ComponentDsl.string;
import static com.bytechef.component.definition.ComponentDsl.trigger;
import static com.bytechef.component.zeplin.constant.ZeplinConstants.ID;
import static com.bytechef.component.zeplin.constant.ZeplinConstants.PROJECT_ID;

import com.bytechef.component.definition.Context.Http;
import com.bytechef.component.definition.OptionsDataSource.TriggerOptionsFunction;
import com.bytechef.component.definition.Parameters;
import com.bytechef.component.definition.TriggerContext;
import com.bytechef.component.definition.TriggerDefinition.HttpHeaders;
import com.bytechef.component.definition.TriggerDefinition.HttpParameters;
import com.bytechef.component.definition.TriggerDefinition.TriggerType;
import com.bytechef.component.definition.TriggerDefinition.WebhookBody;
import com.bytechef.component.definition.TriggerDefinition.WebhookEnableOutput;
import com.bytechef.component.definition.TriggerDefinition.WebhookMethod;
import com.bytechef.component.definition.TriggerDefinition.WebhookValidateResponse;
import com.bytechef.component.definition.TypeReference;
import com.bytechef.component.zeplin.util.ZeplinUtils;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* @author Monika Kušter
*/
public class ZeplinProjectNoteTrigger {

public static final ModifiableTriggerDefinition TRIGGER_DEFINITION = trigger("projectNote")
.title("Project Note")
.description("Triggers when new note is created, deleted or updated in specified project.")
.properties(
string(PROJECT_ID)
.label("Project")
.options((TriggerOptionsFunction<String>) ZeplinUtils::getProjectIdOptions)
.required(true))
.type(TriggerType.DYNAMIC_WEBHOOK)
.output(
outputSchema(
object()
.properties(
object("context")
.properties(
string(ID),
string("type"),
object("data")
.properties(
string(ID),
string("status"),
array("comments")
.items(
object()
.properties(
string(ID),
object("author")
.properties(
string(ID),
string("email"),
string("username")),
string("content"))))),
string("action"),
string("event"))))
.webhookEnable(ZeplinProjectNoteTrigger::webhookEnable)
.webhookDisable(ZeplinProjectNoteTrigger::webhookDisable)
.webhookRequest(ZeplinProjectNoteTrigger::webhookRequest)
.webhookValidateOnEnable(ZeplinProjectNoteTrigger::webhookValidate);

private ZeplinProjectNoteTrigger() {
}

public static WebhookValidateResponse webhookValidate(
Parameters inputParameters, HttpHeaders headers, HttpParameters parameters, WebhookBody body,
WebhookMethod method, TriggerContext context) {

return new WebhookValidateResponse(204);
}

protected static WebhookEnableOutput webhookEnable(
Parameters inputParameters, Parameters connectionParameters, String webhookUrl,
String workflowExecutionId, TriggerContext triggerContext) {

Map<String, String> body = triggerContext
.http(http -> http.post("/projects/" + inputParameters.getRequiredString(PROJECT_ID) + "/webhooks"))
.body(Http.Body.of(
"url", webhookUrl,
"secret", UUID.randomUUID(),
"events", List.of("project.note")))
.configuration(Http.responseType(Http.ResponseType.JSON))
.execute()
.getBody(new TypeReference<>() {});

return new WebhookEnableOutput(Map.of(ID, body.get(ID)), null);
}

protected static void webhookDisable(
Parameters inputParameters, Parameters connectionParameters, Parameters outputParameters,
String workflowExecutionId, TriggerContext triggerContext) {

triggerContext.http(http -> http
.delete("/projects/" + inputParameters.getRequiredString(PROJECT_ID) + "/webhooks/"
+ outputParameters.getRequiredString(ID)))
.configuration(Http.responseType(Http.ResponseType.JSON))
.execute();
}

protected static Object webhookRequest(
Parameters inputParameters, Parameters connectionParameters, HttpHeaders headers, HttpParameters parameters,
WebhookBody body, WebhookMethod method, WebhookEnableOutput output, TriggerContext triggerContext) {

return body.getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.bytechef.component.zeplin.util;

import static com.bytechef.component.definition.ComponentDsl.option;
import static com.bytechef.component.zeplin.constant.ZeplinConstants.ID;

import com.bytechef.component.definition.Context;
import com.bytechef.component.definition.Context.Http;
Expand Down Expand Up @@ -48,7 +49,7 @@ public static List<Option<String>> getProjectIdOptions(
List<Option<String>> options = new ArrayList<>();

for (Map<String, Object> map : body) {
options.add(option((String) map.get("name"), (String) map.get("id")));
options.add(option((String) map.get("name"), (String) map.get(ID)));
}

return options;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2023-present ByteChef Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.bytechef.component.zeplin.trigger;

import static com.bytechef.component.zeplin.constant.ZeplinConstants.ID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.bytechef.component.definition.Context.Http;
import com.bytechef.component.definition.Parameters;
import com.bytechef.component.definition.TriggerContext;
import com.bytechef.component.definition.TriggerDefinition;
import com.bytechef.component.definition.TriggerDefinition.HttpHeaders;
import com.bytechef.component.definition.TriggerDefinition.HttpParameters;
import com.bytechef.component.definition.TriggerDefinition.WebhookBody;
import com.bytechef.component.definition.TriggerDefinition.WebhookEnableOutput;
import com.bytechef.component.definition.TriggerDefinition.WebhookMethod;
import com.bytechef.component.definition.TypeReference;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;

/**
* @author Monika Kušter
*/
class ZeplinProjectNoteTriggerTest {

private final ArgumentCaptor<Http.Body> bodyArgumentCaptor = ArgumentCaptor.forClass(Http.Body.class);
private final Http.Executor mockedExecutor = mock(Http.Executor.class);
private final HttpHeaders mockedHttpHeaders = mock(HttpHeaders.class);
private final Object mockedObject = mock(Object.class);
private final HttpParameters mockedHttpParameters = mock(HttpParameters.class);
private final Parameters mockedParameters = mock(Parameters.class);
private final Http.Response mockedResponse = mock(Http.Response.class);
private final WebhookBody mockedWebhookBody = mock(WebhookBody.class);
private final WebhookEnableOutput mockedWebhookEnableOutput = mock(WebhookEnableOutput.class);
private final WebhookMethod mockedWebhookMethod = mock(WebhookMethod.class);
private final TriggerContext mockedTriggerContext = mock(TriggerContext.class);
private static final String TEST_WORKFLOW_EXECUTION_ID = "testWorkflowExecutionId";

@Test
void testWebhookEnable() {
when(mockedTriggerContext.http(any()))
.thenReturn(mockedExecutor);
when(mockedExecutor.body(bodyArgumentCaptor.capture()))
.thenReturn(mockedExecutor);
when(mockedExecutor.configuration(any()))
.thenReturn(mockedExecutor);
when(mockedExecutor.execute())
.thenReturn(mockedResponse);
when(mockedResponse.getBody(any(TypeReference.class)))
.thenReturn(Map.of(ID, "abc"));
UUID uuid = UUID.randomUUID();

try (MockedStatic<UUID> uuidMockedStatic = mockStatic(UUID.class)) {

uuidMockedStatic.when(UUID::randomUUID)
.thenReturn(uuid);

TriggerDefinition.WebhookEnableOutput webhookEnableOutput = ZeplinProjectNoteTrigger.webhookEnable(
mockedParameters, mockedParameters, "testWebhookUrl", TEST_WORKFLOW_EXECUTION_ID, mockedTriggerContext);

Map<String, ?> parameters = webhookEnableOutput.parameters();
LocalDateTime webhookExpirationDate = webhookEnableOutput.webhookExpirationDate();

Map<String, Object> expectedParameters = Map.of(ID, "abc");

assertEquals(expectedParameters, parameters);
assertNull(webhookExpirationDate);

Http.Body body = bodyArgumentCaptor.getValue();

assertEquals(
Map.of("url", "testWebhookUrl",
"secret", uuid,
"events", List.of("project.note")),
body.getContent());
}
}

@Test
void testWebhookDisable() {
when(mockedTriggerContext.http(any()))
.thenReturn(mockedExecutor);
when(mockedExecutor.configuration(any()))
.thenReturn(mockedExecutor);
when(mockedExecutor.execute())
.thenReturn(mockedResponse);

ZeplinProjectNoteTrigger.webhookDisable(
mockedParameters, mockedParameters, mockedParameters, TEST_WORKFLOW_EXECUTION_ID, mockedTriggerContext);

verify(mockedTriggerContext, times(1)).http(any());
verify(mockedExecutor, times(1)).configuration(any());
verify(mockedExecutor, times(1)).execute();
}

@Test
void testWebhookRequest() {
when(mockedWebhookBody.getContent())
.thenReturn(mockedObject);

Object result = ZeplinProjectNoteTrigger.webhookRequest(
mockedParameters, mockedParameters, mockedHttpHeaders, mockedHttpParameters, mockedWebhookBody,
mockedWebhookMethod, mockedWebhookEnableOutput, mockedTriggerContext);

assertEquals(mockedObject, result);
}
}
Loading
Loading