Repository: go-gitea/gitea
File: models/git/commit_status.go (Go)

Code before:
func (status *CommitStatus) LocaleString(lang translation.Locale) string {
	return lang.TrString("repo.commitstatus." + status.State.String())
}

// HideActionsURL set `TargetURL` to an empty string if the status comes from Gitea Actions
func (status *CommitStatus) HideActionsURL(ctx context.Context) {
	if status.CreatedByGiteaActions(ctx) {
		status.TargetURL = ""
	}
}

// CreatedByGiteaActions returns true if the commit status is created by Gitea Actions
func (status *CommitStatus) CreatedByGiteaActions(ctx context.Context) bool {
	if status.RepoID == 0 {
		return false
	}

	if status.Repo == nil {
		if err := status.loadRepository(ctx); err != nil {
			log.Error("loadRepository: %v", err)
			return false
		}
	}

	prefix := status.Repo.Link() + "/actions"
	return strings.HasPrefix(status.TargetURL, prefix)
}

// CalcCommitStatus returns commit status state via some status, the commit statues should order by id desc
func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
	if len(statuses) == 0 {
		return nil
	}

	states := make(commitstatus.CommitStatusStates, 0, len(statuses))
	targetURL := ""
	for _, status := range statuses {
		states = append(states, status.State)
		if status.TargetURL != "" {
			targetURL = status.TargetURL
		}
	}

	return &CommitStatus{
		RepoID:    statuses[0].RepoID,
		SHA:       statuses[0].SHA,
		State:     states.Combine(),
		TargetURL: targetURL,
	}
}


Review comment (suggestion):
`TargetURL` is a relative URL?

IIRC in this code base, URL in most cases means absolute URL (schema+host+path...)


If it is an absolute URL, Gitea might run on multiple domains

Code after:
	return status.Repo.APIURL() + "/statuses/" + url.PathEscape(status.SHA)
}

// LocaleString returns the locale string name of the Status
func (status *CommitStatus) LocaleString(lang translation.Locale) string {
	return lang.TrString("repo.commitstatus." + status.State.String())
}

// HideActionsURL set `TargetURL` to an empty string if the status comes from Gitea Actions
func (status *CommitStatus) HideActionsURL(ctx context.Context) {
	if _, ok := status.cutTargetURLGiteaActionsPrefix(ctx); ok {
		status.TargetURL = ""
	}
}

func (status *CommitStatus) cutTargetURLGiteaActionsPrefix(ctx context.Context) (string, bool) {
	if status.RepoID == 0 {
		return "", false
	}

	if status.Repo == nil {
		if err := status.loadRepository(ctx); err != nil {
			log.Error("loadRepository: %v", err)
			return "", false
		}
	}

	prefix := status.Repo.Link() + "/actions"
	return strings.CutPrefix(status.TargetURL, prefix)
}

// ParseGiteaActionsTargetURL parses the commit status target URL as Gitea Actions link
func (status *CommitStatus) ParseGiteaActionsTargetURL(ctx context.Context) (runID, jobID int64, ok bool) {
	s, ok := status.cutTargetURLGiteaActionsPrefix(ctx)
	if !ok {
		return 0, 0, false
	}

	parts := strings.Split(s, "/") // expect: /runs/{runID}/jobs/{jobID}
	if len(parts) < 5 || parts[1] != "runs" || parts[3] != "jobs" {
		return 0, 0, false
	}

	runID, err1 := strconv.ParseInt(parts[2], 10, 64)
	jobID, err2 := strconv.ParseInt(parts[4], 10, 64)
	if err1 != nil || err2 != nil {
		return 0, 0, false
	}
	return runID, jobID, true
}