Skip to content

Emable to trigger GitAuto by checking the checkbox #22

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

Merged
merged 1 commit into from
Dec 9, 2024
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ For development, you can do the following:

```shell
forge variables list -e development
forge variables set --encrypt SUPABASE_URL <value>
forge variables set --encrypt SUPABASE_API_KEY <value>
forge variables set --environment development --encrypt SUPABASE_URL <value>
forge variables set --environment development --encrypt SUPABASE_API_KEY <value>
```

For production, you have to do the following:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider adding the explicit commands for production environment variables

For consistency with the development section, consider adding the explicit commands for setting production variables using --environment production. This would make the documentation more complete and prevent any potential confusion.

Expand Down
21 changes: 16 additions & 5 deletions manifest.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
modules:
# The trigger module invokes a function or calls a remote backend when a product event, app lifecycle event, or data security policy event is fired.
# Trigger is used to invoke a function when a Jira issue event is fired.
# https://developer.atlassian.com/platform/forge/manifest-reference/modules/trigger/
# trigger:
# - key: app-lifecycle-trigger
# function: lifecycleHandler
# - key: issue-trigger
# function: webhook
# # https://developer.atlassian.com/platform/forge/events-reference/jira/#issue-events
# events:
# - installed # https://developer.atlassian.com/platform/forge/events-reference/life-cycle/
# - uninstalled
# - avi:jira:created:issue
# - avi:jira:updated:issue
# filter:
# ignoreSelf: true # Prevents infinite loops by ignoring self-generated events
# # expression: event.issue.fields.issuetype.name == 'Bug' # Optional: example filter for bug issues only
# onError: IGNORE_AND_LOG # Will invoke function and log errors

# The jira module provides functionality for Jira products.
jira:issuePanel:
Expand All @@ -22,6 +27,8 @@ modules:
function:
- key: resolver
handler: index.handler
# - key: webhook
# handler: webhook.handler

resources:
- key: main
Expand All @@ -33,6 +40,7 @@ app:

# Environment variables are not supported in the manifest.yml file.
# https://developer.atlassian.com/platform/forge/manifest-reference/permissions/
# It takes a few hours to 1 day to update here: https://developer.atlassian.com/console/myapps/f434bcc5-834f-45e5-ba1d-62e2ee8952cd/manage/permissions
permissions:
scopes:
- storage:app
Expand All @@ -42,9 +50,12 @@ permissions:
backend:
- https://dkrxtcbaqzrodvsagwwn.supabase.co
- https://awegqusxzsmlgxaxyyrq.supabase.co
- https://5ze2tkqk7c27bpl5opy5sbilsi0vrdim.lambda-url.us-west-1.on.aws
- https://gitauto.ngrok.dev

# https://developer.atlassian.com/platform/forge/manifest-reference/variables/
environment:
variables:
- SUPABASE_URL
- SUPABASE_API_KEY
- GITAUTO_URL
31 changes: 28 additions & 3 deletions src/frontend/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from "react";
import { invoke } from "@forge/bridge";
import ForgeReconciler, { Select, Text, useProductContext } from "@forge/react";
import ForgeReconciler, { Select, Text, useProductContext, Checkbox, Stack } from "@forge/react";

const App = () => {
// Get Jira cloud ID (== workspace ID)
Expand Down Expand Up @@ -75,8 +75,26 @@ const App = () => {
}
};

// Handle checkbox
const [isChecked, setIsChecked] = useState(false);
const [isTriggering, setIsTriggering] = useState(false);
const handleCheckboxChange = async (event) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider flattening the control flow in the checkbox handler to improve code readability

The checkbox handling logic can be simplified by flattening the control flow while maintaining the same functionality. Here's a suggested refactor:

const handleCheckboxChange = async (event) => {
  const checked = event.target.checked;
  setIsChecked(checked);

  if (!checked || !selectedRepo) {
    setIsTriggering(false);
    return;
  }

  setIsTriggering(true);
  try {
    await invoke("triggerGitAuto", { 
      cloudId, 
      projectId, 
      issueId, 
      selectedRepo 
    });
  } catch (error) {
    console.error("Error triggering GitAuto:", error);
  }
  setIsTriggering(false);
};

This version:

  • Uses a single guard clause at the top
  • Removes the need for finally block by setting isTriggering directly
  • Maintains the same functionality with clearer flow control
  • Keeps necessary state management for UI feedback

const checked = event.target.checked;
setIsChecked(checked);
if (!checked || !selectedRepo) return;
setIsTriggering(true);
try {
await invoke("triggerGitAuto", { cloudId, projectId, issueId, selectedRepo });
} catch (error) {
console.error("Error triggering GitAuto:", error);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Add user-visible error feedback instead of just logging to console

Consider adding an error state and displaying a message to the user when the operation fails

Suggested implementation:

  const [isChecked, setIsChecked] = useState(false);
  const [isTriggering, setIsTriggering] = useState(false);
  const [error, setError] = useState(null);
    setIsTriggering(true);
    setError(null);
    } catch (error) {
      console.error("Error triggering GitAuto:", error);
      setError("Failed to trigger GitAuto. Please try again or contact support if the problem persists.");
    <Stack space="space.075">
      {error && (
        <Text appearance="danger">
          {error}
        </Text>
      )}

} finally {
setIsTriggering(false);
}
};

return (
<>
// https://developer.atlassian.com/platform/forge/ui-kit-2/stack/
<Stack space="space.075">
<Text>Target GitHub Repository:</Text>
<Select
value={selectedRepo}
Expand All @@ -85,7 +103,14 @@ const App = () => {
isDisabled={isLoading}
placeholder="Select a repository"
/>
</>
<Checkbox
label="Trigger GitAuto to open a pull request"
onChange={handleCheckboxChange}
value={isChecked}
isDisabled={!selectedRepo || isTriggering}
/>
{isTriggering && <Text>Triggering GitAuto...</Text>}
</Stack>
);
};

Expand Down
22 changes: 22 additions & 0 deletions src/resolvers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,26 @@ resolver.define("storeRepo", async ({ payload }) => {
return await storage.set(key, value);
});

resolver.define("triggerGitAuto", async ({ payload }) => {
const { cloudId, projectId, issueId, selectedRepo } = payload;

// Determine the API endpoint based on environment
const endpoint = process.env.GITAUTO_URL + "/webhook";
console.log("Endpoint", endpoint);
const response = await forge.fetch(endpoint, {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (performance): Consider adding a timeout to the external API call

External API calls should have a reasonable timeout to prevent hanging operations

Suggested implementation:

  const response = await forge.fetch(endpoint, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    timeout: 10000, // 10 second timeout
=======

Note: If the forge.fetch implementation doesn't support the timeout option directly, you may need to implement the timeout using Promise.race() with a timeout promise. Let me know if you need that alternative implementation.

method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
cloudId,
projectId,
issueId,
repository: selectedRepo,
}),
});

if (!response.ok) throw new Error(`Failed to trigger GitAuto: ${response.status}`);

return await response.json();
Comment on lines +71 to +73
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Enhance error message with more context from the response

Include response body or more specific error details to help with debugging

Suggested change
if (!response.ok) throw new Error(`Failed to trigger GitAuto: ${response.status}`);
return await response.json();
if (!response.ok) {
const errorBody = await response.text();
throw new Error(
`Failed to trigger GitAuto: ${response.status} - ${response.statusText}\nResponse: ${errorBody}`
);
}
return await response.json();

});

export const handler = resolver.getDefinitions();
Loading