Unverified Commit 2cd82cb8 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Remove individual devs from crate ownership in crates.io (#1396)

parent 5881feaf
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -8,11 +8,13 @@
mod add_owner;
mod get_owners;
mod publish;
mod remove_owner;
mod yank;

pub use add_owner::AddOwner;
pub use get_owners::GetOwners;
pub use publish::Publish;
pub use remove_owner::RemoveOwner;
pub use yank::Yank;

use anyhow::{Context, Result};
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use anyhow::Result;
use smithy_rs_tool_common::shell::{handle_failure, ShellOperation};
use std::process::Command;

pub struct RemoveOwner {
    program: &'static str,
    package_name: String,
    owner: String,
}

impl RemoveOwner {
    pub fn new(package_name: impl Into<String>, owner: impl Into<String>) -> RemoveOwner {
        RemoveOwner {
            program: "cargo",
            package_name: package_name.into(),
            owner: owner.into(),
        }
    }
}

impl ShellOperation for RemoveOwner {
    type Output = ();

    fn run(&self) -> Result<()> {
        let mut command = Command::new(self.program);
        command
            .arg("owner")
            .arg("--remove")
            .arg(&self.owner)
            .arg(&self.package_name);
        let output = command.output()?;
        handle_failure("remove owner", &output)?;
        Ok(())
    }
}

#[cfg(all(test, not(target_os = "windows")))]
mod tests {
    use super::*;
    use smithy_rs_tool_common::shell::ShellOperation;

    #[tokio::test]
    async fn remove_owner_success() {
        RemoveOwner {
            program: "./fake_cargo/cargo_success",
            package_name: "aws-sdk-s3".into(),
            owner: "incorrect_owner".into(),
        }
        .spawn()
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn remove_owner_failed() {
        let result = RemoveOwner {
            program: "./fake_cargo/cargo_fails",
            package_name: "aws-sdk-s3".into(),
            owner: "incorrect_owner".into(),
        }
        .spawn()
        .await;

        assert!(result.is_err(), "expected error, got {:?}", result);
        assert_eq!(
            "Failed to remove owner:\n\
            Status: 1\n\
            Stdout: some stdout failure message\n\n\
            Stderr: some stderr failure message\n\n",
            format!("{}", result.err().unwrap())
        );
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -177,6 +177,17 @@ async fn correct_owner(package: &Package) -> Result<()> {
        Duration::from_secs(5),
        || async {
            let owners = cargo::GetOwners::new(&package.handle.name).spawn().await?;
            let incorrect_owners = owners.iter().filter(|&owner| owner != CRATE_OWNER);
            for incorrect_owner in incorrect_owners {
                cargo::RemoveOwner::new(&package.handle.name, incorrect_owner)
                    .spawn()
                    .await
                    .context("remove incorrect owner")?;
                info!(
                    "Removed incorrect owner `{}` from crate `{}`",
                    incorrect_owner, package.handle
                );
            }
            if !owners.iter().any(|owner| owner == CRATE_OWNER) {
                cargo::AddOwner::new(&package.handle.name, CRATE_OWNER)
                    .spawn()