generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8be690c
commit 883e673
Showing
14 changed files
with
265 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
prompt, | ||
jiraPrompt, | ||
acSummariesPrompt, | ||
compareOldSummaryTemplate | ||
} from './prompts.js' | ||
import core from '@actions/core' | ||
import OpenAI from 'openai' | ||
import { Logger } from './utils.js' | ||
|
||
|
||
export class Ai { | ||
constructor() { | ||
const openAiKey = core.getInput('openAIKey') || process.env.OPENAI_API_KEY | ||
if (!openAiKey) { | ||
throw new Error('OpenAI key is required') | ||
} | ||
this.model = new OpenAI({ | ||
apiKey: openAiKey | ||
}) | ||
} | ||
configuration = { | ||
model: 'gpt-3.5-turbo' | ||
} | ||
model: OpenAI | ||
basePromptTemplate = prompt | ||
jiraPromptTemplate = jiraPrompt | ||
acSummariesPromptTemplate = acSummariesPrompt | ||
compareOldSummaryTemplate(oldSummary: string, newSummary: string): string { | ||
return compareOldSummaryTemplate(oldSummary, newSummary) | ||
} | ||
execute = async (prompt: string) => { | ||
try { | ||
const response = await this.model.chat.completions.create({ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: prompt | ||
} | ||
], | ||
...this.configuration | ||
}) | ||
Logger.log('ai response', { response }) | ||
return response.choices[0].message.content | ||
} catch (e) { | ||
Logger.error('error summarizing changes', e) | ||
return null | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import * as core from '@actions/core' | ||
import * as github from '@actions/github' | ||
import { Logger } from '../utils' | ||
import { Ai } from '../ai' | ||
import { GitHub } from '@actions/github/lib/utils' | ||
|
||
export class GithubClient { | ||
octokit: InstanceType<typeof GitHub> | ||
repo: { owner: string; repo: string } | ||
constructor() { | ||
const { octokit, repo } = this.getGithubContext() | ||
this.octokit = octokit | ||
this.repo = repo | ||
} | ||
|
||
getGithubContext = () => { | ||
const githubToken = | ||
core.getInput('gitHubToken') || process.env.GITHUB_ACCESS_TOKEN || '' | ||
const octokit = github.getOctokit(githubToken) | ||
const repo = github.context.repo | ||
return { octokit, repo, githubToken } | ||
} | ||
|
||
|
||
async getComments(pullRequestNumber: number) { | ||
try { | ||
const response = await this.octokit.rest.issues.listComments({ | ||
...this.repo, | ||
issue_number: pullRequestNumber | ||
}) | ||
return response.data | ||
} catch (error) { | ||
Logger.error('error getting comments', JSON.stringify(error)) | ||
return [] | ||
} | ||
} | ||
|
||
async postComment(comment: string, pullRequestNumber: number) { | ||
try { | ||
await this.octokit.rest.pulls.update({ | ||
...this.repo, | ||
pull_number: pullRequestNumber, | ||
body: comment | ||
}) | ||
} catch (error) { | ||
Logger.error('error posting comment', JSON.stringify(error)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './jira.js' | ||
export * from './github.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { WebhookPayload } from '@actions/github/lib/interfaces' | ||
import { Version2Client } from 'jira.js' | ||
import { Issue } from 'jira.js/out/agile/models' | ||
import * as core from '@actions/core' | ||
|
||
import { Logger } from '../utils' | ||
|
||
export class JiraClient { | ||
client: Version2Client | ||
constructor() { | ||
this.client = this.initializeJiraClient() | ||
} | ||
initializeJiraClient = () => { | ||
const host = core.getInput('jiraHost') || process.env.JIRA_HOST || '' | ||
return new Version2Client({ | ||
host, | ||
authentication: { | ||
basic: { | ||
email: core.getInput('jiraEmail') || process.env.JIRA_EMAIL || '', | ||
apiToken: core.getInput('jiraApiKey') || process.env.JIRA_API_KEY || '' | ||
} | ||
} | ||
}) | ||
} | ||
getJiraTicket = async ({ | ||
title, | ||
branchName, | ||
body | ||
}: { | ||
title?: string | ||
branchName: string | ||
body?: string | ||
}): Promise<Issue[]> => { | ||
const ticketRegex = /([A-Z]+-[0-9]+)/g | ||
const allTickets = (`${body} ${branchName} ${title}` || '').match(ticketRegex) | ||
if (!allTickets?.length) return [] | ||
const ticket = [...new Set(allTickets)] | ||
const issues = await Promise.all( | ||
ticket.map(async t => { | ||
try { | ||
const issue = await this.client.issues.getIssue({ | ||
issueIdOrKey: t | ||
}) | ||
return issue.fields.description | ||
} catch (e) { | ||
Logger.error(`Error while fetching ${t} from JIRA`) | ||
} | ||
}) | ||
) | ||
return issues.filter(e => e) as unknown as Issue[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as core from '@actions/core' | ||
import * as github from '@actions/github' | ||
import { Logger } from '../utils' | ||
import { Ai } from '../ai' | ||
import { GitHub } from '@actions/github/lib/utils' | ||
import { GithubClient } from '../clients' | ||
|
||
export class CommentHandler { | ||
constructor(private readonly repoClient: GithubClient) { } | ||
SIGNATURE = 'Added by woot! 🚂' | ||
async postSummary( | ||
pullRequestNumber: number, | ||
summary: string, | ||
ai: Ai | ||
) { | ||
Logger.log('posted comment', github.context) | ||
const comments = await this.repoClient.getComments(pullRequestNumber) | ||
const existingComment = comments.find( | ||
comment => comment.body?.includes(this.SIGNATURE) | ||
) | ||
let comment = `${summary} \n ${this.SIGNATURE}` | ||
if (existingComment?.body) { | ||
Logger.log('found existing comment, updating') | ||
comment = `${await ai.compareOldSummaryTemplate(existingComment.body, summary)} \n ${this.SIGNATURE}` | ||
} | ||
await this.postComment(comment, pullRequestNumber) | ||
} | ||
|
||
postComment = async (comment: string, pullRequestNumber: number) => { | ||
return this.repoClient.postComment(comment, pullRequestNumber) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export * from './get-changes.js' | ||
export * from './post-comment.js' | ||
export * from './comments-handler.js' | ||
export * from './summarize-changes.js' | ||
export * from './jira.js' |
Oops, something went wrong.