-
Notifications
You must be signed in to change notification settings - Fork 0
/
gh-self-star.html
152 lines (143 loc) · 3.24 KB
/
gh-self-star.html
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
<h1>Measure self-starring <span title="percentage of your starred repos are also your repos" class="infos">ratio</span> of Github users</h1>
<style>
body {
font-family: arial;
}
h1 {
text-align: center;
margin-top: 3em;
margin-bottom: 2em;
}
.infos {
border-bottom: lightblue dashed;
}
input {
font-size: 1.5em;
margin: .5em auto;
}
input[type=submit] {
background: transparent;
border: 1px solid gray;
font-feature-settings: 'liga' 1;
padding: 5px 10px;
}
input[type=submit]:disabled {
border: 1px solid lightgray;
}
form {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
}
pre {
max-width: 400px;
margin: 1em auto;
}
</style>
<form>
<input name="login" placeholder="Github login, ex: mdo" required>
<input name="token" placeholder="Github API token" required>
<input type="submit" value="🔍">
</form>
<pre id="output">
</pre>
<script>
/*
create token: https://developer.github.com/v4/guides/forming-calls/#authenticating-with-graphql
*/
const form = document.forms[0];
if (localStorage.ghToken) {
form.token.value = localStorage.ghToken;
}
form.onsubmit = submit;
async function submit(e) {
if (e) e.preventDefault();
const login = form.login.value;
localStorage.ghToken = form.token.value;
form.elements[2].disabled = true;
try {
const user = await getUser({login});
const totalStars = user.starredRepositories.totalCount;
const orgs = new Set(user.organizations.nodes.map(o => o.login));
let reps;
let cursor;
let count = 0;
for (let i=0; i < 100; i++) {
reps = await getStars({login, after: cursor});
console.log(reps.nodes.length);
count += reps.nodes.filter(({owner}) => owner.login === login || orgs.has(owner.login)).length;
cursor = reps.pageInfo.endCursor;
if (!reps.pageInfo.hasNextPage) {
break;
}
}
console.log(count, totalStars);
output.innerHTML = `
<strong>${login}</strong>'s self-starring factor: ${count} / ${totalStars} = ${(count / totalStars).toFixed(2)}
`;
} catch(err) {
output.innerHTML = `<a href="https://developer.github.com/v4/guides/forming-calls/#authenticating-with-graphql">${err && err.message || 'Create a GH token!'}</a>`
}
form.elements[2].disabled = false;
};
const getUser = ({login}) => gql({
query: `query user($login: String!) {
user(login: $login) {
organizations(first: 100) {
nodes {
login
}
}
starredRepositories {
totalCount
}
}
}`,
variables: {
login
}
})
.then(d => d.user);
const getStars = ({login, after, first = 100}) => gql({
query: `query userStars($login: String!, $first: Int, $after: String) {
user(login: $login) {
starredRepositories(first: $first, after: $after) {
nodes {
id
owner {login}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}`,
variables: {
login, first, after
}
})
.then(d => d.user.starredRepositories);
const gql = ({query, variables}) => fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
Authorization: `bearer ${localStorage.ghToken}`
},
body: JSON.stringify({
query,
variables
})
})
.then(async r => {
if (!r.ok) {
const d = await r.json();
throw new Error(d.message);
}
return r.json().then(d => d.data);
});
if (location.hash.length > 1) {
form.login.value = location.hash.slice(1);
submit();
}
</script>