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/url"
"regexp"
"strings"
"time"
"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.Secret,
)
if keys, err := client.RepoKeys.List(repo.Owner, repo.Name); err == nil {
repokey := strings.TrimSpace(repo.PublicKey)
for _, k := range keys {
if k.Key == repokey {
if err := client.RepoKeys.Delete(repo.Owner, repo.Name, k.Id); err != nil {
return err
} else {
break
}
}
}
title, err := GetKeyTitle(link)
if err != nil {
return err
}
if err := client.RepoKeys.DeleteName(repo.Owner, repo.Name, title); err != nil {
return err
}
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) {
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
// for the specified repository.
func DeleteKey(client *github.Client, owner, name, title, key string) error {
var k = new(github.Key)
k.Title = github.String(title)
k.Key = github.String(key)
keys, _, err := client.Repositories.ListKeys(owner, name, nil)
if err != nil {
var k, _ = GetKey(client, owner, name, title)
if k != nil {
_, err := client.Repositories.DeleteKey(owner, name, *k.ID)
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
}