-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapi.ts
More file actions
151 lines (139 loc) · 4.55 KB
/
api.ts
File metadata and controls
151 lines (139 loc) · 4.55 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
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
const axios = require('axios')
const fs = require('fs')
async function getScoreCard(url: string) {
var api_url = new String("https://api.securityscorecards.dev/projects/");
var repo_url = new String(url);
var repo_url_len = repo_url.length
var sliced_repo_url = repo_url.slice(8, repo_url_len);
var api_repo = api_url.concat(sliced_repo_url.toString());
const options = {
method : 'GET',
url: api_repo,
params: { category: 'all', count: 2},
};
var jsonfile_name = './Scorecard.json'
await axios
.request(options)
.then(function (response: any) {
const jsonString = JSON.stringify(response.data, null, 2)
fs.writeFileSync(jsonfile_name, jsonString, {
flag: 'w'
})
})
.catch(function (_error: any) {
jsonfile_name = '404';
});
return jsonfile_name
}
async function getLicense(token: any, url: any) {
const { graphql } = require("@octokit/graphql");
var url_trim = url.trim();
var url_len = url_trim.length;
var sliced_url = url.slice(19, url_len);
var repo_info = sliced_url.split("/",2);
var json_file = './license.json';
try {
const { repository } = await graphql ({
query: `query repository_details($owner: String!, $repo: String!) {
repository(owner:$owner, name:$repo) {
licenseInfo {
name
}
}
}`,
owner: repo_info[0],
repo: repo_info[1],
headers: {
'Authorization': 'bearer ' + token,
},
});
const jsonString = JSON.stringify(repository, null, 2)
fs.writeFileSync(json_file, jsonString, {
flag: 'w'
})
} catch(error) {
json_file = "404";
}
return json_file
}
async function getContributor(token: any, url: any, action_info: any) { const { Octokit } = require("octokit");
const octokit = new Octokit({
auth: token
});
var url_trim = url.trim();
var url_len = url_trim.length;
var sliced_url = url.slice(19, url_len);
var repo_info = sliced_url.split("/",2);
var json_file = './contribute.json';
try {
const contribute = await octokit.request('GET /repos/{owner}/{repo}/{action}', {
owner: repo_info[0],
repo: repo_info[1],
action: action_info
});
const jsonString = JSON.stringify(contribute.data, null, 2)
fs.writeFileSync(json_file, jsonString, {
flag: 'w'
})
} catch(error) {
json_file = "404";
}
return json_file
}
async function getReadme(token: any, url:any, action_info: any) {
const { Octokit } = require("octokit");
const octokit = new Octokit({
auth: token
});
var url_trim = url.trim();
var url_len = url_trim.length;
var sliced_url = url.slice(19, url_len);
var repo_info = sliced_url.split("/",2);
var json_file = './readme.json';
try {
const readme = await octokit.request('GET /repos/{owner}/{repo}/{action}', {
owner: repo_info[0],
repo: repo_info[1],
action: action_info
});
const jsonString = JSON.stringify(readme.data, null, 2)
fs.writeFileSync(json_file, jsonString, {
flag: 'w'
})
} catch(error) {
json_file = "404";
}
return json_file
}
async function getLang(token: any, url: any, action_info: any) {
const { Octokit } = require("octokit");
const octokit = new Octokit({
auth: token
});
var url_trim = url.trim();
var url_len = url_trim.length;
var sliced_url = url.slice(19, url_len);
var repo_info = sliced_url.split("/",2);
var json_file = './language.json';
try {
const lang = await octokit.request('GET /repos/{owner}/{repo}/{action}', {
owner: repo_info[0],
repo: repo_info[1],
action: action_info
});
const jsonString = JSON.stringify(lang.data, null, 2)
fs.writeFileSync(json_file, jsonString, {
flag: 'w'
})
} catch(error) {
json_file = "404";
}
return json_file
}
async function getGiturl(npmurl: string) {
const { searchPackages } = require('query-registry')
const data = await searchPackages({query: {text: npmurl}});
const url = data["objects"][0].package.links.repository;
return url;
}
module.exports = { getScoreCard, getContributor, getLicense, getReadme, getLang, getGiturl };