From a2c8207998b956f122c9c32dcab81f954c4196d8 Mon Sep 17 00:00:00 2001 From: Olivier Tremblay Date: Sat, 13 Sep 2025 08:40:40 -0400 Subject: [PATCH] initial commit yessss feat: add Vikunja issues provider and integrate tasks retrieval Co-authored-by: aider (openai/qwen2.5-coder:32b-instruct-q4_0) feat: add Vikunja handler to main file Co-authored-by: aider (openai/qwen2.5-coder:32b-instruct-q4_0) refactor: Move Jira user from positional arg to flag parameter Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: replace manual argument parsing with Go flags package Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) feat: replace positional arguments with flags in main function Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: enforce mandatory start/end dates and at least one identifier flag Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: only call relevant tools based on flag presence Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) feat: add vikunja task support to summarize function feat: add flag to pass custom prompt with default prompt as fallback Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) Minor fix fix: comment out vikunja tasks integration for now fix: check for OpenAI environment variables before calling Summarize Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) feat: add timestamped prompt file generation for summarization refactor: remove duplicate os.Getenv calls for OpenAI env vars in SummarizeData Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) refactor: pass OpenAI env vars from main to SummarizeData instead of calling os.Getenv inside the function Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) feat: add file prompt support with fallback to literal prompt string Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: remove single quotes around JIRA user assignee and add debug printing feat: implement accountID lookup for Jira user search in GetIssues function Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: remove debug print statement in Jira user search function fix: Replace user parameter with JIRA_USER environment variable in SetBasicAuth calls Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: add missing os import in summarize.go refactor: split SummarizeData into buildPrompt and callSummarizationEndpoint functions Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: always call SummarizeData to ensure prompt file is created for debugging, but only call OpenAI endpoint if env vars are set Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) refactor: extract callSummarizationEndpoint into Summarizer interface for multiple implementations Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) refactor: move OpenAI variable checks into OpenAISummarizer's Summarize method and always call the summarizer's method Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) feat: implement Ollama Summarizer using official SDK as per article example Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) refactor: simplify OpenAI and Ollama summarizer implementations refactor: simplify Summarizer interface by moving endpoint, token, and model to struct properties Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) feat: add Ollama summarizer support to main command fix: remove extraneous parameters from SummarizeData call and use properly initialized OllamaSummarizer Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) feat: add AnthropicSummarizer implementation using anthropic-sdk-go package Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: change summarizer call condition to check if summarizer is nil instead of environment variables Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) fix: correct client reference and update Anthropic summarizer implementation feat: extract PR rendering to String() method using text/template Co-authored-by: aider (openai/qwen3-coder:30b-a3b-q4_K_M) Some cleanup --- .gitignore | 2 + Makefile | 3 + cmd/acb/main.go | 172 +++++++++++++++++++++++++++++++++ cmd/acb/summarize.go | 197 ++++++++++++++++++++++++++++++++++++++ contributions/gh.go | 113 ++++++++++++++++++++++ go.mod | 16 ++++ go.sum | 24 +++++ issues/jira.go | 152 +++++++++++++++++++++++++++++ issues/vikunja/vikunja.go | 47 +++++++++ 9 files changed, 726 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 cmd/acb/main.go create mode 100644 cmd/acb/summarize.go create mode 100644 contributions/gh.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 issues/jira.go create mode 100644 issues/vikunja/vikunja.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..49d3410 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.aider* +.env* diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a40d072 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +all: + go build ./... + go test ./... diff --git a/cmd/acb/main.go b/cmd/acb/main.go new file mode 100644 index 0000000..2ae98b0 --- /dev/null +++ b/cmd/acb/main.go @@ -0,0 +1,172 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "os" + "time" + + "o5r.ca/autocrossbow/contributions" + "o5r.ca/autocrossbow/issues" + "o5r.ca/autocrossbow/issues/vikunja" +) + +func main() { + // Define flags + var jiraUser = flag.String("jira-user", "", "Jira user to query") + var proj = flag.String("proj", "", "Project name") + var ghusername = flag.String("ghusername", "", "GitHub username") + var start = flag.String("start", "", "Start date") + var end = flag.String("end", "", "End date") + var employeename = flag.String("employeename", "", "Employee name") + var prompt = flag.String("prompt", defaultPrompt, "Custom prompt to use for summarization") + + flag.Parse() + + // Check required flags + if *start == "" || *end == "" { + fmt.Println("Error: start and end dates are required") + flag.Usage() + os.Exit(1) + } + + // Check that at least one of employee name, jira user, project, or ghusername is provided + if *employeename == "" && *jiraUser == "" && *proj == "" && *ghusername == "" { + fmt.Println("Error: at least one of employee name, jira user, project, or ghusername must be provided") + flag.Usage() + os.Exit(1) + } + + // Check if the prompt is a file path and read it if it exists + finalPrompt := *prompt + if _, err := os.Stat(*prompt); err == nil { + // File exists, read it + content, err := os.ReadFile(*prompt) + if err != nil { + fmt.Printf("Error reading prompt file: %v\n", err) + os.Exit(1) + } + finalPrompt = string(content) + } + + var prs map[string][]contributions.PullRequest + var issues []issues.Issue + var vikunjaTasks []vikunja.Task + + // Only call tools if relevant flags are set + if *proj != "" || *ghusername != "" { + prs = DoPrs(*proj, *ghusername, *start, *end) + } + + if *jiraUser != "" { + issues = DoJira(*start, *end, *jiraUser) + } + + // if *employeename != "" { + // vikunjaTasks = DoVikunja(*start, *end) + // } + + // Get environment variables + openaiEndpoint := os.Getenv("OPENAI_ENDPOINT") + openaiToken := os.Getenv("OPENAI_TOKEN") + openaiModel := os.Getenv("OPENAI_MODEL") + anthropicModel := os.Getenv("ANTHROPIC_MODEL") + + // Create appropriate summarizer based on available environment variables + var summarizer Summarizer + if openaiEndpoint != "" && openaiToken != "" { + // Use OpenAI summarizer + summarizer = NewOpenAISummarizer(openaiEndpoint, openaiToken, openaiModel) + } else if anthropicModel != "" { + // Use Anthropic summarizer + summarizer = NewAnthropicSummarizer(anthropicModel) + } else { + // Use Ollama summarizer as fallback + summarizer = NewOllamaSummarizer("", "", openaiModel) + } + + // Always call SummarizeData to ensure prompt file is created for debugging + summ, err := SummarizeData(*employeename, prs, issues, vikunjaTasks, finalPrompt, summarizer) + if err != nil { + fmt.Println(fmt.Errorf("error getting PRs: %w", err)) + os.Exit(1) + } + + // Only call summarization endpoint if we have a valid summarizer + if summarizer != nil { + fmt.Println(summ) + } else { + fmt.Println("No summarization endpoint configured, but prompt file was created for debugging") + } +} + +func DoPrs(proj, ghusername, start, end string) map[string][]contributions.PullRequest { + prs, err := contributions.GetPRs(proj, ghusername, start, end) + if err != nil { + fmt.Println(fmt.Errorf("error getting PRs: %w", err)) + os.Exit(1) + } + + ghf, err := os.Create(fmt.Sprintf("gh-%s-%s-%s-%s-%d.json", proj, ghusername, start, end, time.Now().Unix())) + if err != nil { + fmt.Println(fmt.Errorf("error creating PR file: %w", err)) + os.Exit(1) + } + enc := json.NewEncoder(ghf) + err = enc.Encode(prs) + if err != nil { + fmt.Println(fmt.Errorf("error writing out PRs: %w", err)) + os.Exit(1) + } + return prs +} + +func DoJira(start, end string, user string) []issues.Issue { + host := os.Getenv("JIRA_HOST") + tasks, err := issues.GetIssues(host, user, start, end) + if err != nil { + fmt.Println(fmt.Errorf("error getting Vikunja tasks: %w", err)) + os.Exit(1) + } + + vf, err := os.Create(fmt.Sprintf("jira-%s-%s-%s-%s-%d.json", host, user, start, end, time.Now().Unix())) + if err != nil { + fmt.Println(fmt.Errorf("error creating Jira file: %w", err)) + os.Exit(1) + } + defer vf.Close() + + enc := json.NewEncoder(vf) + err = enc.Encode(tasks) + if err != nil { + fmt.Println(fmt.Errorf("error writing out Jira tasks: %w", err)) + os.Exit(1) + } + return tasks +} + +func DoVikunja(start, end string) []vikunja.Task { + host := os.Getenv("VIKUNJA_HOST") + user := os.Getenv("VIKUNJA_USER") + tasks, err := vikunja.GetTasks(host, user, start, end) + if err != nil { + fmt.Println(fmt.Errorf("error getting Vikunja tasks: %w", err)) + os.Exit(1) + } + + vf, err := os.Create(fmt.Sprintf("vikunja-%s-%s-%s-%s-%d.json", host, user, start, end, time.Now().Unix())) + if err != nil { + fmt.Println(fmt.Errorf("error creating Vikunja file: %w", err)) + os.Exit(1) + } + defer vf.Close() + + enc := json.NewEncoder(vf) + err = enc.Encode(tasks) + if err != nil { + fmt.Println(fmt.Errorf("error writing out Vikunja tasks: %w", err)) + os.Exit(1) + } + return tasks +} diff --git a/cmd/acb/summarize.go b/cmd/acb/summarize.go new file mode 100644 index 0000000..3bec11b --- /dev/null +++ b/cmd/acb/summarize.go @@ -0,0 +1,197 @@ +package main + +import ( + "context" + "fmt" + "os" + "time" + + "o5r.ca/autocrossbow/contributions" + "o5r.ca/autocrossbow/issues" + "o5r.ca/autocrossbow/issues/vikunja" + + "github.com/anthropics/anthropic-sdk-go" + "github.com/anthropics/anthropic-sdk-go/option" + "github.com/ollama/ollama/api" +) + +const defaultPrompt = `I will provide you, for a given period, with an employee name and a list of Pull Request titles and summaries split by repository, and a list of Jira Issues an employee has worked on. I may also provide, optionally, the employee's self-assessment. If I do, integrate that. + +I'd like you to summarize the employee's accomplishments for the quarter +I'd like the summary for the accomplishments to be in prose form, in a few paragraphs separated based on areas of work. Keep answers to 500 words for the summary.` + +// Summarizer interface defines the contract for summarization implementations +type Summarizer interface { + Summarize(prompt string) (string, error) +} + +// OpenAISummarizer implements the Summarizer interface for OpenAI-compatible endpoints +type OpenAISummarizer struct { + endpoint string + token string + model string +} + +// NewOpenAISummarizer creates a new OpenAISummarizer with the given parameters +func NewOpenAISummarizer(endpoint, token, model string) *OpenAISummarizer { + return &OpenAISummarizer{ + endpoint: endpoint, + token: token, + model: model, + } +} + +// Summarize sends the prompt to an OpenAI-compatible endpoint for summarization +func (o *OpenAISummarizer) Summarize(fullPrompt string) (string, error) { + // Check if required environment variables are set + if o.endpoint == "" || o.token == "" { + return "", fmt.Errorf("OpenAI endpoint or token not set") + } + + // Create the request + ctx := context.Background() + client, _ := api.ClientFromEnvironment() + + req := api.GenerateRequest{ + Model: o.model, + Prompt: fullPrompt, + Stream: nil, + } + + var result string + err := client.Generate(ctx, &req, func(resp api.GenerateResponse) error { + result += resp.Response + return nil + }) + if err != nil { + return "", err + } + + return result, nil +} + +// OllamaSummarizer implements the Summarizer interface for Ollama endpoints +type OllamaSummarizer struct { + endpoint string + token string + model string +} + +// NewOllamaSummarizer creates a new OllamaSummarizer with the given parameters +func NewOllamaSummarizer(endpoint, token, model string) *OllamaSummarizer { + return &OllamaSummarizer{ + endpoint: endpoint, + token: token, + model: model, + } +} + +// Summarize sends the prompt to an Ollama endpoint for summarization +func (o *OllamaSummarizer) Summarize(fullPrompt string) (string, error) { + // Check if required parameters are set + if o.model == "" { + return "", fmt.Errorf("Ollama model not set") + } + + // Create the request + ctx := context.Background() + client, _ := api.ClientFromEnvironment() + + req := &api.GenerateRequest{ + Model: o.model, + Prompt: fullPrompt, + Stream: nil, + } + + var result string + err := client.Generate(ctx, req, func(resp api.GenerateResponse) error { + result += resp.Response + return nil + }) + if err != nil { + return "", err + } + + return result, nil +} + +// AnthropicSummarizer implements the Summarizer interface for Anthropic API +type AnthropicSummarizer struct { + client *anthropic.Client + model string +} + +// NewAnthropicSummarizer creates a new AnthropicSummarizer with the given parameters +func NewAnthropicSummarizer(model string) *AnthropicSummarizer { + // Create the Anthropic client with the API key from environment + client := anthropic.NewClient( + option.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")), + ) + + return &AnthropicSummarizer{ + client: &client, + model: model, + } +} + +// Summarize sends the prompt to the Anthropic API for summarization +func (a *AnthropicSummarizer) Summarize(fullPrompt string) (string, error) { + // Check if required parameters are set + if a.model == "" { + return "", fmt.Errorf("Anthropic model not set") + } + + // Create the request + ctx := context.Background() + + message, err := a.client.Messages.New(ctx, anthropic.MessageNewParams{ + Model: anthropic.Model(a.model), + MaxTokens: 10000, + Messages: []anthropic.MessageParam{ + anthropic.NewUserMessage(anthropic.NewTextBlock(fullPrompt)), + }, + }) + if err != nil { + return "Blew up here", err + } + + return fmt.Sprintf("%+v\n", message.Content), nil +} + +// buildPrompt constructs the prompt string from PRs, issues, and tasks +func buildPrompt(employeename string, prs contributions.PRMap, issues []issues.Issue, tasks []vikunja.Task, prompt string) string { + // Build a prompt string + fullPrompt := prompt + fmt.Sprintf("\n\nHere's the PRs and Tickets for the employee %s:\n\n", employeename) + fullPrompt += prs.String() + fullPrompt += "Issues:\n" + for _, issue := range issues { + fullPrompt += fmt.Sprintf("Summary: %s\n", issue.Summary) + fullPrompt += fmt.Sprintf("Description: %s\n", issue.Description) + fullPrompt += "--------\n" + } + + // Save prompt to file for debugging + promptf, err := os.Create(fmt.Sprintf("prompt-%s-%d.json", employeename, time.Now().Unix())) + if err != nil { + fmt.Println(fmt.Errorf("error creating PR file: %w", err)) + os.Exit(1) + } + promptf.WriteString(fullPrompt) + defer promptf.Close() + + return fullPrompt +} + +// SummarizeData builds the prompt and calls the summarization endpoint +func SummarizeData(employeename string, prs map[string][]contributions.PullRequest, issues []issues.Issue, tasks []vikunja.Task, prompt string, summarizer Summarizer) (string, error) { + // Build the prompt + fullPrompt := buildPrompt(employeename, prs, issues, tasks, prompt) + + // Always call the summarizer's Summarize method + result, err := summarizer.Summarize(fullPrompt) + if err != nil { + return "", err + } + + return result, nil +} diff --git a/contributions/gh.go b/contributions/gh.go new file mode 100644 index 0000000..6614911 --- /dev/null +++ b/contributions/gh.go @@ -0,0 +1,113 @@ +package contributions + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "strings" + "text/template" +) + +var ghc = http.Client{} +var gh_token = os.Getenv("GH_TOKEN") + +type SearchResponse struct { + Items []PRResp +} + +type PRResp struct { + Body string + Title string + HtmlUrl string `json:"html_url"` + ClosedAt string `json:"closed_at"` + RepositoryUrl string `json:"repository_url"` +} + +type PullRequest struct { + Body string + Title string + ClosedAt string + URL string +} + +var prtmpl = template.Must(template.New("pr").Parse(`- Title: {{.Title}} + Body: {{.Body}} +`)) + +// String returns a formatted string representation of the PullRequest +func (pr PullRequest) String() string { + var buf bytes.Buffer + err := prtmpl.Execute(&buf, pr) + if err != nil { + return "" // Return empty string on error + } + + return buf.String() +} + +type PRMap map[string][]PullRequest + +var prlisttmpl = template.Must(template.New("prlist").Parse(`{{range $k, $v := .}} + {{$k}}: + {{$v}} +{{end}}`)) + +func (p PRMap) String() string { + var buf bytes.Buffer + err := prlisttmpl.Execute(&buf, p) + if err != nil { + return "" // Return empty string on error + } + + return buf.String() +} + +func GetPRs(org, username string, from string, to string) (PRMap, error) { + req, err := http.NewRequest( + http.MethodGet, + fmt.Sprintf( + "https://api.github.com/search/issues?q=type:pr+org:%s+author:%s+is:closed+merged:%s..%s", + org, + username, + from, + to, + ), + nil) + if err != nil { + return nil, fmt.Errorf("error making request: %w", err) + } + + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", gh_token)) + resp, err := ghc.Do(req) + if err != nil { + return nil, fmt.Errorf("error talking to github: %w", err) + } + + if resp.StatusCode > 400 { + b := bytes.NewBuffer(nil) + io.Copy(b, resp.Body) + return nil, fmt.Errorf("error talking to github: %s", b.String()) + } + r := &SearchResponse{} + dec := json.NewDecoder(resp.Body) + err = dec.Decode(&r) + if err != nil { + return nil, fmt.Errorf("error decoding response: %w", err) + } + + allthings := PRMap{} + + for _, pr := range r.Items { + reponameparts := strings.Split(pr.RepositoryUrl, "/") + reponame := reponameparts[len(reponameparts)-1] + if prs, ok := allthings[reponame]; !ok { + allthings[reponame] = []PullRequest{{pr.Body, pr.Title, pr.ClosedAt, pr.HtmlUrl}} + } else { + allthings[reponame] = append(prs, PullRequest{pr.Body, pr.Title, pr.ClosedAt, pr.HtmlUrl}) + } + } + return allthings, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c521219 --- /dev/null +++ b/go.mod @@ -0,0 +1,16 @@ +module o5r.ca/autocrossbow + +go 1.25.0 + +require ( + github.com/anthropics/anthropic-sdk-go v1.18.0 // indirect + github.com/eliziario/jira-lib v0.4.0 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/ollama/ollama v0.12.11 // indirect + github.com/tidwall/gjson v1.18.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/sjson v1.2.5 // indirect + golang.org/x/crypto v0.40.0 // indirect + golang.org/x/sys v0.36.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d2aea89 --- /dev/null +++ b/go.sum @@ -0,0 +1,24 @@ +github.com/anthropics/anthropic-sdk-go v1.18.0 h1:jfxRA7AqZoCm83nHO/OVQp8xuwjUKtBziEdMbfmofHU= +github.com/anthropics/anthropic-sdk-go v1.18.0/go.mod h1:WTz31rIUHUHqai2UslPpw5CwXrQP3geYBioRV4WOLvE= +github.com/eliziario/jira-lib v0.4.0 h1:7ALdGwDKPvUlSvGMe93SN+Xv5arqrzbTkf/rkn9WR78= +github.com/eliziario/jira-lib v0.4.0/go.mod h1:LOG+j6Aey6sjlPGJpmLUhArCR9+vGwaWZtyCvgQKTwM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/ollama/ollama v0.12.11 h1:QOoD6hSCXuGO9bkWLL7h53XZPD1hG8jaun5mirIyNFM= +github.com/ollama/ollama v0.12.11/go.mod h1:RUSmYywUWx/YZMaHrqtnT1ZChu+iSz/7jx2aO9+Mgfg= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= +golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34= +golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc= +golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM= +golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY= +golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k= +golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= diff --git a/issues/jira.go b/issues/jira.go new file mode 100644 index 0000000..fe9280b --- /dev/null +++ b/issues/jira.go @@ -0,0 +1,152 @@ +package issues + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + + "github.com/eliziario/jira-lib/pkg/adf" +) + +var jcl = http.Client{} + +type Issue struct { + Summary string + Description string + Comments []Comment +} + +type Comment struct { + Author Author + Body string +} + +type Author struct { + DisplayName string + EmailAddress string +} + +type JiraSearchResp struct { + Issues []RespIssue +} + +type RespIssue struct { + Key string + Fields map[string]any +} + +type UserSearchResp struct { + AccountID string `json:"accountId"` + Name string `json:"name"` +} + +func getUserAccountID(instance, user string) (string, error) { + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/rest/api/3/user/search?query=%s", instance, user), nil) + if err != nil { + return "", fmt.Errorf("error building jira user search request: %w", err) + } + req.SetBasicAuth(os.Getenv("JIRA_USER"), os.Getenv("JIRA_TOKEN")) + req.Header.Add("Accept", "application/json") + resp, err := jcl.Do(req) + if err != nil { + return "", fmt.Errorf("error executing jira user search request: %w", err) + } + if resp.StatusCode >= 400 { + b := bytes.NewBuffer(nil) + io.Copy(b, resp.Body) + return "", fmt.Errorf("error talking to jira: %s", b.String()) + } + + dec := json.NewDecoder(resp.Body) + var users []UserSearchResp + err = dec.Decode(&users) + if err != nil { + return "", fmt.Errorf("error decoding jira user search response: %w", err) + } + + if len(users) == 0 { + return "", fmt.Errorf("no user found with query %s", user) + } + + return users[0].AccountID, nil +} + +func GetIssues(instance, user, from, to string) ([]Issue, error) { + // First get the user's account ID + accountID, err := getUserAccountID(instance, user) + if err != nil { + return nil, fmt.Errorf("error getting user account ID: %w", err) + } + + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/rest/api/3/search/jql", instance), nil) + if err != nil { + return nil, fmt.Errorf("error building jira search request: %w", err) + } + q := req.URL.Query() + q.Add("jql", fmt.Sprintf("assignee was %s and resolved >= %s and resolved <= %s", accountID, from, to)) + q.Add("fields", "*all") + q.Add("expand", "renderedFields") + req.URL.RawQuery = q.Encode() + req.SetBasicAuth(os.Getenv("JIRA_USER"), os.Getenv("JIRA_TOKEN")) + req.Header.Add("Accept", "application/json") + resp, err := jcl.Do(req) + if err != nil { + return nil, fmt.Errorf("error executing jira search request: %w", err) + } + if resp.StatusCode >= 400 { + b := bytes.NewBuffer(nil) + io.Copy(b, resp.Body) + return nil, fmt.Errorf("error talking to jira: %s", b.String()) + } + + dec := json.NewDecoder(resp.Body) + var jsr *JiraSearchResp + err = dec.Decode(&jsr) + if err != nil { + return nil, fmt.Errorf("error decoding jira search response: %w", err) + } + + out := []Issue{} + + for _, i := range jsr.Issues { + iss := Issue{} + if s, ok := i.Fields["summary"]; ok { + iss.Summary, _ = s.(string) + } + if d, ok := i.Fields["description"]; ok { + doc := ifaceToADF(d) + iss.Description = adf.NewTranslator(doc, adf.NewMarkdownTranslator()).Translate() + } + if comms, ok := i.Fields["comment"].([]map[string]any); ok && len(comms) > 0 { + iss.Comments = make([]Comment, 0, len(comms)) + for _, c2 := range comms { + iss.Comments = append(iss.Comments, Comment{Author{DisplayName: c2["displayName"].(string), EmailAddress: c2["emailAddress"].(string)}, c2["body"].(string)}) + + } + } + out = append(out, iss) + + } + + return out, nil +} +func ifaceToADF(v any) *adf.ADF { + if v == nil { + return nil + } + + var doc *adf.ADF + + js, err := json.Marshal(v) + if err != nil { + return nil // ignore invalid data + } + if err = json.Unmarshal(js, &doc); err != nil { + return nil // ignore invalid data + } + + return doc +} diff --git a/issues/vikunja/vikunja.go b/issues/vikunja/vikunja.go new file mode 100644 index 0000000..5308e7c --- /dev/null +++ b/issues/vikunja/vikunja.go @@ -0,0 +1,47 @@ +package vikunja + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "os" +) + +type Task struct { + Title string `json:"title"` + Description string `json:"description"` + Status int `json:"status"` // Assuming 1 is for completed tasks +} + +func GetTasks(instance, user, from, to string) ([]Task, error) { + req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("https://%s/api/v1/user/tasks", instance), nil) + if err != nil { + return nil, fmt.Errorf("error building vikunja request: %w", err) + } + q := req.URL.Query() + q.Add("user_id", user) + q.Add("status", "1") // Assuming 1 is for completed tasks + req.URL.RawQuery = q.Encode() + req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("VIKUNJA_TOKEN"))) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, fmt.Errorf("error executing vikunja request: %w", err) + } + defer resp.Body.Close() + + if resp.StatusCode >= 400 { + b := bytes.NewBuffer(nil) + io.Copy(b, resp.Body) + return nil, fmt.Errorf("error talking to vikunja: %s", b.String()) + } + + var tasks []Task + err = json.NewDecoder(resp.Body).Decode(&tasks) + if err != nil { + return nil, fmt.Errorf("error decoding vikunja response: %w", err) + } + + return tasks, nil +}