From c17d41318ca3b2c1a37e669774c6d8d1ffd84707 Mon Sep 17 00:00:00 2001 From: Jared Hatfield Date: Thu, 26 Nov 2026 09:46:26 +0000 Subject: [PATCH] Remove cached JSON and generated HTML for archived repositories Archived repositories are now tracked during the repo listing so their stale cached JSON data and previously generated detail pages are deleted on each crawl, keeping output consistent with repos that are no longer processed. Adds tests covering paginated listing and file removal. --- README.md | 2 +- main.go | 31 +++++++++++++++++++++---- main_test.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1ed2775..ed2882e 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ Additionally, a `timestamp.json` file is created: The tool processes public repositories from the specified organization: -- Skips archived repositories +- Skips archived repositories and removes their cached JSON data and generated detail pages during crawls - Skips repositories without releases - Compares the default branch against the latest release tag - Captures all commits between the release and branch HEAD diff --git a/main.go b/main.go index dd30038..7bbee5f 100644 --- a/main.go +++ b/main.go @@ -112,11 +112,17 @@ func runCrawl(owner string, limit int) { log.Fatalf("Failed to create output directory: %v", err) } - repos, err := listPublicRepos(ctx, client, owner, limit) + repos, archivedRepos, err := listPublicRepos(ctx, client, owner, limit) if err != nil { log.Fatalf("Failed to list repositories: %v", err) } + for _, repo := range archivedRepos { + if err := removeArchivedRepoFiles(outputDir, "output", repo.GetName()); err != nil { + log.Fatalf("Failed to remove cached files for archived repository %s: %v", repo.GetName(), err) + } + } + fmt.Printf("Found %d public repositories\n", len(repos)) processedCount := 0 @@ -278,8 +284,9 @@ func runGenerate() { fmt.Printf(" Open %s/index.html in your browser\n", outputDir) } -func listPublicRepos(ctx context.Context, client *github.Client, owner string, limit int) ([]*github.Repository, error) { +func listPublicRepos(ctx context.Context, client *github.Client, owner string, limit int) ([]*github.Repository, []*github.Repository, error) { var allRepos []*github.Repository + var archivedRepos []*github.Repository opt := &github.RepositoryListByOrgOptions{ Type: "public", ListOptions: github.ListOptions{PerPage: 100}, @@ -288,18 +295,19 @@ func listPublicRepos(ctx context.Context, client *github.Client, owner string, l for { repos, resp, err := client.Repositories.ListByOrg(ctx, owner, opt) if err != nil { - return nil, err + return nil, nil, err } for _, repo := range repos { if repo.GetArchived() { + archivedRepos = append(archivedRepos, repo) continue } allRepos = append(allRepos, repo) } if limit > 0 && len(allRepos) >= limit { - return allRepos[:limit], nil + return allRepos[:limit], archivedRepos, nil } if resp.NextPage == 0 { @@ -308,7 +316,20 @@ func listPublicRepos(ctx context.Context, client *github.Client, owner string, l opt.Page = resp.NextPage } - return allRepos, nil + return allRepos, archivedRepos, nil +} + +// Remove both cached crawl data and any previously generated detail page. +func removeArchivedRepoFiles(dataDir, outputDir, name string) error { + for _, filename := range []string{ + filepath.Join(dataDir, name+".json"), + filepath.Join(outputDir, name+".html"), + } { + if err := os.Remove(filename); err != nil && !os.IsNotExist(err) { + return fmt.Errorf("remove %s: %w", filename, err) + } + } + return nil } func checkLatestRelease(ctx context.Context, client *github.Client, owner, repo string) (bool, *github.RepositoryRelease) { diff --git a/main_test.go b/main_test.go index 134ad6d..c0159b3 100644 --- a/main_test.go +++ b/main_test.go @@ -2,7 +2,13 @@ package main import ( "bytes" + "context" + "fmt" + "net/http" + "net/http/httptest" + "encoding/json/v2" + "github.com/google/go-github/v62/github" "os" "path/filepath" "testing" @@ -69,3 +75,62 @@ func TestLoadLastCrawlTimestamp(t *testing.T) { t.Fatal("accepted duplicate timestamp") } } + +func TestArchivedRepositories(t *testing.T) { + for _, limit := range []int{0, 1} { + t.Run(fmt.Sprintf("limit_%d", limit), func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/orgs/example/repos" || r.URL.Query().Get("type") != "public" { + t.Errorf("unexpected request: %s", r.URL) + } + if r.URL.Query().Get("page") == "2" { + fmt.Fprint(w, `[{"name":"active","archived":false}]`) + return + } + w.Header().Set("Link", fmt.Sprintf(`; rel="next"`, r.Host)) + fmt.Fprint(w, `[{"name":"archived","archived":true}]`) + })) + defer server.Close() + client, err := github.NewClient(server.Client()).WithEnterpriseURLs(server.URL+"/", server.URL+"/") + if err != nil { + t.Fatal(err) + } + // Use the test server directly, without the enterprise API path prefix. + client.BaseURL.Path = "/" + repos, archived, err := listPublicRepos(context.Background(), client, "example", limit) + if err != nil { + t.Fatal(err) + } + if len(repos) != 1 || repos[0].GetName() != "active" || len(archived) != 1 || archived[0].GetName() != "archived" { + t.Fatalf("unexpected repositories: active=%v archived=%v", repos, archived) + } + dataDir, outputDir := t.TempDir(), t.TempDir() + for _, name := range []string{"active", "archived"} { + if err := writeJSON(filepath.Join(dataDir, name+".json"), RepositoryData{Name: name}); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(outputDir, name+".html"), []byte(name), 0600); err != nil { + t.Fatal(err) + } + } + for _, repo := range archived { + if err := removeArchivedRepoFiles(dataDir, outputDir, repo.GetName()); err != nil { + t.Fatal(err) + } + } + for _, path := range []string{filepath.Join(dataDir, "archived.json"), filepath.Join(outputDir, "archived.html")} { + if _, err := os.Stat(path); !os.IsNotExist(err) { + t.Errorf("archived file still exists: %s (%v)", path, err) + } + } + for _, path := range []string{filepath.Join(dataDir, "active.json"), filepath.Join(outputDir, "active.html")} { + if _, err := os.Stat(path); err != nil { + t.Error(err) + } + } + if err := removeArchivedRepoFiles(dataDir, outputDir, "archived"); err != nil { + t.Fatal(err) + } + }) + } +}