Skip to content
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

Add support for repo archiving #1165

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust_team_data/src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub struct Repo {
pub teams: Vec<RepoTeam>,
pub members: Vec<RepoMember>,
pub branch_protections: Vec<BranchProtection>,
pub archived: bool,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
Expand Down
41 changes: 35 additions & 6 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub(crate) struct Data {
people: HashMap<String, Person>,
teams: HashMap<String, Team>,
archived_teams: Vec<Team>,
repos: HashMap<(String, String), Repo>,
repos: Vec<Repo>,
archived_repos: Vec<Repo>,
config: Config,
}

Expand All @@ -20,11 +21,12 @@ impl Data {
people: HashMap::new(),
teams: HashMap::new(),
archived_teams: Vec::new(),
repos: HashMap::new(),
repos: Vec::new(),
archived_repos: Vec::new(),
config: load_file(Path::new("config.toml"))?,
};

data.load_dir("repos", true, |this, org, repo: Repo, path: &Path| {
fn validate_repo(org: &str, repo: &Repo, path: &Path) -> anyhow::Result<()> {
if repo.org != org {
bail!(
"repo '{}' is located in the '{}' org directory but its org is '{}'",
Expand All @@ -40,12 +42,31 @@ impl Data {
path.file_name().unwrap().to_str().unwrap()
)
}
Ok(())
}

data.load_dir("repos", true, |this, org, repo: Repo, path: &Path| {
if org == "archive" {
return Ok(());
}

this.repos
.insert((repo.org.clone(), repo.name.clone()), repo);
validate_repo(org, &repo, path)?;
this.repos.push(repo);
Ok(())
})?;

if Path::new("repos/archive").is_dir() {
data.load_dir(
"repos/archive",
true,
|this, org, repo: Repo, path: &Path| {
validate_repo(org, &repo, path)?;
this.archived_repos.push(repo);
Ok(())
},
)?;
}

data.load_dir("people", false, |this, _dir, person: Person, _path| {
person.validate()?;
this.people.insert(person.github().to_string(), person);
Expand Down Expand Up @@ -154,7 +175,15 @@ impl Data {
}

pub(crate) fn repos(&self) -> impl Iterator<Item = &Repo> {
self.repos.values()
self.repos.iter()
}

pub(crate) fn archived_repos(&self) -> impl Iterator<Item = &Repo> {
self.archived_repos.iter()
}

pub(crate) fn all_repos(&self) -> impl Iterator<Item = &Repo> {
self.repos().chain(self.archived_repos())
}

pub(crate) fn archived_teams(&self) -> impl Iterator<Item = &Team> {
Expand Down
9 changes: 8 additions & 1 deletion src/static_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ impl<'a> Generator<'a> {

fn generate_repos(&self) -> Result<(), Error> {
let mut repos: IndexMap<String, Vec<v1::Repo>> = IndexMap::new();
for r in self.data.repos() {
let repo_iter = self
.data
.repos()
.map(|repo| (repo, false))
.chain(self.data.archived_repos().map(|repo| (repo, true)));

for (r, archived) in repo_iter {
let branch_protections: Vec<_> = r
.branch_protections
.iter()
Expand Down Expand Up @@ -98,6 +104,7 @@ impl<'a> Generator<'a> {
})
.collect(),
branch_protections,
archived,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... Do we want to include archived repos in the repos endpoint or continue to only show current repos and have a dedicared archived_repos endpoint?

I think I would prefer the dedicated endpoint approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we don't even have an endpoint for archived teams. So maybe we should not even expose the archived repos as an endpoint?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not including the archived status may cause issues for sync-team, since it won't know the difference between a normal repo, an archived repo, and a repo that is not yet tracked.

(IMHO, I think a flag is sufficient since this only really matters for sync-team, and it is also similar to the way GitHub's API works.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhh, of course, we need to expose that information, that's the whole point of this PR, silly me :) The flag will definitely make both team and sync-team much simpler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It just seems simpler to me to keep that information completely separate (in 2 endpoints) rather than exposing this IMO very fundamental difference as a simple boolean. However, I don't want to block on this so I'll mark the PR as ready to merge if others disagree.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think visibility is as fundamental as archived state. An archived repo is effectively only there for historical purposes. It's not actively worked on or interacted with in any way. It is in essence only related to non-archived repos in that it used to be a non-archived repo; otherwise, there's essentially no interaction with an archived repo (e.g., no PRs, no new issues, no branches, no CI runs, etc.). Everything we want to control about a repo essentially does not apply to archived repos, because archived repos are not interacted with outside of rare cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That raises a good point. Should archived repos be interactable and have any properties set at all? E.g., what happens if you change a branch protection for an archived repo? Should that do something? (Will that unarchive the repo automatically?).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was definitely working under the assumption that archived repos are in a read-only state (more or less by definition), and in order to change them, you would need to unarchive them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is fine if sync-team makes changes to archived repos. It won't unarchive them, and the API seems to be fine with making changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: I don't think it matters much. There might be small things, for example the description. Someone may want to change that, and I think it would be kinda fussy to have to unarchive, change, and then re-archive. But I don't expect that to happen much, so I think it probably doesn't matter.

};

self.add(&format!("v1/repos/{}.json", r.name), &repo)?;
Expand Down
12 changes: 9 additions & 3 deletions src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ fn validate_inactive_members(data: &Data, errors: &mut Vec<String>) {
let all_members = data.people().map(|p| p.github()).collect::<HashSet<_>>();
// All the individual contributors to any Rust controlled repos
let all_ics = data
.repos()
.all_repos()
.flat_map(|r| r.access.individuals.keys())
.map(|n| n.as_str())
.collect::<HashSet<_>>();
Expand Down Expand Up @@ -729,11 +729,17 @@ fn validate_zulip_group_extra_people(data: &Data, errors: &mut Vec<String>) {
});
}

/// Ensure repos reference valid teams
/// Ensure repos reference valid teams and that they are unique
fn validate_repos(data: &Data, errors: &mut Vec<String>) {
let allowed_orgs = data.config().allowed_github_orgs();
let github_teams = data.github_teams();
wrapper(data.repos(), errors, |repo, _| {
let mut repo_map = HashSet::new();

wrapper(data.all_repos(), errors, |repo, _| {
if !repo_map.insert(format!("{}/{}", repo.org, repo.name)) {
bail!("The repo {}/{} is duplicated", repo.org, repo.name);
}

if !allowed_orgs.contains(&repo.org) {
bail!(
"The repo '{}' is in an invalid org '{}'",
Expand Down
28 changes: 27 additions & 1 deletion tests/static-api/_expected/v1/repos.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
{
"test-org": [
{
"org": "test-org",
"name": "archived_repo",
"description": "An archived repo!",
"bots": [],
"teams": [
{
"name": "foo",
"permission": "admin"
}
],
"members": [],
"branch_protections": [
{
"pattern": "master",
"ci_checks": [
"CI"
],
"dismiss_stale_review": false,
"required_approvals": 1,
"allowed_merge_teams": []
}
],
"archived": true
},
{
"org": "test-org",
"name": "some_repo",
Expand All @@ -24,7 +49,8 @@
"foo"
]
}
]
],
"archived": false
}
]
}
25 changes: 25 additions & 0 deletions tests/static-api/_expected/v1/repos/archived_repo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"org": "test-org",
"name": "archived_repo",
"description": "An archived repo!",
"bots": [],
"teams": [
{
"name": "foo",
"permission": "admin"
}
],
"members": [],
"branch_protections": [
{
"pattern": "master",
"ci_checks": [
"CI"
],
"dismiss_stale_review": false,
"required_approvals": 1,
"allowed_merge_teams": []
}
],
"archived": true
}
3 changes: 2 additions & 1 deletion tests/static-api/_expected/v1/repos/some_repo.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"foo"
]
}
]
],
"archived": false
}
11 changes: 11 additions & 0 deletions tests/static-api/repos/archive/test-org/archived_repo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
org = "test-org"
name = "archived_repo"
description = "An archived repo!"
bots = []

[access.teams]
foo = "admin"

[[branch-protections]]
pattern = "master"
ci-checks = ["CI"]