-
Notifications
You must be signed in to change notification settings - Fork 306
Vendor Prow with Jira QA Contact/Target Version removal; add name-based custom field helper #5017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
deepsm007
wants to merge
1
commit into
openshift:main
Choose a base branch
from
deepsm007:jira-customfield-configurable-downstream
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+25,107
−2,147
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,108 @@ | ||
| package jira | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "maps" | ||
| "sync" | ||
|
|
||
| jiraapi "github.com/andygrunwald/go-jira" | ||
|
|
||
| jirautil "sigs.k8s.io/prow/pkg/jira" | ||
| ) | ||
|
|
||
| // CustomFieldResolver resolves field names to IDs; fetches the field list once and caches. | ||
| // Use SetFallbackIDs to supply name→ID when API lookup is empty (e.g. instance-specific IDs). | ||
| type CustomFieldResolver struct { | ||
| client *jiraapi.Client | ||
| mu sync.RWMutex | ||
| byName map[string]string | ||
| loaded bool | ||
| fallbackIDs map[string]string // optional: field name -> custom field ID | ||
| } | ||
|
|
||
| func NewCustomFieldResolver(client *jiraapi.Client) *CustomFieldResolver { | ||
| return &CustomFieldResolver{ | ||
| client: client, | ||
| byName: make(map[string]string), | ||
| fallbackIDs: make(map[string]string), | ||
| } | ||
| } | ||
|
|
||
| // SetFallbackIDs sets the optional name→custom field ID map (e.g. "QA Contact" -> "customfield_12316243"). | ||
| // Safe to call concurrently; replaces the previous map with a copy. | ||
| func (r *CustomFieldResolver) SetFallbackIDs(ids map[string]string) { | ||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
| if ids == nil { | ||
| r.fallbackIDs = make(map[string]string) | ||
| return | ||
| } | ||
| r.fallbackIDs = maps.Clone(ids) | ||
| } | ||
|
|
||
| func (r *CustomFieldResolver) loadFields(ctx context.Context) error { | ||
| r.mu.RLock() | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if r.loaded { | ||
| r.mu.RUnlock() | ||
| return nil | ||
| } | ||
| r.mu.RUnlock() | ||
|
|
||
| r.mu.Lock() | ||
| defer r.mu.Unlock() | ||
| if r.loaded { | ||
| return nil | ||
| } | ||
| if r.client == nil { | ||
| return errors.New("jira client is nil") | ||
| } | ||
| fields, resp, err := r.client.Field.GetListWithContext(ctx) | ||
| if err != nil { | ||
| return jirautil.HandleJiraError(resp, err) | ||
| } | ||
| for _, f := range fields { | ||
| r.byName[f.Name] = f.ID | ||
| } | ||
| r.loaded = true | ||
| return nil | ||
| } | ||
|
|
||
| // FieldID returns the field ID for name, or "" if not found. | ||
| // If name lookup is empty and SetFallbackIDs supplied that name, that ID is returned. | ||
| func (r *CustomFieldResolver) FieldID(ctx context.Context, fieldName string) (string, error) { | ||
| if err := r.loadFields(ctx); err != nil { | ||
| return "", err | ||
| } | ||
| r.mu.RLock() | ||
| id := r.byName[fieldName] | ||
| if id == "" { | ||
| id = r.fallbackIDs[fieldName] | ||
| } | ||
| r.mu.RUnlock() | ||
| return id, nil | ||
| } | ||
|
|
||
| // Value returns the custom field value for the issue by field name. | ||
| func (r *CustomFieldResolver) Value(ctx context.Context, issue *jiraapi.Issue, fieldName string) (any, error) { | ||
| if issue == nil || issue.Fields == nil { | ||
| return nil, nil | ||
| } | ||
| id, err := r.FieldID(ctx, fieldName) | ||
| if err != nil || id == "" { | ||
| return nil, err | ||
| } | ||
| return ValueByID(issue, id), nil | ||
| } | ||
|
|
||
| // ValueByID returns the custom field value for the issue by raw field ID (e.g. customfield_12345). | ||
| // Use when name-based resolution is not available or as a temp override. | ||
| func ValueByID(issue *jiraapi.Issue, fieldID string) any { | ||
| if issue == nil || issue.Fields == nil || issue.Fields.Unknowns == nil || fieldID == "" { | ||
| return nil | ||
| } | ||
| if v, ok := issue.Fields.Unknowns[fieldID]; ok { | ||
| return v | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,134 @@ | ||
| package jira | ||
|
|
||
| import ( | ||
| "context" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| jiraapi "github.com/andygrunwald/go-jira" | ||
| ) | ||
|
|
||
| func TestValueByID(t *testing.T) { | ||
| t.Parallel() | ||
| tests := []struct { | ||
| name string | ||
| issue *jiraapi.Issue | ||
| fieldID string | ||
| want any | ||
| }{ | ||
| { | ||
| name: "nil issue", | ||
| issue: nil, | ||
| fieldID: "customfield_123", | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "nil Fields", | ||
| issue: &jiraapi.Issue{}, | ||
| fieldID: "customfield_123", | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "nil Unknowns", | ||
| issue: &jiraapi.Issue{Fields: &jiraapi.IssueFields{}}, | ||
| fieldID: "customfield_123", | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "empty fieldID", | ||
| issue: &jiraapi.Issue{ | ||
| Fields: &jiraapi.IssueFields{Unknowns: map[string]interface{}{"customfield_123": "val"}}, | ||
| }, | ||
| fieldID: "", | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "found", | ||
| issue: &jiraapi.Issue{ | ||
| Fields: &jiraapi.IssueFields{ | ||
| Unknowns: map[string]interface{}{"customfield_123": "qa-user"}, | ||
| }, | ||
| }, | ||
| fieldID: "customfield_123", | ||
| want: "qa-user", | ||
| }, | ||
| { | ||
| name: "not found", | ||
| issue: &jiraapi.Issue{ | ||
| Fields: &jiraapi.IssueFields{ | ||
| Unknowns: map[string]interface{}{"customfield_999": "other"}, | ||
| }, | ||
| }, | ||
| fieldID: "customfield_123", | ||
| want: nil, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got := ValueByID(tt.issue, tt.fieldID) | ||
| if !reflect.DeepEqual(got, tt.want) { | ||
| t.Errorf("ValueByID() = %v, want %v", got, tt.want) | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestValueByID_with_fallback_id(t *testing.T) { | ||
| t.Parallel() | ||
| // ValueByID is used with a known custom field ID (e.g. customfield_12316243). | ||
| // SetFallbackIDs can map names to these IDs when name-based API lookup is empty. | ||
| issue := &jiraapi.Issue{ | ||
| Fields: &jiraapi.IssueFields{ | ||
| Unknowns: map[string]interface{}{"customfield_12316243": "qa@example.com"}, | ||
| }, | ||
| } | ||
| got := ValueByID(issue, "customfield_12316243") | ||
| if !reflect.DeepEqual(got, "qa@example.com") { | ||
| t.Errorf("ValueByID = %v, want qa@example.com", got) | ||
| } | ||
| } | ||
|
|
||
| // TestFieldID_and_Value_use_fallback exercises the fallback path when the field list | ||
| // is loaded but the name is not in the API response (e.g. instance-specific custom field). | ||
| func TestFieldID_and_Value_use_fallback(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := context.Background() | ||
| // Resolver with no client, already "loaded" with empty byName so we skip API and use fallback. | ||
| r := &CustomFieldResolver{ | ||
| client: nil, | ||
| byName: map[string]string{}, | ||
| loaded: true, | ||
| fallbackIDs: map[string]string{"QA Contact": "customfield_12316243"}, | ||
| } | ||
| id, err := r.FieldID(ctx, "QA Contact") | ||
| if err != nil { | ||
| t.Fatalf("FieldID() error = %v", err) | ||
| } | ||
| if id != "customfield_12316243" { | ||
| t.Errorf("FieldID() = %q, want customfield_12316243", id) | ||
| } | ||
| issue := &jiraapi.Issue{ | ||
| Fields: &jiraapi.IssueFields{ | ||
| Unknowns: map[string]interface{}{"customfield_12316243": "qa@example.com"}, | ||
| }, | ||
| } | ||
| val, err := r.Value(ctx, issue, "QA Contact") | ||
| if err != nil { | ||
| t.Fatalf("Value() error = %v", err) | ||
| } | ||
| if !reflect.DeepEqual(val, "qa@example.com") { | ||
| t.Errorf("Value() = %v, want qa@example.com", val) | ||
| } | ||
| } | ||
|
|
||
| // TestFieldID_nil_client_returns_error ensures we return an error instead of panicking. | ||
| func TestFieldID_nil_client_returns_error(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := context.Background() | ||
| r := NewCustomFieldResolver(nil) | ||
| r.SetFallbackIDs(map[string]string{"QA Contact": "customfield_12316243"}) | ||
| _, err := r.FieldID(ctx, "QA Contact") | ||
| if err == nil { | ||
| t.Error("FieldID() expected error when client is nil") | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.