Skip to content

Commit

Permalink
+ replacing variables in build files {{user}} {{repo}} {{branch}} {{s…
Browse files Browse the repository at this point in the history
…ha}}
  • Loading branch information
AlexKomrakov committed Sep 13, 2015
1 parent 950f829 commit 799333d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .config.yml → config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ adress: 0.0.0.0:9000
deployfile: .deploy.yml
sessionsecretkey: kurf56ys
logs:
error: error.log
gohub: info.log
error: error.log
gohub: info.log
oauth:
clientid: 95516ec894645a4366f2
clientsecret: fee08040cfd8aafba8c3ff6ca2b85a084cc836de
Expand Down
37 changes: 25 additions & 12 deletions service/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,48 @@ package service
import (
"errors"
"bytes"
"strings"
"github.com/google/go-github/github"
"github.com/alexkomrakov/gohub/mongo"
)

func ProcessHook(event, body string) {
var user, repo, sha, branch string
var params map[string]string

switch event {
case "pull_request":
pullRequestEvent, _ := ParsePullRequestEvent(body)
user = *pullRequestEvent.Repo.Owner.Login
repo = *pullRequestEvent.Repo.Name
sha = *pullRequestEvent.PullRequest.Head.SHA
params["user"] = *pullRequestEvent.Repo.Owner.Login
params["repo"] = *pullRequestEvent.Repo.Name
params["sha"] = *pullRequestEvent.PullRequest.Head.SHA
case "push":
pushEvent, _ := ParsePushEvent(body)
user = *pushEvent.Repo.Owner.Name
repo = *pushEvent.Repo.Name
sha = *pushEvent.After
branch = *pushEvent.Ref
params["user"] = *pushEvent.Repo.Owner.Name
params["repo"] = *pushEvent.Repo.Name
params["sha"] = *pushEvent.After
params["branch"] = *pushEvent.Ref
default:
panic("Not supported event: " + event)
}
token := mongo.GetToken(user)
body = ReplaceVariables(params, body)

token := mongo.GetToken(params["user"])
client := GetGithubClient(token)
file, _ := GetFileContent(client, user, repo, sha, GetServerConfig().DeployFile)
file, _ := GetFileContent(client, params["user"], params["repo"], params["sha"], GetServerConfig().DeployFile)
deploy, _ := GetYamlConfig(file)

if deploy[event].Branch == "" || deploy[event].Branch == branch {
RunCommands(deploy, client, event, mongo.CommitCredentials{mongo.RepositoryCredentials{user, repo}, sha})
if deploy[event].Branch == "" || deploy[event].Branch == params["branch"] {
RunCommands(deploy, client, event, mongo.CommitCredentials{mongo.RepositoryCredentials{params["user"], params["repo"]}, params["sha"]})
}
}

func ReplaceVariables(params map[string]string, text string) string {
for variable, value := range params {
r := strings.NewReplacer("{{" + variable + "}}", value)
text = r.Replace(text)
}

return text
}

func RunCommands(deploy map[string]mongo.DeployScenario, client *github.Client, event string, commit_credentials mongo.CommitCredentials) (build mongo.Build) {
Expand Down
4 changes: 1 addition & 3 deletions service/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

const (
config_file = ".config.yml"
config_file = "config.yml"
)

type ServerConfig struct {
Expand All @@ -25,8 +25,6 @@ type ServerConfig struct {
Events []string
}



func GetServerConfig() (config ServerConfig) {
b, err := ioutil.ReadFile(config_file)
if err != nil {
Expand Down

0 comments on commit 799333d

Please sign in to comment.