Skip to content

Instantly share code, notes, and snippets.

@gr2m
Last active April 30, 2021 21:12
Show Gist options
  • Save gr2m/89b659fc885062a5d22221d62fe9655f to your computer and use it in GitHub Desktop.
Save gr2m/89b659fc885062a5d22221d62fe9655f to your computer and use it in GitHub Desktop.
A Kit script (https://www.scriptkit.com/) to copy the first name of a GitHub user
// Menu: Copy GitHub user name
// Description: Copies a GitHub user's first name, fallback to @login
// Author: Gregor Martynus
// Twitter: @gr2m
const { Octokit } = await npm("octokit");
const { createOAuthDeviceAuth } = await npm("@octokit/auth-oauth-device");
// set GitHub Token unless it's already set
let octokit;
if (env.GITHUB_TOKEN_NO_SCOPES) {
octokit = new Octokit({
auth: env.GITHUB_TOKEN_NO_SCOPES,
});
} else {
const auth = createOAuthDeviceAuth({
clientType: "oauth-app",
clientId: "34e4eac44e03b0daa82b",
onVerification(verification) {
copy(verification.user_code);
arg({
placeholder: `Press <enter> after granting permissions`,
ignoreBlur: true,
hint: `
Open <a href="${verification.verification_uri}">${verification.verification_uri}</a>, paste code from clipboard
`,
});
},
});
const { token } = await auth({
type: "oauth",
});
octokit = new Octokit({
auth: token,
});
await cli("set-env-var", "GITHUB_TOKEN_NO_SCOPES", token);
}
const { data: me } = await octokit.rest.users.getAuthenticated();
const username = await arg("Enter a GitHub username:");
let { data: user } = await octokit.rest.users.getByUsername({ username });
const userFirstNameOrUsername = user.name.trim()
? user.name.trim().split(/\s+/)[0]
: `@${user.login}`;
copy(userFirstNameOrUsername);
setPlaceholder(`"${userFirstNameOrUsername}" copied to your clipboard`);
setIgnoreBlur(false);
setHint(`Press esc to close this prompt`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment