2014-06-04 21:25:38 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-06-09 22:47:35 +00:00
|
|
|
"net/url"
|
2014-06-04 21:25:38 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
"code.google.com/p/goauth2/oauth"
|
2014-06-12 22:28:05 +00:00
|
|
|
"github.com/drone/drone/shared/httputil"
|
2014-09-02 07:18:17 +00:00
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
"github.com/drone/go-github/github"
|
|
|
|
)
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
const (
|
|
|
|
DefaultAPI = "https://api.github.com/"
|
|
|
|
DefaultURL = "https://github.com"
|
|
|
|
DefaultScope = "repo,repo:status,user:email"
|
2014-06-04 21:25:38 +00:00
|
|
|
)
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
type GitHub struct {
|
|
|
|
URL string
|
|
|
|
API string
|
|
|
|
Client string
|
|
|
|
Secret string
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
func New(url, api, client, secret string) *GitHub {
|
|
|
|
var github = GitHub{
|
|
|
|
URL: url,
|
|
|
|
API: api,
|
|
|
|
Client: client,
|
|
|
|
Secret: secret,
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
2014-09-02 07:18:17 +00:00
|
|
|
// the API must have a trailing slash
|
|
|
|
if !strings.HasSuffix(github.API, "/") {
|
|
|
|
github.API += "/"
|
|
|
|
}
|
|
|
|
// the URL must NOT have a trailing slash
|
|
|
|
if strings.HasSuffix(github.URL, "/") {
|
|
|
|
github.URL = github.URL[:len(github.URL)-1]
|
|
|
|
}
|
|
|
|
return &github
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDefault(client, secret string) *GitHub {
|
|
|
|
return New(DefaultURL, DefaultAPI, client, secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Authorize handles GitHub API Authorization.
|
|
|
|
func (r *GitHub) Authorize(res http.ResponseWriter, req *http.Request) (*model.Login, error) {
|
|
|
|
var config = &oauth.Config{
|
|
|
|
ClientId: r.Client,
|
|
|
|
ClientSecret: r.Secret,
|
|
|
|
Scope: DefaultScope,
|
|
|
|
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", r.URL),
|
|
|
|
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", r.URL),
|
|
|
|
RedirectURL: fmt.Sprintf("%s/login/%s", httputil.GetURL(req), r.GetKind()),
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the OAuth code
|
|
|
|
var code = req.FormValue("code")
|
|
|
|
var state = req.FormValue("state")
|
|
|
|
if len(code) == 0 {
|
|
|
|
var random = GetRandom()
|
|
|
|
httputil.SetCookie(res, req, "github_state", random)
|
|
|
|
http.Redirect(res, req, config.AuthCodeURL(random), http.StatusSeeOther)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cookieState := httputil.GetCookie(req, "github_state")
|
|
|
|
httputil.DelCookie(res, req, "github_state")
|
|
|
|
if cookieState != state {
|
|
|
|
return nil, fmt.Errorf("Error matching state in OAuth2 redirect")
|
|
|
|
}
|
|
|
|
|
|
|
|
var trans = &oauth.Transport{Config: config}
|
|
|
|
var token, err = trans.Exchange(code)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = NewClient(r.API, token.AccessToken)
|
|
|
|
var useremail, errr = GetUserEmail(client)
|
|
|
|
if errr != nil {
|
|
|
|
return nil, errr
|
|
|
|
}
|
|
|
|
|
|
|
|
var login = new(model.Login)
|
|
|
|
login.ID = int64(*useremail.ID)
|
|
|
|
login.Access = token.AccessToken
|
|
|
|
login.Login = *useremail.Login
|
|
|
|
login.Name = *useremail.Name
|
|
|
|
login.Email = *useremail.Email
|
|
|
|
return login, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetKind returns the internal identifier of this remote GitHub instane.
|
|
|
|
func (r *GitHub) GetKind() string {
|
|
|
|
if r.IsEnterprise() {
|
|
|
|
return model.RemoteGithubEnterprise
|
|
|
|
} else {
|
|
|
|
return model.RemoteGithub
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHost returns the hostname of this remote GitHub instance.
|
|
|
|
func (r *GitHub) GetHost() string {
|
|
|
|
uri, _ := url.Parse(r.URL)
|
|
|
|
return uri.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEnterprise returns true if the remote system is an
|
|
|
|
// instance of GitHub Enterprise Edition.
|
|
|
|
func (r *GitHub) IsEnterprise() bool {
|
|
|
|
return r.URL != DefaultURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepos fetches all repositories that the specified
|
|
|
|
// user has access to in the remote system.
|
|
|
|
func (r *GitHub) GetRepos(user *model.User) ([]*model.Repo, error) {
|
|
|
|
var repos []*model.Repo
|
|
|
|
var client = NewClient(r.API, user.Access)
|
|
|
|
var list, err = GetAllRepos(client)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var remote = r.GetKind()
|
|
|
|
var hostname = r.GetHost()
|
|
|
|
var enterprise = r.IsEnterprise()
|
|
|
|
|
|
|
|
for _, item := range list {
|
|
|
|
var repo = model.Repo{
|
|
|
|
UserID: user.ID,
|
|
|
|
Remote: remote,
|
|
|
|
Host: hostname,
|
|
|
|
Owner: *item.Owner.Login,
|
|
|
|
Name: *item.Name,
|
|
|
|
Private: *item.Private,
|
2014-09-03 07:23:36 +00:00
|
|
|
URL: *item.HTMLURL,
|
2014-09-02 07:18:17 +00:00
|
|
|
CloneURL: *item.GitURL,
|
|
|
|
GitURL: *item.GitURL,
|
|
|
|
SSHURL: *item.SSHURL,
|
|
|
|
Role: &model.Perm{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if enterprise || repo.Private {
|
|
|
|
repo.CloneURL = *item.SSHURL
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no permissions we should skip the repository
|
|
|
|
// entirely, since this should never happen
|
|
|
|
if item.Permissions == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
repo.Role.Admin = (*item.Permissions)["admin"]
|
|
|
|
repo.Role.Write = (*item.Permissions)["push"]
|
|
|
|
repo.Role.Read = (*item.Permissions)["pull"]
|
|
|
|
repos = append(repos, &repo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return repos, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetScript fetches the build script (.drone.yml) from the remote
|
|
|
|
// repository and returns in string format.
|
|
|
|
func (r *GitHub) GetScript(user *model.User, repo *model.Repo, hook *model.Hook) ([]byte, error) {
|
|
|
|
var client = NewClient(r.API, user.Access)
|
|
|
|
return GetFile(client, repo.Owner, repo.Name, ".drone.yml", hook.Sha)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// Activate activates a repository by adding a Post-commit hook and
|
|
|
|
// a Public Deploy key, if applicable.
|
|
|
|
func (r *GitHub) Activate(user *model.User, repo *model.Repo, link string) error {
|
|
|
|
var client = NewClient(r.API, user.Access)
|
|
|
|
var title, err = GetKeyTitle(link)
|
2014-06-09 22:47:35 +00:00
|
|
|
if err != nil {
|
2014-09-02 07:18:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the CloneURL is using the SSHURL then we know that
|
|
|
|
// we need to add an SSH key to GitHub.
|
|
|
|
if repo.SSHURL == repo.CloneURL {
|
|
|
|
_, err = CreateUpdateKey(client, repo.Owner, repo.Name, title, repo.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-06-09 22:47:35 +00:00
|
|
|
}
|
2014-09-02 07:18:17 +00:00
|
|
|
|
|
|
|
_, err = CreateUpdateHook(client, repo.Owner, repo.Name, link)
|
|
|
|
return err
|
2014-06-09 22:47:35 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// ParseHook parses the post-commit hook from the Request body
|
2014-06-04 21:25:38 +00:00
|
|
|
// and returns the required data in a standard format.
|
2014-09-02 07:18:17 +00:00
|
|
|
func (r *GitHub) ParseHook(req *http.Request) (*model.Hook, error) {
|
2014-06-04 21:25:38 +00:00
|
|
|
// handle github ping
|
2014-09-02 07:18:17 +00:00
|
|
|
if req.Header.Get("X-Github-Event") == "ping" {
|
2014-06-04 21:25:38 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle github pull request hook differently
|
2014-09-02 07:18:17 +00:00
|
|
|
if req.Header.Get("X-Github-Event") == "pull_request" {
|
|
|
|
return r.ParsePullRequestHook(req)
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get the payload of the message
|
2014-09-02 07:18:17 +00:00
|
|
|
var payload = req.FormValue("payload")
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// parse the github Hook payload
|
2014-09-02 07:18:17 +00:00
|
|
|
var data, err = github.ParseHook([]byte(payload))
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure this is being triggered because of a commit
|
|
|
|
// and not something like a tag deletion or whatever
|
2014-09-02 07:18:17 +00:00
|
|
|
if data.IsTag() ||
|
|
|
|
data.IsGithubPages() ||
|
|
|
|
data.IsHead() == false ||
|
|
|
|
data.IsDeleted() {
|
2014-06-04 21:25:38 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
var hook = new(model.Hook)
|
2014-06-04 21:25:38 +00:00
|
|
|
hook.Repo = data.Repo.Name
|
|
|
|
hook.Owner = data.Repo.Owner.Login
|
|
|
|
hook.Sha = data.Head.Id
|
|
|
|
hook.Branch = data.Branch()
|
|
|
|
|
2014-06-12 19:44:19 +00:00
|
|
|
if len(hook.Owner) == 0 {
|
|
|
|
hook.Owner = data.Repo.Owner.Name
|
|
|
|
}
|
|
|
|
|
2014-06-04 21:25:38 +00:00
|
|
|
// extract the author and message from the commit
|
|
|
|
// this is kind of experimental, since I don't know
|
|
|
|
// what I'm doing here.
|
|
|
|
if data.Head != nil && data.Head.Author != nil {
|
|
|
|
hook.Message = data.Head.Message
|
|
|
|
hook.Timestamp = data.Head.Timestamp
|
|
|
|
hook.Author = data.Head.Author.Email
|
|
|
|
} else if data.Commits != nil && len(data.Commits) > 0 && data.Commits[0].Author != nil {
|
|
|
|
hook.Message = data.Commits[0].Message
|
|
|
|
hook.Timestamp = data.Commits[0].Timestamp
|
|
|
|
hook.Author = data.Commits[0].Author.Email
|
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
return hook, nil
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 07:18:17 +00:00
|
|
|
// ParsePullRequestHook parses the pull request hook from the Request body
|
|
|
|
// and returns the required data in a standard format.
|
|
|
|
func (r *GitHub) ParsePullRequestHook(req *http.Request) (*model.Hook, error) {
|
|
|
|
var payload = req.FormValue("payload")
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// parse the payload to retrieve the pull-request
|
|
|
|
// hook meta-data.
|
2014-09-02 07:18:17 +00:00
|
|
|
var data, err = github.ParsePullRequestHook([]byte(payload))
|
2014-06-04 21:25:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore these
|
|
|
|
if data.Action != "opened" && data.Action != "synchronize" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO we should also store the pull request branch (ie from x to y)
|
|
|
|
// we can find it here: data.PullRequest.Head.Ref
|
2014-09-02 07:18:17 +00:00
|
|
|
var hook = model.Hook{
|
2014-06-04 21:25:38 +00:00
|
|
|
Owner: data.Repo.Owner.Login,
|
|
|
|
Repo: data.Repo.Name,
|
|
|
|
Sha: data.PullRequest.Head.Sha,
|
|
|
|
Branch: data.PullRequest.Base.Ref,
|
|
|
|
Author: data.PullRequest.User.Login,
|
|
|
|
Gravatar: data.PullRequest.User.GravatarId,
|
|
|
|
Timestamp: time.Now().UTC().String(),
|
|
|
|
Message: data.PullRequest.Title,
|
|
|
|
PullRequest: strconv.Itoa(data.Number),
|
2014-06-12 19:44:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(hook.Owner) == 0 {
|
|
|
|
hook.Owner = data.Repo.Owner.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return &hook, nil
|
2014-06-04 21:25:38 +00:00
|
|
|
}
|