示例#1
0
文件: webhooks.go 项目: mvdan/gibot
func onPush(body io.Reader) error {
	var pe api.PushEvent
	if err := json.NewDecoder(body).Decode(&pe); err != nil {
		return fmt.Errorf("invalid push event body: %v", err)
	}
	repo, err := getRepo(pe.Repository)
	if err != nil {
		return err
	}
	user, err := repo.GetUser(pe.UserID)
	if err != nil {
		return fmt.Errorf("unknown user: %v", err)
	}
	branch := getBranch(pe.Ref)
	if branch == "" {
		return fmt.Errorf("no branch")
	}
	commits := make([]*api.Commit, 0, len(pe.Commits))
	for _, c := range pe.Commits {
		if mergeMessage.MatchString(c.Message) {
			continue
		}
		commits = append(commits, c)
	}
	var message string
	switch len(commits) {
	case 0:
		return fmt.Errorf("empty commits")
	case 1:
		// Message here means Title, how useful.
		title := gitlab.ShortTitle(commits[0].Message)
		short := gitlab.ShortCommit(commits[0].ID)
		message = fmt.Sprintf("%s pushed to %s: %s - %s",
			user.Username, branch, title, repo.CommitURL(short))
	default:
		url := repo.CompareURL(pe.Before, pe.After)
		message = fmt.Sprintf("%s pushed %d commits to %s - %s",
			user.Username, len(commits), branch, url)
	}
	sendNotices(config.Feeds, repo.Name, message)
	return nil
}
示例#2
0
文件: webhooks.go 项目: mvdan/gibot
func onMergeRequest(body io.Reader) error {
	var me api.MergeEvent
	if err := json.NewDecoder(body).Decode(&me); err != nil {
		return fmt.Errorf("invalid issue event body: %v", err)
	}
	attrs := me.ObjectAttributes
	title := gitlab.ShortTitle(attrs.Title)
	var message string
	switch attrs.Action {
	case "open":
		message = fmt.Sprintf("%s opened !%d: %s - %s",
			me.User.Username, attrs.Iid, title, attrs.URL)
	case "close", "reopen", "update", "merge":
		return nil
	default:
		return fmt.Errorf("merge action we don't handle: %s", attrs.Action)
	}
	repo, err := getRepo(me.Repository)
	if err != nil {
		return err
	}
	sendNotices(config.Feeds, repo.Name, message)
	return nil
}