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

feat: race-of-sloths opening to everybody #200

Merged
merged 3 commits into from
Nov 5, 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
2 changes: 1 addition & 1 deletion bot/src/events/actions/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl PullRequestFinalize {
.await?;
info.executed = true;

if !info.allowed_repo || info.paused {
if info.paused_repo || info.blocked_repo {
return Ok(EventResult::success(false));
}

Expand Down
2 changes: 1 addition & 1 deletion bot/src/events/actions/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl PullRequestMerge {
context.near.send_merge(pr).await?;
info.merged = true;

if !info.allowed_repo || info.paused {
if info.paused_repo || info.blocked_repo {
return Ok(EventResult::success(false));
}

Expand Down
2 changes: 1 addition & 1 deletion bot/src/events/actions/stale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl PullRequestStale {
..*check_info
};

if !check_info.allowed_repo || check_info.paused {
if check_info.paused_repo || check_info.blocked_repo {
return Ok(EventResult::success(false));
}

Expand Down
16 changes: 16 additions & 0 deletions bot/src/events/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ impl Context {
.await
}

pub async fn add_repo(&self, repo_info: &RepoInfo) -> anyhow::Result<()> {
let result = self.near.add_repo(&repo_info.owner, &repo_info.repo).await;
info!("Added repo {repo_info:?} to near");
let message = format!(
"New repo in the [{}](https://github.com/{}/{}/pull/{}) was {}",
repo_info.full_id,
repo_info.owner,
repo_info.repo,
repo_info.number,
result.as_ref().map(|_| "added").unwrap_or("failed")
);
self.telegram.send_to_telegram(&message, &Level::INFO);

result.map(|_| ())
}

pub async fn reply_with_text(
&self,
repo_info: &RepoInfo,
Expand Down
6 changes: 3 additions & 3 deletions bot/src/events/issue_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ impl Command {
first_reply: bool,
sender: &User,
) -> anyhow::Result<EventResult> {
if !check_info.allowed_repo {
if check_info.blocked_repo {
info!(
"Sloth called for a PR from not allowed org: {}. Skipping",
"Sloth called for a PR from blocked repo: {}. Skipping",
repo_info.full_id
);
if first_reply {
context
.reply_with_error(
repo_info,
None,
MsgCategory::ErrorOrgNotInAllowedListMessage,
MsgCategory::ErrorRepoIsBanned,
vec![("pr_author_username", sender.login.clone())],
)
.await?;
Expand Down
12 changes: 8 additions & 4 deletions bot/src/events/pr_commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,21 @@ impl Command {
sender: &User,
first_reply: bool,
) -> anyhow::Result<EventResult> {
if !check_info.allowed_repo {
if check_info.new_repo {
context.add_repo(&pr.repo_info).await?;
}

if check_info.blocked_repo {
info!(
"Sloth called for a PR from not allowed org: {}. Skipping",
"Sloth called for a PR from blocked repo: {}. Skipping",
pr.repo_info.full_id
);
if first_reply {
context
.reply_with_error(
&pr.repo_info,
None,
MsgCategory::ErrorOrgNotInAllowedListMessage,
MsgCategory::ErrorRepoIsBanned,
vec![("pr_author_username", pr.author.login.clone())],
)
.await?;
Expand All @@ -99,7 +103,7 @@ impl Command {
return Ok(EventResult::Skipped);
}

if check_info.paused && !matches!(self, Command::Unpause(_) | Command::Pause(_)) {
if check_info.paused_repo && !matches!(self, Command::Unpause(_) | Command::Pause(_)) {
info!(
"Sloth called for a PR from paused repo: {}. Skipping",
pr.repo_info.full_id
Expand Down
8 changes: 4 additions & 4 deletions bot/src/events/pr_commands/pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl BotPaused {
check_info: &mut PRInfo,
sender: &User,
) -> anyhow::Result<EventResult> {
if check_info.paused {
if check_info.paused_repo {
info!(
"Tried to pause a PR from paused repo: {}. Skipping",
pr.repo_info.full_id
Expand Down Expand Up @@ -58,7 +58,7 @@ impl BotPaused {
.near
.send_pause(&pr.repo_info.owner, &pr.repo_info.repo)
.await?;
check_info.paused = true;
check_info.paused_repo = true;
context
.reply(
&pr.repo_info,
Expand Down Expand Up @@ -102,12 +102,12 @@ impl BotUnpaused {
return Ok(EventResult::Skipped);
}

if info.paused {
if info.paused_repo {
context
.near
.send_unpause(&repo_info.owner, &repo_info.repo)
.await?;
info.paused = false;
info.paused_repo = false;
debug!("Unpaused PR {}", repo_info.full_id);
let msg = if self.from_issue {
MsgCategory::UnpauseIssueMessage
Expand Down
11 changes: 5 additions & 6 deletions bot/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub enum MsgCategory {
ErrorPausedMessage,
ErrorLateScoringMessage,
ErrorSelfScore,
ErrorOrgNotInAllowedListMessage,
ErrorRepoIsBanned,

FirstTimeContribution,
FirstWeekContribution,
Expand Down Expand Up @@ -257,9 +257,7 @@ impl MessageLoader {
MsgCategory::ErrorLateIncludeMessage => &self.error_late_include_messages,
MsgCategory::ErrorLateScoringMessage => &self.error_late_scoring_messages,
MsgCategory::ErrorSelfScore => &self.error_selfscore_messages,
MsgCategory::ErrorOrgNotInAllowedListMessage => {
&self.error_org_not_in_allowed_list_messages
}
MsgCategory::ErrorRepoIsBanned => &self.error_org_not_in_allowed_list_messages,
MsgCategory::ErrorPausePausedMessage => &self.error_pause_paused_messages,
MsgCategory::ErrorUnpauseUnpausedMessage => &self.error_unpause_unpaused_messages,
MsgCategory::ErrorPausedMessage => &self.error_paused_messages,
Expand Down Expand Up @@ -672,8 +670,9 @@ mod tests {

let mut pr_info = shared::PRInfo {
votes: vec![],
allowed_repo: true,
paused: false,
new_repo: false,
paused_repo: false,
blocked_repo: false,
merged: false,
executed: false,
excluded: false,
Expand Down
101 changes: 59 additions & 42 deletions contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use shared::{
TimePeriodString, UserId, UserPeriodDataV2, VersionedAccount, VersionedPR, VersionedStreak,
VersionedStreakUserData, VersionedUserPeriodData,
};
use types::{Repository, VersionedRepository};
use types::{Repository, RepositoryStatus, RepositoryV2, VersionedRepository};

pub mod events;
pub mod migrate;
Expand Down Expand Up @@ -155,7 +155,7 @@ impl Contract {
override_exclude: bool,
) {
self.assert_sloth();
self.assert_repo_allowed(&organization, &repo);
self.assert_repo_active(&organization, &repo);
let (user_id, _) = self.get_or_create_account(&user);

let pr_id = format!("{organization}/{repo}/{pr_number}");
Expand Down Expand Up @@ -245,23 +245,6 @@ impl Contract {
self.excluded_prs.insert(pr_id);
}

pub fn exclude_repo(&mut self, organization: String, repo: String) {
self.assert_sloth();

self.repos.remove(&(organization, repo));
}

pub fn bulk_remove_orgs(&mut self, allowed_orgs: Vec<AllowedRepos>) {
self.assert_sloth();

for allowed_org in allowed_orgs {
for repo in allowed_org.repos {
self.repos
.remove(&(allowed_org.organization.to_string(), repo.login));
}
}
}

pub fn bulk_include_orgs(&mut self, allowed_orgs: Vec<AllowedRepos>) {
self.assert_sloth();

Expand All @@ -282,45 +265,79 @@ impl Contract {
}
}

pub fn ban_repo(&mut self, organization: String, repo: String) {
self.assert_sloth();

self.repos.insert(
(organization, repo),
VersionedRepository::V2(RepositoryV2 {
status: RepositoryStatus::Blocked,
}),
);
}

pub fn unban_repo(&mut self, organization: String, repo: String) {
self.assert_sloth();

self.repos.insert(
(organization, repo),
VersionedRepository::V2(RepositoryV2 {
status: RepositoryStatus::Active,
}),
);
}

pub fn include_repo(&mut self, organization: String, repo: String) {
self.assert_sloth();

self.repos.insert(
(organization, repo),
VersionedRepository::V1(Repository { paused: false }),
VersionedRepository::V2(RepositoryV2 {
status: RepositoryStatus::Active,
}),
);
}

pub fn pause_repo(&mut self, organization: String, repo: String) {
self.assert_sloth();

let repo = self.repos.get_mut(&(organization, repo));
if repo.is_none() {
env::panic_str("Repository is not allowlisted")
}

let repo = repo.unwrap();
match repo {
VersionedRepository::V1(repo) => {
repo.paused = true;
let repository = self.repos.get(&(organization.clone(), repo.clone()));
let repository = repository.map(|r| {
let repository: RepositoryV2 = r.into();
repository
});
if let Some(repository) = repository {
if repository.status == RepositoryStatus::Blocked {
env::panic_str("Repository is blocked")
}
}
self.repos.insert(
(organization, repo),
VersionedRepository::V2(RepositoryV2 {
status: RepositoryStatus::Paused,
}),
);
}

pub fn unpause_repo(&mut self, organization: String, repo: String) {
self.assert_sloth();

let repo = self.repos.get_mut(&(organization, repo));
if repo.is_none() {
env::panic_str("Repository is not allowlisted")
}

let repo = repo.unwrap();
match repo {
VersionedRepository::V1(repo) => {
repo.paused = false;
let repository = self.repos.get(&(organization.clone(), repo.clone()));
let repository = repository.map(|r| {
let repository: RepositoryV2 = r.into();
repository
});
if let Some(repository) = repository {
if repository.status == RepositoryStatus::Blocked {
env::panic_str("Repository is blocked")
}
}
self.repos.insert(
(organization, repo),
VersionedRepository::V2(RepositoryV2 {
status: RepositoryStatus::Active,
}),
);
}

pub fn sloth_stale(&mut self, pr_id: String) {
Expand Down Expand Up @@ -575,14 +592,14 @@ impl Contract {
}
}

pub fn assert_repo_allowed(&self, organization: &str, repo: &str) {
pub fn assert_repo_active(&self, organization: &str, repo: &str) {
let repo: Option<_> = self.repos.get(&(organization.to_owned(), repo.to_owned()));
if let Some(repo) = repo {
if repo.is_paused() {
env::panic_str("Repo is paused")
if !repo.is_active() {
env::panic_str("Repo is not active")
}
} else {
env::panic_str("Repo is not allowlisted")
env::panic_str("Repo is not supported")
}
}
}
17 changes: 7 additions & 10 deletions contract/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ use super::*;
impl Contract {
#[init(ignore_state)]
#[private]
pub fn migrate() -> Self {
pub fn migrate(list: Vec<String>) -> Self {
let mut state: Self = env::state_read().unwrap();

let users = state.users.len();
for user_id in 0..users {
let period_data = state.period_data(user_id, &"102024".to_string());
if let Some(period_data) = period_data {
state.sloths_per_period.insert(
(user_id, "rosctober2024".to_string()),
VersionedUserPeriodData::V2(period_data),
);
}
for repo in list {
let mut split = repo.split('/');
let owner = split.next().unwrap();
let name = split.next().unwrap();

state.repos.remove(&(owner.to_string(), name.to_string()));
}

state
Expand Down
1 change: 1 addition & 0 deletions contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl ContractExt {
repos: vec![Repo {
login: "devbot".to_owned(),
paused: false,
blocked: false,
}],
}],
);
Expand Down
Loading
Loading