Skip to content

Commit

Permalink
fix bug when skipping archived repos (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceschae authored Jan 31, 2025
1 parent d4a3719 commit 076d550
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
10 changes: 10 additions & 0 deletions mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ var (
repoName2 = "terratest"
repoName3 = "fetch"
repoName4 = "terraform-kubernetes-helm"
repoName5 = "terraform-google-load-balancer"
)

var (
repoURL1 = "https://github.com/gruntwork-io/terragrunt"
repoURL2 = "https://github.com/gruntwork-io/terratest"
repoURL3 = "https://github.com/gruntwork-io/fetch"
repoURL4 = "https://github.com/gruntwork-io/terraform-kubernetes-helm"
repoURL5 = "https://github.com/gruntwork-io/terraform-google-load-balancer"
)

var archivedFlag = true
Expand Down Expand Up @@ -57,6 +59,14 @@ var MockGithubRepositories = []*github.Repository{
HTMLURL: &repoURL4,
Archived: &archivedFlag,
},
{
Owner: &github.User{
Login: &ownerName,
},
Name: &repoName5,
HTMLURL: &repoURL5,
Archived: &archivedFlag,
},
}

// This mocks the PullRequest service in go-github that is used in production to call the associated GitHub endpoint
Expand Down
28 changes: 15 additions & 13 deletions repository/fetch-repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,22 @@ func getReposByOrg(config *config.GitXargsConfig) ([]*github.Repository, error)
}

// github.RepositoryListByOrgOptions doesn't seem to be able to filter out archived repos
// So re-slice the repos list if --skip-archived-repos is passed and the repository is in archived/read-only state
for i, repo := range repos {
if config.SkipArchivedRepos && repo.GetArchived() {
logger.WithFields(logrus.Fields{
"Name": repo.GetFullName(),
}).Debug("Skipping archived repository")

// Track repos to skip because of archived status for our final run report
config.Stats.TrackSingle(stats.ReposArchivedSkipped, repo)

reposToAdd = append(repos[:i], repos[i+1:]...)
} else {
reposToAdd = repos
// So filter the repos list if --skip-archived-repos is passed and the repository is in archived/read-only state
if config.SkipArchivedRepos {
for _, repo := range repos {
if repo.GetArchived() {
logger.WithFields(logrus.Fields{
"Name": repo.GetFullName(),
}).Debug("Skipping archived repository")

// Track repos to skip because of archived status for our final run report
config.Stats.TrackSingle(stats.ReposArchivedSkipped, repo)
} else {
reposToAdd = append(reposToAdd, repo)
}
}
} else {
reposToAdd = repos
}

allRepos = append(allRepos, reposToAdd...)
Expand Down
2 changes: 1 addition & 1 deletion repository/fetch-repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ func TestSkipArchivedRepos(t *testing.T) {

githubRepos, reposByOrgLookupErr := getReposByOrg(config)

assert.Equal(t, len(githubRepos), len(mocks.MockGithubRepositories)-1)
assert.Equal(t, len(githubRepos), len(mocks.MockGithubRepositories)-2)
assert.NoError(t, reposByOrgLookupErr)
}

0 comments on commit 076d550

Please sign in to comment.