예제 #1
0
파일: client.go 프로젝트: jingweno/gh
func (client *Client) FindOrCreateToken(user, password, twoFactorCode string) (token string, err error) {
	url, e := octokit.AuthorizationsURL.Expand(nil)
	if e != nil {
		err = &ClientError{e}
		return
	}

	basicAuth := octokit.BasicAuth{Login: user, Password: password, OneTimePassword: twoFactorCode}
	c := octokit.NewClientWith(client.apiEndpoint(), nil, basicAuth)
	authsService := c.Authorizations(client.requestURL(url))

	if twoFactorCode != "" {
		// dummy request to trigger a 2FA SMS since a HTTP GET won't do it
		authsService.Create(nil)
	}

	auths, result := authsService.All()
	if result.HasError() {
		err = &ClientError{result.Err}
		return
	}

	for _, auth := range auths {
		if auth.App.URL == OAuthAppURL {
			token = auth.Token
			break
		}
	}

	if token == "" {
		authParam := octokit.AuthorizationParams{}
		authParam.Scopes = append(authParam.Scopes, "repo")
		authParam.Note = "gh"
		authParam.NoteURL = OAuthAppURL

		auth, result := authsService.Create(authParam)
		if result.HasError() {
			err = &ClientError{result.Err}
			return
		}

		token = auth.Token
	}

	return
}
예제 #2
0
파일: client.go 프로젝트: pengwynn/gh
func (client *Client) GhLatestTagName() (tagName string, err error) {
	url, err := octokit.ReleasesURL.Expand(octokit.M{"owner": "jingweno", "repo": "gh"})
	if err != nil {
		return
	}

	c := octokit.NewClientWith(client.apiEndpoint(), nil, nil)
	releases, result := c.Releases(client.requestURL(url)).All()
	if result.HasError() {
		err = fmt.Errorf("Error getting gh release: %s", result.Err)
		return
	}

	if len(releases) == 0 {
		err = fmt.Errorf("No gh release is available")
		return
	}

	tagName = releases[0].TagName

	return
}
예제 #3
0
파일: client.go 프로젝트: pengwynn/gh
func (client *Client) octokit() (c *octokit.Client) {
	tokenAuth := octokit.TokenAuth{AccessToken: client.Credentials.AccessToken}
	c = octokit.NewClientWith(client.apiEndpoint(), nil, tokenAuth)

	return
}