-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
97 lines (83 loc) · 2.31 KB
/
index.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
const core = require('@actions/core');
const fs = require('fs');
const fetch = require("node-fetch");
const moment = require("moment");
const dataDir = `./.github/actioncloud/issue-tracker`;
if (!fs.existsSync(dataDir)){
fs.mkdirSync(dataDir, { recursive: true });
}
const dataFilePath = dataDir + '/data.json'
let issueData = [];
fs.readFile(dataFilePath, 'utf8', (err, data) => {
if (!err) {
var jsonObj = JSON.parse(data);
if (typeof jsonObj === "object") {
issueData = jsonObj
}
}
});
const githubToken = core.getInput('github-token');
const repo = process.env.GITHUB_REPOSITORY;
const repoInfo = repo.split("/");
const repoOwner = repoInfo[0];
const repoName = repoInfo[1];
const body = state =>
JSON.stringify({
query: `
query {
repository(owner:"${repoOwner}", name:"${repoName}") {
issues(states:${state}) {
totalCount
}
}
}`
});
function getIssues(body) {
const url = "https://api.github.com/graphql";
const options = {
method: "POST",
body: body,
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `bearer ${githubToken}`
}
};
return fetch(url, options)
.then(resp => resp.json())
.then(data => {
return data.data.repository.issues.totalCount;
}).catch((err) => {console.log(err)});
}
function getOpenIssues() {
return getIssues(body("OPEN"));
}
function getClosedIssues() {
return getIssues(body("CLOSED"));
}
function storeData(record) {
issueData.push(record);
}
function dumpData() {
const jsonData = JSON.stringify(issueData);
fs.writeFile(dataFilePath, jsonData, (err) => {
if (err) throw err;
});
}
function printBadgeLink() {
const actioncloudBadge = '[![](https://img.shields.io/badge/ActionCloud%20App-Issue%20Tracker-blue)](https://free.actioncloud.io/apps/github-issue-tracker?owner=' + repoOwner + '&repo=' + repoName + ')';
console.log(`::set-output name=actioncloud-badge::${actioncloudBadge}`)
}
async function run() {
var openIssues = await getOpenIssues();
var closedIssues = await getClosedIssues();
const now = moment().unix();
storeData({
timestamp: now,
openIssues: openIssues,
closedIssues: closedIssues
});
dumpData();
printBadgeLink();
}
run();