PioneerSquareLabs / otto-playground

A playground where Otto can run free while hopefully not accidentally reformatting your hard drive
https://otto-playground.vercel.app
13 stars 0 forks source link

Create src/pages/api/github.ts #747

Closed otto-jacob closed 1 year ago

otto-jacob commented 1 year ago

Summary:

Create the API endpoint src/pages/api/github.ts. Here is the description: Next.js API route for GitHub integration. Be sure to add the proper extension when creating the file. Here are the instructions:

Issue Description

This issue aims to provide detailed instructions for creating a new Next.js API endpoint github.ts that will handle GitHub integration for our application. The purpose of this endpoint is to allow users to authenticate with GitHub, create repositories, and manage GitHub issues related to their projects.

Prerequisites

Before starting, make sure you have a good understanding of the following concepts:

Step-by-step Instructions

  1. Create a new file named github.ts inside the src/pages/api directory.

  2. Import the necessary modules and types at the top of the file:

    import { NextApiRequest, NextApiResponse } from "next";
    import { getServerAuthSession } from "~/server/auth";
    import { prisma } from "~/server/db";
    import { z } from "zod";
    import { Project, User } from "~/types";
  3. Define the GitHubIntegrationResponse TypeScript type that represents the expected output of the API endpoint. This type should include the following properties:

    • success: A boolean indicating whether the operation was successful or not.
    • message: A string containing a human-readable message describing the result of the operation.
    • data: An optional object containing any relevant data returned by the operation (e.g., repository information, issue details, etc.).
    type GitHubIntegrationResponse = {
     success: boolean;
     message: string;
     data?: any;
    };
  4. Create the main API route handler function with the following signature:

    export default async (req: NextApiRequest, res: NextApiResponse<GitHubIntegrationResponse>) => {
     // Implementation goes here
    };
  5. Inside the handler function, first, retrieve the user's session using the getServerAuthSession helper function:

    const session = await getServerAuthSession({ req, res });
    const user = session?.user;

    If the user is not authenticated, return an error response:

    if (!user) {
     res.status(401).json({ success: false, message: "Unauthorized" });
     return;
    }
  6. Based on the req.method, handle different HTTP actions (e.g., GET, POST, PUT, DELETE) for various GitHub-related operations:

    • For example, if the method is GET, you can retrieve a list of repositories for the authenticated user.
    • If the method is POST, you can create a new repository or issue.
    • If the method is PUT, you can update an existing repository or issue.
    • If the method is DELETE, you can delete a repository or issue.

    Make sure to validate all input data using Zod and interact with the database using Prisma.

  7. For each action, use the GitHub API (OctoKit SDK) to perform the corresponding operation, such as creating a repository or managing issues. Make sure to handle any errors that might occur during these operations.

  8. After performing the GitHub operation, update the relevant records in the database (e.g., create or update a Project record with the new repository information).

  9. Finally, return a success response with the appropriate data:

    res.status(200).json({ success: true, message: "Operation successful", data: { /* relevant data */ } });
  10. Make sure to handle any errors that might occur during the execution of the API route and return an appropriate error response.

Important Notes

Acceptance Criteria

otto-jacob commented 1 year ago

Hello human! 👋

This PR was created by Otto to address the issue Create src/pages/api/github.ts

Next Steps

  1. Please review the PR carefully. Auto-generated code can and will contain subtle bugs and mistakes.

  2. If you identify code that needs to be changed, please reject the PR with a specific reason. Be as detailed as possible in your comments. Otto will take these comments, make changes to the code and push up changes. Please note that this process will take a few minutes.

  3. Once the code looks good, approve the PR and merge the code.

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
otto-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 6, 2023 10:12pm