LDNDeveloper

Andrew Pallant

Software & Web Developer


Donate To Support My Blog Donate if this post helped you. Coffee money is always welcomed!




Copilot to Write ScriptRunner Scripts

Physical Link: Copilot to Write ScriptRunner Scripts


When it comes to scripting, I’ve picked up a few tricks to boost my productivity. First off, time management is my secret weapon. I allocate specific chunks of time for different tasks, ensuring I stay focused and efficient. It’s like having a well-organized toolbox—I know exactly which tool to grab when I need it.

Now, let’s talk about my trusty sidekick: Copilot. Picture this—it’s like having a coding buddy who finishes your sentences. When I sit down to write a script, I engage in a collaborative thought dance with Copilot. I describe what I want to achieve, and Copilot responds with detailed code snippets. It’s like brainstorming with an AI genie—rub the lamp, state your intent, and voilà!

Integration of Copilot

Integrating Copilot into my workflow is smoother than a well-oiled machine. I’ve found that thinking aloud is the key. I narrate my intentions, and Copilot listens attentively. It’s like having a co-pilot (pun intended) who understands my context. Copilot adapts to my coding style, preferences, and quirks over time. It’s not just about code; it’s about building a symbiotic relationship.

  • write a script to run on ScriptRunner using Groovy to get a list of Jira tickets for a given date range
  • rewrite the script using rest API instead of java classes ( this is because Java classes will not work in Jira Cloud )
  • amend the script using Jira’s issue notify API, send an HTML formatted list to a specific user

Example Output

import groovyx.net.http.RESTClient

// Set your Jira credentials and base URL
def username = "your-username"
def password = "your-password"
def jiraBaseUrl = "https://your-jira-instance.com"

// Set your desired date range (start and end dates)
def startDate = "2023-01-01"
def endDate = "2023-01-31"

// Create a JQL query to search for issues created between the specified dates
def jqlQuery = "created >= \"$startDate\" AND created <= \"$endDate\""

// Define the Jira REST API endpoint for issue search
def apiEndpoint = "${jiraBaseUrl}/rest/api/2/search"

// Make the REST API request
def restClient = new RESTClient(apiEndpoint)
def response = restClient.get(
    path: '',
    query: [jql: jqlQuery, maxResults: 1000], // Adjust as needed
    auth: [username, password]
)

// Parse the response JSON
def data = response.data

// Create an HTML-formatted list of issues
def htmlList = "<ul>"
data.issues.each { issue ->
    htmlList += "<li><a href='${jiraBaseUrl}/browse/${issue.key}'>${issue.key}</a>: ${issue.fields.summary}</li>"
}
htmlList += "</ul>"

// Define the recipient's email address
def recipientEmail = "user@example.com"

// Send the email using Jira's IssueNotify API
def issueNotifyApiEndpoint = "${jiraBaseUrl}/rest/api/2/issue/${issue.key}/notify"
restClient.post(
    path: issueNotifyApiEndpoint,
    body: [
        subject: "Jira Issues Report",
        textBody: "Here are the issues created between $startDate and $endDate:",
        htmlBody: htmlList,
        to: recipientEmail
    ],
    auth: [username, password]
)

// Print a success message
println "Email sent to $recipientEmail with the list of issues."

Challenges and Solutions

 Now, let’s address the quirks. Copilot occasionally gets a bit too excited, offering complex code structures that make my eyebrows raise. But fear not—I wield the editor’s pen. I simplify those suggestions, aligning them with my preferred coding style. It’s a delicate balance between leveraging Copilot’s brilliance and maintaining readability.

And then there’s the matter of existing patterns. Copilot tends to stick to what it knows, like a creature of habit. But I’m no stranger to adventure. To keep the creative juices flowing, I actively seek fresh approaches beyond the familiar. Copilot is my trusty co-creator, but I’m the artist adding brushstrokes of innovation.

So there you have it—the tale of how Copilot and I waltz through scripts, learning, adapting, and occasionally tripping over our own syntax. It’s a beautiful partnership—one that combines the best of human ingenuity and artificial intelligence.

Finally

Use the tools that you have to make your scripting easier. Good luck with your future script automation!

The post Copilot to Write ScriptRunner Scripts appeared first on LDNDeveloper.

Author:
Categories: AI, Copilot, Groovy, Jira, Microsoft, Script Automation, ScriptRunner, Scripts


©2024 LdnDeveloper