Skip to content
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
15 changes: 15 additions & 0 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ pub enum Selection<'a, T: ?Sized> {
Except(&'a T),
}

#[derive(Debug, serde::Deserialize)]
pub struct CollaboratorPermission {
pub permission: String,
Copy link
Member

Choose a reason for hiding this comment

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

We could use an enum here to make it a bit nicer to deal with.

Copy link
Member Author

Choose a reason for hiding this comment

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

I also wanted use an enum but GitHub's own documentation/schema says it's just a string, but on the other hand they say it can only be admin, write, read, and none. Unsure what to do.

Copy link
Member

Choose a reason for hiding this comment

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

Let's leave it. If we use the collaborator permissions on more places, then we can refactor it.

}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IssueRepository {
pub organization: String,
Expand Down Expand Up @@ -594,6 +599,16 @@ impl IssueRepository {
}
}
}

pub(crate) async fn collaborator_permission(
&self,
client: &GithubClient,
username: &str,
) -> anyhow::Result<CollaboratorPermission> {
let url = format!("{}/collaborators/{username}/permission", self.url(client));
let permission = client.json(client.get(&url)).await?;
Ok(permission)
}
}

impl Issue {
Expand Down
16 changes: 15 additions & 1 deletion src/handlers/review_submitted.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use anyhow::Context as _;

use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label, PullRequestReviewState};
use crate::{config::ReviewSubmittedConfig, github::Event, handlers::Context};

Expand All @@ -21,7 +23,19 @@ pub(crate) async fn handle(
return Ok(());
}

if event.issue.assignees.contains(&event.comment.user) {
// Let's switch the review labels if the user who issued the changes requested:
// - is one of the assignees
// - or has write/admin permission on the repository
if event.issue.assignees.contains(&event.comment.user) || {
let perm = event
.issue
.repository()
.collaborator_permission(&ctx.github, &event.comment.user.login)
.await
.context("failed to get the user repository permission")?;

perm.permission == "write" || perm.permission == "admin"
} {
// Remove review labels
event
.issue
Expand Down
Loading