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 an option to only announce PRs from specific users #26

Merged
merged 8 commits into from
Jun 6, 2024

Conversation

aracho1
Copy link
Contributor

@aracho1 aracho1 commented May 31, 2024

What does this change?

Co-authored-by: @lindseydew

We would like to introduce an option to make the PR announcer only announce PRs from specific users e.g. scala steward.

How to test

We have tested this on our MSS dev chat, which worked as expected.

How can we measure success?

We can now make the PR announcer alert PRs from a select list of users.

Have we considered potential risks?

We have set this new flag to be optional so existing announcers should not be affected.

@aracho1 aracho1 changed the title Add included users list Add an option to only announce PRs from specific users May 31, 2024
src/main.rs Outdated
@@ -51,6 +52,18 @@ async fn scan_repository(
continue;
}

if !announced_users.contains(&pull_request.user().id().to_string().as_str()) {
Copy link
Member

@AshCorr AshCorr May 31, 2024

Choose a reason for hiding this comment

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

I think announced_users will default to an empty vector? Would this mean that the default would be to ignore everyone?

Wondering if we need to make announced_users a Optional<Vec<&str>> instead and check if it exists here?

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 makes sense, thank you for the suggestion!

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively check if the Vec is empty and skip this check if it is, I prefer the explicit Optional however.

Copy link
Contributor

@marsavar marsavar May 31, 2024

Choose a reason for hiding this comment

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

I agree with Ash here, with a preference for the option type.

Also, since you know user id's are of type i32, you could parse them here already so you avoid the awkward conversion &pull_request.user().id().to_string().as_str().

I.e.
You could do something like

    let announced_users: Option<Vec<i32>> = env::var("GITHUB_ANNOUNCED_USERS")
        .ok()
        .map(|s| s.split(',').map(|id| id.parse().unwrap()).collect());

And then change the signature of scan_repository so that it accepts announced_users: Option<&[i32]>

so the final check would look something like

        if let Some(announced_users) = announced_users {
            if !announced_users.contains(pull_request.user().id()) {
                info!("Users to announce: {:?}", announced_users);
                info!(
                "Ignoring PR {}({}) as it was raised by a user not included in the announced users list {}({})",
                pull_request.id(),
                pull_request.title(),
                pull_request.user().id(),
                pull_request.user().login()
            );
                continue;
            }
        }

@aracho1 aracho1 requested review from AshCorr and marsavar June 4, 2024 10:13
@@ -32,6 +36,7 @@ runs:
GITHUB_REPOSITORIES: ${{ inputs.github-repositories }}
GITHUB_TOKEN: ${{ inputs.github-token }}
GITHUB_IGNORED_USERS: ${{ inputs.github-ignored-users }}
GITHUB_ANNOUNCED_USERS: ${{ inputs.github-announced-users }}
Copy link
Member

Choose a reason for hiding this comment

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

Not quite sure how Github handles this if inputs.github-announced-users isn't defined, it might interpet it as an empty string:

GITHUB_ANNOUNCED_USERS: ""

In which case our code to check if the environment variable is present or not won't correctly detect that the GITHUB_ANNOUNCED_USERS is undefined. Sorry to suggest Option but now I'm thinking just checking the length of our announced users array might be better than checking if the variable is present or not!

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's a good spot, thanks @AshCorr. How about we do something like this to handle the empty string?

let announced_users: Option<Vec<i32>> = env::var("GITHUB_ANNOUNCED_USERS")
        .ok()
        .and_then(|s| {
            if s.is_empty() {
                None
            } else {
                Some(s.split(',').map(|id| id.parse().unwrap()).collect())
            }
        });

Copy link
Member

Choose a reason for hiding this comment

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

Aha! Yea that would work!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just pushed the change, happy to +1 @AshCorr ?

@aracho1 aracho1 requested a review from AshCorr June 4, 2024 13:54
Copy link
Member

@AshCorr AshCorr left a comment

Choose a reason for hiding this comment

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

Fantastic 👍

@aracho1 aracho1 merged commit fc5bcb6 into main Jun 6, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants