Created
August 2, 2018 12:20
-
-
Save omgitsads/464abdf48835b7e6c7f23384904f6266 to your computer and use it in GitHub Desktop.
Add new primary email address to a user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'octokit' | |
Octokit.configure do |c| | |
c.api_endpoint = ENV['GITHUB_API'] | |
end | |
# Create Admin Client with PAT with site_admin scope. | |
admin_client = Octokit::EnterpriseAdminClient.new( | |
access_token: ENV['GITHUB_PERSONAL_ACCESS_TOKEN'] | |
) | |
# Create end user impersonation token | |
token = admin_client.create_impersonation_token(ENV['GITHUB_USER'], | |
scopes: ['user', 'user:email']) | |
# Create end user client | |
user_client = Octokit::Client.new(access_token: token.token) | |
user = user_client.user | |
# Fetch exsiting emails | |
existing_emails = user_client.emails | |
# Add New email address | |
new_email = "#{user.login}@#{ENV['NEW_DOMAIN']}" | |
user_client.add_email(new_email) | |
# Remove all old emails | |
existing_emails.each do |email| | |
user_client.remove_email(email.email) | |
end | |
# Add emails back | |
existing_emails.each do |email| | |
user_client.add_email(email.email) | |
end | |
# Remove Impersonation Token | |
admin_client.delete_impersonation_token(ENV['GITHUB_USER']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GITHUB_API=https://[hostname]/api/v3 \ | |
GITHUB_PERSONAL_ACCESS_TOKEN=[ACCESS_TOKEN] \ | |
GITHUB_USER=[USER] \ | |
NEW_DOMAIN=[DOMAIN] \ | |
./add-new-primary-email.rb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment