forked from screwdriver-cd/scm-github-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (97 loc) · 3.83 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
114
'use strict';
const SDGraphQLClient = require('./sd-graphql');
const queries = require('./queries');
class GithubScmGraphQL {
/**
* constructor
* @param {Object} config The config object
* @param {String} config.graphQlUrl The graphql url
*/
constructor(config) {
this.sdGql = new SDGraphQLClient({
graphqlUrl: config.graphQlUrl || 'https://api.github.com/graphql'
});
}
/**
* Gets the enterprise user account schema
* @param {String} config The config object
* @param {String} config.slug The github enterprise slug
* @param {String} config.login The github user login
* @param {String} config.token The access token
* @returns Object https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/objects#enterpriseuseraccount
*/
async getEnterpriseUserAccount(config) {
const { slug, login, token } = config;
const { data } = await this.sdGql.query({
query: queries.GetEnterpriseUserAccount,
variables: { login },
token
});
if (data && data.user && data.user.enterprises) {
const { totalCount, nodes, pageInfo } = data.user.enterprises;
if (nodes && totalCount > 0 && pageInfo.hasNextPage === false) {
const enterprise = nodes.find(node => node.slug === slug);
if (enterprise) {
return {
login,
type: 'EnterpriseUserAccount'
};
}
}
}
return null;
}
/**
* Helper method to paginate the enterprise members recursively
* @param {*} config The config object
* @param {*} cursor The current page
* @param {*} members The list of members
* @returns Array https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/unions#enterprisemember
*/
async _listEnterpriseMembersHelper(config, cursor, allMembers = []) {
const { slug, token } = config;
const { data } = await this.sdGql.query({
query: queries.ListEnterpriseMembers,
variables: { slug, cursor },
token
});
if (data && data.enterprise && data.enterprise.members) {
const { nodes, pageInfo } = data.enterprise.members;
if (nodes) {
allMembers.push(...nodes);
}
if (pageInfo && pageInfo.hasNextPage) {
return this._listEnterpriseMembersHelper(config, pageInfo.endCursor, allMembers);
}
}
return allMembers;
}
/**
* List of enterprise members schema
* @param {String} config The config object
* @param {String} config.slug The github enterprise slug
* @param {String} config.token The access token
* @returns Array https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/unions#enterprisemember
*/
async listEnterpriseMembers(config) {
return this._listEnterpriseMembersHelper(config, null, []);
}
/**
* Gets the the github user schema
* @param {Object} config The config object
* @param {String} config.login The github user login
* @param {String} config.toke The access token
* @returns Object https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/objects#user
* or https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/objects#enterpriseuseraccount
*/
async getUser(config) {
const { login, token } = config;
const { data } = await this.sdGql.query({
query: queries.GetUser,
variables: { login },
token
});
return data.user;
}
}
module.exports = GithubScmGraphQL;