JACoB PR for Issue Add Jira Webhook integration #142
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Summary:
Summary:
Description
Implement a feature in Jacob that integrates with Jira by setting up a webhook to listen for new Jira issues. When a new Jira issue is created, Jacob will automatically generate a corresponding to-do, mirroring the existing functionality for GitHub issues. This integration aims to streamline task management and ensure consistency across both platforms.
Features
JIRA_API_KEYfromprocess.env.settings.tsx) that allows users to log in with their Jira accounts.Implementation Details
Webhook Setup
JIRA_API_KEYsecurely stored inprocess.env.Update Settings Page (
settings.tsx)Database Schema Modification
To-Do Workflow Integration
Expected Behavior
Benefits
Environment and Configuration
JIRA_API_KEYis correctly set in the environment.Additional Notes
Be sure to research the
jira-clientpackage here: https://github.com/jira-node/node-jira-client but ideally use an oauth token instead of username/password. Use this URL to install your 3LO app: https://auth.atlassian.com/authorize?audience=api.atlassian.com&client_id=z9MOoQe9g8QmMCGACf8fMOKnIQAMdoGb&scope=read%3Ame&redirect_uri=https%3A%2F%2Fapp.jacb.ai%2Fapi%2Fjira%2Fcallback&state=${YOUR_USER_BOUND_VALUE}&response_type=code&prompt=consent and the callback url should be https://app.jacb.ai/api/jira/callback@jacob-ai-bot --skip-build
Plan:
Step 1: Edit
/src/server/db/tables/users.table.tsTask: Add 'jiraToken' field to UsersTable
Instructions:
In the file '/src/server/db/tables/users.table.ts', add a new nullable text field 'jiraToken' to the 'columns' definition in the 'UsersTable' class. This field will securely store the user's Jira access token. Ensure that appropriate data types and security considerations are applied.
Exit Criteria:
The 'UsersTable' class includes the new 'jiraToken' field, and it can be accessed in user records.
Step 2: Create
/src/server/db/migrations/20241101000000_addJiraTokenToUsers.tsTask: Create migration to add 'jiraToken' field to users table
Instructions:
Create a new migration file at '/src/server/db/migrations/20241101000000_addJiraTokenToUsers.ts'. In this migration, modify the 'users' table to add a new nullable text column 'jiraToken'. Use the database migration utilities provided by the project to properly define the change. Ensure the migration can be run without errors and can be rolled back if necessary.
Exit Criteria:
The migration file exists, and running migrations adds the 'jiraToken' column to the 'users' table in the database.
Step 3: Create
/src/app/dashboard/[org]/[repo]/settings/page.tsxTask: Create a Settings page with 'Log in with Jira' option
Instructions:
Create a new Next.js page at '/src/app/dashboard/[org]/[repo]/settings/page.tsx'. This page should render a settings interface for the user, integrating with the existing dashboard layout. Include a 'Log in with Jira' button that initiates the Jira OAuth 2.0 authorization flow by redirecting to the API route '/api/auth/jira/authorize'. Ensure that the button triggers the authorization flow correctly when clicked.
Exit Criteria:
The settings page is accessible, displays correctly, and includes a functional 'Log in with Jira' button that initiates the Jira OAuth flow.
Step 4: Create
/src/app/api/auth/jira/authorize/route.tsTask: Create API route to initiate Jira OAuth authorization
Instructions:
Create a new API route at '/src/app/api/auth/jira/authorize/route.ts'. This route should initiate the Jira OAuth 2.0 authorization flow by redirecting the user to Jira's authorization URL with the necessary parameters, including client_id, redirect_uri, response_type, scopes, and state. Ensure to generate and store the PKCE code challenge and verifier securely for the OAuth flow.
Exit Criteria:
Accessing this API route redirects the user to Jira's authorization URL to begin the OAuth process, with all required parameters correctly set.
Step 5: Create
/src/app/api/auth/jira/callback/route.tsTask: Create API route to handle Jira OAuth callback
Instructions:
Create a new API route at '/src/app/api/auth/jira/callback/route.ts'. This route should handle the OAuth 2.0 callback from Jira after the user authorizes the application. It should exchange the authorization code received from Jira for an access token using Jira's OAuth 2.0 token endpoint. Once the access token is obtained, securely store it in the 'jiraToken' field of the user's record in the database. Handle any errors gracefully and provide appropriate feedback.
Exit Criteria:
The API route successfully processes the OAuth callback, exchanges the authorization code for an access token, and stores the 'jiraToken' in the database.
Step 6: Create
/src/app/api/webhooks/jira/route.tsTask: Create API route to handle Jira webhooks
Instructions:
Create a new API route at '/src/app/api/webhooks/jira/route.ts'. This route should handle incoming POST requests from Jira webhooks. Implement logic to parse the webhook payload, specifically handling events where new issues are created (event type 'jira:issue_created'). When such an event is received, extract the issue details and create a corresponding to-do in Jacob, following the same workflow as for GitHub issues. Ensure that all necessary fields are extracted and mapped correctly.
Exit Criteria:
The API route receives Jira webhook events for new issue creations and successfully creates corresponding to-dos in Jacob.
Step 7: Edit
/src/server/auth.tsTask: Update auth session callback to include 'jiraToken' in session
Instructions:
In the file '/src/server/auth.ts', update the 'session' callback function to include the user's 'jiraToken' in the session object. After retrieving the user from the database, include 'jiraToken' in 'session.user' to make it accessible throughout the application when needed. Ensure that sensitive data is handled securely and that the session remains protected.
Exit Criteria:
The session object now includes 'jiraToken' in 'session.user', making it accessible in the application where user session data is available.