Created
April 6, 2016 12:45
-
-
Save mattbostock/1bd8562723e0e579fe7ce6887115f1e9 to your computer and use it in GitHub Desktop.
Noddy script to create all users from GitHub Enterprise and GitHub.com organisations in Drone: https://github.com/drone/drone
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
package main | |
import ( | |
"fmt" | |
"net/url" | |
"os" | |
"code.google.com/p/goauth2/oauth" | |
drone "github.com/drone/drone/client" | |
"github.com/google/go-github/github" | |
) | |
const ( | |
droneURL = "https://example.com:443" | |
gheOrg = "example" | |
gitHubOrg = "example" | |
) | |
var ( | |
droneToken = os.Getenv("DRONE_TOKEN") | |
gheToken = os.Getenv("GHE_TOKEN") | |
gitHubToken = os.Getenv("GITHUB_TOKEN") | |
token string | |
) | |
func init() { | |
if droneToken == "" { | |
panic("DRONE_TOKEN environment variable not set") | |
} | |
if gitHubToken == "" { | |
panic("GITHUB_TOKEN environment variable not set") | |
} | |
if gheToken == "" { | |
panic("GHE_TOKEN environment variable not set") | |
} | |
} | |
func main() { | |
createUsersFromGitHubInstance("enterprise.github.com", gheOrg) | |
createUsersFromGitHubInstance("github.com", gitHubOrg) | |
} | |
func createUsersFromGitHubInstance(remote string, org string) { | |
switch remote { | |
case "github.com": | |
token = gitHubToken | |
case "enterprise.github.com": | |
token = gheToken | |
} | |
t := &oauth.Transport{ | |
Token: &oauth.Token{AccessToken: token}, | |
} | |
ghClient := github.NewClient(t.Client()) | |
if remote == "enterprise.github.com" { | |
ghClient.BaseURL, _ = url.Parse("https://ghe-url.example.com/api/v3/") | |
} | |
nextPage := 1 | |
var members []github.User | |
for nextPage != 0 { | |
opts := &github.ListMembersOptions{ | |
PublicOnly: false, | |
ListOptions: github.ListOptions{Page: nextPage, PerPage: 100}, | |
} | |
m, resp, err := ghClient.Organizations.ListMembers(org, opts) | |
if err != nil { | |
panic(err) | |
} | |
members = append(members, m...) | |
nextPage = resp.NextPage | |
} | |
droneClient := drone.New(droneToken, droneURL) | |
for _, member := range members { | |
fmt.Printf("Creating %s user %q...", remote, *member.Login) | |
_, err := droneClient.Users.Create(remote, *member.Login) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
fmt.Println("Done.") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment