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

Make timezone autocomplete search case insensitive #15

Merged
merged 13 commits into from
Sep 29, 2023
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
commit-message:
prefix: "[actions] "
schedule:
interval: "weekly"
groups:
actions: {}
9 changes: 4 additions & 5 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ on:
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.reBumfupository as <account>/<repo>
IMAGE_NAME: kekonn/fercord

jobs:
Expand All @@ -34,10 +33,10 @@ jobs:

# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@4b4e9c3e2d4531116a6f8ba8e71fc6e2cb6e6c8c
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
Expand All @@ -57,7 +56,7 @@ jobs:
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@c4ee3adeed93b1fa6a762f209fb01608c1a22f1e
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
Expand All @@ -68,7 +67,7 @@ jobs:
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v4.0.0
uses: docker/build-push-action@v4
with:
context: .
file: Docker/Dockerfile
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ on:
push:
tags:
- '**[0-9]+.[0-9]+.[0-9]+*'
pull_request:
branches:
- "main"

jobs:
# Run 'cargo dist plan' to determine what tasks we need to do
Expand Down
17 changes: 9 additions & 8 deletions .github/workflows/rust-clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
# More details at https://github.com/rust-lang/rust-clippy
# and https://rust-lang.github.io/rust-clippy/

name: rust-clippy analyze
name: Run clippy on PR

on:
pull_request:
types: ["ready_for_review", "auto_merge_enabled", "edited"]
# The branches below must be a subset of the branches above
branches: [ "main" ]

Expand All @@ -24,18 +25,18 @@ jobs:
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Install Rust toolchain
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af #@v1
uses: dtolnay/rust-toolchain@stable
with:
profile: minimal
toolchain: stable
components: clippy
override: true

- uses: swatinem/rust-cache@v2

- uses: taiki-e/install-action@cargo-binstall
- name: Install required cargo
run: cargo install clippy-sarif sarif-fmt
run: cargo binstall -y clippy-sarif sarif-fmt

- name: Run rust-clippy
run:
Expand All @@ -44,7 +45,7 @@ jobs:
continue-on-error: true

- name: Upload analysis results to GitHub
uses: github/codeql-action/upload-sarif@v1
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: rust-clippy-results.sarif
wait-for-processing: true
38 changes: 32 additions & 6 deletions src/discord/commands.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use anyhow::{ Result, anyhow };
use anyhow::{anyhow, Result};
use chrono::{DateTime, TimeZone, Utc};
use chrono_english::{ Dialect, parse_date_string };
use tracing::{ trace_span, event, Level, field, debug, warn };
use chrono_english::{Dialect, parse_date_string};
use chrono_tz::{Tz, TZ_VARIANTS};
use poise::serenity_prelude as serenity;
use tracing::{debug, event, field, Level, trace_span, warn};

use crate::{
discord::Context,
storage::{
model::{ guild_timezone::GuildTimezone, reminder::Reminder },
kv::KVClient,
db::Repository,
kv::KVClient,
model::{guild_timezone::GuildTimezone, reminder::Reminder},
},
};

Expand Down Expand Up @@ -261,12 +261,38 @@ async fn autocomplete_timezone<'a>(
}

fn filter_timezones(pattern: &str) -> impl Iterator<Item = String> + '_ {
let lower_pattern = pattern.to_lowercase();
TZ_VARIANTS.iter()
.filter_map(move |tz|
tz
.name()
.find(pattern)
.to_lowercase()
.find(&lower_pattern)
.map(|_| tz.name())
)
.map(|tz| tz.to_string())
}

#[cfg(test)]
mod tests {
use anyhow::Result;

use super::*;

#[test]
fn can_find_timezone() -> Result<()> {
let mixed_timezones = vec!["eUrOpe/Brussel", "europe/brussels", "EUROPE/BRUSSELS"];
let expected = String::from("Europe/Brussels");
for search in mixed_timezones {
let found: Vec<String> = filter_timezones(search).collect();

if let Some(found_tz) = found.first() {
assert_eq!(expected, *found_tz, "Found timezone does not match expected {}", expected);
} else {
return Err(anyhow!("Could not find timezone matching {}", expected));
}
}

Ok(())
}
}
5 changes: 3 additions & 2 deletions src/discord/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::discord::commands::*;
use chrono::Utc;
use tracing_test::traced_test;

use crate::discord::commands::*;

/// `"5 minutes"`
const EXPECTED: &str = "5 minutes";

Expand All @@ -13,7 +14,7 @@ fn parsed_moment_is_future() {
let now = Utc::now();

// Act
let parsed = parse_human_time(input, Utc);
let parsed = parse_human_time(input, Utc, None);

// Assert
debug_assert!(parsed.is_ok(), "We did not get a successful parse");
Expand Down