Skip to content

Commit

Permalink
fix pr label application. fix assign label autocomplete.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Jun 30, 2023
1 parent 57c93a5 commit f1eb025
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 18 deletions.
40 changes: 36 additions & 4 deletions src/BashCompletion.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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 @{_} _ _ _ = []
Expand Down
56 changes: 42 additions & 14 deletions src/Commands.idr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <label> ...`.
export
Expand All @@ -54,24 +93,13 @@ label : Config => Git => Octokit =>
-> Promise ()
label @{config} labels =
do (_, openPr) <- identifyOrCreatePR !currentBranch
let finalLabels = unslugify config.repoLabels <$> labels
let finalLabels = unslugifyLabel config.repoLabels <$> labels
allLabels <- addLabels openPr finalLabels
renderIO $ vsep
[ "Added" <++> putLabels finalLabels <+> " to PR."
, pretty "All labels for PR of \{openPr.headRef}:" <++> putLabels allLabels <+> "."
]
where
||| 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.
unslugify : (configLabels : List String) -> (slugifiedLabel : String) -> String
unslugify configLabels slugifiedLabel =
case find (== slugifiedLabel) configLabels of
Just label => label
Nothing => BashCompletion.unslugify slugifiedLabel

putLabel : String -> Doc AnsiStyle
putLabel = enclose "\"" "\"" . annotate (color Green) . pretty

Expand All @@ -88,7 +116,7 @@ pr : Config => Git => Octokit =>
-> (labelArgs : List String)
-> Promise ()
pr {isDraft} labelSlugs =
if all ("#" `isPrefixOf`) labelSlugs
if all isHashPrefix labelSlugs
then do (actionTaken, pr) <- identifyOrCreatePR {isDraft} !currentBranch
case actionTaken of
Identified => putStrLn pr.webURI
Expand Down Expand Up @@ -117,7 +145,7 @@ assign args {dry} = do
partitionedArgs : (List String, List String, List String)
partitionedArgs =
let (userArgs, otherArgs) = partition (isPrefixOf "+") args
(labelArgs, teams) = partition (isPrefixOf "#") otherArgs
(labelArgs, teams) = partition isHashPrefix otherArgs
(users, labels) = mapHom (map $ drop 1) (userArgs, labelArgs)
in (users, teams, labels)

Expand Down

0 comments on commit f1eb025

Please sign in to comment.