-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.babel.js
200 lines (181 loc) · 5.17 KB
/
gulpfile.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import gulp from 'gulp'
import uuid from 'uuid'
import level from 'level'
import LevelPromise from 'level-promise'
import forkdb from 'forkdb'
import fdbp from 'forkdb-promise'
import fetch from 'github-fetch'
import Storage from 'o-storage-forkdb'
import Dataset from 'o-utils-dataset'
import SimpleRDF from 'simplerdf'
import JsonldParser from 'rdf-parser-jsonld'
const config = require('./config.json')
const parser = new JsonldParser()
const cache = forkdb(level(config.db.cache.level), { dir: config.db.cache.fork })
const mappings = level(config.db.mappings.level)
LevelPromise(mappings)
const dataset = new Dataset(new Storage(config.db.lod))
const prefix = 'https://api.github.com/'
const options = { token: config.api.token }
const context = {
'@vocab': 'http://schema.org/',
'id': '@id',
'type': '@type',
'ldp': 'http://www.w3.org/ns/ldp#',
'hydra': 'http://www.w3.org/ns/hydra/core#',
'flow': 'http://www.w3.org/2005/01/wf/flow#',
'foaf': 'http://xmlns.com/foaf/0.1/',
'resource': 'ldp:membershipResource',
'icr': 'ldp:insertedContentRelation',
'rel': 'ldp:hasMemberRelation',
'rev': 'ldp:isMemberOfRelation',
'primaryTopic': 'foaf:primaryTopic',
'contains': 'ldp:contains'
}
function transformRepo (data, uri) {
let doc = {
'@context': context,
'@graph': []
}
let resource = {
id: uri,
type: [ 'ldp:Resource', 'hydra:Resource' ]
}
doc['@graph'].push(resource)
let creation = {
id: resource.id + '#creation',
type: [ 'CreativeWork', 'SoftwareSourceCode' ],
name: data.name,
description: data.description
}
doc['@graph'].push(creation)
resource.primaryTopic = creation.id
return parser.parse(doc)
}
function transformIssue (data, uri) {
let doc = {
'@context': context,
'@graph': []
}
let resource = {
id: uri,
type: [ 'ldp:Resource', 'hydra:Resource' ]
}
doc['@graph'].push(resource)
let task = {
id: resource.id + '#task',
type: 'flow:Task',
name: data.title,
description: data.body
}
doc['@graph'].push(task)
resource.primaryTopic = task.id
return parser.parse(doc)
}
function fetchRepoWithIssues (repo) {
let repoUrl = prefix + 'repos/' + repo.source
let issuesUrl = prefix + 'repos/' + repo.source + '/issues'
return fetch(repoUrl, options).then((data) => {
return fdbp.put(cache, repoUrl, data)
}).then(() => {
return fetch(issuesUrl, options)
}).then((data) => {
fdbp.put(cache, issuesUrl, data)
})
}
gulp.task('fetch', (done) => {
Promise.all(config.repos.map(fetchRepoWithIssues))
.then(() => {
console.log('fetched ' + config.repos.length + ' repos')
done()
}).catch((err) => {
console.log(err)
done()
})
})
function ghRepoUrl (repo) {
return prefix + 'repos/' + repo.source
}
function ghIssuesUrl (repo) {
return prefix + 'repos/' + repo.source + '/issues'
}
function gh2lod (gh, uriSpace, transformFunction) {
return mappings.get(gh.id)
.then((resourceUri) => {
// TODO implement update
return Promise.resolve(resourceUri)
}, (err) => {
if (err.type === 'NotFoundError') {
let resourceUri = uriSpace + uuid.v4()
return transformFunction(gh, resourceUri)
.then((graph) => {
return Promise.all([
dataset.createResource(resourceUri, graph),
mappings.put(gh.id, resourceUri)
])
}).then(() => {
return Promise.resolve(resourceUri)
})
} else {
return Promise.reject(err)
}
})
}
/**
* 1. get github json from cache
* 2. update or create creation
* 3. get or create tasks container
* 4. update or create issues
* 5. update tasks container (contains)
*/
function processRepo (repo) {
let creationUrl
let tasksUri
let link = {
'http://www.w3.org/ns/ldp#hasMemberRelation': 'http://www.w3.org/2005/01/wf/flow#task'
}
return fdbp.get(cache, ghRepoUrl(repo))
.then((ghRepoStr) => {
let json = JSON.parse(ghRepoStr)
return gh2lod(json, repo.uriSpace, transformRepo)
}).then((url) => {
creationUrl = url
return dataset.getLinkedContainerUri(creationUrl, link)
}).then((containerUri) => {
if (containerUri) {
return Promise.resolve(containerUri)
} else {
containerUri = repo.uriSpace + uuid.v4()
return dataset.createLinkedContainer(containerUri, creationUrl, link)
}
}).then((containerUri) => {
tasksUri = containerUri
return processIssues(repo)
}).then((memberUris) => {
let tasks = SimpleRDF(context, tasksUri)
tasks.contains = memberUris
return dataset.appendToResource(tasksUri, tasks.graph())
})
}
function processIssues (repo) {
return fdbp.get(cache, ghIssuesUrl(repo))
.then((string) => {
let ghIssues = JSON.parse(string)
return Promise.all(ghIssues.map((ghIssue) => {
return gh2lod(ghIssue, repo.uriSpace, transformIssue)
}))
})
}
gulp.task('process', (done) => {
Promise.all(config.repos.map(processRepo))
.then(() => {
console.log('processed ' + config.repos.length + ' repos')
done()
}).catch((err) => {
console.log(err)
done()
})
})
gulp.task('publish', () => {
console.log('TODO')
})