Skip to content

Commit

Permalink
remove state from builder
Browse files Browse the repository at this point in the history
  • Loading branch information
bivas committed Jul 10, 2017
1 parent 8d618e4 commit 3820476
Showing 1 changed file with 43 additions and 49 deletions.
92 changes: 43 additions & 49 deletions bot/connector/github/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,33 @@ var (
"pull_request_review_comment"}
)

type eventDataBuilder struct {
type builderContext struct {
secret []byte
client *ghClient
data *eventData
}

func (builder *eventDataBuilder) validate(payload []byte, request *http.Request) bool {
if len(builder.secret) == 0 {
type eventDataBuilder struct {
}

func (builder *eventDataBuilder) validate(context *builderContext, payload []byte, request *http.Request) bool {
if len(context.secret) == 0 {
return true
}
h := hmac.New(sha1.New, builder.secret)
h := hmac.New(sha1.New, context.secret)
h.Write(payload)
result := fmt.Sprintf("sha1=%s", hex.EncodeToString(h.Sum(nil)))
return request.Header.Get("X-Hub-Signature") == result
}

func (builder *eventDataBuilder) readPayload(r *http.Request) (*payload, []byte, error) {
func (builder *eventDataBuilder) readPayload(context *builderContext, r *http.Request) (*payload, []byte, error) {
body := r.Body
defer body.Close()
raw, err := ioutil.ReadAll(io.LimitReader(body, r.ContentLength))
if err != nil {
return nil, raw, err
}
if !builder.validate(raw, r) {
if !builder.validate(context, raw, r) {
return nil, raw, fmt.Errorf("Payload could not be validated")
}
var pr payload
Expand All @@ -58,35 +61,35 @@ func (builder *eventDataBuilder) readPayload(r *http.Request) (*payload, []byte,
return &pr, raw, nil
}

func (builder *eventDataBuilder) readFromJson(payload *payload) {
builder.data.number = payload.Number
builder.data.title = payload.PullRequest.Title
builder.data.changedFiles = payload.PullRequest.ChangedFiles
builder.data.additions = payload.PullRequest.Additions
builder.data.deletions = payload.PullRequest.Deletions
builder.data.ref = payload.PullRequest.Base.Ref
builder.data.origin = strings.ToLower(payload.PullRequest.User.Login)
builder.data.state = payload.PullRequest.State
func (builder *eventDataBuilder) readFromJson(context *builderContext, payload *payload) {
context.data.number = payload.Number
context.data.title = payload.PullRequest.Title
context.data.changedFiles = payload.PullRequest.ChangedFiles
context.data.additions = payload.PullRequest.Additions
context.data.deletions = payload.PullRequest.Deletions
context.data.ref = payload.PullRequest.Base.Ref
context.data.origin = strings.ToLower(payload.PullRequest.User.Login)
context.data.state = payload.PullRequest.State
}

func (builder *eventDataBuilder) readFromClient() {
id := builder.data.number
builder.data.assignees = builder.client.GetAssignees(id)
builder.data.state = builder.client.GetState(id)
builder.data.labels = builder.client.GetLabels(id)
builder.data.comments = builder.client.GetComments(id)
fileNames := builder.client.GetFileNames(id)
builder.data.fileNames = fileNames
func (builder *eventDataBuilder) readFromClient(context *builderContext) {
id := context.data.number
context.data.assignees = context.client.GetAssignees(id)
context.data.state = context.client.GetState(id)
context.data.labels = context.client.GetLabels(id)
context.data.comments = context.client.GetComments(id)
fileNames := context.client.GetFileNames(id)
context.data.fileNames = fileNames
stringSet := util.StringSet{Transformer: filepath.Ext}
builder.data.fileExt = stringSet.AddAll(fileNames).Values()
context.data.fileExt = stringSet.AddAll(fileNames).Values()
}

func (builder *eventDataBuilder) checkProcessState() bool {
func (builder *eventDataBuilder) checkProcessState(context *builderContext) bool {
util.Logger.Debug("Current issue [(%d) %s] state is '%s'",
builder.data.GetNumber(),
builder.data.GetTitle(),
builder.data.state)
return builder.data.state != "closed"
context.data.GetNumber(),
context.data.GetTitle(),
context.data.state)
return context.data.state != "closed"
}

func (builder *eventDataBuilder) PartialBuildFromRequest(config bot.ClientConfig, r *http.Request) (bot.EventData, bool, error) {
Expand All @@ -105,8 +108,8 @@ func (builder *eventDataBuilder) PartialBuildFromRequest(config bot.ClientConfig
util.Logger.Debug("Got GitHub '%s' event", githubEvent)
return nil, false, nil
}
builder.secret = []byte(config.GetSecret())
pl, raw, err := builder.readPayload(r)
context := &builderContext{secret: []byte(config.GetSecret())}
pl, raw, err := builder.readPayload(context, r)
if err != nil {
return nil, false, err
}
Expand All @@ -117,22 +120,13 @@ func (builder *eventDataBuilder) PartialBuildFromRequest(config bot.ClientConfig
}
repo := pl.Repository.Name
owner := pl.Repository.Owner.Login
builder.data = &eventData{owner: owner, repo: repo, payload: raw}
builder.readFromJson(pl)
return builder.data, builder.checkProcessState(), nil
context.data = &eventData{owner: owner, repo: repo, payload: raw}
builder.readFromJson(context, pl)
return context.data, builder.checkProcessState(context), nil
}

func (builder *eventDataBuilder) BuildFromRequest(config bot.ClientConfig, r *http.Request) (bot.EventData, bool, error) {
_, ok, err := builder.PartialBuildFromRequest(config, r)
if !ok || err != nil {
return nil, ok, err
}
repo := builder.data.repo
owner := builder.data.owner
builder.client = newClient(config, owner, repo)
builder.data.client = builder.client
builder.readFromClient()
return builder.data, builder.checkProcessState(), nil
panic("Don't use anymore")
}

func (builder *eventDataBuilder) BuildFromPayload(config bot.ClientConfig, raw []byte) (bot.EventData, bool, error) {
Expand All @@ -147,11 +141,11 @@ func (builder *eventDataBuilder) BuildFromPayload(config bot.ClientConfig, raw [
}
repo := pl.Repository.Name
owner := pl.Repository.Owner.Login
builder.client = newClient(config, owner, repo)
builder.data = &eventData{owner: owner, repo: repo, payload: raw, client: builder.client}
builder.readFromJson(&pl)
builder.readFromClient()
return builder.data, builder.checkProcessState(), nil
context := &builderContext{client: newClient(config, owner, repo)}
context.data = &eventData{owner: owner, repo: repo, payload: raw, client: context.client}
builder.readFromJson(context, &pl)
builder.readFromClient(context)
return context.data, builder.checkProcessState(context), nil
}

func init() {
Expand Down

0 comments on commit 3820476

Please sign in to comment.