From 05af8d43a791c8d3d0934018a5ea5d92d5b81998 Mon Sep 17 00:00:00 2001 From: Benoit Verhaeghe Date: Sun, 15 Sep 2024 11:31:25 +0200 Subject: [PATCH] add simple import of users --- .../GHModelImporterTest.class.st | 4 +- .../GHApi.class.st | 6 +++ .../GHModelImporter.class.st | 53 ++++++++++++++++++- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/GitHubHealth-Model-Importer-Tests/GHModelImporterTest.class.st b/src/GitHubHealth-Model-Importer-Tests/GHModelImporterTest.class.st index 8623193..3e11684 100644 --- a/src/GitHubHealth-Model-Importer-Tests/GHModelImporterTest.class.st +++ b/src/GitHubHealth-Model-Importer-Tests/GHModelImporterTest.class.st @@ -20,7 +20,7 @@ GHModelImporterTest >> setUp [ { #category : 'tests' } GHModelImporterTest >> testParseCommitsResult [ - + "we remove the id of author entries to not trigger the call to import user" | commits | commits := importer parseCommitsResult: '[ { @@ -56,7 +56,6 @@ GHModelImporterTest >> testParseCommitsResult [ "comments_url": "https://api.github.com/repos/moosetechnology/Moose/commits/f5ac58f4afe4632b0a26d1e968439c78962da289/comments", "author": { "login": "ClotildeToullec", - "id": 39184695, "node_id": "MDQ6VXNlcjM5MTg0Njk1", "avatar_url": "https://avatars.githubusercontent.com/u/39184695?v=4", "gravatar_id": "", @@ -135,7 +134,6 @@ GHModelImporterTest >> testParseCommitsResult [ "comments_url": "https://api.github.com/repos/moosetechnology/Moose/commits/eb31d04f01254d0caf7c7a5b03546e3f6a5c3d58/comments", "author": { "login": "ClotildeToullec", - "id": 39184695, "node_id": "MDQ6VXNlcjM5MTg0Njk1", "avatar_url": "https://avatars.githubusercontent.com/u/39184695?v=4", "gravatar_id": "", diff --git a/src/GitHubHealth-Model-Importer/GHApi.class.st b/src/GitHubHealth-Model-Importer/GHApi.class.st index 7f35630..b18d9b1 100644 --- a/src/GitHubHealth-Model-Importer/GHApi.class.st +++ b/src/GitHubHealth-Model-Importer/GHApi.class.st @@ -103,3 +103,9 @@ GHApi >> reposOfOrganization: anOrganizationName [ ^ self client get: self baseAPIUrl , '/orgs/' , anOrganizationName, '/repos' ] + +{ #category : 'api - users' } +GHApi >> user: aUserID [ + + ^ self client get: self baseAPIUrl , '/user/' , aUserID asString +] diff --git a/src/GitHubHealth-Model-Importer/GHModelImporter.class.st b/src/GitHubHealth-Model-Importer/GHModelImporter.class.st index ddfbf1a..5445b19 100644 --- a/src/GitHubHealth-Model-Importer/GHModelImporter.class.st +++ b/src/GitHubHealth-Model-Importer/GHModelImporter.class.st @@ -171,6 +171,21 @@ GHModelImporter >> importRepositoriesOfGroup: groupResult [ ^ groupResult ] +{ #category : 'api' } +GHModelImporter >> importUser: userID [ + + | result userResult | + (glhModel allWithType: GLHUser) + detect: [ :user | user id = userID ] + ifFound: [ :user | ^ user ]. + ('Import user: ' , userID printString) recordInfo. + result := self api user: userID. + userResult := self parseUserResult: result. + ^ glhModel + add: userResult + unless: [ :current :new | current id = new id ] +] + { #category : 'initialization' } GHModelImporter >> initialize [ @@ -225,7 +240,25 @@ GHModelImporter >> parseCommitsResult: result [ getter: [ :object | #ignore ] setter: [ :glhCommit :value | glhCommit message: (value at: #message) ]. - (mapping mapInstVar: #parent_ids) valueSchema: #ArrayOfIds ]. + + mapping + mapProperty: #author + getter: [ :object | #ignore ] + setter: [ :glhCommit :value | + glhCommit author_name: (value at: #login). + value + at: #id + ifPresent: [ :authorId | + glhCommit commitCreator: (self importUser: authorId) ] ]. + + mapping + mapProperty: #committer + getter: [ :object | #ignore ] + setter: [ :glhCommit :value | + glhCommit committer_name: (value at: #login) ]. + + (mapping mapInstVar: #parent_ids to: #parents) valueSchema: + #ArrayOfIds ]. reader for: DateAndTime customDo: [ :mapping | mapping decoder: [ :string | DateAndTime fromString: string ] ]. @@ -290,6 +323,24 @@ GHModelImporter >> parsePipelinesResult: pipelineOverview [ ^ reader nextAs: GHAPIPipelineOverview ] +{ #category : 'parsing' } +GHModelImporter >> parseUserResult: result [ + + | reader | + reader := NeoJSONReader on: result readStream. + reader for: GLHUser do: [ :mapping | + mapping mapInstVar: #id to: #id. + mapping mapInstVar: #public_email to: #email. + mapping mapInstVar: #username to: #login. + mapping mapInstVar: #bio to: #bio. + mapping mapInstVar: #organization to: #company. + mapping mapInstVar: #followers to: #followers. + mapping mapInstVar: #following to: #following. + mapping mapInstVar: #web_url to: #html_url. + mapping mapInstVar: #avatar_url to: #avatar_url ]. + ^ reader nextAs: GLHUser +] + { #category : 'api' } GHModelImporter >> privateToken [ ^ self api privateToken