-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (105 loc) · 2.99 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// see also
// https://docs.github.com/en/issues/planning-and-tracking-with-projects/automating-your-project/using-the-api-to-manage-projects
// https://gist.github.com/richkuz/e8842fce354edbd4e12dcbfa9ca40ff6
// ProjectV2 is really badly documented
import { gql, request } from 'graphql-request'
import fs from 'fs'
import { Parser } from 'json2csv'
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
const OWNER = 'epiverse-connect';
const endpoint = 'https://api.github.com/graphql';
const query = gql`
{
organization(login: "${OWNER}") {
projectV2(number: 1) {
id
updatedAt
items(first: 100) {
nodes {
id
content {
... on DraftIssue {
title
}
... on PullRequest {
title
url
}
... on Issue {
title
url
}
}
deliverable: fieldValueByName(name:"Deliverable") {
__typename
... on ProjectV2ItemFieldSingleSelectValue {
id
name
}
}
category: fieldValueByName(name:"Category") {
__typename
... on ProjectV2ItemFieldSingleSelectValue {
id
name
}
}
status: fieldValueByName(name:"Status") {
__typename
... on ProjectV2ItemFieldSingleSelectValue {
id
name
}
}
start: fieldValueByName(name:"Start date") {
__typename
... on ProjectV2ItemFieldDateValue {
id
date
}
}
end: fieldValueByName(name:"End date") {
__typename
... on ProjectV2ItemFieldDateValue {
id
date
}
}
}
}
}
}
}
`
const fetchProjects = async () => {
const headers = {
Authorization: `Bearer ${GITHUB_TOKEN}`,
};
try {
const data = await request(endpoint, query, {}, headers);
return data.organization.projectV2.items;
} catch (error) {
console.error(error);
}
};
const fields = ['deliverable', 'phase', 'title', 'url', 'status', 'startDate', 'endDate'];
fetchProjects().then(projects => {
const opts = { fields };
const data = projects.nodes.map(project => ({
deliverable: project.deliverable ? project.deliverable.name : '',
phase: project.category ? project.category.name : '',
title: project.content.title,
url: project.content.url,
status: project.status ? project.status.name : '',
startDate: project.start.date,
endDate: project.end.date,
}));
try {
const parser = new Parser(opts);
const csv = parser.parse(data);
fs.writeFileSync('projects.csv', csv);
console.log('CSV file successfully written');
} catch (err) {
// console.error(err);
}
});