Skip to content

Commit

Permalink
Optionally output relative PR age (#19)
Browse files Browse the repository at this point in the history
* Conditionally output relative PR age

If the `SHOW_PR_AGE` environment variable is "true", include the PR's
relative age (from now) in the output.

The relative ages are:
- "less than a day ago"
- "1 day ago"
- "n days ago"

Given the action only runs once/day, I didn't think it was helpful to
include units less than a day.

* Address PR comments

Co-authored-by: Sam Cutler <[email protected]>
Co-authored-by: Ashleigh Carr <[email protected]>

* Avoid nested string formatting

---------

Co-authored-by: Sam Cutler <[email protected]>
Co-authored-by: Ashleigh Carr <[email protected]>
Co-authored-by: Mario Savarese <[email protected]>
  • Loading branch information
4 people authored Aug 15, 2023
1 parent 0d7f25a commit f7c799f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ log = "0.4.14"
simple_logger = "2.1.0"
anyhow = "1.0.53"
getset = "0.1.2"
chrono = "0.4.19"
chrono = { version = "0.4.19", features = ["serde"] }
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ inputs:
description: "Comma delimited list of label names to ignore"
required: true
default: "Stale"
show-pr-age:
description: "Include PR age in chat message output"
required: false
# Values: "true" or "false"
default: "false"
runs:
using: "docker"
image: "Dockerfile"
Expand All @@ -29,3 +34,4 @@ runs:
GITHUB_IGNORED_USERS: ${{ inputs.github-ignored-users }}
GITHUB_IGNORED_LABELS: ${{ inputs.github-ignored-labels }}
GOOGLE_WEBHOOK_URL: ${{ inputs.google-webhook-url }}
SHOW_PR_AGE: ${{ inputs.show-pr-age }}
2 changes: 2 additions & 0 deletions src/github.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{Context, Result};
use chrono::{DateTime, Utc};
use getset::Getters;
use serde::Deserialize;

Expand All @@ -14,6 +15,7 @@ pub struct GithubPullRequest {
number: i32,
head: GithubBranch,
labels: Vec<GithubLabel>,
created_at: DateTime<Utc>,
}

#[derive(Deserialize, Getters, Debug)]
Expand Down
48 changes: 34 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod github;
mod google;

use anyhow::{Context, Error};
use chrono::Datelike;
use chrono::{DateTime, Datelike, Utc};
use log::{info, Level};
use std::env;

Expand Down Expand Up @@ -116,6 +116,9 @@ async fn main() -> Result<(), Error> {
let ignored_users: Vec<&str> = ignored_users.split(",").collect();
let ignored_labels: String = env::var("GITHUB_IGNORED_LABELS").unwrap_or("".to_string());
let ignored_labels: Vec<&str> = ignored_labels.split(",").collect();
let show_pr_age: bool = env::var("SHOW_PR_AGE")
.map(|v| v == "true")
.unwrap_or(false);

let mut pull_requests_to_review: Vec<GithubPullRequest> = vec![];

Expand All @@ -132,7 +135,6 @@ async fn main() -> Result<(), Error> {
}

if !pull_requests_to_review.is_empty() {

let weekday = match chrono::offset::Local::now().date().weekday() {
chrono::Weekday::Mon => "Monday",
chrono::Weekday::Tue => "Tuesday",
Expand Down Expand Up @@ -161,20 +163,38 @@ async fn main() -> Result<(), Error> {
.await?;

for pull_request in pull_requests_to_review {
GoogleChatMessage::from(format!(
"<{}|{}#{}> - {}\n",
pull_request.html_url().replace("https://", ""),
pull_request.head().repo().name(),
pull_request.number(),
pull_request.title()
))
.send(&webhook_url, &thread_key)
.await?;
GoogleChatMessage::from(make_message(pull_request, show_pr_age))
.send(&webhook_url, &thread_key)
.await?;
}
}
else {
info!("No open PRs found, no action taken.");
} else {
info!("No open PRs found, no action taken.");
}

Ok(())
}

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()
);

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

format!("{}{}\n", message, age_output)
}

fn get_age(d1: DateTime<Utc>, d2: DateTime<Utc>) -> String {
match d1.signed_duration_since(d2).num_days() {
0 => "opened less than a day ago".to_string(),
1 => "opened 1 day ago".to_string(),
n => format!("opened {} days ago", n),
}
}

0 comments on commit f7c799f

Please sign in to comment.