Skip to content

API: adding issue labels by name attaches both the repo and the org label when they share a name #39651

Closed

Description

@JayOfTheKeyboard

Gitea Version

main at 9b1d6f4 (2026-10-07). The code dates from #32988, so 1.24 and later should have it.

What happened?

POST and PUT /repos/{owner}/{repo}/issues/{index}/labels accept label names. prepareForReplaceOrAdd (routers/api/v1/repo/issue_label.go) looks each name up in the repository's labels and, for an org-owned repo, in the organization's labels, then attaches every match. A name that exists at both levels attaches two labels. #37192 shows that repo and org labels with the same name are allowed and expected to stay allowed, so this happens in normal use.

I hit it from a board tool that sets labels by name, and now resolve names to IDs myself to avoid it.

Measured on main with an integration test (sqlite, fixtures: repo org3/repo3, org label orglabel4, plus a repo label also named orglabel4). Each row starts from an issue with no labels:

Request HTTP Labels attached
POST ["orglabel4"] (name at repo and org level) 200 2 (org id 4 and repo id 11)
PUT ["orglabel4"] 200 2
POST ["scope/x"], exclusive, at both levels 200 1: the repo label here, because it was added last; the other is dropped by exclusivity, so the winner depends on order
POST ["orglabel3"] (org only) 200 1 (correct)
POST ["no-such-label"] 200 0, no error
POST ["repo3label1", "no-such-label"] 200 1, the unknown name dropped silently
PUT ["repo3label1-typo"] on an issue holding repo3label1 200 0: the typo removed every label
POST [99999] (unknown ID) 200 0, no error

Expected: one name gives one label. Actual: two labels, or with exclusive labels, whichever happens to be processed last.

The web UI does not hit this, because the sidebar sends IDs. The one other place that resolves label names for issues, issue templates (SetSelectedLabelNames in routers/web/repo/issue_page_meta.go), already picks a single label: it builds a name map from repo labels then org labels, so the org label wins there (by map overwrite, and case-insensitively).

Options:

  1. Repository label wins. The more specific label shadows the org one, the way a repo setting overrides an org default. Small change in prepareForReplaceOrAdd, and labels passed by ID are untouched. It does disagree with issue templates, which pick the org label.
  2. Organization label wins. Matches issue templates as they behave today. But a repo that defines its own label of that name probably did so on purpose.
  3. Reject the ambiguous name (422 naming the label). Nothing is guessed, but a request that works today starts failing.

Separately, and possibly not wanted: unknown names (and unknown IDs) are dropped with a 200. With PUT this means a typo silently removes all labels. Returning 422 for a name that matches nothing would catch it, but it changes API behaviour clients may rely on (the comment on GetLabelIDsInRepoByNames says the silent drop is intended), and unknown IDs behave the same way. So I would treat it as a separate decision and have left it alone.

I have option 1 ready with an integration test (it fails on main with 2 labels where 1 is expected and passes with the fix): branch fix/api-issue-labels-name-repo-shadows-org on my fork. If you prefer option 2 or 3, I can change it to that before opening the PR.

How are you running Gitea?

Reproduced from source on main with the integration test harness (sqlite, go test -run TestAPIAddIssueLabelsWithLabelNames ./tests/integration). First seen on Forgejo, which has the same code.


Investigated and drafted with Claude Code (Claude Opus 5.5); I have checked the measurements and the code references.

Activity

  1. added a commit that references this issue on Oct 22, 2026
    7448b49
  2. added a commit that references this issue on Oct 22, 2026
    a814cf7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions