github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/mungegithub/github/webhook.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package github
    18  
    19  import (
    20  	"io/ioutil"
    21  	"net/http"
    22  	"strings"
    23  
    24  	"github.com/golang/glog"
    25  	"github.com/google/go-github/github"
    26  )
    27  
    28  // WebHook listen for events and list changed issues asynchronously
    29  type WebHook struct {
    30  	GithubKey string `json:"-"`
    31  	Status    *StatusChange
    32  }
    33  
    34  // ServeHTTP receives the webhook, and process it
    35  func (webhook WebHook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    36  	payload, err := github.ValidatePayload(r, []byte(webhook.GithubKey))
    37  	if err != nil {
    38  		glog.Error(err)
    39  		http.Error(w, "Failed to validate payload", 400)
    40  		return
    41  	}
    42  	event, err := github.ParseWebHook(github.WebHookType(r), payload)
    43  	if err != nil {
    44  		glog.Error(err)
    45  		http.Error(w, "Failed to parse event", 400)
    46  		return
    47  
    48  	}
    49  
    50  	switch event := event.(type) {
    51  	case *github.StatusEvent:
    52  		if event.Commit != nil && event.Commit.SHA != nil {
    53  			webhook.Status.CommitStatusChanged(*event.Commit.SHA)
    54  		}
    55  	}
    56  }
    57  
    58  // HTTPHandlerInstaller is anything that can hook up HTTP requests to handlers.
    59  type HTTPHandlerInstaller interface {
    60  	Handle(pattern string, handler http.Handler)
    61  }
    62  
    63  // NewWebHookAndListen creates a new WebHook and listen to it
    64  func NewWebHookAndListen(githubKeyFile string, server HTTPHandlerInstaller) *WebHook {
    65  	data, err := ioutil.ReadFile(githubKeyFile)
    66  	if err != nil {
    67  		glog.Fatalf("Error reading github webhook secret file '%s': %v", githubKeyFile, err)
    68  	}
    69  	githubKey := strings.TrimSpace(string(data))
    70  
    71  	webhook := WebHook{
    72  		Status:    NewStatusChange(),
    73  		GithubKey: githubKey,
    74  	}
    75  
    76  	server.Handle("/webhook", webhook)
    77  
    78  	return &webhook
    79  }
    80  
    81  // UpdatePullRequest will add the pull-request's last commit
    82  func (webhook *WebHook) UpdatePullRequest(id int, head string) {
    83  	webhook.Status.UpdatePullRequestHead(id, head)
    84  }
    85  
    86  // PopIssues returns the list of issues that changed since last time it was called
    87  func (webhook *WebHook) PopIssues() []int {
    88  	return webhook.Status.PopChangedPullRequests()
    89  }