This repository was archived by the owner on Nov 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.js
More file actions
125 lines (111 loc) · 2.63 KB
/
api.js
File metadata and controls
125 lines (111 loc) · 2.63 KB
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
const request = require('request');
const http = require('http');
const url = require('url');
const PORT = process.env.PORT || 3000;
let inventory = {
version: "2.0.0",
agency: "CFPB",
measurementType: {
method: "projects"
},
releases: []
};
codeJson = (params, cb) => {
let after = '';
if (params.after) {
after = ` after:"${params.after}"`
}
const query = `query($username:String!) {
organization(login:$username) {
name
repositories(first:100 ${after}) {
nodes {
name
description
license
url
owner {
login
}
repositoryTopics(first:3) {
edges {
node {
topic {
name
}
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}`;
const variables = `{
"username": "${params.org}"
}`;
const options = {
method: 'POST',
url: 'https://api.github.com/graphql',
headers: {
'User-Agent': 'request',
'Content-Type': 'application/json',
'Authorization': `bearer ${process.env.GITHUB_TOKEN}`
},
body: {
query: query,
variables: variables
},
json: true
};
request(options, (err, res, body) => {
console.log('\npageInfo: ', body.data.organization.repositories.pageInfo);
if (body.data.organization.repositories.pageInfo.hasNextPage) {
console.log(
`There are more results. Open http://localhost:${PORT}?org=${params.org}\
&after=${body.data.organization.repositories.pageInfo.endCursor} \
to see the next page.`
);
}
if (err) return console.error(error);
if (!body.data.organization) return cb("Invalid GitHub organization!");
const inventory = handleResponse(body);
cb(null, inventory);
});
};
handleResponse = (body) => {
let releases = body.data.organization.repositories.nodes;
inventory.releases = releases.map((release) => {
let tags = release.repositoryTopics.edges.map((edge) => {
return edge.node.topic.name;
});
tags.push(inventory.agency);
tags.sort();
if (!release.description) {
release.description = ''
}
return {
name: release.name,
description: release.description,
repositoryURL: release.url,
permissions: {
licenses: [
{
name: "Public Domain"
}
],
usageType: "openSource"
},
laborHours: -1,
tags: tags,
contact: {
email: "tech@consumerfinance.gov"
},
}
});
return inventory;
}
module.exports = codeJson;