Ejemplo n.º 1
0
func (h *Handler) HandleMessage(m *nsq.Message) error {
	prHook, err := octokat.ParsePullRequestHook(m.Body)
	if err == nil {
		return h.handlePullRequest(prHook)
	}

	// if there was an error it means
	// it wasnt an Issue or Pull Request Hook
	// so we don't care about it
	return nil
}
Ejemplo n.º 2
0
// HandleMessage reads the nsq message body and parses it as a github webhook
func (h *Handler) HandleMessage(m *nsq.Message) error {
	prHook, err := octokat.ParsePullRequestHook(m.Body)
	if err != nil {
		// if there was an error it means
		// it wasnt an Issue or Pull Request Hook
		// so we don't care about it
		return nil
	}

	// we only want the prs that are opened
	if !prHook.IsOpened() {
		return nil
	}

	// get the PR
	pr := prHook.PullRequest

	// initialize github client
	gh := h.Client
	repo := getRepo(prHook.Repo)
	prID := strconv.Itoa(prHook.Number)

	// checkout the repository in a temp dir
	temp, err := ioutil.TempDir("", fmt.Sprintf("pr-%d", prHook.Number))
	if err != nil {
		return err
	}
	defer os.RemoveAll(temp)

	if err := fetchPullRequest(temp, pr.Base.Repo.HTMLURL, prHook.Number); err != nil {
		return err
	}

	prFiles, err := gh.PullRequestFiles(repo, prID, &octokat.Options{})
	if err != nil {
		return err
	}

	if err = validateFormat(gh, repo, pr.Head.Sha, temp, prID, prFiles); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 3
0
func handlePullRequest(w http.ResponseWriter, r *http.Request) {
	logrus.Debugf("Got a pull request hook")

	// parse the pull request
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		logrus.Errorf("Error reading github pull request handler body: %v", err)
		w.WriteHeader(500)
		return
	}

	prHook, err := octokat.ParsePullRequestHook(body)
	if err != nil {
		logrus.Errorf("Error parsing pull request hook: %v", err)
		w.WriteHeader(500)
		return
	}

	pr := prHook.PullRequest
	baseRepo := fmt.Sprintf("%s/%s", pr.Base.Repo.Owner.Login, pr.Base.Repo.Name)

	logrus.Infof("Received GitHub pull request notification for %s %d (%s): %s", baseRepo, pr.Number, pr.URL, prHook.Action)

	// ignore everything we don't care about
	if prHook.Action != "opened" && prHook.Action != "reopened" && prHook.Action != "synchronize" {
		logrus.Debugf("Ignoring PR hook action %q", prHook.Action)
		return
	}

	g := github.GitHub{
		AuthToken: config.GHToken,
		User:      config.GHUser,
	}

	pullRequest, err := g.LoadPullRequest(prHook)
	if err != nil {
		logrus.Errorf("Error loading the pull request: %v", err)
		w.WriteHeader(500)
		return
	}

	valid, err := g.DcoVerified(pullRequest)

	if err != nil {
		logrus.Errorf("Error validating DCO: %v", err)
		w.WriteHeader(500)
		return
	}

	// DCO not valid, we don't start the build
	if !valid {
		logrus.Errorf("Invalid DCO for %s #%d. Aborting build", baseRepo, pr.Number)
		w.WriteHeader(200)
		return
	}

	mergeable, err := g.IsMergeable(pullRequest)
	if err != nil {
		logrus.Errorf("Error checking if PR is mergeable: %v", err)
		w.WriteHeader(500)
		return
	}

	// PR is not mergeable, so don't start the build
	if !mergeable {
		logrus.Errorf("Unmergeable PR for %s #%d. Aborting build", baseRepo, pr.Number)
		w.WriteHeader(200)
		return
	}

	var builds []Build
	// Only run full jobs if there are code related changes
	if !pullRequest.Content.IsNonCodeOnly() {
		// get the builds
		var err error
		builds, err = config.getBuilds(baseRepo, false)
		if err != nil {
			logrus.Warn(err)
		}
	}

	// If there are doc-changes validate them
	if pullRequest.Content.HasDocsChanges() {
		build, err := config.getBuildByContextAndRepo("doc", baseRepo)
		if err != nil {
			logrus.Warnf("Adding doc build to %s for %d failed: %v", baseRepo, pr.Number, err)
		} else {
			builds = append(builds, build)
		}
	}

	// If there are vendoring changes validate them
	if pullRequest.Content.HasVendoringChanges() {
		build, err := config.getBuildByContextAndRepo("vendor", baseRepo)
		if err != nil {
			logrus.Warnf("Adding vendor build to %s for %d failed: %v", baseRepo, pr.Number, err)
		} else {
			builds = append(builds, build)
		}
	}

	// schedule the jenkins builds
	for _, build := range builds {
		if err := config.scheduleJenkinsBuild(baseRepo, pr.Number, build); err != nil {
			logrus.Error(err)
			w.WriteHeader(500)
		}
	}

	return
}
Ejemplo n.º 4
0
func handlePullRequest(w http.ResponseWriter, r *http.Request) {
	log.Debugf("Got a pull request hook")

	// parse the pull request
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Errorf("Error reading github pull request handler body: %v", err)
		w.WriteHeader(500)
		return
	}

	prHook, err := octokat.ParsePullRequestHook(body)
	if err != nil {
		log.Errorf("Error parsing pull request hook: %v", err)
		w.WriteHeader(500)
		return
	}

	pr := prHook.PullRequest
	baseRepo := fmt.Sprintf("%s/%s", pr.Base.Repo.Owner.Login, pr.Base.Repo.Name)

	log.Infof("Received GitHub pull request notification for %s %d (%s): %s", baseRepo, pr.Number, pr.URL, prHook.Action)

	// ignore everything we don't care about
	if prHook.Action != "opened" && prHook.Action != "reopened" && prHook.Action != "synchronize" {
		log.Debugf("Ignoring PR hook action %q", prHook.Action)
		return
	}

	g := github.GitHub{
		AuthToken: config.GHToken,
		User:      config.GHUser,
	}

	mergeable, err := g.IsMergeable(prHook)

	if err != nil {
		log.Errorf("Error checking if PR is mergeable: %v", err)
		w.WriteHeader(500)
		return
	}

	// PR is not mergeable, so don't start the build
	if !mergeable {
		log.Errorf("Unmergeable PR for %s #%d. Aborting build", baseRepo, pr.Number)
		w.WriteHeader(200)
		return
	}

	// get the builds
	builds, err := config.getBuilds(baseRepo, false)
	if err != nil {
		log.Error(err)
		w.WriteHeader(500)
		return
	}

	// schedule the jenkins builds
	for _, build := range builds {
		if err := config.scheduleJenkinsBuild(baseRepo, pr.Number, build); err != nil {
			log.Error(err)
			w.WriteHeader(500)
		}
	}

	return
}