Skip to content

Commit

Permalink
Improve import of user, title, labels fields (needed for GitHub issues)
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed May 14, 2024
1 parent a8e50a2 commit af51aaa
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 7 deletions.
1 change: 1 addition & 0 deletions tasklite-core/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ tests:
source-dirs: test
dependencies:
- aeson
- bytestring
- hourglass
- hspec >= 2.11 && < 3.0
- iso8601-duration
Expand Down
34 changes: 28 additions & 6 deletions tasklite-core/source/ImportExport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,22 @@ import Protolude (
(<$>),
(<&>),
(=<<),
(>>=),
(||),
)
import Protolude qualified as P

import Config (Config (dataDir, dbName))
import Control.Arrow ((>>>))
import Data.Aeson (Value)
import Data.Aeson as Aeson (
FromJSON (parseJSON),
ToJSON,
Value (Array, Object, String),
eitherDecode,
encode,
withObject,
(.!=),
(.:),
(.:?),
)
Expand Down Expand Up @@ -243,9 +246,10 @@ instance FromJSON ImportTask where
)
createdUtc = fromMaybe zeroTime parsedCreatedUtc

o_title <- o .:? "title"
o_body <- o .:? "body"
description <- o .:? "description"
let body = fromMaybe "" (o_body <|> description)
let body = fromMaybe "" (o_title <|> o_body <|> description)

o_priority_adjustment <- o .:? "priority_adjustment"
urgency <- o .:? "urgency"
Expand Down Expand Up @@ -281,11 +285,23 @@ instance FromJSON ImportTask where
else Nothing

o_tags <- o .:? "tags"
o_labels <- o .:? "labels"
(o_labelsMb :: Maybe [Value]) <- o .:? "labels"
let labels =
o_labelsMb
<&> ( ( \o_labels ->
o_labels <&> \case
String txt -> Just txt
Object obj ->
P.flip parseMaybe obj (\o_ -> o_ .:? "name" .!= "")
_ -> Nothing
)
>>> P.catMaybes
)

project <- o .:? "project"
let
projects = fmap (: []) project
tags = fromMaybe [] (o_tags <> o_labels <> projects)
projects = project <&> (: [])
tags = fromMaybe [] (o_tags <> labels <> projects)

due <- o .:? "due"
o_due_utc <- o .:? "due_utc"
Expand Down Expand Up @@ -423,10 +439,16 @@ instance FromJSON ImportTask where
)
Nothing -> theNotes

o_user <- o .:? "user"
(o_userMb :: Maybe Value) <- o .:? "user"
o_author <- o .:? "author"
let
userMaybe = o_user <|> o_author
o_userNormMb =
o_userMb >>= \case
String txt -> Just txt
Object obj ->
P.flip parseMaybe obj (\o_ -> o_ .:? "login" .!= "")
_ -> Nothing
userMaybe = o_userNormMb <|> o_author
user = fromMaybe "" userMaybe

o_metadata <- o .:? "metadata"
Expand Down
1 change: 1 addition & 0 deletions tasklite-core/tasklite-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ test-suite tasklite-test
MissingH >=1.4 && <1.7
, aeson
, base >=4.18 && <5
, bytestring
, hourglass
, hspec >=2.11 && <3.0
, iso8601-duration
Expand Down
35 changes: 34 additions & 1 deletion tasklite-core/test/ImportExportSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Protolude (
import Protolude qualified as P

import Data.Aeson (decode, eitherDecode, eitherDecodeStrictText)
import Data.ByteString.Lazy qualified as BSL
import Data.Hourglass (timePrint, toFormat)
import Data.Text (unpack)
import Data.Text qualified as T
Expand All @@ -30,7 +31,8 @@ import Test.Hspec (
import Config (defaultConfig)
import FullTask (FullTask, emptyFullTask)
import FullTask qualified
import ImportExport (insertImportTask)
import ImportExport (ImportTask (ImportTask, notes, tags, task), insertImportTask)
import Task (Task (body, ulid, user, modified_utc), emptyTask)
import TaskToNote (TaskToNote (TaskToNote))
import TaskToNote qualified
import TestUtils (withMemoryDb)
Expand Down Expand Up @@ -176,3 +178,34 @@ spec = do
(taskToNote.ulid & T.take 10)
`shouldBe` (expectedTaskToNote.ulid & T.take 10)
_ -> P.die "Found more than one note"

it "imports a GitHub issue" $ do
gitHubIssue <- BSL.readFile "test/fixtures/github-issue.json"
withMemoryDb conf $ \memConn -> do
let
expectedImpTask =
ImportTask
{ task =
emptyTask
{ Task.ulid = "01hrz2qz7g0000f4wgrw89nzvm"
, Task.body = "Support getting the note body from stdin"
, Task.user = "ad-si"
, Task.modified_utc = "2024-03-14 18:14:14"
}
, notes = []
, tags = []
}

case eitherDecode gitHubIssue of
Left error -> P.die $ "Error decoding JSON: " <> show error
Right importTaskRecord -> do
_ <- insertImportTask memConn importTaskRecord
taskList :: [Task] <- query_ memConn "SELECT * FROM tasks"
case taskList of
[task] -> do
task.ulid `shouldBe` expectedImpTask.task.ulid
task.body `shouldBe` expectedImpTask.task.body
task.user `shouldBe` expectedImpTask.task.user
task.modified_utc `shouldBe` expectedImpTask.task.modified_utc

_ -> P.die "Found more than one note"
70 changes: 70 additions & 0 deletions tasklite-core/test/fixtures/github-issue.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"url": "https://api.github.com/repos/ad-si/TaskLite/issues/72",
"repository_url": "https://api.github.com/repos/ad-si/TaskLite",
"labels_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/labels{/name}",
"comments_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/comments",
"events_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/events",
"html_url": "https://github.com/ad-si/TaskLite/issues/72",
"id": 2186980639,
"node_id": "I_kwDOC26QQc6CWq0f",
"number": 72,
"title": "Support getting the note body from stdin",
"user": {
"login": "ad-si",
"id": 36796532,
"node_id": "MDQ6VXNlcjM2Nzk2NTMy",
"avatar_url": "https://avatars.githubusercontent.com/u/36796532?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/ad-si",
"html_url": "https://github.com/ad-si",
"followers_url": "https://api.github.com/users/ad-si/followers",
"following_url": "https://api.github.com/users/ad-si/following{/other_user}",
"gists_url": "https://api.github.com/users/ad-si/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ad-si/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ad-si/subscriptions",
"organizations_url": "https://api.github.com/users/ad-si/orgs",
"repos_url": "https://api.github.com/users/ad-si/repos",
"events_url": "https://api.github.com/users/ad-si/events{/privacy}",
"received_events_url": "https://api.github.com/users/ad-si/received_events",
"type": "User",
"site_admin": false
},
"labels": [
{
"id": 1404940648,
"node_id": "MDU6TGFiZWwxNDA0OTQwNjQ4",
"url": "https://api.github.com/repos/ad-si/TaskLite/labels/enhancement",
"name": "enhancement",
"color": "a2eeef",
"default": true,
"description": "New feature or request"
}
],
"state": "open",
"locked": false,
"assignee": null,
"assignees": [],
"milestone": null,
"comments": 0,
"created_at": "2024-03-14T18:14:14Z",
"updated_at": "2024-03-14T18:14:14Z",
"closed_at": null,
"author_association": "OWNER",
"active_lock_reason": null,
"body": "Maybe something like `cat note.txt | tasklite note 3bc4` should work.\r\n\r\nTaskLite could check if `3bc4` is a valid ULID, and if stdin contains anything.\r\nOtherwise use a flag like `cat note.txt | tasklite note --stdin 3bc4` or similar.\r\n\r\n---\r\nTracking issue for @robwhitaker's comment https://github.com/ad-si/TaskLite/issues/2#issuecomment-586727359",
"reactions": {
"url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/reactions",
"total_count": 0,
"+1": 0,
"-1": 0,
"laugh": 0,
"hooray": 0,
"confused": 0,
"heart": 0,
"rocket": 0,
"eyes": 0
},
"timeline_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/timeline",
"performed_via_github_app": null,
"state_reason": null
}

0 comments on commit af51aaa

Please sign in to comment.