|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const SDGraphQLClient = require('./sd-graphql'); |
| 4 | +const queries = require('./queries'); |
| 5 | + |
| 6 | +class GithubScmGraphQL { |
| 7 | + /** |
| 8 | + * constructor |
| 9 | + * @param {Object} config The config object |
| 10 | + * @param {String} config.graphQlUrl The graphql url |
| 11 | + */ |
| 12 | + constructor(config) { |
| 13 | + this.sdGql = new SDGraphQLClient({ |
| 14 | + graphqlUrl: config.graphQlUrl || 'https://api.github.com/graphql' |
| 15 | + }); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Gets the enterprise user account schema |
| 20 | + * @param {String} config The config object |
| 21 | + * @param {String} config.slug The github enterprise slug |
| 22 | + * @param {String} config.login The github user login |
| 23 | + * @param {String} config.token The access token |
| 24 | + * @returns Object https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/objects#enterpriseuseraccount |
| 25 | + */ |
| 26 | + async getEnterpriseUserAccount(config) { |
| 27 | + const { slug, login, token } = config; |
| 28 | + |
| 29 | + const { data } = await this.sdGql.query({ |
| 30 | + query: queries.GetEnterpriseUserAccount, |
| 31 | + variables: { slug, query: login }, |
| 32 | + token |
| 33 | + }); |
| 34 | + |
| 35 | + if (data && data.enterprise) { |
| 36 | + const { members } = data.enterprise; |
| 37 | + |
| 38 | + if (members && members.totalCount === 1) { |
| 39 | + return members.nodes[0]; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + return null; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Helper method to paginate the enterprise members recursively |
| 48 | + * @param {*} config The config object |
| 49 | + * @param {*} cursor The current page |
| 50 | + * @param {*} members The list of members |
| 51 | + * @returns Array https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/unions#enterprisemember |
| 52 | + */ |
| 53 | + async _listEnterpriseMembersHelper(config, cursor, allMembers = []) { |
| 54 | + const { slug, token } = config; |
| 55 | + |
| 56 | + const { data } = await this.sdGql.query({ |
| 57 | + query: queries.ListEnterpriseMembers, |
| 58 | + variables: { slug, cursor }, |
| 59 | + token |
| 60 | + }); |
| 61 | + |
| 62 | + if (data && data.enterprise && data.enterprise.members) { |
| 63 | + const { nodes, pageInfo } = data.enterprise.members; |
| 64 | + |
| 65 | + if (nodes) { |
| 66 | + allMembers.push(...nodes); |
| 67 | + } |
| 68 | + if (pageInfo && pageInfo.hasNextPage) { |
| 69 | + return this._listEnterpriseMembersHelper(config, pageInfo.endCursor, allMembers); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return allMembers; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * List of enterprise members schema |
| 78 | + * @param {String} config The config object |
| 79 | + * @param {String} config.slug The github enterprise slug |
| 80 | + * @param {String} config.token The access token |
| 81 | + * @returns Array https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/unions#enterprisemember |
| 82 | + */ |
| 83 | + async listEnterpriseMembers(config) { |
| 84 | + return this._listEnterpriseMembersHelper(config, null, []); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Gets the the github user schema |
| 89 | + * @param {Object} config The config object |
| 90 | + * @param {String} config.login The github user login |
| 91 | + * @param {String} config.toke The access token |
| 92 | + * @returns Object https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/objects#user |
| 93 | + * or https://docs.github.com/en/enterprise-cloud@latest/graphql/reference/objects#enterpriseuseraccount |
| 94 | + */ |
| 95 | + async getUser(config) { |
| 96 | + const { login, token } = config; |
| 97 | + const { data } = await this.sdGql.query({ |
| 98 | + query: queries.GetUser, |
| 99 | + variables: { login }, |
| 100 | + token |
| 101 | + }); |
| 102 | + |
| 103 | + return data.user; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +module.exports = GithubScmGraphQL; |
0 commit comments