Skip to content

Commit 133da2f

Browse files
pritamstyz4everppaul
andauthored
feat: update EAU query (#3)
Co-authored-by: ppaul <pritam.paul@yahooinc.com>
1 parent 6105af9 commit 133da2f

File tree

3 files changed

+88
-34
lines changed

3 files changed

+88
-34
lines changed

index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,22 @@ class GithubScmGraphQL {
2828

2929
const { data } = await this.sdGql.query({
3030
query: queries.GetEnterpriseUserAccount,
31-
variables: { slug, query: login },
31+
variables: { login },
3232
token
3333
});
3434

35-
if (data && data.enterprise) {
36-
const { members } = data.enterprise;
35+
if (data && data.user && data.user.enterprises) {
36+
const { totalCount, nodes, pageInfo } = data.user.enterprises;
37+
38+
if (nodes && totalCount > 0 && pageInfo.hasNextPage === false) {
39+
const enterprise = nodes.find(node => node.slug === slug);
3740

38-
if (members && members.totalCount === 1) {
39-
return members.nodes[0];
41+
if (enterprise) {
42+
return {
43+
login,
44+
type: 'EnterpriseUserAccount'
45+
};
46+
}
4047
}
4148
}
4249

queries.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,28 @@
33
const gql = require('graphql-tag');
44

55
module.exports.GetEnterpriseUserAccount = gql`
6-
query GetEnterpriseUserAccount($slug: String!, $query: String!) {
7-
enterprise(slug: $slug) {
6+
query GetEnterpriseUserAccount($login: String!) {
7+
user(login: $login) {
88
name
99
id
10-
members(query: $query, first: 1, role: MEMBER) {
10+
login
11+
enterprises(first: 100) {
1112
totalCount
13+
pageInfo {
14+
endCursor
15+
hasNextPage
16+
}
1217
nodes {
13-
type: __typename
14-
... on EnterpriseUserAccount {
15-
id
16-
name
17-
login
18-
}
19-
... on User {
20-
id
21-
name
22-
login
23-
}
18+
id
19+
name
20+
slug
2421
}
2522
}
2623
}
2724
}
2825
`;
2926

27+
// needs `admin:enterprise` scope for EAU fragment
3028
module.exports.ListEnterpriseMembers = gql`
3129
query ListEnterpriseMembers($slug: String!, $cursor: String) {
3230
enterprise(slug: $slug) {

test/index.test.js

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,44 +60,93 @@ describe('GithubGraphQL', () => {
6060
it('should get enterprise user account', async () => {
6161
const slug = 'slug';
6262
const login = 'ai_humanoid';
63-
const data = {
64-
enterprise: {
65-
members: {
66-
totalCount: 1,
67-
nodes: [mockUser1]
63+
const response = {
64+
data: {
65+
user: {
66+
name: 'AI Humanoid',
67+
id: 'U_abcdef',
68+
login: 'ai_humanoid',
69+
enterprises: {
70+
totalCount: 1,
71+
pageInfo: {
72+
hasNextPage: false,
73+
cursor: 'abcdefg'
74+
},
75+
nodes: [
76+
{
77+
id: 'EUA_abcdef',
78+
name: 'AI Humanoid',
79+
slug: 'slug'
80+
}
81+
]
82+
}
6883
}
6984
}
7085
};
7186

72-
githubGql.sdGql.query.resolves({ data });
87+
githubGql.sdGql.query.resolves(response);
7388

7489
const result = await githubGql.getEnterpriseUserAccount({
7590
slug,
7691
login,
7792
token
7893
});
7994

80-
assert.deepEqual(result, data.enterprise.members.nodes[0]);
95+
assert.deepEqual(result, {
96+
login,
97+
type: 'EnterpriseUserAccount'
98+
});
8199
assert.calledWith(githubGql.sdGql.query, {
82100
query: queries.GetEnterpriseUserAccount,
83-
variables: { slug, query: login },
101+
variables: { login },
84102
token
85103
});
86104
});
87105

88-
it('should return null if no enterprise user account', async () => {
106+
it('should return null if no user is not part of enterprise', async () => {
89107
const slug = 'slug';
90108
const login = 'ai_humanoid';
91-
const data = {
92-
enterprise: {
93-
members: {
94-
totalCount: 0
109+
const response = {
110+
data: {
111+
user: {
112+
name: 'AI Humanoid',
113+
id: 'U_abcdef',
114+
login: 'ai_humanoid',
115+
enterprises: null
95116
}
117+
},
118+
errors: {
119+
type: 'FORBIDDEN',
120+
path: ['user', 'enterprises']
96121
}
97122
};
98123

99-
githubGql.sdGql.query.resolves({ data });
124+
githubGql.sdGql.query.resolves(response);
125+
126+
const result = await githubGql.getEnterpriseUserAccount({
127+
slug,
128+
login,
129+
token
130+
});
131+
132+
assert.equal(result, null);
133+
assert.calledWith(githubGql.sdGql.query, {
134+
query: queries.GetEnterpriseUserAccount,
135+
variables: { login },
136+
token
137+
});
138+
});
139+
140+
it('should return null if no user does not exists', async () => {
141+
const slug = 'slug';
142+
const login = 'ai_humanoid';
143+
const response = {
144+
data: {
145+
user: null
146+
}
147+
};
100148

149+
githubGql.sdGql.query.resolves(response);
101150
const result = await githubGql.getEnterpriseUserAccount({
102151
slug,
103152
login,
@@ -107,7 +156,7 @@ describe('GithubGraphQL', () => {
107156
assert.equal(result, null);
108157
assert.calledWith(githubGql.sdGql.query, {
109158
query: queries.GetEnterpriseUserAccount,
110-
variables: { slug, query: login },
159+
variables: { login },
111160
token
112161
});
113162
});

0 commit comments

Comments
 (0)