Skip to content

Commit

Permalink
Do not log if repo is private (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
marsavar authored Jul 4, 2024
1 parent f3c14d3 commit a53095a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 145 deletions.
64 changes: 5 additions & 59 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ serde_json = "1.0.79"
log = "0.4.14"
simple_logger = "2.1.0"
anyhow = "1.0.53"
getset = "0.1.2"
chrono = { version = "0.4.19", features = ["serde"] }
url = "2.5.0"
56 changes: 25 additions & 31 deletions src/github.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,47 @@
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use getset::Getters;
use serde::Deserialize;

#[derive(Deserialize, Getters, Debug)]
#[getset(get = "pub", get_copy = "pub")]
#[derive(Deserialize, Debug)]
pub struct GithubPullRequest {
id: usize,
url: String,
html_url: String,
title: String,
user: GithubUser,
draft: bool,
number: usize,
head: GithubBranch,
labels: Vec<GithubLabel>,
created_at: DateTime<Utc>,
pub id: usize,
pub url: String,
pub html_url: String,
pub title: String,
pub user: GithubUser,
pub draft: bool,
pub number: usize,
pub head: GithubBranch,
pub labels: Vec<GithubLabel>,
pub created_at: DateTime<Utc>,
}

#[derive(Deserialize, Getters, Debug)]
#[getset(get = "pub", get_copy = "pub")]
#[derive(Deserialize, Debug)]
pub struct GithubBranch {
repo: GithubRepository,
pub repo: GithubRepository,
}

#[derive(Deserialize, Getters, Debug)]
#[getset(get = "pub", get_copy = "pub")]
#[derive(Deserialize, Debug)]
pub struct GithubRepository {
name: String,
pub name: String,
pub visibility: String,
}

#[derive(Deserialize, Getters, Debug)]
#[getset(get = "pub", get_copy = "pub")]
#[derive(Deserialize, Debug)]
pub struct GithubLabel {
name: String,
pub name: String,
}

#[derive(Deserialize, Getters, Debug)]
#[getset(get = "pub")]
#[derive(Deserialize, Debug)]
pub struct GithubUser {
id: usize,
login: String,
pub id: usize,
pub login: String,
}

#[derive(Deserialize, Getters, Debug)]
#[getset(get = "pub")]
#[derive(Deserialize, Debug)]
pub struct GithubReview {
id: usize,
state: String,
pub id: usize,
pub state: String,
}

impl GithubPullRequest {
Expand All @@ -74,7 +68,7 @@ impl GithubPullRequest {
}

pub async fn reviews(&self, token: &str) -> Result<Vec<GithubReview>> {
let api_url = format!("{}/reviews", self.url());
let api_url = format!("{}/reviews", self.url);

let response = reqwest::Client::new()
.get(&api_url)
Expand Down
113 changes: 59 additions & 54 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate getset;

mod github;
mod google;

Expand All @@ -11,6 +9,8 @@ use std::env;
use github::GithubPullRequest;
use google::GoogleChatMessage;

const PUBLIC_REPO: &str = "public";

async fn scan_repository(
repository_name: &str,
github_token: &str,
Expand All @@ -26,56 +26,59 @@ async fn scan_repository(
info!("Found {} PR's", pull_requests.len());

for pull_request in pull_requests {
info!(
"Processing PR {}({})",
pull_request.id(),
pull_request.title()
);
let is_public = pull_request.head.repo.visibility == PUBLIC_REPO;

if *pull_request.draft() {
info!(
"Ignoring PR {}({}) as it is a draft",
pull_request.id(),
pull_request.title()
);
info!("Processing PR {}({})", pull_request.id, pull_request.title);

if pull_request.draft {
if is_public {
info!(
"Ignoring PR {}({}) as it is a draft",
pull_request.id, pull_request.title
);
}
continue;
}

if ignored_users.contains(&pull_request.user().id().to_string().as_str()) {
info!(
"Ignoring PR {}({}) as it was raised by an ignored user {}({})",
pull_request.id(),
pull_request.title(),
pull_request.user().id(),
pull_request.user().login()
);
if ignored_users.contains(&pull_request.user.id.to_string().as_str()) {
if is_public {
info!(
"Ignoring PR {}({}) as it was raised by an ignored user {}({})",
pull_request.id,
pull_request.title,
pull_request.user.id,
pull_request.user.login
);
}
continue;
}

if let Some(announced_users) = announced_users {
if !announced_users.contains(pull_request.user().id()) {
info!("Users to announce: {:?}", announced_users);
info!(
if !announced_users.contains(&pull_request.user.id) {
if is_public {
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()
pull_request.id,
pull_request.title,
pull_request.user.id,
pull_request.user.login
);
}
continue;
}
}

let mut has_ignore_label = false;

for label in pull_request.labels() {
if ignored_labels.contains(&label.name().as_str()) {
info!(
"Ignoring PR {}({}) as it has an ignored label ({})",
pull_request.id(),
pull_request.title(),
label.name()
);
for label in &pull_request.labels {
if ignored_labels.contains(&label.name.as_str()) {
if is_public {
info!(
"Ignoring PR {}({}) as it has an ignored label ({})",
pull_request.id, pull_request.title, label.name
);
}
has_ignore_label = true;
}
}
Expand All @@ -86,24 +89,26 @@ async fn scan_repository(

let pull_request_reviews = pull_request.reviews(github_token).await?;

info!(
"Found {} reviews for PR {}({})",
pull_request_reviews.len(),
pull_request.id(),
pull_request.title()
);
if is_public {
info!(
"Found {} reviews for PR {}({})",
pull_request_reviews.len(),
pull_request.id,
pull_request.title
);
}

let mut has_approved_reviews = false;

for pull_request_review in pull_request_reviews {
info!(
"Processing review {} for PR {}({})",
pull_request_review.id(),
pull_request.id(),
pull_request.title()
);
if is_public {
info!(
"Processing review {} for PR {}({})",
pull_request_review.id, pull_request.id, pull_request.title
);
}

if pull_request_review.state() == "APPROVED" {
if pull_request_review.state == "APPROVED" {
has_approved_reviews = true;
}
}
Expand Down Expand Up @@ -200,14 +205,14 @@ async fn main() -> Result<(), Error> {
fn make_message(pull_request: GithubPullRequest, show_pr_age: bool) -> String {
let message = format!(
"<{}|{}#{}> - {}",
pull_request.html_url().replace("https://", ""),
pull_request.head().repo().name(),
pull_request.number(),
pull_request.title()
pull_request.html_url.replace("https://", ""),
pull_request.head.repo.name,
pull_request.number,
pull_request.title
);

let age_output = match show_pr_age {
true => format!(" - (_{}_)", get_age(Utc::now(), *pull_request.created_at())),
true => format!(" - (_{}_)", get_age(Utc::now(), pull_request.created_at)),
false => "".to_string(),
};

Expand Down

0 comments on commit a53095a

Please sign in to comment.