From f1eb02573a7a4a9ad15d8c1735bf3561bad4d3c6 Mon Sep 17 00:00:00 2001 From: Matt Polzin Date: Fri, 30 Jun 2023 11:08:14 -0500 Subject: [PATCH] fix pr label application. fix assign label autocomplete. --- src/BashCompletion.idr | 40 +++++++++++++++++++++++++++--- src/Commands.idr | 56 +++++++++++++++++++++++++++++++----------- 2 files changed, 78 insertions(+), 18 deletions(-) diff --git a/src/BashCompletion.idr b/src/BashCompletion.idr index 1b043fa..5d95a59 100644 --- a/src/BashCompletion.idr +++ b/src/BashCompletion.idr @@ -29,10 +29,42 @@ slugify : String -> String slugify = pack . replaceOn ' ' '◌' . unpack ||| Take a slugified phrase and undo the transformation to get the original phrase back. -export +public export unslugify : String -> String unslugify = pack . replaceOn '◌' ' ' . unpack +hashify : String -> String +hashify = strCons '#' + +public export +unhashify : String -> String +unhashify str = case strM str of + StrNil => "" + (StrCons '#' str') => str' + (StrCons '\\' str') => + case strM str' of + StrNil => str + (StrCons '#' str'') => str'' + (StrCons _ _) => str + (StrCons _ _) => str + +namespace TestUnhashify + test1 : unhashify "" = "" + test1 = Refl + + test2 : unhashify "\\" = "\\" + test2 = Refl + + test3 : unhashify "#hello" = "hello" + test3 = Refl + + test4 : unhashify "\\hello" = "\\hello" + test4 = Refl + + test5 : unhashify "\\#hello" = "hello" + test5 = Refl + +export isHashPrefix : String -> Bool isHashPrefix str = ("#" `isPrefixOf` str) || ("\\#" `isPrefixOf` str) @@ -112,7 +144,7 @@ opts @{config} "graph" partialTeamName previous = -- then pr (handled partially above, but when labels are specified, handled here) opts @{config} "pr" partialArg _ = if isHashPrefix partialArg - then (strCons '#') . slugify <$> config.repoLabels + then hashify . slugify <$> config.repoLabels else [] -- finally, assign auto-completes with @@ -122,7 +154,7 @@ opts @{config} "assign" "--" _ = config.teamSlugs opts @{config} "assign" partialArg _ = if partialArg `isPrefixOf` "--dry" then ["--dry"] - else filter (isPrefixOf partialArg) slugsOrLoginsOrLabels + else slugsOrLoginsOrLabels where -- If the word being typed is prefixed with '+' return user logins -- but otherwise return team slugs. @@ -131,7 +163,7 @@ opts @{config} "assign" partialArg _ = if "+" `isPrefixOf` partialArg then (strCons '+') <$> config.orgMembers else if isHashPrefix partialArg - then (strCons '#') . slugify <$> config.repoLabels + then hashify . slugify <$> config.repoLabels else config.teamSlugs opts @{_} _ _ _ = [] diff --git a/src/Commands.idr b/src/Commands.idr index 3414728..2968f37 100644 --- a/src/Commands.idr +++ b/src/Commands.idr @@ -46,6 +46,45 @@ reflect : Config => Octokit => Promise () reflect = reflectOnSelf +||| In order to support tab completion of multi-word labels, spaces have been turned into +||| another character to "slugify" the labels. Still, it is possible the user has entered +||| a label that literally contains the character used during slugification, so to +||| unslugify, we first see if a label appears in the configured list of labels. If it does +||| then we use it exactly but if it doesn't then we unslugify it before using it. +unslugifyLabel : (configLabels : List String) -> (slugifiedLabel : String) -> String +unslugifyLabel configLabels slugifiedLabel = + case find (== slugifiedLabel) configLabels of + Just label => label + Nothing => BashCompletion.unslugify $ BashCompletion.unhashify slugifiedLabel + +namespace TestUnslugifyLabel + test1 : unslugifyLabel ["hello", "world"] "hello" = "hello" + test1 = Refl + + test2 : unslugifyLabel ["hello", "world"] "#world" = "world" + test2 = Refl + + test3 : unslugifyLabel ["hello", "world"] "\\#hello" = "hello" + test3 = Refl + + test4 : unslugifyLabel ["hello world"] "hello world" = "hello world" + test4 = Refl + + test5 : unslugifyLabel ["hello world"] "#hello world" = "hello world" + test5 = Refl + + test6 : unslugifyLabel ["hello world"] "\\#hello world" = "hello world" + test6 = Refl + + test7 : unslugifyLabel ["hello world"] "hello◌world" = "hello world" + test7 = Refl + + test8 : unslugifyLabel ["hello world"] "#hello◌world" = "hello world" + test8 = Refl + + test9 : unslugifyLabel ["hello world"] "\\#hello◌world" = "hello world" + test9 = Refl + ||| Apply the given labels to the current PR when the user executes ||| `harmony label