func()

in googlechat/main.go [110:263]


func (g *googlechatNotifier) writeMessage(build *cbpb.Build) (*chat.Message, error) {

	var icon string

	switch build.Status {
	case cbpb.Build_SUCCESS:
		icon = "https://www.gstatic.com/images/icons/material/system/2x/check_circle_googgreen_48dp.png"
	case cbpb.Build_FAILURE, cbpb.Build_INTERNAL_ERROR:
		icon = "https://www.gstatic.com/images/icons/material/system/2x/error_red_48dp.png"
	case cbpb.Build_TIMEOUT:
		icon = "https://www.gstatic.com/images/icons/material/system/2x/hourglass_empty_black_48dp.png"
	default:
		icon = "https://www.gstatic.com/images/icons/material/system/2x/question_mark_black_48dp.png"
	}

	logURL, err := notifiers.AddUTMParams(build.LogUrl, notifiers.ChatMedium)
	if err != nil {
		return nil, fmt.Errorf("failed to add UTM params: %w", err)
	}

	// Basic card setup
	duration := build.GetFinishTime().AsTime().Sub(build.GetStartTime().AsTime())
	duration_min, duration_sec := int(duration.Minutes()), int(duration.Seconds())-int(duration.Minutes())*60
	duration_fmt := fmt.Sprintf("%d min %d sec", duration_min, duration_sec)

	card := &chat.Card{
		Header: &chat.CardHeader{
			Title:    fmt.Sprintf("Build %s Status: %s", build.Id[:8], build.Status),
			Subtitle: build.ProjectId,
			ImageUrl: icon,
		},
		Sections: []*chat.Section{
			{
				Widgets: []*chat.WidgetMarkup{
					{
						KeyValue: &chat.KeyValue{
							TopLabel: "Duration",
							Content:  duration_fmt,
						},
					},
				},
			},
		},
	}

	// Optional section: display trigger information
	if build.BuildTriggerId != "" {

		log.Infof("Detected a build trigger id: %s", build.BuildTriggerId)

		/*
			//TODO(glasnt): Get trigger information for Uri links.
			//  The repo name in `build` does not include the owner information
			//  You need to inspect the trigger object to get the full repo name and/or the git URI.

			ctx := context.Background()
			cbapi, _ := cloudbuild.NewClient(ctx)
			trigger_info := cbapi.GetBuildTrigger(ctx, &cbpb.GetBuildTriggerRequest{ProjectId: build.ProjectId, TriggerId: build.BuildTriggerId,})
			log.Infof("Trigger Repo URI: %s", trigger_info.??)
		*/

		repo_name := build.Substitutions["REPO_NAME"]
		trigger_name := build.Substitutions["TRIGGER_NAME"]
		commit := build.Substitutions["SHORT_SHA"]

		// Branch, Tag, or None.
		branch_tag_label := "Branch"
		branch_tag_value := build.Substitutions["BRANCH_NAME"]

		if branch_tag_value == "" {
			branch_tag_label = "Tag"
			branch_tag_value = build.Substitutions["TAG_NAME"]

			if branch_tag_value == "" {
				branch_tag_label = "Branch/Tag"
				branch_tag_value = "[no branch or tag]"
			}
		}

		card.Header.Subtitle = fmt.Sprintf("%s on %s", trigger_name, build.ProjectId)

		build_info := &chat.Section{
			Header: "Trigger information",
			Widgets: []*chat.WidgetMarkup{
				{
					KeyValue: &chat.KeyValue{
						TopLabel: "Trigger",
						Content:  trigger_name,
					},
				},
				{
					KeyValue: &chat.KeyValue{
						TopLabel: "Repo",
						Content:  repo_name,
					},
				},
				{
					KeyValue: &chat.KeyValue{
						TopLabel: branch_tag_label,
						Content:  branch_tag_value,
					},
				},
				{
					KeyValue: &chat.KeyValue{
						TopLabel: "Commit",
						Content:  commit,
					},
				},
			},
		}

		card.Sections = append(card.Sections, build_info)
	}

	// Optional section: display information about errors
	if build.FailureInfo != nil {
		failure_info := &chat.Section{
			Header: "Error information",
			Widgets: []*chat.WidgetMarkup{
				{
					TextParagraph: &chat.TextParagraph{
						Text: build.FailureInfo.GetDetail(),
					},
				},
			},
		}
		card.Sections = append(card.Sections, failure_info)
	}

	// Append action button
	action_section := &chat.Section{
		Widgets: []*chat.WidgetMarkup{
			{
				Buttons: []*chat.Button{
					{
						TextButton: &chat.TextButton{
							Text: "open logs",
							OnClick: &chat.OnClick{
								OpenLink: &chat.OpenLink{
									Url: logURL,
								},
							},
						},
					},
				},
			},
		},
	}

	card.Sections = append(card.Sections, action_section)

	msg := chat.Message{Cards: []*chat.Card{card}}
	return &msg, nil
}