Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/normative/normalized-event/v1/jira-poll-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ deployment. The config gate's `jiraProjectKeyRe` accepts single-character
project keys, and the CLI always scopes the query to the project, so such a
project is polled on the default query too: every one of its events fails
validation and the all-invalid guard fails each cycle (see the Jira guide's
troubleshooting table). The project-key check inside `searchCandidates` runs
only when no JQL is set at all — direct API use such as the behaviour-test
step, not the CLI — so it never intercepts these keys.
troubleshooting table). The poller's project-set validation
(`validateProjectSet`, via `config.ValidJiraProjectKey`) runs on every cycle
and accepts the same keys as the config gate, so it never intercepts
single-character keys.
One further consumer has no downstream key check: `fullsend dispatch
--input-driver json` (or `--route <agent>` with a JSON event) validates the
key at parse time, so it now rejects such a key instead of printing a route
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/jira_lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ func (l *jiraLifecycle) comment(accountID, displayName, body string) {
// runJiraPoll for GitHub and runJiraPollGitLab for GitLab.
func (l *jiraLifecycle) options() jirapoll.Options {
opts := jirapoll.Options{
TargetRepo: l.repo,
JiraBaseURL: lifecycleJiraBaseURL,
JiraProject: "PROJ",
TargetRepo: l.repo,
JiraBaseURL: lifecycleJiraBaseURL,
JiraProjects: []string{"PROJ"},
}
if l.host == jirapoll.TargetForgeGitLab {
opts.TargetForge = jirapoll.TargetForgeGitLab
Expand Down
17 changes: 10 additions & 7 deletions internal/cli/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,15 @@ type jiraPollSettings struct {
baseURL string // tracker.jira.base_url: the Jira site URL
apiURL string // tracker.jira.api_url, or its default
project string // tracker.jira.project
jql string // tracker.jira.poll.query (or the default) scoped to project
query string // tracker.jira.poll.query, or the default; the poller scopes it to project
enabled bool // tracker.jira.poll.enabled is true
}

// loadJiraPollSettings reads the Jira site, API gateway, project, and poll
// settings from tracker.jira in the effective config.yaml. The config is
// the only source of these settings: base_url and project are required,
// api_url and poll.query fall back to their defaults, and the JQL is always
// the query scoped to the project as `project = "<project>" AND (<query>)`
// api_url and poll.query fall back to their defaults, and the poller always
// scopes the query to the project as `project = "<project>" AND (<query>)`
// with any ORDER BY last. The removed --jira-url, --jira-project, and --jql
// flags are rejected with a pointer to the config fields, and environment
// variables such as JIRA_BASE_URL are not read.
Expand Down Expand Up @@ -385,7 +385,7 @@ func loadJiraPollSettings(fullsendDir, jiraURL, jiraProject, jql string) (jiraPo
baseURL: jiraCfg.BaseURL,
apiURL: jiraCfg.EffectiveAPIURL(),
project: jiraCfg.Project,
jql: jiraCfg.PollJQL(),
query: jiraCfg.PollQuery(),
enabled: jiraCfg.PollEnabled(),
}, nil
}
Expand Down Expand Up @@ -573,9 +573,12 @@ func jiraPollOptions(args jiraPollArgs, matcher *celMatcher, getenv func(string)
return jirapoll.Options{
TargetRepo: args.targetRepo,
JiraBaseURL: args.jira.baseURL,
JiraProject: args.jira.project,
JQL: args.jira.jql,
OutputPath: args.outputPath,
// tracker.jira.project is the poller's one-item project set; the
// poller scopes the query to it, so the search JQL is
// config.JiraTrackingConfig.PollJQL's.
JiraProjects: []string{args.jira.project},
JQL: args.jira.query,
OutputPath: args.outputPath,

Authorization: jiraPollAuthorization(matcher.jiraAuth, getenv),
}
Expand Down
17 changes: 14 additions & 3 deletions internal/cli/poll_jira_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,36 @@ func TestLoadJiraPollSettings_ReadsConfig(t *testing.T) {
baseURL: "https://example.atlassian.net",
apiURL: "https://gateway.example.com",
project: "PROJ",
jql: `project = "PROJ" AND (labels = "fullsend") ORDER BY updated DESC`,
query: `labels = "fullsend" ORDER BY updated DESC`,
enabled: true,
}, got)

// The poller gets the project as a one-item set and the query before
// scoping, and scopes it to exactly the JQL PollJQL builds.
opts := jiraPollOptions(jiraPollArgs{jira: got}, &celMatcher{}, noJiraEnv)
assert.Equal(t, []string{"PROJ"}, opts.JiraProjects)
assert.Equal(t, `project = "PROJ" AND (labels = "fullsend") ORDER BY updated DESC`,
config.ScopeJQLToProjects(opts.JiraProjects, opts.JQL))
}

func TestLoadJiraPollSettings_Defaults(t *testing.T) {
dir := writeJiraPollConfig(t, "tracker:\n jira:\n base_url: https://example.atlassian.net\n project: PROJ\n")
got, err := loadJiraPollSettings(dir, "", "", "")
require.NoError(t, err)
assert.Equal(t, config.DefaultJiraAPIURL, got.apiURL)
assert.Equal(t, `project = "PROJ" AND (statusCategory != Done) ORDER BY updated DESC`, got.jql)
assert.Equal(t, config.DefaultJiraPollQuery, got.query)
assert.Equal(t, `project = "PROJ" AND (statusCategory != Done) ORDER BY updated DESC`,
config.ScopeJQLToProjects([]string{got.project}, got.query))
assert.False(t, got.enabled, "an omitted poll mapping leaves polling disabled")
}

func TestLoadJiraPollSettings_QueryWithoutOrderBy(t *testing.T) {
dir := writeJiraPollConfig(t, "tracker:\n jira:\n base_url: https://example.atlassian.net\n project: PROJ\n poll:\n enabled: false\n query: 'labels = a OR labels = b'\n")
got, err := loadJiraPollSettings(dir, "", "", "")
require.NoError(t, err)
assert.Equal(t, `project = "PROJ" AND (labels = a OR labels = b)`, got.jql,
assert.Equal(t, `labels = a OR labels = b`, got.query)
assert.Equal(t, `project = "PROJ" AND (labels = a OR labels = b)`,
config.ScopeJQLToProjects([]string{got.project}, got.query),
"an OR query stays inside the project scope")
assert.False(t, got.enabled)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/poll_jira_gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func TestValidateJiraGitLabPollArgs(t *testing.T) {
settings := jiraPollSettings{baseURL: "https://acme.atlassian.net", project: "PROJ", jql: `project = "PROJ"`, enabled: true}
settings := jiraPollSettings{baseURL: "https://acme.atlassian.net", project: "PROJ", query: "statusCategory != Done", enabled: true}
base := jiraPollGitLabFlags{
projectPath: "group/sub/project",
gitlabURL: "https://gitlab.com",
Expand Down
14 changes: 7 additions & 7 deletions internal/cli/poll_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ trigger: |
func TestValidateJiraPollArgs(t *testing.T) {
fullsendDir := t.TempDir()

settings := jiraPollSettings{baseURL: "https://acme.atlassian.net", project: "PROJ", jql: `project = "PROJ"`, enabled: true}
settings := jiraPollSettings{baseURL: "https://acme.atlassian.net", project: "PROJ", query: config.DefaultJiraPollQuery, enabled: true}

tests := []struct {
name string
Expand Down Expand Up @@ -783,16 +783,16 @@ authorization:
require.NoError(t, err)

args := jiraPollArgs{
jira: jiraPollSettings{baseURL: "https://jira.example.com", apiURL: config.DefaultJiraAPIURL, project: "PROJ", jql: "project = PROJ", enabled: true},
jira: jiraPollSettings{baseURL: "https://jira.example.com", apiURL: config.DefaultJiraAPIURL, project: "PROJ", query: `labels = "fullsend"`, enabled: true},
targetRepo: "group/sub/repo", outputPath: "out.json", fullsendDir: dir,
}
opts := jiraPollOptions(args, m, noJiraEnv)
assert.Equal(t, jirapoll.Options{
TargetRepo: "group/sub/repo",
JiraBaseURL: "https://jira.example.com",
JiraProject: "PROJ",
JQL: "project = PROJ",
OutputPath: "out.json",
TargetRepo: "group/sub/repo",
JiraBaseURL: "https://jira.example.com",
JiraProjects: []string{"PROJ"},
JQL: `labels = "fullsend"`,
OutputPath: "out.json",
Authorization: jirapoll.Authorization{
Configured: true,
Write: jirapoll.ActorList{Groups: []string{"devs"}},
Expand Down
38 changes: 32 additions & 6 deletions internal/config/issue_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,29 @@ var jqlOrderByRe = regexp.MustCompile(`(?i)\border\s+by\b`)
// value is not split. An empty predicate (a query that is only an
// ORDER BY clause) yields `project = "<project>"` plus the ORDER BY.
func ScopeJQLToProject(project, query string) string {
return ScopeJQLToProjects([]string{project}, query)
}

// ScopeJQLToProjects scopes query to exactly projects the way
// ScopeJQLToProject scopes it to one project. A one-item set yields the
// same JQL as ScopeJQLToProject; a larger set uses
// `project in ("<A>", "<B>") AND (<predicate>)`. The caller validates the
// keys and the predicate's parentheses (see ValidJiraProjectKey and
// JQLParensBalanced): this function only formats.
func ScopeJQLToProjects(projects []string, query string) string {
predicate, orderBy := splitJQLOrderBy(query)
predicate = strings.TrimSpace(predicate)
// Quote the key so a reserved-word key (AND, OR, IN) stays a value.
scoped := fmt.Sprintf("project = %q", project)
// Quote each key so a reserved-word key (AND, OR, IN) stays a value.
quoted := make([]string, len(projects))
for i, project := range projects {
quoted[i] = fmt.Sprintf("%q", project)
}
var scoped string
if len(quoted) == 1 {
scoped = "project = " + quoted[0]
} else {
scoped = "project in (" + strings.Join(quoted, ", ") + ")"
}
if predicate != "" {
scoped += " AND (" + predicate + ")"
}
Expand Down Expand Up @@ -463,6 +482,12 @@ func cloneTracking(src *TrackingConfig) *TrackingConfig {
// followed by uppercase letters, digits, or underscores.
var jiraProjectKeyRe = regexp.MustCompile(`^[A-Z][A-Z0-9_]*$`)

// ValidJiraProjectKey reports whether key is a Jira project key in the
// form tracker.jira.project accepts (see jiraProjectKeyRe).
func ValidJiraProjectKey(key string) bool {
return jiraProjectKeyRe.MatchString(key)
}

// validPathSegments reports whether p is a slash-separated path with at
// least minSegments segments and no empty segment.
func validPathSegments(p string, minSegments int) bool {
Expand Down Expand Up @@ -561,16 +586,17 @@ func (j *JiraTrackingConfig) ValidateConnection() error {
if !jiraProjectKeyRe.MatchString(j.Project) {
return fmt.Errorf("tracker.jira: project %q must be a Jira project key (for example PROJ)", j.Project)
}
if j.Poll != nil && !jqlParensBalanced(j.Poll.Query) {
if j.Poll != nil && !JQLParensBalanced(j.Poll.Query) {
return fmt.Errorf("tracker.jira: poll.query has unbalanced parentheses: the query is wrapped in parentheses to scope it to the project")
}
return nil
}

// jqlParensBalanced reports whether the parentheses of query, ignoring
// JQLParensBalanced reports whether the parentheses of query, ignoring
// those inside quoted strings, are balanced and never close before they
// open.
func jqlParensBalanced(query string) bool {
// open. A query that passes cannot close the parenthesis ScopeJQLToProjects
// wraps it in, so it cannot widen the project scope.
func JQLParensBalanced(query string) bool {
quoted := quotedRanges(query)
depth := 0
for i := 0; i < len(query); i++ {
Expand Down
19 changes: 19 additions & 0 deletions internal/config/jira_tracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,25 @@ func TestScopeJQLToProject(t *testing.T) {
}
}

func TestScopeJQLToProjects(t *testing.T) {
query := `labels = a OR project = OTHER ORDER BY updated DESC`
assert.Equal(t, ScopeJQLToProject("PROJ", query), ScopeJQLToProjects([]string{"PROJ"}, query),
"a one-item set scopes exactly as ScopeJQLToProject")
assert.Equal(t, `project in ("ALPHA", "BETA") AND (labels = a OR project = OTHER) ORDER BY updated DESC`,
ScopeJQLToProjects([]string{"ALPHA", "BETA"}, query))
assert.Equal(t, `project in ("AND", "OR") ORDER BY key`, ScopeJQLToProjects([]string{"AND", "OR"}, "ORDER BY key"),
"reserved-word keys stay quoted values")
}

func TestValidJiraProjectKey(t *testing.T) {
for _, key := range []string{"P", "PROJ", "MY_PROJ", "ABC123"} {
assert.True(t, ValidJiraProjectKey(key), key)
}
for _, key := range []string{"", "proj", "1PROJ", "_PROJ", "PR OJ", `PROJ"`} {
assert.False(t, ValidJiraProjectKey(key), key)
}
}

func TestScopeJQLToProject_QuotesReservedWordKey(t *testing.T) {
for _, key := range []string{"AND", "OR", "IN"} {
assert.Equal(t, `project = "`+key+`" AND (labels = a)`, ScopeJQLToProject(key, "labels = a"))
Expand Down
Loading
Loading