Example #1
0
// Client returns  clients for accessing github.
func (c *Config) Clients() (*Clients, error) {
	orgClient := github.NewClient(
		oauth2.NewClient(
			oauth2.NoContext,
			oauth2.StaticTokenSource(
				&oauth2.Token{
					AccessToken: c.OrganizationKey,
				},
			),
		),
	)
	userClient := github.NewClient(
		oauth2.NewClient(
			oauth2.NoContext,
			oauth2.StaticTokenSource(
				&oauth2.Token{
					AccessToken: c.UserKey,
				},
			),
		),
	)
	return &Clients{
		OrgClient:  orgClient,
		UserClient: userClient,
	}, nil

}
Example #2
0
func GenerateGithub(name string, token string, exclude string) map[string]*Repo {
	repos := make(map[string]*Repo)

	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	client := github.NewClient(tc)
	opt := &github.RepositoryListByOrgOptions{
		Type:        "all",
		ListOptions: github.ListOptions{PerPage: 100},
	}

	for {
		newRepos, resp, err := client.Repositories.ListByOrg(name, opt)
		check(err)

		for _, newRepo := range newRepos {
			url := fmt.Sprintf("[email protected]:%s/%s.git", name, *newRepo.Name)
			repos[*newRepo.Name] = &Repo{
				Url: url,
			}
		}

		if resp.NextPage == 0 {
			break
		}
		opt.ListOptions.Page = resp.NextPage
	}

	return repos
}
Example #3
0
//RegisterResult registers the supplied result
func (githubClient *GitHubClient) RegisterResult(result Result) error {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: githubClient.Token},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	client := github.NewClient(tc)

	if githubClient.BaseUrl != nil {
		client.BaseURL = githubClient.BaseUrl
	}

	log.Info("Submitting result")
	repositories := client.Repositories
	status, _, err := repositories.CreateStatus(
		githubClient.From,
		githubClient.Repo,
		result.SHA,
		&github.RepoStatus{
			State:       github.String(result.State),
			TargetURL:   github.String(result.Url),
			Description: github.String(result.Message),
			Context:     github.String("continuous-integraion/walter"),
		})
	log.Infof("Submit status: %s", status)
	if err != nil {
		log.Errorf("Failed to register result: %s", err)
	}
	return err
}
Example #4
0
func main() {
	flag.Usage = usage
	flag.Parse()
	cachingHTTPClient = util.CachingHttpClient()

	if *tokenFlag == "" || cachingHTTPClient == nil {
		flag.Usage()
		return
	}

	ds = clientFlags.CreateDataset()
	if ds == nil {
		flag.Usage()
		return
	}
	defer ds.Store().Close()

	if err := clientFlags.CreateProgressFile(); err != nil {
		fmt.Println(err)
	} else {
		defer clientFlags.CloseProgressFile()
	}

	token := oauth2.Token{AccessToken: *tokenFlag}
	authHTTPClient = oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(&token))

	start = time.Now()
	var user = getUser()
	printStats(user)

	userRef := ds.Store().WriteValue(user)
	fmt.Printf("userRef: %s\n", userRef.TargetRef())
	_, err := ds.Commit(userRef)
	d.Exp.NoError(err)
}
Example #5
0
func (d *Driver) getClient() *godo.Client {
	token := &oauth2.Token{AccessToken: d.AccessToken}
	tokenSource := oauth2.StaticTokenSource(token)
	client := oauth2.NewClient(oauth2.NoContext, tokenSource)

	return godo.NewClient(client)
}
Example #6
0
func getClient(token string) *github.Client {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	return github.NewClient(tc)
}
Example #7
0
func gistHandler(w http.ResponseWriter, r *http.Request) {
	log.Debug("gistHandler()")

	// get access token
	c, err := r.Cookie("access_token")
	if err != nil {
		log.Error("get cookie error: ", err)
		http.Error(w, "Github Auth Error", http.StatusInternalServerError)
		return
	}

	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: c.Value},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	// github api
	client := github.NewClient(tc)

	gist, err := makeGist(r.Body)
	if err != nil {
		log.Errorf("make gist error: %v", err)
		http.Error(w, "Server Error", http.StatusInternalServerError)
		return
	}

	retGist, _, err := client.Gists.Create(&gist)
	if err != nil {
		log.Error("create gist error: ", err)
		http.Error(w, "Post Gist Error", http.StatusInternalServerError)
		return
	}

	io.WriteString(w, *retGist.HTMLURL)
}
Example #8
0
// retrieveCommits fetches the latest deployed commits as well
// as the latest GitHub commits for a given Project.
// it will also check if the user has permission to pull.
func retrieveCommits(r *http.Request, project goship.Project, deployUser string) (goship.Project, error) {
	// define a wait group to wait for all goroutines to finish
	var wg sync.WaitGroup
	githubToken := os.Getenv(gitHubAPITokenEnvVar)
	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: githubToken})
	client := github.NewClient(oauth2.NewClient(oauth2.NoContext, ts))
	for i, environment := range project.Environments {
		for j, host := range environment.Hosts {
			// start a goroutine for SSHing on to the machine
			wg.Add(1)
			go getCommit(&wg, project, environment, host, deployUser, i, j)
		}
		wg.Add(1)
		go getLatestGitHubCommit(&wg, project, environment, client, project.RepoOwner, project.RepoName, i)
	}
	// wait for goroutines to finish
	wg.Wait()
	for i, e := range project.Environments {

		project.Environments[i] = e
		for j, host := range e.Hosts {
			host.GitHubCommitURL = host.LatestGitHubCommitURL(project)
			host.GitHubDiffURL = host.LatestGitHubDiffURL(project, e)
			host.ShortCommitHash = host.LatestShortCommitHash()
			project.Environments[i].Hosts[j] = host
		}
	}
	u, err := getUser(r)
	if err != nil {
		log.Printf("Failed to get user %s", err)
	}
	return filterProject(project, r, u), err
}
Example #9
0
func create(desc string, pub bool, files map[string]string) (*github.Gist, error) {

	ghat := getAccessToken()

	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: ghat},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	client := github.NewClient(tc)

	f := make(map[github.GistFilename]github.GistFile)

	for k := range files {
		_k := github.GistFilename(k)
		f[_k] = github.GistFile{Content: github.String(files[k])}
	}

	gist := &github.Gist{
		Description: github.String(desc),
		Public:      github.Bool(pub),
		Files:       f,
	}

	gist, _, err := client.Gists.Create(gist)

	return gist, err
}
Example #10
0
func initializeClient(token string) {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	client = github.NewClient(tc)
}
Example #11
0
// Logs in to github using oauth.
// Returns error if login fails.
func Login(userkey string) error {
	file, err := ioutil.ReadFile(*Path + ".issue/config.json")
	if err != nil {
		GitLog.Println("open config: ", err)
		os.Exit(1)
	}
	temp := new(Config)
	if err = json.Unmarshal(file, temp); err != nil {
		GitLog.Println("parse config: ", err)
		os.Exit(1)
	}
	Conf = temp
	token := Conf.Token
	if Conf.Secure {
		key := []byte(userkey)
		token = encrypt.Decrypt(key, token)
	}
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	client = github.NewClient(tc)
	user, _, err := client.Users.Get("")

	if err != nil {
		GitLog.Printf("\nerror: %v\n", err)
		return err
	}
	log.Printf("\nLogged into: %v\n", github.Stringify(user.Login))
	GitLog.Print("Logged into: ", github.Stringify(user.Login))
	return nil
}
Example #12
0
func BuildHTTPClient(proxy, token string) *http.Client {
	var base http.RoundTripper
	base = http.DefaultTransport

	// Proxy layer
	if len(proxy) > 0 {
		u, _ := url.Parse(proxy)
		base = &http.Transport{
			Proxy: http.ProxyURL(u),
		}
	}

	// Authentication layer
	if len(token) > 0 {
		ts := oauth2.StaticTokenSource(
			&oauth2.Token{AccessToken: token},
		)
		base = &oauth2.Transport{
			Source: ts,
			Base:   base,
		}
	}

	// Rate limiting
	transport := &RateLimitedTransport{
		Base: base,
	}

	return &http.Client{
		Transport: transport,
	}
}
Example #13
0
func cmdSetup(c *cli.Context) {
	token := c.Args().First()

	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	client := qiita.NewClient(tc)
	user, err := client.AuthenticatedUser.Show()
	if err != nil {
		fmt.Println("Auth failed")
		os.Exit(1)
	}

	loadConfig()
	viper.Set("accessToken", token)
	viper.Set("id", user.Id)

	err = saveConfig()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	fmt.Println("Token saved")
}
Example #14
0
// NewClient returns a new BitBalloon API client
func NewClient(config *Config) *Client {
	client := &Client{}

	if config.BaseUrl != "" {
		client.BaseUrl, _ = url.Parse(config.BaseUrl)
	} else {
		client.BaseUrl, _ = url.Parse(defaultBaseURL)
	}

	if config.HttpClient != nil {
		client.client = config.HttpClient
	} else if config.AccessToken != "" {
		client.client = oauth2.NewClient(
			oauth2.NoContext,
			oauth2.StaticTokenSource(
				&oauth2.Token{
					AccessToken: config.AccessToken,
				},
			),
		)
	}

	if &config.UserAgent != nil {
		client.UserAgent = config.UserAgent
	} else {
		client.UserAgent = userAgent
	}

	client.Sites = &SitesService{client: client}
	client.Deploys = &DeploysService{client: client}

	return client
}
func (grp githubRegistryProvider) createGithubClient(credentialName string) (*http.Client, *github.Client, error) {
	if credentialName == "" {
		return http.DefaultClient, github.NewClient(nil), nil
	}
	c, err := grp.cp.GetCredential(credentialName)

	if err != nil {
		log.Printf("Failed to fetch credential %s: %v", credentialName, err)
		log.Print("Trying to use unauthenticated client")
		return http.DefaultClient, github.NewClient(nil), nil
	}

	if c != nil {
		if c.APIToken != "" {
			ts := oauth2.StaticTokenSource(
				&oauth2.Token{AccessToken: string(c.APIToken)},
			)
			tc := oauth2.NewClient(oauth2.NoContext, ts)
			return tc, github.NewClient(tc), nil
		}
		if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" {
			tp := github.BasicAuthTransport{
				Username: c.BasicAuth.Username,
				Password: c.BasicAuth.Password,
			}
			return tp.Client(), github.NewClient(tp.Client()), nil
		}

	}
	return nil, nil, fmt.Errorf("No suitable credential found for %s", credentialName)

}
Example #16
0
func GetClient() *godo.Client {
	token := &oauth2.Token{AccessToken: token}
	t := oauth2.StaticTokenSource(token)

	oauthClient := oauth2.NewClient(oauth2.NoContext, t)
	return godo.NewClient(oauthClient)
}
Example #17
0
func downloadReleaseFromGithub(r repo, artifact string, alias string) {
	client := github.NewClient(nil)

	config, err := loadConfig()
	if err != nil {
		DieWithError(err, "Could not load user config")
	}

	if token, ok := config.Config["token"]; ok {
		log.Debug("Using Github token from config.")
		// if the user has a token stored, use it
		ts := oauth2.StaticTokenSource(
			&oauth2.Token{AccessToken: token},
		)
		tc := oauth2.NewClient(oauth2.NoContext, ts)
		client = github.NewClient(tc)
	} else {
		log.Debug("No Github token found in config. Add it with `mup config` if you need it")
	}

	// with a provided token, this will also get repos for the authenticated user
	// TODO: support pagination
	releases, resp, err := client.Repositories.ListReleases(r.Owner, r.Repo, nil)
	if resp.StatusCode == 404 {
		Die("Could not find repository %s/%s. Maybe it doesn't exist, or you need to add your token for μpdater to have access.", r.Owner, r.Repo)
	} else if err != nil {
		DieWithError(err, "Error reaching Github")
	}

	updateFromGithubReleases(client, r, releases, "", artifact, alias)
}
Example #18
0
// borrowed from minishift until it supports install / download binaries
func getLatestVersionFromGitHub(githubOwner, githubRepo string) (semver.Version, error) {
	if githubClient == nil {
		token := os.Getenv("GH_TOKEN")
		var tc *http.Client
		if len(token) > 0 {
			ts := oauth2.StaticTokenSource(
				&oauth2.Token{AccessToken: token},
			)
			tc = oauth2.NewClient(oauth2.NoContext, ts)
		}
		githubClient = github.NewClient(tc)
	}
	client := githubClient
	var (
		release *github.RepositoryRelease
		resp    *github.Response
		err     error
	)
	release, resp, err = client.Repositories.GetLatestRelease(githubOwner, githubRepo)
	if err != nil {
		return semver.Version{}, err
	}
	defer resp.Body.Close()
	latestVersionString := release.TagName
	if latestVersionString != nil {
		return semver.Make(strings.TrimPrefix(*latestVersionString, "v"))

	}
	return semver.Version{}, fmt.Errorf("Cannot get release name")
}
Example #19
0
func insertEntry(env, owner, repoName, fromRevision, toRevision, user string, success bool, time time.Time) error {
	path := path.Join(*dataPath, env+".json")
	err := prepareDataFiles(path)
	if err != nil {
		return err
	}

	e, err := readEntries(env)
	if err != nil {
		return err
	}
	gt := os.Getenv(gitHubAPITokenEnvVar)
	ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: gt})
	c := github.NewClient(oauth2.NewClient(oauth2.NoContext, ts))
	com, _, err := c.Git.GetCommit(owner, repoName, toRevision)
	if err != nil {
		log.Println("Error getting commit msg: ", err)
	}
	var m string
	if com.Message != nil {
		m = *com.Message
	}
	diffURL := diffURL(owner, repoName, fromRevision, toRevision)
	d := DeployLogEntry{DiffURL: diffURL, ToRevisionMsg: m, User: user, Time: time, Success: success}
	e = append(e, d)
	err = writeJSON(e, path)
	if err != nil {
		return err
	}
	return nil
}
Example #20
0
// newSlackServer returns an http handler for handling Slack slash commands at <url>/slack.
func newSlackServer(q conveyor.BuildQueue, c *cli.Context) http.Handler {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: c.String("github.token")},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	cy := github.NewClient(tc)

	r := slash.NewMux()
	r.Match(slash.MatchSubcommand(`help`), slack.Help)
	r.MatchText(
		regexp.MustCompile(`enable (?P<owner>\S+?)/(?P<repo>\S+)`),
		slack.NewEnable(
			cy,
			slack.NewHook(c.String("url"), c.String("github.secret")),
		),
	)
	r.MatchText(
		regexp.MustCompile(`build (?P<owner>\S+?)/(?P<repo>\S+)@(?P<branch>\S+)`),
		slack.NewBuild(
			cy,
			q,
			fmt.Sprintf(logsURLTemplate, c.String("url")),
		),
	)

	return slash.NewServer(slash.ValidateToken(r, c.String("slack.token")))
}
Example #21
0
func getVersions() ([]*github.RepositoryRelease, error) {
	if githubClient == nil {
		token := os.Getenv("GH_TOKEN")
		var tc *http.Client
		if len(token) > 0 {
			ts := oauth2.StaticTokenSource(
				&oauth2.Token{AccessToken: token},
			)
			tc = oauth2.NewClient(oauth2.NoContext, ts)
		}
		githubClient = github.NewClient(tc)
	}
	client := githubClient

	releases, resp, err := client.Repositories.ListReleases(githubOwner, githubRepo, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if len(releases) == 0 {
		return nil, fmt.Errorf("There were no OpenShift Releases available")
	}
	return releases, nil
}
Example #22
0
func MakeClient(token string) *github.Client {
	var client *http.Client
	var transport http.RoundTripper
	if *useMemoryCache {
		transport = httpcache.NewMemoryCacheTransport()
	} else {
		transport = http.DefaultTransport
	}
	if len(token) > 0 {
		rateLimitTransport := &RateLimitRoundTripper{
			delegate: transport,
			// Global limit is 5000 Q/Hour, try to only use 1800 to make room for other apps
			throttle: util.NewTokenBucketRateLimiter(0.5, 10),
		}
		ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
		client = &http.Client{
			Transport: &oauth2.Transport{
				Base:   rateLimitTransport,
				Source: oauth2.ReuseTokenSource(nil, ts),
			},
		}
	} else {
		rateLimitTransport := &RateLimitRoundTripper{
			delegate: transport,
			throttle: util.NewTokenBucketRateLimiter(0.01, 10),
		}
		client = &http.Client{
			Transport: rateLimitTransport,
		}
	}
	return github.NewClient(client)
}
Example #23
0
func (gh *Client) Authenticate(username string, password string) error {
	var token []byte
	gh.User = &User{
		Login:    username,
		Password: password,
	}

	// conf := &oauth2.Config{
	// 	Endpoint: githubOauth2.Endpoint,
	// }
	gh.Store.Get(s.ConfigBucket, "gh_token", &token)
	if len(token) == 0 {
		var err error
		token, err = gh.createToken()
		if err != nil {
			return errwrap.Wrapf("[Github] Authentication error: %v", err)
		}
	}

	ts := oauth2.StaticTokenSource(
		&oauth2.Token{
			AccessToken: string(token),
		},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	gh.client = github.NewClient(tc)
	user, _, _ := gh.client.Users.Get("")
	gh.User.Login = *user.Login
	gh.User.issues = map[string][]*github.Issue{}
	gh.User.Password = ""
	gh.Fetch()
	return nil
}
Example #24
0
func authWithGitHub(tkn string) *github.Client {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: tkn},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	return github.NewClient(tc)
}
Example #25
0
func startCrawler(user string, accessToken string, quit chan struct{}, wg *sync.WaitGroup) {
	authTransport := &oauth2.Transport{
		Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken}),
	}
	cacheTransport := &Transport{Transport: authTransport, lastEtag: ""}
	httpClient := &http.Client{Transport: cacheTransport}
	client := github.NewClient(httpClient)

L:
	for {
		select {
		case <-quit:
			// TODO: cleanup
			fmt.Fprintln(os.Stdout, "Clean up")
			wg.Done()
			break L
		default:
			fmt.Fprintln(os.Stdout, "Call API")
			events, resp, err := client.Activity.ListEventsPerformedByUser(user, false, &github.ListOptions{PerPage: 300})
			if err != nil && resp.StatusCode != 304 {
				fmt.Fprintln(os.Stderr, err)
			}
			fmt.Fprintln(os.Stdout, resp.StatusCode)
			fmt.Fprintln(os.Stdout, events)
		}
		time.Sleep(3 * time.Second)
	}
}
Example #26
0
// VerifyCredential verifies whether the users DO credentials (access token) is
// valid or not
func (s *Stack) VerifyCredential(c *stack.Credential) error {
	cred := c.Credential.(*Credential)

	if err := cred.Valid(); err != nil {
		return err
	}

	oauthClient := oauth2.NewClient(
		oauth2.NoContext,
		oauth2.StaticTokenSource(&oauth2.Token{AccessToken: cred.AccessToken}),
	)

	client := godo.NewClient(oauthClient)

	// let's retrieve our Account information. If it's successful, we're good
	// to go
	_, _, err := client.Account.Get()
	if err != nil {
		return &stack.Error{
			Err: err,
		}
	}

	return nil
}
Example #27
0
func main() {

	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: GITHUB_TOKEN},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)

	githubClient := github.NewClient(tc)
	release, err := githubClient.Repositories.GetLatestRelease("FRC1360", "Stronghold2016")

	if err != nil {
		panic(err)
	}

	if downloadFromUrl(release.ZipballURL) {

		message := "Code Release Downloaded: " + release.AssetsURL + "\nSaved to Backup Server - Running copy cron job now."
		api := slack.New(SLACK_TOKEN)
		channel, err := api.FindChannelByName(SLACK_CHANNEL)
		if err != nil {
			panic(err)
		}
		err = api.ChatPostMessage(channel.Id, message, nil)
		if err != nil {
			panic(err)
		}
	}

}
Example #28
0
func ExampleRun() {
	kube, err := kubernetes.NewClient("example.com", &http.Client{
		Transport: &oauth2.Transport{
			Source: oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "aCcessWbU3toKen"}),
		}})
	if err != nil {
		log.Fatalf("failed to create client: %v", err)
	}
	kube.Run(&api.Pod{
		TypeMeta: api.TypeMeta{
			APIVersion: "v1beta3",
			Kind:       "Pod",
		},
		ObjectMeta: api.ObjectMeta{
			Name: "my-nginx-pod",
			Labels: map[string]string{
				"tag": "prod",
			},
		},
		Spec: api.PodSpec{
			Containers: []api.Container{
				{
					Name:  "my-nginx-container",
					Image: "nginx:latest",
				},
			},
		},
	})
}
Example #29
0
func init() {
	flag.BoolVar(&isVerbose, "v", false, "")
	flag.BoolVar(&isVerbose, "verbose", false, "")
	flag.Usage = func() {
		fmt.Println(path.Base(os.Args[0]), "- Print DNSPOD table changes")
		fmt.Println()
		fmt.Println("Options:")
		fmt.Println("    -v, --verbose    Show more output")
		fmt.Println()
		fmt.Println("Source: https://github.com/caiguanhao/dnspodd")
	}
	flag.Parse()

	githubClient = github.NewClient(oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: string(GITHUB_TOKEN)},
	)))

	if PROXY_URL != "" {
		proxy, err := url.Parse(PROXY_URL)
		if err == nil {
			http.DefaultTransport = &http.Transport{
				Proxy: func(req *http.Request) (*url.URL, error) {
					if req.URL.Host == "api.github.com" {
						return proxy, nil
					}
					return nil, nil
				},
			}
		}
	}
}
Example #30
0
func NewClient(token string, repo *Repo) *Client {
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(oauth2.NoContext, ts)
	return &Client{repo, github.NewClient(tc)}
}