Skip to content

Store and get selected repos on ticket to Atlassian storage #20

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 8, 2024

Conversation

hiroshinishio
Copy link
Contributor

No description provided.

@hiroshinishio hiroshinishio merged commit 0156f3d into main Dec 8, 2024
1 check passed
@hiroshinishio hiroshinishio deleted the wes branch December 8, 2024 14:52
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @hiroshinishio - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

try {
await invoke("storeRepo", { cloudId, projectId, issueId, value });
} catch (error) {
console.error("Error saving repo:", error);
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 user feedback for error cases

Silent failures can lead to confusion. Consider showing a notification or error message to the user when the save operation fails.

      console.error("Error saving repo:", error);
      toast.error("Failed to save repository. Please try again.");

@@ -28,8 +32,23 @@ resolver.define("getGithubRepos", async ({ payload }) => {
if (!response.ok) throw new Error(`Failed to fetch repositories: ${response.status}`);
Copy link

Choose a reason for hiding this comment

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

suggestion: Include response body in error message for better debugging

Consider including the response body in the error message to provide more context for debugging: const body = await response.text(); throw new Error(Failed to fetch repositories: ${response.status} - ${body});

Suggested change
if (!response.ok) throw new Error(`Failed to fetch repositories: ${response.status}`);
if (!response.ok) {
const body = await response.text();
throw new Error(`Failed to fetch repositories: ${response.status} - ${body}`);
}

import { invoke } from "@forge/bridge";
import ForgeReconciler, { Select, Text, useProductContext } from "@forge/react";

const App = () => {
// Get Jira cloud ID (== workspace ID)
const context = useProductContext();
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 consolidating related state management and repository logic into dedicated custom hooks

The code can be simplified by combining related effects and extracting repository logic into a custom hook. Here's how:

  1. Combine context-related state management:
const useJiraContext = () => {
  const context = useProductContext();
  const [contextData, setContextData] = useState({
    cloudId: null,
    projectId: null,
    issueId: null
  });

  useEffect(() => {
    if (context) {
      setContextData({
        cloudId: context.cloudId,
        projectId: context.extension.project.id,
        issueId: context.extension.issue.id
      });
    }
  }, [context]);

  return contextData;
};
  1. Extract repository management into a custom hook:
const useRepositoryManagement = (cloudId, projectId, issueId) => {
  const [githubRepos, setGithubRepos] = useState([]);
  const [isLoading, setIsLoading] = useState(true);
  const [selectedRepo, setSelectedRepo] = useState("");

  useEffect(() => {
    if (!cloudId || !projectId) return;

    const loadData = async () => {
      setIsLoading(true);
      try {
        const [repos, savedRepo] = await Promise.all([
          invoke("getGithubRepos", { cloudId, projectId }),
          invoke("getStoredRepo", { cloudId, projectId, issueId })
        ]);
        setGithubRepos(repos.map(repo => `${repo.github_owner_name}/${repo.github_repo_name}`));
        setSelectedRepo(savedRepo || "");
      } catch (error) {
        console.error("Error loading repository data:", error);
      } finally {
        setIsLoading(false);
      }
    };
    loadData();
  }, [cloudId, projectId, issueId]);

  const handleRepoChange = async (value) => {
    setSelectedRepo(value);
    if (!cloudId || !projectId) return;
    try {
      await invoke("storeRepo", { cloudId, projectId, issueId, value });
    } catch (error) {
      console.error("Error saving repo:", error);
    }
  };

  return { githubRepos, selectedRepo, isLoading, handleRepoChange };
};

This refactoring reduces complexity by:

  • Combining related context effects into a single hook
  • Centralizing repository management logic
  • Reducing state management boilerplate
  • Parallel loading of repository data

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant