forked from hackforla/brigade-project-index-statusboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
135 lines (124 loc) · 5.17 KB
/
api.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
import { Octokit } from '@octokit/rest';
import JSZip from 'jszip';
import _ from 'lodash';
import toml from 'toml';
import axios from 'axios';
export function getLastUpdate(){
// TODO Promise of last commit
}
function slugify(n){
return n.toLowerCase().replace(/[^a-zA-Z0-9\-]+/g,"-")
}
export function getProjectIndex(org_type){
console.log("getting project index")
// We will make calls out to Github for the latest index information
const octokit = Octokit({
auth: process.env.GITHUB_TOKEN
})
// return promise the resolves once we have unzipped and merged
// all the projects / organizations
let promise = new Promise( (resolve, reject) => {
octokit.repos.getArchiveLink({
owner:'codeforamerica',
repo:'brigade-project-index',
archive_format:'zipball',
ref:'index/v1'
}).then( (response) => {
// Download the latest brigade index in a zip archive
const index_zip = new JSZip();
index_zip.loadAsync( response.data ).then( archive => {
// Iterate through all the .toml files
const orgs = [];
const projects = [];
const promises = [];
// Parse them into organizations / projects
index_zip.folder('').forEach( (path) => {
const parts = path.split('/');
const t = parts[1];
// Tag projects with org name / project name per path
if( t == "projects" && parts.length == 4 ){
const f = index_zip.file(path);
if(f){
promises.push( f.async('string').then( data => {
const p = toml.parse(data);
p.name = parts[parts.length - 1].replace('.toml','');
p.brigade = parts[parts.length - 2];
projects.push(p)
}) )
}
// tag with org name per path
}else if(t == "organizations" && parts.length == 3 ){
const f = index_zip.file(path);
if(f){
promises.push( index_zip.file(path).async('string').then( data => {
const o = toml.parse(data);
o.projects = [];
o.name = parts[parts.length - 1].replace('.toml','');
o.slug = slugify(o.name)
orgs.push(o)
}))
}
}
});
// After all async loads are finished, we combine the projects into the orgs
// and keep our original promise
Promise.all(promises).then( result => {
console.log(`Loaded ${orgs.length} orgs and ${projects.length} projects.. joining them`);
const orgs_by_name = _.keyBy(orgs,'name');
projects.forEach( proj => {
if( orgs_by_name[proj.brigade]){
proj.brigade_slug = slugify(proj.brigade);
orgs_by_name[proj.brigade].projects.push(proj);
}
});
if(org_type != null){
resolve(orgs.filter( o => {
var valid = true;
org_type.forEach( t => {
if(!o.tags.includes(t)){
valid = false;
}
})
return valid;
}));
}else{
resolve(orgs);
}
});
}).catch( error => {
console.log(error.message);
})
}).catch( error => {
console.log(error);
})
});
return promise;
}
export function getDiscourseTagList(){
console.log("getting tag list")
const URL = "https://discourse.codeforamerica.org/tags.json";
let promise = new Promise( (resolve, reject) => {
axios.get(URL).then( response => {
resolve(response.data);
}).catch( error => {
reject(error);
});
})
return promise;
}
// index project subcategories
// https://discourse.codeforamerica.org/categories.json -> projects.subcategory_ids => 30 (e.g. courbot)
// https://discourse.codeforamerica.org/c/30/show.json => slug
// translates into https://discourse.codeforamerica.org/c/projects/courtbot/30
// found via https://meta.discourse.org/t/how-do-i-get-subcategory-by-id-using-discourse-api/137790
// and digging in the json
export default function test(){ console.log("test") }
/*
getProjectIndex().then( orgs => {
console.log(`${orgs.length} promised orgs have been returned!`);
fs.writeFile( 'out.json', JSON.stringify(orgs), (err) => {
if (err) throw err;
console.log("out.json written");
});
});
*/