-
Notifications
You must be signed in to change notification settings - Fork 4
feat: implement Jira integration for PR handling #44
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
base: main
Are you sure you want to change the base?
Changes from all commits
36f990f
65ea29f
e581782
a038ad5
f5bece6
0fd258d
622f89c
a6ca420
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { Context } from 'probot'; | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| const mdToAdf = require('md-to-adf') as (markdown: string) => { toJSON: () => { type: string; version: number; content: unknown[] } }; | ||
|
|
||
| const JIRA_ISSUE_KEY_REGEX = /^[A-Z][A-Z0-9]+-\d+$/i; | ||
|
|
||
| interface AdfBlock { | ||
| type: string; | ||
| content?: unknown[]; | ||
| attrs?: unknown; | ||
| } | ||
|
|
||
| function prBodyToAdfContent(body: string | null): AdfBlock[] { | ||
| const raw = body?.trim() || 'no description'; | ||
| try { | ||
| const adf = mdToAdf(raw); | ||
| const json = adf?.toJSON?.(); | ||
| const content = json?.content; | ||
| if (Array.isArray(content) && content.length > 0) { | ||
| return content as AdfBlock[]; | ||
| } | ||
| } catch { | ||
| // fallback to plain text | ||
| } | ||
| return [ | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: `PR description: ${raw}` }], | ||
| }, | ||
| ]; | ||
| } | ||
|
|
||
| export const isJiraTaskKey = (arg: string): boolean => JIRA_ISSUE_KEY_REGEX.test(arg.trim()); | ||
|
|
||
| interface HandleJiraArg { | ||
| context: Context; | ||
| boardName: string; | ||
| parentTaskKey?: string; | ||
| pr: { | ||
| number: number; | ||
| title: string; | ||
| body: string | null; | ||
| html_url: string; | ||
| labels: string[]; | ||
| milestone?: string | null; | ||
| user?: { | ||
| login?: string; | ||
| } | null; | ||
| }; | ||
| requestedBy: string; | ||
| commentId: number; | ||
| } | ||
|
|
||
| const getEnv = (name: string): string => { | ||
| const value = process.env[name]; | ||
|
|
||
| if (!value?.trim()) { | ||
| throw new Error(`Missing required env var: ${name}`); | ||
| } | ||
|
|
||
| return value.trim(); | ||
| }; | ||
|
|
||
| export const handleJira = async ({ context, boardName, parentTaskKey, pr, requestedBy }: HandleJiraArg): Promise<string> => { | ||
| const jiraBaseUrl = getEnv('JIRA_BASE_URL').replace(/\/$/, ''); | ||
| const jiraApiToken = getEnv('JIRA_API_TOKEN'); | ||
| const hasCommunityLabel = pr.labels.some((label) => label.toLowerCase() === 'community'); | ||
| const isSubtask = Boolean(parentTaskKey); | ||
| const projectKey = parentTaskKey ? parentTaskKey.replace(/-\d+$/, '') : boardName; | ||
|
|
||
| const payload = { | ||
| fields: { | ||
| project: { | ||
| key: projectKey, | ||
| }, | ||
| ...(isSubtask ? { parent: { key: parentTaskKey } } : {}), | ||
| summary: `[PR #${pr.number}] ${pr.title}`, | ||
| issuetype: { | ||
| name: isSubtask ? 'Sub-task' : 'Task', | ||
| }, | ||
| ...(hasCommunityLabel ? { labels: ['community'] } : {}), | ||
| ...(pr.milestone?.trim() ? { fixVersions: [{ name: pr.milestone.trim() }] } : {}), | ||
| description: { | ||
| type: 'doc', | ||
| version: 1, | ||
| content: [ | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'Task automatically created by dionisio-bot.' }], | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: `PR: ${pr.html_url}` }], | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: 'PR description:' }], | ||
| }, | ||
| ...prBodyToAdfContent(pr.body), | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: `PR author: ${pr.user?.login ?? 'unknown'}` }], | ||
| }, | ||
| { | ||
| type: 'paragraph', | ||
| content: [{ type: 'text', text: `Requested by: ${requestedBy}` }], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const response = await fetch(`${jiraBaseUrl}/rest/api/3/issue`, { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Authorization': `Basic ${jiraApiToken}`, | ||
| 'Accept': 'application/json', | ||
| 'Content-Type': 'application/json', | ||
| }, | ||
| body: JSON.stringify(payload), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| const body = await response.text(); | ||
| throw new Error(`Jira request failed (${response.status}): ${body}`); | ||
| } | ||
| const task = (await response.json()) as { key?: string }; | ||
|
|
||
| await context.octokit.issues.update({ | ||
| ...context.issue(), | ||
| body: `${pr.body?.trim() || 'no description'} \n\n Task: [${task.key}]`, | ||
| }); | ||
|
Comment on lines
+129
to
+132
|
||
|
|
||
| return task.key ?? ''; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { handleBackport } from './handleBackport'; | |||||||||||||
| import { run } from './Queue'; | ||||||||||||||
| import { consoleProps } from './createPullRequest'; | ||||||||||||||
| import { handleRebase } from './handleRebase'; | ||||||||||||||
| import { handleJira, isJiraTaskKey } from './handleJira'; | ||||||||||||||
|
|
||||||||||||||
| export = (app: Probot) => { | ||||||||||||||
| app.log.useLevelLabels = false; | ||||||||||||||
|
|
@@ -214,6 +215,64 @@ export = (app: Probot) => { | |||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (command === 'jira' && args?.trim()) { | ||||||||||||||
| const rawArg = args.trim().replace(/^["']|["']$/g, ''); | ||||||||||||||
| const asSubtask = isJiraTaskKey(rawArg); | ||||||||||||||
|
|
||||||||||||||
| await context.octokit.reactions.createForIssueComment({ | ||||||||||||||
| ...context.issue(), | ||||||||||||||
| comment_id: comment.id, | ||||||||||||||
| content: 'eyes', | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| try { | ||||||||||||||
| await handleJira({ | ||||||||||||||
| context, | ||||||||||||||
| boardName: rawArg, | ||||||||||||||
| ...(asSubtask ? { parentTaskKey: rawArg } : {}), | ||||||||||||||
| pr: { | ||||||||||||||
| number: pr.data.number, | ||||||||||||||
| title: pr.data.title, | ||||||||||||||
| body: pr.data.body, | ||||||||||||||
| html_url: pr.data.html_url, | ||||||||||||||
| labels: pr.data.labels.map((label) => label.name), | ||||||||||||||
| milestone: pr.data.milestone?.title ?? undefined, | ||||||||||||||
| user: pr.data.user, | ||||||||||||||
| }, | ||||||||||||||
| requestedBy: comment.user.login, | ||||||||||||||
| commentId: comment.id, | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| await context.octokit.reactions.createForIssueComment({ | ||||||||||||||
| ...context.issue(), | ||||||||||||||
| comment_id: comment.id, | ||||||||||||||
| content: '+1', | ||||||||||||||
| }); | ||||||||||||||
|
Comment on lines
+246
to
+251
|
||||||||||||||
| await context.octokit.reactions.createForIssueComment({ | |
| ...context.issue(), | |
| comment_id: comment.id, | |
| content: '+1', | |
| }); |
Copilot
AI
Feb 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment says "thinking face" but the reaction used is confused. Update the comment to match the actual reaction (or change the reaction if a different one was intended).
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| declare module 'md-to-adf' { | ||
| interface AdfDocument { | ||
| toJSON(): { type: string; version: number; content: unknown[] }; | ||
| } | ||
|
|
||
| function mdToAdf(markdown: string): AdfDocument; | ||
|
|
||
| export = mdToAdf; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the code sends
Authorization: Basic ${JIRA_API_TOKEN}, it’s unclear what exact format the env var should contain (raw API token vs base64-encodedemail:token). Add a short comment in the example env file describing the expected format (and any additional required Jira identity like email) to prevent misconfiguration.