• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

suculent / thinx-device-api / #252646970

27 Oct 2017 03:17PM UTC coverage: 12.466% (+1.3%) from 11.197%
#252646970

push

suculent
added support for displaying/exporting extended SigFox attributes

37 of 1808 branches covered (2.05%)

Branch coverage included in aggregate %.

735 of 4385 relevant lines covered (16.76%)

0.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

12.22
/lib/thinx/sources.js
1
/** This THiNX-RTM API module is responsible for managing Sources. */
2

3
var Sources = (function() {
1✔
4

5
        var app_config = require("../../conf/config.json");
1✔
6
        if (typeof(process.env.CIRCLE_USERNAME) !== "undefined") {
1!
7
                console.log("ยป Configuring for Circle CI...");
×
8
                app_config = require("../../conf/config-test.json");
×
9
        }
10
        var db = app_config.database_uri;
1✔
11

12
        var prefix = "";
1✔
13
        try {
1✔
14
                var pfx_path = app_config.project_root + '/conf/.thx_prefix';
1✔
15
                if (fs.existsSync(pfx_path)) {
1!
16
                        prefix = fs.readFileSync(pfx_path) + "_";
×
17
                }
18
        } catch (e) {
19
                //console.log(e);
20
        }
21

22
        var userlib = require("nano")(db).use(prefix + "managed_users");
1✔
23
        var devicelib = require("nano")(db).use(prefix + "managed_devices");
1✔
24
        var watcher = require("./repository");
1✔
25

26
        var mkdirp = require("mkdirp");
1✔
27
        var sha256 = require("sha256");
1✔
28
        var exec = require("child_process");
1✔
29
        var fs = require("fs");
1✔
30
        var path = require("path");
1✔
31

32
        // public
33
        var _public = {
1✔
34

35
                /**
36
                 * List:
37
                 */
38

39
                list: function(owner, callback) {
40
                        userlib.get(owner, function(err, doc) {
×
41

42
                                if (err) {
×
43
                                        console.log(err);
×
44
                                        callback(false, err);
×
45
                                        return;
×
46
                                }
47

48
                                if (!doc) {
×
49
                                        console.log("Owner " + owner + " not found.");
×
50
                                        callback(false, "user_not_found");
×
51
                                        return;
×
52
                                }
53

54
                                callback(true, doc.repos);
×
55
                        });
56
                },
57

58
                /**
59
                 * Add
60
                 */
61

62
                add: function(owner, alias, url, branch, callback) {
63

64
                        var source = {
×
65
                                alias: alias,
66
                                url: url,
67
                                branch: branch
68
                        };
69

70
                        if (typeof(owner) === "undefined") {
×
71
                                callback(false, "owner_undefined");
×
72
                        }
73

74
                        // Prefetch and infer platform... (may take a while)
75

76
                        var OWNER_PATH = app_config.project_root + app_config.build_root + "/" +
×
77
                                owner;
78

79
                        console.log("owner path: " + OWNER_PATH);
×
80

81
                        var REPO_PATH = OWNER_PATH + "/" + alias;
×
82

83
                        console.log("repo path: " + REPO_PATH);
×
84

85
                        mkdirp(REPO_PATH, function(err) {
×
86
                                if (err) {
×
87
                                        console.log(err);
×
88
                                        callback(false, err);
×
89
                                        return;
×
90
                                }
91

92
                                try {
×
93
                                        var CLEANUP = "cd " + REPO_PATH + "; rm -rf *";
×
94
                                        exec.execSync(CLEANUP);
×
95
                                } catch (e) {
96
                                        //
97
                                }
98

99
                                var origin_branch = branch.replace("origin/", "");
×
100
                                var CMD = "cd " + REPO_PATH + "; git clone -b " + origin_branch +
×
101
                                        " " + url +
102
                                        "; cd *; echo { \"basename\":\"$(basename $(pwd))\", \"branch\":\"" +
103
                                        origin_branch + "\" } > basename.json; cat basename.json";
104

105
                                if (branch === null) {
×
106
                                        CMD = "cd " + REPO_PATH + "; git clone " + url +
×
107
                                                "; cd *; echo { \"basename\":\"$(basename $(pwd))\", \"branch\":\"" +
108
                                                branch + "\" } > basename.json; cat basename.json";
109
                                }
110

111
                                var result = null;
×
112

113
                                try {
×
114

115
                                        result = exec.execSync(CMD).toString().replace("\n", "");
×
116

117
                                        console.log("git clone result: " + result);
×
118

119
                                } catch (e) {
120
                                        console.log(
×
121
                                                "GIT Fetch Failed: " +
122
                                                e);
123
                                        callback(false, "Git fetch failed.");
×
124
                                        return;
×
125
                                }
126

127
                                var directories = fs.readdirSync(REPO_PATH).filter(
×
128
                                        file => fs.lstatSync(path.join(REPO_PATH, file)).isDirectory()
×
129
                                );
130

131
                                console.log("Directories: " + JSON.stringify(directories));
×
132

133
                                var REPO_INNER_PATH = REPO_PATH + "/" + directories[0];
×
134

135
                                // TODO: only if directories.count > 0
136

137
                                console.log("REPO_INNER_PATH: " + REPO_INNER_PATH);
×
138

139
                                /*
140
                                                                watcher.checkRepositoryChange(REPO_INNER_PATH, false, function(err,
141
                                                                        result) {
142

143
                                                                        if (err) {
144
                                                                                console.log(err);
145
                                                                                callback(false, err);
146
                                                                                return;
147
                                                                        }*/
148

149
                                source.platform = watcher.getPlatform(REPO_INNER_PATH);
×
150
                                source.initial_platform = source.platform; // should happen only on add
×
151

152
                                userlib.get(owner, function(err, doc) {
×
153

154
                                        if (err) {
×
155
                                                console.log(err);
×
156
                                                callback(false, err);
×
157
                                                return;
×
158
                                        }
159

160
                                        if (!doc) {
×
161
                                                console.log("Owner " + owner + " not found.");
×
162
                                                callback(false, "user_not_found");
×
163
                                                return;
×
164
                                        }
165

166
                                        // Update user with new repos
167
                                        userlib.destroy(doc._id, doc._rev, function(err) {
×
168
                                                delete doc._rev;
×
169
                                                var sid = sha256(JSON.stringify(source));
×
170
                                                doc.repos[sid] = source;
×
171
                                                userlib.insert(doc, doc._id, function(err, body, header) {
×
172
                                                        if (err) {
×
173
                                                                console.log("/api/user/source ERROR:" + err);
×
174
                                                                callback(false, "source_not_added");
×
175
                                                                return;
×
176
                                                        } else {
177
                                                                callback(true, {
×
178
                                                                        success: true,
179
                                                                        source_id: sid
180
                                                                });
181
                                                        }
182
                                                });
183
                                        }); // userlib
184
                                });
185
                        });
186
                        //});
187
                },
188

189
                /**
190
                 * Revoke Source for owner
191
                 * @param {string} owner - owner._id
192
                 * @param {string} sources - array of source_ids to be revoked
193
                 * @param {function} callback(success, message) - operation result callback
194
                 */
195

196
                remove: function(owner, removed_sources, callback) {
197

198
                        userlib.get(owner, function(err, doc) {
×
199

200
                                if (err) {
×
201
                                        console.log(err);
×
202
                                        return;
×
203
                                }
204

205
                                if (!doc) {
×
206
                                        console.log("Owner " + owner + " not found.");
×
207
                                        callback(false, "user_not_found");
×
208
                                        return;
×
209
                                }
210

211
                                var sources = doc.repos;
×
212
                                var source_ids = Object.keys(sources);
×
213
                                var really_removed_repos = [];
×
214
                                for (var source_id in removed_sources) {
×
215
                                        var removed_source_id = removed_sources[source_id];
×
216
                                        var sources_source_id = sources[removed_source_id];
×
217
                                        if ((typeof(sources_source_id) !== "undefined") && (sources_source_id !== null)) {
×
218
                                                delete sources[removed_source_id];
×
219
                                                really_removed_repos.push(source_ids[removed_source_id]);
×
220
                                        }
221
                                }
222

223
                                // Update user with new repos
224
                                userlib.destroy(doc._id, doc._rev, function(err) {
×
225
                                        doc.repos = sources;
×
226
                                        delete doc._rev;
×
227
                                        userlib.insert(doc, doc._id, function(err, body, header) {
×
228
                                                if (err) {
×
229
                                                        console.log("/api/user/source ERROR:" + err);
×
230
                                                        callback(false, "source_not_removed");
×
231
                                                        return;
×
232
                                                } else {
233
                                                        callback(true, {
×
234
                                                                success: true,
235
                                                                source_ids: really_removed_repos
236
                                                        });
237
                                                }
238
                                        });
239
                                }); // userlib
240

241
                                devicelib.view("devicelib", "devices_by_owner", {
×
242
                                                key: owner,
243
                                                include_docs: true
244
                                        },
245

246
                                        function(err, body) {
247

248
                                                if (err) {
×
249
                                                        console.log(err);
×
250
                                                        // no devices to be detached
251
                                                }
252

253
                                                if (body.rows.length === 0) {
×
254
                                                        console.log("no-devices to be detached; body: " +
×
255
                                                                JSON.stringify(body));
256
                                                        // no devices to be detached
257
                                                }
258

259
                                                function upsert(device) {
260
                                                        devicelib.destroy(device._id, device._rev, function(err) {
×
261
                                                                delete device._rev;
×
262
                                                                devicelib.insert(device,
×
263
                                                                        device._rev,
264
                                                                        function(err) {
265
                                                                                console.log(err);
×
266
                                                                        }
267
                                                                );
268
                                                        });
269
                                                }
270

271
                                                for (var rindex in body.rows) {
×
272

273
                                                        var device;
274
                                                        if (typeof(body.rows[rindex]) === "undefined") continue;
×
275
                                                        if (body.rows[rindex].value !== null) {
×
276
                                                                device = body.rows[rindex].value;
×
277
                                                        }
278

279
                                                        if ((typeof(device) === "undefined")) continue;
×
280
                                                        if (device === null) continue;
×
281
                                                        if (device.source === null) continue;
×
282

283
                                                        for (var sindex in removed_sources) {
×
284
                                                                var removed_source_id = removed_sources[sindex];
×
285
                                                                if (device.source == removed_source_id) {
×
286
                                                                        console.log(
×
287
                                                                                "repo_revoke alias equal: Will destroy/insert device: " +
288
                                                                                device.source + " to " + source_id
289
                                                                        );
290
                                                                        device.source = null;
×
291
                                                                        upsert(device);
×
292
                                                                }
293
                                                        }
294

295
                                                }
296
                                        });
297
                        });
298
                }
299

300
        };
301

302
        return _public;
1✔
303

304
})();
305

306
exports.list = Sources.list;
1✔
307
exports.add = Sources.add;
1✔
308
exports.remove = Sources.remove;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc