Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit af51aaa

Browse files
committed
Improve import of user, title, labels fields (needed for GitHub issues)
1 parent a8e50a2 commit af51aaa

File tree

5 files changed

+134
-7
lines changed

5 files changed

+134
-7
lines changed

tasklite-core/package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ tests:
101101
source-dirs: test
102102
dependencies:
103103
- aeson
104+
- bytestring
104105
- hourglass
105106
- hspec >= 2.11 && < 3.0
106107
- iso8601-duration

tasklite-core/source/ImportExport.hs

+28-6
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,22 @@ import Protolude (
4040
(<$>),
4141
(<&>),
4242
(=<<),
43+
(>>=),
4344
(||),
4445
)
4546
import Protolude qualified as P
4647

4748
import Config (Config (dataDir, dbName))
4849
import Control.Arrow ((>>>))
50+
import Data.Aeson (Value)
4951
import Data.Aeson as Aeson (
5052
FromJSON (parseJSON),
5153
ToJSON,
5254
Value (Array, Object, String),
5355
eitherDecode,
5456
encode,
5557
withObject,
58+
(.!=),
5659
(.:),
5760
(.:?),
5861
)
@@ -243,9 +246,10 @@ instance FromJSON ImportTask where
243246
)
244247
createdUtc = fromMaybe zeroTime parsedCreatedUtc
245248

249+
o_title <- o .:? "title"
246250
o_body <- o .:? "body"
247251
description <- o .:? "description"
248-
let body = fromMaybe "" (o_body <|> description)
252+
let body = fromMaybe "" (o_title <|> o_body <|> description)
249253

250254
o_priority_adjustment <- o .:? "priority_adjustment"
251255
urgency <- o .:? "urgency"
@@ -281,11 +285,23 @@ instance FromJSON ImportTask where
281285
else Nothing
282286

283287
o_tags <- o .:? "tags"
284-
o_labels <- o .:? "labels"
288+
(o_labelsMb :: Maybe [Value]) <- o .:? "labels"
289+
let labels =
290+
o_labelsMb
291+
<&> ( ( \o_labels ->
292+
o_labels <&> \case
293+
String txt -> Just txt
294+
Object obj ->
295+
P.flip parseMaybe obj (\o_ -> o_ .:? "name" .!= "")
296+
_ -> Nothing
297+
)
298+
>>> P.catMaybes
299+
)
300+
285301
project <- o .:? "project"
286302
let
287-
projects = fmap (: []) project
288-
tags = fromMaybe [] (o_tags <> o_labels <> projects)
303+
projects = project <&> (: [])
304+
tags = fromMaybe [] (o_tags <> labels <> projects)
289305

290306
due <- o .:? "due"
291307
o_due_utc <- o .:? "due_utc"
@@ -423,10 +439,16 @@ instance FromJSON ImportTask where
423439
)
424440
Nothing -> theNotes
425441

426-
o_user <- o .:? "user"
442+
(o_userMb :: Maybe Value) <- o .:? "user"
427443
o_author <- o .:? "author"
428444
let
429-
userMaybe = o_user <|> o_author
445+
o_userNormMb =
446+
o_userMb >>= \case
447+
String txt -> Just txt
448+
Object obj ->
449+
P.flip parseMaybe obj (\o_ -> o_ .:? "login" .!= "")
450+
_ -> Nothing
451+
userMaybe = o_userNormMb <|> o_author
430452
user = fromMaybe "" userMaybe
431453

432454
o_metadata <- o .:? "metadata"

tasklite-core/tasklite-core.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ test-suite tasklite-test
144144
MissingH >=1.4 && <1.7
145145
, aeson
146146
, base >=4.18 && <5
147+
, bytestring
147148
, hourglass
148149
, hspec >=2.11 && <3.0
149150
, iso8601-duration

tasklite-core/test/ImportExportSpec.hs

+34-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Protolude (
1414
import Protolude qualified as P
1515

1616
import Data.Aeson (decode, eitherDecode, eitherDecodeStrictText)
17+
import Data.ByteString.Lazy qualified as BSL
1718
import Data.Hourglass (timePrint, toFormat)
1819
import Data.Text (unpack)
1920
import Data.Text qualified as T
@@ -30,7 +31,8 @@ import Test.Hspec (
3031
import Config (defaultConfig)
3132
import FullTask (FullTask, emptyFullTask)
3233
import FullTask qualified
33-
import ImportExport (insertImportTask)
34+
import ImportExport (ImportTask (ImportTask, notes, tags, task), insertImportTask)
35+
import Task (Task (body, ulid, user, modified_utc), emptyTask)
3436
import TaskToNote (TaskToNote (TaskToNote))
3537
import TaskToNote qualified
3638
import TestUtils (withMemoryDb)
@@ -176,3 +178,34 @@ spec = do
176178
(taskToNote.ulid & T.take 10)
177179
`shouldBe` (expectedTaskToNote.ulid & T.take 10)
178180
_ -> P.die "Found more than one note"
181+
182+
it "imports a GitHub issue" $ do
183+
gitHubIssue <- BSL.readFile "test/fixtures/github-issue.json"
184+
withMemoryDb conf $ \memConn -> do
185+
let
186+
expectedImpTask =
187+
ImportTask
188+
{ task =
189+
emptyTask
190+
{ Task.ulid = "01hrz2qz7g0000f4wgrw89nzvm"
191+
, Task.body = "Support getting the note body from stdin"
192+
, Task.user = "ad-si"
193+
, Task.modified_utc = "2024-03-14 18:14:14"
194+
}
195+
, notes = []
196+
, tags = []
197+
}
198+
199+
case eitherDecode gitHubIssue of
200+
Left error -> P.die $ "Error decoding JSON: " <> show error
201+
Right importTaskRecord -> do
202+
_ <- insertImportTask memConn importTaskRecord
203+
taskList :: [Task] <- query_ memConn "SELECT * FROM tasks"
204+
case taskList of
205+
[task] -> do
206+
task.ulid `shouldBe` expectedImpTask.task.ulid
207+
task.body `shouldBe` expectedImpTask.task.body
208+
task.user `shouldBe` expectedImpTask.task.user
209+
task.modified_utc `shouldBe` expectedImpTask.task.modified_utc
210+
211+
_ -> P.die "Found more than one note"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"url": "https://api.github.com/repos/ad-si/TaskLite/issues/72",
3+
"repository_url": "https://api.github.com/repos/ad-si/TaskLite",
4+
"labels_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/labels{/name}",
5+
"comments_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/comments",
6+
"events_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/events",
7+
"html_url": "https://github.com/ad-si/TaskLite/issues/72",
8+
"id": 2186980639,
9+
"node_id": "I_kwDOC26QQc6CWq0f",
10+
"number": 72,
11+
"title": "Support getting the note body from stdin",
12+
"user": {
13+
"login": "ad-si",
14+
"id": 36796532,
15+
"node_id": "MDQ6VXNlcjM2Nzk2NTMy",
16+
"avatar_url": "https://avatars.githubusercontent.com/u/36796532?v=4",
17+
"gravatar_id": "",
18+
"url": "https://api.github.com/users/ad-si",
19+
"html_url": "https://github.com/ad-si",
20+
"followers_url": "https://api.github.com/users/ad-si/followers",
21+
"following_url": "https://api.github.com/users/ad-si/following{/other_user}",
22+
"gists_url": "https://api.github.com/users/ad-si/gists{/gist_id}",
23+
"starred_url": "https://api.github.com/users/ad-si/starred{/owner}{/repo}",
24+
"subscriptions_url": "https://api.github.com/users/ad-si/subscriptions",
25+
"organizations_url": "https://api.github.com/users/ad-si/orgs",
26+
"repos_url": "https://api.github.com/users/ad-si/repos",
27+
"events_url": "https://api.github.com/users/ad-si/events{/privacy}",
28+
"received_events_url": "https://api.github.com/users/ad-si/received_events",
29+
"type": "User",
30+
"site_admin": false
31+
},
32+
"labels": [
33+
{
34+
"id": 1404940648,
35+
"node_id": "MDU6TGFiZWwxNDA0OTQwNjQ4",
36+
"url": "https://api.github.com/repos/ad-si/TaskLite/labels/enhancement",
37+
"name": "enhancement",
38+
"color": "a2eeef",
39+
"default": true,
40+
"description": "New feature or request"
41+
}
42+
],
43+
"state": "open",
44+
"locked": false,
45+
"assignee": null,
46+
"assignees": [],
47+
"milestone": null,
48+
"comments": 0,
49+
"created_at": "2024-03-14T18:14:14Z",
50+
"updated_at": "2024-03-14T18:14:14Z",
51+
"closed_at": null,
52+
"author_association": "OWNER",
53+
"active_lock_reason": null,
54+
"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",
55+
"reactions": {
56+
"url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/reactions",
57+
"total_count": 0,
58+
"+1": 0,
59+
"-1": 0,
60+
"laugh": 0,
61+
"hooray": 0,
62+
"confused": 0,
63+
"heart": 0,
64+
"rocket": 0,
65+
"eyes": 0
66+
},
67+
"timeline_url": "https://api.github.com/repos/ad-si/TaskLite/issues/72/timeline",
68+
"performed_via_github_app": null,
69+
"state_reason": null
70+
}

0 commit comments

Comments
 (0)