-
-
Notifications
You must be signed in to change notification settings - Fork 34
fix(sync): avoid downloading unchanged issue comments #203
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,66 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSyncAndRefreshForceCommentDownload(t *testing.T) { | ||
| for _, command := range []string{"sync", "refresh"} { | ||
| t.Run(command, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| dir := t.TempDir() | ||
| configPath, dbPath := filepath.Join(dir, "config.toml"), filepath.Join(dir, "archive.db") | ||
| if err := New().Run(ctx, []string{"--config", configPath, "init", "--db", dbPath}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| calls := 0 | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.URL.Path { | ||
| case "/repos/fixture/repo": | ||
| _ = json.NewEncoder(w).Encode(map[string]any{"id": 123}) | ||
| case "/repos/fixture/repo/issues": | ||
| row := githubIssueJSON(1, "issue", "Discussion") | ||
| row["comments"] = 1 | ||
| _ = json.NewEncoder(w).Encode([]map[string]any{row}) | ||
| case "/repos/fixture/repo/issues/1/comments": | ||
| calls++ | ||
| _ = json.NewEncoder(w).Encode([]map[string]any{{"id": 11, "body": "saved discussion"}}) | ||
| default: | ||
| t.Errorf("unexpected request: %s", r.URL.Path) | ||
| http.Error(w, "unexpected", http.StatusBadRequest) | ||
| } | ||
| })) | ||
| defer server.Close() | ||
| t.Setenv("GITHUB_TOKEN", "test-gh-token") | ||
| t.Setenv("GITCRAWL_GITHUB_BASE_URL", server.URL) | ||
| args := []string{"--config", configPath, command, "fixture/repo", "--include-comments", "--limit", "1", "--json"} | ||
| if command == "refresh" { | ||
| args = append(args, "--no-embed", "--no-cluster") | ||
| } | ||
| for run, wantCalls := range []int{1, 1, 2} { | ||
| var stdout bytes.Buffer | ||
| app := New() | ||
| app.Stdout = &stdout | ||
| if run == 2 { | ||
| args = append(args, "--force") | ||
| } | ||
| if err := app.Run(ctx, args); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if calls != wantCalls { | ||
| t.Fatalf("run=%d calls=%d want=%d", run, calls, wantCalls) | ||
| } | ||
| if run == 1 && !strings.Contains(stdout.String(), `"comments_synced": 0`) { | ||
| t.Fatalf("reused comments counted as downloaded: %s", stdout.String()) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } |
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,76 @@ | ||
| package store | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "time" | ||
| ) | ||
|
|
||
| // IssueCommentReuse identifies the exact saved discussion validated by a fresh | ||
| // parent timestamp and comment count. PR reviews are deliberately excluded. | ||
| type IssueCommentReuse struct { | ||
| ThreadID int64 | ||
| ObservationSequence int64 | ||
| CommentIDs []int64 | ||
| } | ||
|
|
||
| func (s *Store) ReusableIssueComments(ctx context.Context, repoID int64, number int, updatedAt string, count int) (*IssueCommentReuse, error) { | ||
| updated, err := time.Parse(time.RFC3339Nano, updatedAt) | ||
| if err != nil || count < 0 { | ||
| return nil, nil | ||
| } | ||
| var snapshot IssueCommentReuse | ||
| var source string | ||
| err = s.q().QueryRowContext(ctx, ` | ||
| select t.id, r.source_updated_at, r.observation_sequence | ||
| from threads t | ||
| join thread_child_observation_reservations r on r.thread_id = t.id and r.family = 'comments' | ||
| where t.repo_id = ? and t.number = ? | ||
| and not exists ( | ||
| select 1 from sync_attempt_failures f | ||
| where f.repo_id = t.repo_id and f.number = t.number | ||
| and f.operation = 'issue_comments' and f.resolved_at is null | ||
| ) | ||
| `, repoID, number).Scan(&snapshot.ThreadID, &source, &snapshot.ObservationSequence) | ||
| if err == sql.ErrNoRows { | ||
| return nil, nil | ||
| } | ||
| if err != nil { | ||
| return nil, fmt.Errorf("read reusable issue comments: %w", err) | ||
| } | ||
| observed, err := time.Parse(time.RFC3339Nano, source) | ||
| if err != nil || !observed.Equal(updated) || snapshot.ObservationSequence <= 0 { | ||
| return nil, nil | ||
| } | ||
| memberIDs, found, err := s.ThreadChildObservationMemberIDs(ctx, snapshot.ThreadID, ThreadChildComments, snapshot.ObservationSequence) | ||
| if err != nil || !found { | ||
| return nil, err | ||
| } | ||
| comments, err := s.ListComments(ctx, snapshot.ThreadID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| byID := make(map[int64]Comment, len(comments)) | ||
| for _, comment := range comments { | ||
| byID[comment.ID] = comment | ||
| } | ||
| for _, id := range memberIDs { | ||
| comment, found := byID[id] | ||
| if !found { | ||
| return nil, nil | ||
| } | ||
| if comment.CommentType == "issue_comment" { | ||
| // Portable pruning keeps observation membership but strips raw payloads | ||
| // and may truncate bodies. Fetch again before claiming full evidence. | ||
| if comment.RawJSON == "" || comment.Body == "" { | ||
| return nil, nil | ||
| } | ||
| snapshot.CommentIDs = append(snapshot.CommentIDs, id) | ||
| } | ||
| } | ||
| if len(snapshot.CommentIDs) != count { | ||
| return nil, nil | ||
| } | ||
| return &snapshot, 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No test feeds the same instant in another form (
+00:00instead ofZ, or fractional seconds), so replacingEqualwith a string comparison keeps every test green. One invalidation-table row where the freshupdated_atis2026-04-26T00:00:00+00:00and reuse still happens would pin theEqualsemantics.