Running gofmt on remote for bitbucket server

This commit is contained in:
Joachim Hill-Grannec 2016-04-20 11:59:47 -07:00
parent 262f505779
commit 95b1f347ba
4 changed files with 189 additions and 219 deletions

View file

@ -11,31 +11,29 @@ package bitbucketserver
// Needs a pem private key added to /var/lib/bitbucketserver/private_key.pem
// After that you should be good to go
import (
"net/url"
log "github.com/Sirupsen/logrus"
"net/http"
"github.com/drone/drone/model"
"fmt"
"io/ioutil"
"strconv"
"encoding/json"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/drone/drone/model"
"github.com/mrjones/oauth"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
type BitbucketServer struct {
URL string
URL string
ConsumerKey string
GitUserName string
GitPassword string
ConsumerRSA string
Open bool
Consumer oauth.Consumer
Open bool
Consumer oauth.Consumer
}
func Load(config string) *BitbucketServer{
func Load(config string) *BitbucketServer {
url_, err := url.Parse(config)
if err != nil {
@ -68,16 +66,12 @@ func Load(config string) *BitbucketServer{
bitbucketserver.Consumer = *NewClient(bitbucketserver.ConsumerRSA, bitbucketserver.ConsumerKey, bitbucketserver.URL)
return &bitbucketserver
}
func (bs *BitbucketServer) Login(res http.ResponseWriter, req *http.Request) (*model.User, bool, error){
func (bs *BitbucketServer) Login(res http.ResponseWriter, req *http.Request) (*model.User, bool, error) {
log.Info("Starting to login for bitbucketServer")
log.Info("getting the requestToken")
requestToken, url, err := bs.Consumer.GetRequestTokenAndUrl("oob")
if err != nil {
@ -94,7 +88,7 @@ func (bs *BitbucketServer) Login(res http.ResponseWriter, req *http.Request) (*m
var request_oauth_token = req.FormValue("oauth_token")
requestToken.Token = request_oauth_token
accessToken, err := bs.Consumer.AuthorizeToken(requestToken, code)
if err !=nil {
if err != nil {
log.Error(err)
}
@ -111,7 +105,7 @@ func (bs *BitbucketServer) Login(res http.ResponseWriter, req *http.Request) (*m
bits, err := ioutil.ReadAll(response.Body)
userName := string(bits)
response1, err := client.Get(bs.URL + "/rest/api/1.0/users/" +userName)
response1, err := client.Get(bs.URL + "/rest/api/1.0/users/" + userName)
contents, err := ioutil.ReadAll(response1.Body)
defer response1.Body.Close()
var mUser User
@ -124,7 +118,6 @@ func (bs *BitbucketServer) Login(res http.ResponseWriter, req *http.Request) (*m
user.Avatar = avatarLink(mUser.EmailAddress)
return &user, bs.Open, nil
}
@ -133,11 +126,11 @@ func (bs *BitbucketServer) Auth(token, secret string) (string, error) {
if len(token) == 0 {
return "", fmt.Errorf("Hasn't logged in yet")
}
return token, nil;
return token, nil
}
func (bs *BitbucketServer) Repo(u *model.User, owner, name string) (*model.Repo, error){
log.Info("Staring repo for bitbucketServer with user " + u.Login + " " + owner + " " + name )
func (bs *BitbucketServer) Repo(u *model.User, owner, name string) (*model.Repo, error) {
log.Info("Staring repo for bitbucketServer with user " + u.Login + " " + owner + " " + name)
client := NewClientWithToken(&bs.Consumer, u.Token)
@ -169,19 +162,17 @@ func (bs *BitbucketServer) Repo(u *model.User, owner, name string) (*model.Repo,
repo := &model.Repo{}
repo.Clone = cloneLink
repo.Link = repoLink
repo.Name=bsRepo.Slug
repo.Owner=bsRepo.Project.Key
repo.AllowPush=true
repo.FullName = bsRepo.Project.Key +"/" +bsRepo.Slug
repo.Name = bsRepo.Slug
repo.Owner = bsRepo.Project.Key
repo.AllowPush = true
repo.FullName = bsRepo.Project.Key + "/" + bsRepo.Slug
repo.Branch = "master"
repo.Kind = model.RepoGit
return repo, nil;
return repo, nil
}
func (bs *BitbucketServer) Repos(u *model.User) ([]*model.RepoLite, error){
func (bs *BitbucketServer) Repos(u *model.User) ([]*model.RepoLite, error) {
log.Info("Staring repos for bitbucketServer " + u.Login)
var repos = []*model.RepoLite{}
@ -199,16 +190,15 @@ func (bs *BitbucketServer) Repos(u *model.User) ([]*model.RepoLite, error){
for _, repo := range repoResponse.Values {
repos = append(repos, &model.RepoLite{
Name: repo.Slug,
FullName: repo.Project.Key + "/" + repo.Slug,
Owner: repo.Project.Key,
FullName: repo.Project.Key + "/" + repo.Slug,
Owner: repo.Project.Key,
})
}
return repos, nil;
return repos, nil
}
func (bs *BitbucketServer) Perm(u *model.User, owner, repo string) (*model.Perm, error){
func (bs *BitbucketServer) Perm(u *model.User, owner, repo string) (*model.Perm, error) {
//TODO: find the real permissions
log.Info("Staring perm for bitbucketServer")
@ -216,21 +206,21 @@ func (bs *BitbucketServer) Perm(u *model.User, owner, repo string) (*model.Perm,
perms.Pull = true
perms.Admin = true
perms.Push = true
return perms , nil
return perms, nil
}
func (bs *BitbucketServer) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error){
log.Info(fmt.Sprintf("Staring file for bitbucketServer login: %s repo: %s buildevent: %s string: %s",u.Login, r.Name, b.Event, f))
func (bs *BitbucketServer) File(u *model.User, r *model.Repo, b *model.Build, f string) ([]byte, error) {
log.Info(fmt.Sprintf("Staring file for bitbucketServer login: %s repo: %s buildevent: %s string: %s", u.Login, r.Name, b.Event, f))
client := NewClientWithToken(&bs.Consumer, u.Token)
fileURL := fmt.Sprintf("%s/projects/%s/repos/%s/browse/%s?raw", bs.URL,r.Owner,r.Name,f)
fileURL := fmt.Sprintf("%s/projects/%s/repos/%s/browse/%s?raw", bs.URL, r.Owner, r.Name, f)
log.Info(fileURL)
response, err := client.Get(fileURL)
if err != nil {
log.Error(err)
}
if response.StatusCode == 404 {
return nil,nil
return nil, nil
}
defer response.Body.Close()
responseBytes, err := ioutil.ReadAll(response.Body)
@ -238,16 +228,15 @@ func (bs *BitbucketServer) File(u *model.User, r *model.Repo, b *model.Build, f
log.Error(err)
}
return responseBytes, nil;
return responseBytes, nil
}
func (bs *BitbucketServer) Status(u *model.User, r *model.Repo, b *model.Build, link string) error{
func (bs *BitbucketServer) Status(u *model.User, r *model.Repo, b *model.Build, link string) error {
log.Info("Staring status for bitbucketServer")
return nil;
return nil
}
func (bs *BitbucketServer) Netrc(user *model.User, r *model.Repo) (*model.Netrc, error){
func (bs *BitbucketServer) Netrc(user *model.User, r *model.Repo) (*model.Netrc, error) {
log.Info("Starting the Netrc lookup")
u, err := url.Parse(bs.URL)
if err != nil {
@ -260,28 +249,28 @@ func (bs *BitbucketServer) Netrc(user *model.User, r *model.Repo) (*model.Netrc,
}, nil
}
func (bs *BitbucketServer) Activate(u *model.User, r *model.Repo, k *model.Key, link string) error{
log.Info(fmt.Sprintf("Staring activate for bitbucketServer user: %s repo: %s key: %s link: %s",u.Login,r.Name,k,link))
func (bs *BitbucketServer) Activate(u *model.User, r *model.Repo, k *model.Key, link string) error {
log.Info(fmt.Sprintf("Staring activate for bitbucketServer user: %s repo: %s key: %s link: %s", u.Login, r.Name, k, link))
client := NewClientWithToken(&bs.Consumer, u.Token)
hook, err := bs.CreateHook(client, r.Owner,r.Name, "com.atlassian.stash.plugin.stash-web-post-receive-hooks-plugin:postReceiveHook",link)
if err !=nil {
hook, err := bs.CreateHook(client, r.Owner, r.Name, "com.atlassian.stash.plugin.stash-web-post-receive-hooks-plugin:postReceiveHook", link)
if err != nil {
return err
}
log.Info(hook)
return nil;
return nil
}
func (bs *BitbucketServer) Deactivate(u *model.User, r *model.Repo, link string) error{
log.Info(fmt.Sprintf("Staring deactivating for bitbucketServer user: %s repo: %s link: %s",u.Login,r.Name,link))
func (bs *BitbucketServer) Deactivate(u *model.User, r *model.Repo, link string) error {
log.Info(fmt.Sprintf("Staring deactivating for bitbucketServer user: %s repo: %s link: %s", u.Login, r.Name, link))
client := NewClientWithToken(&bs.Consumer, u.Token)
err := bs.DeleteHook(client, r.Owner,r.Name, "com.atlassian.stash.plugin.stash-web-post-receive-hooks-plugin:postReceiveHook",link)
if err !=nil {
err := bs.DeleteHook(client, r.Owner, r.Name, "com.atlassian.stash.plugin.stash-web-post-receive-hooks-plugin:postReceiveHook", link)
if err != nil {
return err
}
return nil;
return nil
}
func (bs *BitbucketServer) Hook(r *http.Request) (*model.Repo, *model.Build, error){
func (bs *BitbucketServer) Hook(r *http.Request) (*model.Repo, *model.Build, error) {
log.Info("Staring hook for bitbucketServer")
defer r.Body.Close()
contents, err := ioutil.ReadAll(r.Body)
@ -292,8 +281,6 @@ func (bs *BitbucketServer) Hook(r *http.Request) (*model.Repo, *model.Build, err
var hookPost postHook
json.Unmarshal(contents, &hookPost)
buildModel := &model.Build{}
buildModel.Event = model.EventPush
buildModel.Ref = hookPost.RefChanges[0].RefID
@ -303,24 +290,22 @@ func (bs *BitbucketServer) Hook(r *http.Request) (*model.Repo, *model.Build, err
//All you really need is the name and owner. That's what creates the lookup key, so it needs to match the repo info. Just an FYI
repo := &model.Repo{}
repo.Name=hookPost.Repository.Slug
repo.Name = hookPost.Repository.Slug
repo.Owner = hookPost.Repository.Project.Key
repo.AllowTag=false
repo.AllowDeploy=false
repo.AllowPull=false
repo.AllowPush=true
repo.FullName = hookPost.Repository.Project.Key +"/" +hookPost.Repository.Slug
repo.AllowTag = false
repo.AllowDeploy = false
repo.AllowPull = false
repo.AllowPush = true
repo.FullName = hookPost.Repository.Project.Key + "/" + hookPost.Repository.Slug
repo.Branch = "master"
repo.Kind = model.RepoGit
return repo, buildModel, nil;
return repo, buildModel, nil
}
func (bs *BitbucketServer) String() string {
return "bitbucketserver"
}
type HookDetail struct {
Key string `"json:key"`
Name string `"json:name"`
@ -335,33 +320,26 @@ type Hook struct {
Details *HookDetail `"json:details"`
}
// Enable hook for named repository
func (bs *BitbucketServer)CreateHook(client *http.Client, project, slug, hook_key, link string) (*Hook, error) {
func (bs *BitbucketServer) CreateHook(client *http.Client, project, slug, hook_key, link string) (*Hook, error) {
// Set hook
hookBytes := []byte(fmt.Sprintf(`{"hook-url-0":"%s"}`,link))
hookBytes := []byte(fmt.Sprintf(`{"hook-url-0":"%s"}`, link))
// Enable hook
enablePath := fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/enabled",
project, slug, hook_key)
doPut(client, bs.URL + enablePath, hookBytes)
doPut(client, bs.URL+enablePath, hookBytes)
return nil, nil
}
// Disable hook for named repository
func (bs *BitbucketServer)DeleteHook(client *http.Client, project, slug, hook_key, link string) error {
func (bs *BitbucketServer) DeleteHook(client *http.Client, project, slug, hook_key, link string) error {
enablePath := fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/enabled",
project, slug, hook_key)
doDelete(client, bs.URL + enablePath)
doDelete(client, bs.URL+enablePath)
return nil
}

View file

@ -1,17 +1,16 @@
package bitbucketserver
import (
"net/http"
"crypto/tls"
log "github.com/Sirupsen/logrus"
"io/ioutil"
"encoding/pem"
"crypto/x509"
"encoding/pem"
log "github.com/Sirupsen/logrus"
"github.com/mrjones/oauth"
"io/ioutil"
"net/http"
)
func NewClient(ConsumerRSA string, ConsumerKey string, URL string) *oauth.Consumer{
func NewClient(ConsumerRSA string, ConsumerKey string, URL string) *oauth.Consumer {
//TODO: make this configurable
privateKeyFileContents, err := ioutil.ReadFile(ConsumerRSA)
log.Info("Tried to read the key")
@ -42,7 +41,7 @@ func NewClient(ConsumerRSA string, ConsumerKey string, URL string) *oauth.Consum
return c
}
func NewClientWithToken(Consumer *oauth.Consumer, AccessToken string) *http.Client{
func NewClientWithToken(Consumer *oauth.Consumer, AccessToken string) *http.Client {
var token oauth.AccessToken
token.Token = AccessToken
client, err := Consumer.MakeHttpClient(&token)
@ -51,9 +50,3 @@ func NewClientWithToken(Consumer *oauth.Consumer, AccessToken string) *http.Clie
}
return client
}

View file

@ -1,28 +1,28 @@
package bitbucketserver
import (
"net/http"
"bytes"
"io/ioutil"
"fmt"
"strings"
"crypto/md5"
log "github.com/Sirupsen/logrus"
"encoding/hex"
"fmt"
log "github.com/Sirupsen/logrus"
"io/ioutil"
"net/http"
"strings"
)
func avatarLink(email string) (url string) {
hasher := md5.New()
hasher.Write([]byte(strings.ToLower(email)))
emailHash := fmt.Sprintf("%v", hex.EncodeToString(hasher.Sum(nil)))
avatarURL := fmt.Sprintf("https://www.gravatar.com/avatar/%s.jpg",emailHash)
avatarURL := fmt.Sprintf("https://www.gravatar.com/avatar/%s.jpg", emailHash)
log.Info(avatarURL)
return avatarURL
}
func doPut(client *http.Client, url string, body []byte) {
request, err := http.NewRequest("PUT", url, bytes.NewBuffer(body))
request.Header.Add("Content-Type","application/json")
request.Header.Add("Content-Type", "application/json")
response, err := client.Do(request)
if err != nil {
log.Error(err)

View file

@ -1,64 +1,63 @@
package bitbucketserver
type postHook struct {
Changesets struct {
Filter interface{} `json:"filter"`
IsLastPage bool `json:"isLastPage"`
Limit int `json:"limit"`
Size int `json:"size"`
Start int `json:"start"`
Values []struct {
Changes struct {
Filter interface{} `json:"filter"`
IsLastPage bool `json:"isLastPage"`
Limit int `json:"limit"`
Size int `json:"size"`
Start int `json:"start"`
Values []struct {
ContentID string `json:"contentId"`
Executable bool `json:"executable"`
Link struct {
Rel string `json:"rel"`
URL string `json:"url"`
} `json:"link"`
NodeType string `json:"nodeType"`
Path struct {
Components []string `json:"components"`
Extension string `json:"extension"`
Name string `json:"name"`
Parent string `json:"parent"`
ToString string `json:"toString"`
} `json:"path"`
PercentUnchanged int `json:"percentUnchanged"`
SrcExecutable bool `json:"srcExecutable"`
Type string `json:"type"`
} `json:"values"`
} `json:"changes"`
FromCommit struct {
DisplayID string `json:"displayId"`
ID string `json:"id"`
} `json:"fromCommit"`
Link struct {
Rel string `json:"rel"`
URL string `json:"url"`
} `json:"link"`
ToCommit struct {
Author struct {
EmailAddress string `json:"emailAddress"`
Name string `json:"name"`
} `json:"author"`
AuthorTimestamp int `json:"authorTimestamp"`
DisplayID string `json:"displayId"`
ID string `json:"id"`
Message string `json:"message"`
Parents []struct {
DisplayID string `json:"displayId"`
ID string `json:"id"`
} `json:"parents"`
} `json:"toCommit"`
} `json:"values"`
} `json:"changesets"`
Filter interface{} `json:"filter"`
IsLastPage bool `json:"isLastPage"`
Limit int `json:"limit"`
Size int `json:"size"`
Start int `json:"start"`
Values []struct {
Changes struct {
Filter interface{} `json:"filter"`
IsLastPage bool `json:"isLastPage"`
Limit int `json:"limit"`
Size int `json:"size"`
Start int `json:"start"`
Values []struct {
ContentID string `json:"contentId"`
Executable bool `json:"executable"`
Link struct {
Rel string `json:"rel"`
URL string `json:"url"`
} `json:"link"`
NodeType string `json:"nodeType"`
Path struct {
Components []string `json:"components"`
Extension string `json:"extension"`
Name string `json:"name"`
Parent string `json:"parent"`
ToString string `json:"toString"`
} `json:"path"`
PercentUnchanged int `json:"percentUnchanged"`
SrcExecutable bool `json:"srcExecutable"`
Type string `json:"type"`
} `json:"values"`
} `json:"changes"`
FromCommit struct {
DisplayID string `json:"displayId"`
ID string `json:"id"`
} `json:"fromCommit"`
Link struct {
Rel string `json:"rel"`
URL string `json:"url"`
} `json:"link"`
ToCommit struct {
Author struct {
EmailAddress string `json:"emailAddress"`
Name string `json:"name"`
} `json:"author"`
AuthorTimestamp int `json:"authorTimestamp"`
DisplayID string `json:"displayId"`
ID string `json:"id"`
Message string `json:"message"`
Parents []struct {
DisplayID string `json:"displayId"`
ID string `json:"id"`
} `json:"parents"`
} `json:"toCommit"`
} `json:"values"`
} `json:"changesets"`
RefChanges []struct {
FromHash string `json:"fromHash"`
RefID string `json:"refId"`
@ -66,23 +65,23 @@ type postHook struct {
Type string `json:"type"`
} `json:"refChanges"`
Repository struct {
Forkable bool `json:"forkable"`
ID int `json:"id"`
Name string `json:"name"`
Project struct {
ID int `json:"id"`
IsPersonal bool `json:"isPersonal"`
Key string `json:"key"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Public bool `json:"public"`
ScmID string `json:"scmId"`
Slug string `json:"slug"`
State string `json:"state"`
StatusMessage string `json:"statusMessage"`
} `json:"repository"`
Forkable bool `json:"forkable"`
ID int `json:"id"`
Name string `json:"name"`
Project struct {
ID int `json:"id"`
IsPersonal bool `json:"isPersonal"`
Key string `json:"key"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Public bool `json:"public"`
ScmID string `json:"scmId"`
Slug string `json:"slug"`
State string `json:"state"`
StatusMessage string `json:"statusMessage"`
} `json:"repository"`
}
type Repos struct {
@ -94,28 +93,28 @@ type Repos struct {
Forkable bool `json:"forkable"`
ID int `json:"id"`
Links struct {
Clone []struct {
Href string `json:"href"`
Name string `json:"name"`
} `json:"clone"`
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Clone []struct {
Href string `json:"href"`
Name string `json:"name"`
} `json:"clone"`
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Project struct {
Description string `json:"description"`
ID int `json:"id"`
Key string `json:"key"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Description string `json:"description"`
ID int `json:"id"`
Key string `json:"key"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Public bool `json:"public"`
ScmID string `json:"scmId"`
Slug string `json:"slug"`
@ -130,10 +129,10 @@ type User struct {
EmailAddress string `json:"emailAddress"`
ID int `json:"id"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Slug string `json:"slug"`
Type string `json:"type"`
@ -143,28 +142,28 @@ type BSRepo struct {
Forkable bool `json:"forkable"`
ID int `json:"id"`
Links struct {
Clone []struct {
Href string `json:"href"`
Name string `json:"name"`
} `json:"clone"`
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Clone []struct {
Href string `json:"href"`
Name string `json:"name"`
} `json:"clone"`
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Project struct {
Description string `json:"description"`
ID int `json:"id"`
Key string `json:"key"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Description string `json:"description"`
ID int `json:"id"`
Key string `json:"key"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Name string `json:"name"`
Public bool `json:"public"`
Type string `json:"type"`
} `json:"project"`
Public bool `json:"public"`
ScmID string `json:"scmId"`
Slug string `json:"slug"`