<commit_before>package jira

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"mime/multipart"
	"net/url"
	"strings"

	"github.com/coryb/oreo"

	"github.com/go-jira/jira/jiradata"
)

type IssueQueryProvider interface {
	ProvideIssueQueryString() string
    ProvideDefaultProject() string
}

type IssueOptions struct {
	Fields        []string `json:"fields,omitempty" yaml:"fields,omitempty"`
	Expand        []string `json:"expand,omitempty" yaml:"expand,omitempty"`
	Properties    []string `json:"properties,omitempty" yaml:"properties,omitempty"`
	FieldsByKeys  bool     `json:"fieldsByKeys,omitempty" yaml:"fieldsByKeys,omitempty"`
	UpdateHistory bool     `json:"updateHistory,omitempty" yaml:"updateHistory,omitempty"`
    Project       string `json:"project,omitempty" yaml:"project,omitempty"`
}

func (o *IssueOptions) ProvideDefaultProject() string {
    return o.Project
}

func (o *IssueOptions) ProvideIssueQueryString() string {
	params := []string{}
	if len(o.Fields) > 0 {
		params = append(params, "fields="+strings.Join(o.Fields, ","))
	}
	if len(o.Expand) > 0 {
		params = append(params, "expand="+strings.Join(o.Expand, ","))
	}
	if len(o.Properties) > 0 {
		params = append(params, "properties="+strings.Join(o.Properties, ","))
	}
	if o.FieldsByKeys {
		params = append(params, "fieldsByKeys=true")
	}
	if o.UpdateHistory {
		params = append(params, "updateHistory=true")
	}
	if len(params) > 0 {
		return "?" + strings.Join(params, "&")
	}
	return ""
}

func CaseInsensitiveContains(s, substr string) bool {
    s, substr = strings.ToUpper(s), strings.ToUpper(substr)
    return strings.Contains(s, substr)
}

// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-getIssue
func (j *Jira) GetIssue(issue string, iqg IssueQueryProvider) (*jiradata.Issue, error) {
	return GetIssue(j.UA, j.Endpoint, issue, iqg)
}

func GetIssue(ua HttpClient, endpoint string, issue string, iqg IssueQueryProvider) (*jiradata.Issue, error) {
	query := ""
    pro := iqg.ProvideDefaultProject()
	if iqg != nil {
		query = iqg.ProvideIssueQueryString()
	}
	uri := URLJoin(endpoint, "rest/api/2/issue", issue)
	uri += query
	resp, err := ua.GetJSON(uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		results := &jiradata.Issue{}
		return results, json.NewDecoder(resp.Body).Decode(results)
	}
    // Ticket not found, maybe try prepend Project value?
    if ! CaseInsensitiveContains(issue,pro) {
        issue = pro+"-"+issue
        return GetIssue(ua, endpoint, issue, iqg)
    }
	return nil, responseError(resp)
}

func (j *Jira) GetIssueWorklog(issue string) (*jiradata.Worklogs, error) {
	return GetIssueWorklog(j.UA, j.Endpoint, issue)
}

// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue/{issueIdOrKey}/worklog-getIssueWorklog
func GetIssueWorklog(ua HttpClient, endpoint string, issue string) (*jiradata.Worklogs, error) {
	startAt := 0
	total := 1
	maxResults := 100
	worklogs := jiradata.Worklogs{}
	for startAt < total {
		uri := URLJoin(endpoint, "rest/api/2/issue", issue, "worklog")
		uri += fmt.Sprintf("?startAt=%d&maxResults=%d", startAt, maxResults)
		resp, err := ua.GetJSON(uri)
		if err != nil {
			return nil, err
		}
		defer resp.Body.Close()

		if resp.StatusCode == 200 {<commit_msg>Forgot you use TAB instead of spaces<commit_after>package jira

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"mime/multipart"
	"net/url"
	"strings"

	"github.com/coryb/oreo"

	"github.com/go-jira/jira/jiradata"
)

type IssueQueryProvider interface {
	ProvideIssueQueryString() string
	ProvideDefaultProject() string
}

type IssueOptions struct {
	Fields	    []string `json:"fields,omitempty" yaml:"fields,omitempty"`
	Expand	    []string `json:"expand,omitempty" yaml:"expand,omitempty"`
	Properties	[]string `json:"properties,omitempty" yaml:"properties,omitempty"`
	FieldsByKeys  bool	 `json:"fieldsByKeys,omitempty" yaml:"fieldsByKeys,omitempty"`
	UpdateHistory bool	 `json:"updateHistory,omitempty" yaml:"updateHistory,omitempty"`
	Project	      string `json:"project,omitempty" yaml:"project,omitempty"`
}

func (o *IssueOptions) ProvideDefaultProject() string {
	return o.Project
}

func (o *IssueOptions) ProvideIssueQueryString() string {
	params := []string{}
	if len(o.Fields) > 0 {
		params = append(params, "fields="+strings.Join(o.Fields, ","))
	}
	if len(o.Expand) > 0 {
		params = append(params, "expand="+strings.Join(o.Expand, ","))
	}
	if len(o.Properties) > 0 {
		params = append(params, "properties="+strings.Join(o.Properties, ","))
	}
	if o.FieldsByKeys {
		params = append(params, "fieldsByKeys=true")
	}
	if o.UpdateHistory {
		params = append(params, "updateHistory=true")
	}
	if len(params) > 0 {
		return "?" + strings.Join(params, "&")
	}
	return ""
}

func CaseInsensitiveContains(s, substr string) bool {
	s, substr = strings.ToUpper(s), strings.ToUpper(substr)
	return strings.Contains(s, substr)
}

// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue-getIssue
func (j *Jira) GetIssue(issue string, iqg IssueQueryProvider) (*jiradata.Issue, error) {
	return GetIssue(j.UA, j.Endpoint, issue, iqg)
}

func GetIssue(ua HttpClient, endpoint string, issue string, iqg IssueQueryProvider) (*jiradata.Issue, error) {
	query := ""
	pro := iqg.ProvideDefaultProject()
	if iqg != nil {
		query = iqg.ProvideIssueQueryString()
	}
	uri := URLJoin(endpoint, "rest/api/2/issue", issue)
	uri += query
	resp, err := ua.GetJSON(uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode == 200 {
		results := &jiradata.Issue{}
		return results, json.NewDecoder(resp.Body).Decode(results)
	}
	// Ticket not found, maybe try prepend Project value?
	if ! CaseInsensitiveContains(issue,pro) {
		issue = pro+"-"+issue
		return GetIssue(ua, endpoint, issue, iqg)
	}
	return nil, responseError(resp)
}

func (j *Jira) GetIssueWorklog(issue string) (*jiradata.Worklogs, error) {
	return GetIssueWorklog(j.UA, j.Endpoint, issue)
}

// https://docs.atlassian.com/jira/REST/cloud/#api/2/issue/{issueIdOrKey}/worklog-getIssueWorklog
func GetIssueWorklog(ua HttpClient, endpoint string, issue string) (*jiradata.Worklogs, error) {
	startAt := 0
	total := 1
	maxResults := 100
	worklogs := jiradata.Worklogs{}
	for startAt < total {
		uri := URLJoin(endpoint, "rest/api/2/issue", issue, "worklog")
		uri += fmt.Sprintf("?startAt=%d&maxResults=%d", startAt, maxResults)
		resp, err := ua.GetJSON(uri)
		if err != nil {
			return nil, err
		}
		defer resp.Body.Close()

		if resp.StatusCode == 200 {
{'fraction_non_alphanumeric': 0.15178982946689198, 'fraction_numerical': 0.005838070364111231, 'mean_word_length': 4.675675675675675, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6549159', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2258, 'n_words': 486}