-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiwrapper.js
163 lines (150 loc) · 4.21 KB
/
apiwrapper.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class ApiWrapper {
#octokit;
constructor({ octokit }) {
this.#octokit = octokit;
}
async fetchAssignedProjects({ pullRequestId }) {
const { node: { projectsV2: { nodes } } } = await this.#octokit.graphql.paginate(`
query paginate($cursor: String) {
node(id:"${pullRequestId}") {
... on PullRequest {
number
projectsV2(first: 100, after: $cursor) {
nodes {
id
title
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}`);
return nodes;
}
// we need to know the item id of a PR in the project.
// we know this item exists
async fetchItemForPRId({ project, pullRequestId }) {
const { node: { items: { nodes } } } = await this.#octokit.graphql.paginate(`
query paginate($cursor: String) {
node(id:"${project.id}") {
... on ProjectV2 {
number
items(first: 100, after: $cursor) {
nodes {
id
content {
... on PullRequest {
id
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}
}`);
return nodes.find((item) => item.content.id === pullRequestId);
}
// requires GitHub App installation token with read and write
// permissions for projects v2 and pull requests
async deleteProjectItem({ project, item, clientMutationId }) {
const { deleteProjectV2Item: deletedItemId } = await this.#octokit.graphql(`
mutation deleteProjectV2Item {
deleteProjectV2Item(
input: {
clientMutationId: "${clientMutationId}",
itemId: "${item.id}",
projectId: "${project.id}",
}
)
{
deletedItemId
}
}`);
return deletedItemId;
}
async fetchRepositoryAndProjects({ owner, repositoryName }) {
const { repository } = await this.#octokit.graphql.paginate(`
query paginate($cursor: String) {
repository(owner: "${owner}", name: "${repositoryName}") {
name
id
projectsV2(first: 100, after: $cursor) {
nodes {
id
title
number
fields(first: 5) {
nodes {
... on ProjectV2SingleSelectField {
id
name
options {
id
name
}
}
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`);
return repository;
}
// requires GitHub App installation token with read and write
// permissions for projects v2 and pull requests
async assignPRtoProject({ pullRequestId, project, clientMutationId }) {
const { addProjectV2ItemById: { item } } = await this.#octokit.graphql(`
mutation assignPRtoProject {
addProjectV2ItemById(
input: {
clientMutationId: "${clientMutationId}",
contentId: "${pullRequestId}",
projectId: "${project.id}",
}
)
{
item {
id
}
}
}`);
return item;
}
async updateItemFieldValue({
clientMutationId, project, item, statusField, todoOption,
}) {
// https://docs.github.com/en/graphql/reference/mutations#updateprojectv2itemfieldvalue
const result = await this.#octokit.graphql(`
mutation updateItemFieldValue {
updateProjectV2ItemFieldValue(
input: {
clientMutationId: "${clientMutationId}",
projectId: "${project.id}",
fieldId: "${statusField.id}",
itemId: "${item.id}",
value: {
singleSelectOptionId: "${todoOption.id}"
}
}
){
projectV2Item {
id
}
}
}`);
return result;
}
}
export { ApiWrapper }; // eslint-disable-line import/prefer-default-export