Skip to content

Commit b23784f

Browse files
fix: filter PRs by timestamp to avoid returning stale results
When polling for a linked PR after assigning Copilot to an issue, we now capture the assignment time before the mutation and filter to only return PRs created after that time. This prevents the tool from incorrectly returning old PRs from previous Copilot assignments.
1 parent e1cfb91 commit b23784f

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

pkg/github/issues.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,10 +1611,11 @@ func (d *mvpDescription) String() string {
16111611

16121612
// linkedPullRequest represents a PR linked to an issue by Copilot.
16131613
type linkedPullRequest struct {
1614-
Number int
1615-
URL string
1616-
Title string
1617-
State string
1614+
Number int
1615+
URL string
1616+
Title string
1617+
State string
1618+
CreatedAt time.Time
16181619
}
16191620

16201621
// pollConfigKey is a context key for polling configuration.
@@ -1644,7 +1645,8 @@ func getPollConfig(ctx context.Context) PollConfig {
16441645

16451646
// findLinkedCopilotPR searches for a PR created by the copilot-swe-agent bot that references the given issue.
16461647
// It queries the issue's timeline for CrossReferencedEvent items from PRs authored by copilot-swe-agent.
1647-
func findLinkedCopilotPR(ctx context.Context, client *githubv4.Client, owner, repo string, issueNumber int) (*linkedPullRequest, error) {
1648+
// The createdAfter parameter filters to only return PRs created after the specified time.
1649+
func findLinkedCopilotPR(ctx context.Context, client *githubv4.Client, owner, repo string, issueNumber int, createdAfter time.Time) (*linkedPullRequest, error) {
16481650
// Query timeline items looking for CrossReferencedEvent from PRs by copilot-swe-agent
16491651
var query struct {
16501652
Repository struct {
@@ -1655,11 +1657,12 @@ func findLinkedCopilotPR(ctx context.Context, client *githubv4.Client, owner, re
16551657
CrossReferencedEvent struct {
16561658
Source struct {
16571659
PullRequest struct {
1658-
Number int
1659-
URL string
1660-
Title string
1661-
State string
1662-
Author struct {
1660+
Number int
1661+
URL string
1662+
Title string
1663+
State string
1664+
CreatedAt githubv4.DateTime
1665+
Author struct {
16631666
Login string
16641667
}
16651668
} `graphql:"... on PullRequest"`
@@ -1681,19 +1684,23 @@ func findLinkedCopilotPR(ctx context.Context, client *githubv4.Client, owner, re
16811684
return nil, err
16821685
}
16831686

1684-
// Look for a PR from copilot-swe-agent
1687+
// Look for a PR from copilot-swe-agent created after the assignment time
16851688
for _, node := range query.Repository.Issue.TimelineItems.Nodes {
16861689
if node.TypeName != "CrossReferencedEvent" {
16871690
continue
16881691
}
16891692
pr := node.CrossReferencedEvent.Source.PullRequest
16901693
if pr.Number > 0 && pr.Author.Login == "copilot-swe-agent" {
1691-
return &linkedPullRequest{
1692-
Number: pr.Number,
1693-
URL: pr.URL,
1694-
Title: pr.Title,
1695-
State: pr.State,
1696-
}, nil
1694+
// Only return PRs created after the assignment time
1695+
if pr.CreatedAt.Time.After(createdAfter) {
1696+
return &linkedPullRequest{
1697+
Number: pr.Number,
1698+
URL: pr.URL,
1699+
Title: pr.Title,
1700+
State: pr.State,
1701+
CreatedAt: pr.CreatedAt.Time,
1702+
}, nil
1703+
}
16971704
}
16981705
}
16991706

@@ -1882,6 +1889,9 @@ func AssignCopilotToIssue(t translations.TranslationHelperFunc) inventory.Server
18821889
// The header will be read by the HTTP transport if it's configured to do so
18831890
ctxWithFeatures := withGraphQLFeatures(ctx, "issues_copilot_assignment_api_support")
18841891

1892+
// Capture the time before assignment to filter out older PRs during polling
1893+
assignmentTime := time.Now().UTC()
1894+
18851895
if err := client.Mutate(
18861896
ctxWithFeatures,
18871897
&updateIssueMutation,
@@ -1895,7 +1905,7 @@ func AssignCopilotToIssue(t translations.TranslationHelperFunc) inventory.Server
18951905
return nil, nil, fmt.Errorf("failed to update issue with agent assignment: %w", err)
18961906
}
18971907

1898-
// Poll for a linked PR created by Copilot
1908+
// Poll for a linked PR created by Copilot after the assignment
18991909
pollConfig := getPollConfig(ctx)
19001910

19011911
var linkedPR *linkedPullRequest
@@ -1904,7 +1914,7 @@ func AssignCopilotToIssue(t translations.TranslationHelperFunc) inventory.Server
19041914
time.Sleep(pollConfig.Delay)
19051915
}
19061916

1907-
pr, err := findLinkedCopilotPR(ctx, client, params.Owner, params.Repo, int(params.IssueNumber))
1917+
pr, err := findLinkedCopilotPR(ctx, client, params.Owner, params.Repo, int(params.IssueNumber), assignmentTime)
19081918
if err != nil {
19091919
// Log but don't fail - polling errors are non-fatal
19101920
continue

0 commit comments

Comments
 (0)