plugin = {
name = "jira-sync",
version = "1.0.0",
author = "Markus Maiwald",
description = "Sync ProGit issues to Jira",
hooks = {
on_issue_created = true,
on_issue_updated = true,
on_sync_push = true,
}
}
local jira_url = ""
local jira_project = ""
local jira_api_token = ""
function init()
jira_url = context.config.jira_url or os.getenv("JIRA_URL") or ""
jira_project = context.config.jira_project or os.getenv("JIRA_PROJECT") or ""
jira_api_token = context.config.jira_api_token or os.getenv("JIRA_API_TOKEN") or ""
if jira_url == "" then
error("Jira URL not configured. Set JIRA_URL environment variable or config.jira_url")
end
print("Jira Sync plugin initialized")
print(" URL: " .. jira_url)
print(" Project: " .. jira_project)
end
function on_issue_created(issue)
print("Creating Jira issue for: " .. issue.title)
local jira_issue = {
fields = {
project = { key = jira_project },
summary = issue.title,
description = issue.description,
issuetype = { name = "Task" },
labels = issue.tags,
}
}
print(" Would POST to: " .. jira_url .. "/rest/api/2/issue")
print(" Payload: " .. json_encode(jira_issue))
return {
success = true,
jira_key = "PROJ-123", }
end
function on_issue_updated(issue)
local jira_key = issue.metadata.jira_key
if not jira_key then
print("Issue not synced to Jira, skipping update")
return { success = false, reason = "not_synced" }
end
print("Updating Jira issue " .. jira_key .. " for: " .. issue.title)
local update = {
fields = {
summary = issue.title,
description = issue.description,
labels = issue.tags,
}
}
print(" Would PUT to: " .. jira_url .. "/rest/api/2/issue/" .. jira_key)
return { success = true }
end
function on_sync_push(issues)
print("Syncing " .. #issues .. " issues to Jira")
local synced = 0
for i, issue in ipairs(issues) do
if not issue.metadata.jira_key then
on_issue_created(issue)
synced = synced + 1
end
end
print("Synced " .. synced .. " new issues to Jira")
return { success = true, synced = synced }
end
function json_encode(obj)
local result = "{"
for k, v in pairs(obj) do
if type(v) == "table" then
result = result .. '"' .. k .. '":' .. json_encode(v) .. ","
elseif type(v) == "string" then
result = result .. '"' .. k .. '":"' .. v .. '",'
else
result = result .. '"' .. k .. '":' .. tostring(v) .. ","
end
end
result = result:sub(1, -2) .. "}"
return result
end