reuse more existing code

This commit is contained in:
Ulrich Schreiner
2015-02-06 12:02:02 +01:00
parent f57640a402
commit 7b02878df9
2 changed files with 19 additions and 24 deletions

View File

@@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"regexp" "regexp"
"strings"
"time" "time"
"github.com/drone/drone/shared/httputil" "github.com/drone/drone/shared/httputil"
@@ -251,18 +250,12 @@ func (r *Bitbucket) Deactivate(user *model.User, repo *model.Repo, link string)
user.Access, user.Access,
user.Secret, user.Secret,
) )
title, err := GetKeyTitle(link)
if keys, err := client.RepoKeys.List(repo.Owner, repo.Name); err == nil { if err != nil {
repokey := strings.TrimSpace(repo.PublicKey) return err
for _, k := range keys { }
if k.Key == repokey { if err := client.RepoKeys.DeleteName(repo.Owner, repo.Name, title); err != nil {
if err := client.RepoKeys.Delete(repo.Owner, repo.Name, k.Id); err != nil { return err
return err
} else {
break
}
}
}
} }
return client.Brokers.DeleteUrl(repo.Owner, repo.Name, link, bitbucket.BrokerTypePost) return client.Brokers.DeleteUrl(repo.Owner, repo.Name, link, bitbucket.BrokerTypePost)
} }
@@ -305,3 +298,13 @@ func (r *Bitbucket) OpenRegistration() bool {
func (r *Bitbucket) GetToken(user *model.User) (*model.Token, error) { func (r *Bitbucket) GetToken(user *model.User) (*model.Token, error) {
return nil, nil return nil, nil
} }
// GetKeyTitle is a helper function that generates a title for the
// RSA public key based on the username and domain name.
func GetKeyTitle(rawurl string) (string, error) {
var uri, err = url.Parse(rawurl)
if err != nil {
return "", err
}
return fmt.Sprintf("drone@%s", uri.Host), nil
}

View File

@@ -243,19 +243,11 @@ func GetKeyTitle(rawurl string) (string, error) {
// DeleteKey is a helper function that deletes a deploy key // DeleteKey is a helper function that deletes a deploy key
// for the specified repository. // for the specified repository.
func DeleteKey(client *github.Client, owner, name, title, key string) error { func DeleteKey(client *github.Client, owner, name, title, key string) error {
var k = new(github.Key) var k, _ = GetKey(client, owner, name, title)
k.Title = github.String(title) if k != nil {
k.Key = github.String(key) _, err := client.Repositories.DeleteKey(owner, name, *k.ID)
keys, _, err := client.Repositories.ListKeys(owner, name, nil)
if err != nil {
return err return err
} }
for _, rk := range keys {
if rk.Key != nil && rk.Key == k.Key {
_, err = client.Repositories.DeleteKey(owner, name, *rk.ID)
return err
}
}
return nil return nil
} }