-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Feat add Get pull requests associated to a commit's SHA #36617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kevo-1
wants to merge
9
commits into
go-gitea:main
Choose a base branch
from
kevo-1:feature/commit-pulls-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3cdf14c
Feat add Get pull requests by commit SHA
kevo-1 23a3cbf
fix Test cases and return empty pr list
kevo-1 2b30da5
Apply suggestions from code review
kevo-1 67beb95
chore: make fmt
kevo-1 a4f0c00
chore: run make fmt & lint-go
kevo-1 0329d2c
Feat add get PRs associated with a commit SHA
kevo-1 0bb107f
chore: make generate-swagger
kevo-1 25e8caa
Fix issues addressed
kevo-1 bc2a874
Add 10s timeout for the GetBranchesContaining
kevo-1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
tests/integration/api_repo_get_commit_pull_request_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // Copyright 2026 The Gitea Authors. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package integration | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| auth_model "code.gitea.io/gitea/models/auth" | ||
| "code.gitea.io/gitea/models/unittest" | ||
| user_model "code.gitea.io/gitea/models/user" | ||
| api "code.gitea.io/gitea/modules/structs" | ||
| "code.gitea.io/gitea/tests" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAPIReposGetCommitPullRequests(t *testing.T) { | ||
| defer tests.PrepareTestEnv(t)() | ||
|
|
||
| user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) | ||
| session := loginUser(t, user.Name) | ||
| token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadRepository) | ||
|
|
||
| // Helper: query the /pulls endpoint and decode the response | ||
| getCommitPRs := func(t *testing.T, sha string, expectedStatus int) []*api.PullRequest { | ||
| t.Helper() | ||
| req := NewRequestf(t, "GET", "/api/v1/repos/%s/repo1/commits/%s/pulls", user.Name, sha). | ||
| AddTokenAuth(token) | ||
| resp := MakeRequest(t, req, expectedStatus) | ||
|
|
||
| var prs []*api.PullRequest | ||
| DecodeJSON(t, resp, &prs) | ||
| return prs | ||
| } | ||
|
|
||
| t.Run("MergedCommit", func(t *testing.T) { | ||
| // PR #1 (fixture id=1) has merged_commit_id = 1a8823cd1a9549fde083f992f6b9b87a7ab74fb3 | ||
| // This tests the DB-level lookup by merged_commit_id | ||
| mergedCommitSHA := "1a8823cd1a9549fde083f992f6b9b87a7ab74fb3" | ||
|
|
||
| prs := getCommitPRs(t, mergedCommitSHA, http.StatusOK) | ||
|
|
||
| assert.NotEmpty(t, prs, "Should find the PR by its merge commit SHA") | ||
| assert.Equal(t, int64(2), prs[0].Index, "Should be PR index 2 (fixture PR #1)") | ||
| assert.Equal(t, "master", prs[0].Base.Name) | ||
| }) | ||
|
|
||
| t.Run("CommitInPRBranch", func(t *testing.T) { | ||
| // Commit 5c050d3b is on branch2 (PR #2, fixture id=2) and pr-to-update (PR #5, fixture id=5) | ||
| // This tests the git branch containment strategy | ||
| commitOnBranch := "5c050d3b6d2db231ab1f64e324f1b6b9a0b181c2" | ||
|
|
||
| prs := getCommitPRs(t, commitOnBranch, http.StatusOK) | ||
|
|
||
| assert.NotEmpty(t, prs, "Should find PRs whose branches contain this commit") | ||
|
|
||
| // Verify we found at least the PR with head_branch=branch2 | ||
| foundPR2 := false | ||
| for _, pr := range prs { | ||
| if pr.Index == 3 { // PR #2 has issue_id=3 so index=3 | ||
| foundPR2 = true | ||
| assert.Equal(t, "branch2", pr.Head.Name) | ||
| } | ||
| } | ||
| assert.True(t, foundPR2, "Expected to find PR with head_branch=branch2") | ||
| }) | ||
|
|
||
| t.Run("InvalidCommitSHA", func(t *testing.T) { | ||
| prs := getCommitPRs(t, "invalidsha", http.StatusOK) | ||
| assert.Empty(t, prs, "Should return empty array for invalid SHA") | ||
| }) | ||
|
|
||
| t.Run("NonexistentCommit", func(t *testing.T) { | ||
| // Valid SHA format but doesn't exist in repo | ||
| prs := getCommitPRs(t, "0000000000000000000000000000000000000000", http.StatusOK) | ||
| assert.Empty(t, prs, "Should return empty array for nonexistent commit") | ||
| }) | ||
|
kevo-1 marked this conversation as resolved.
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems it missed the permission check. It needs a read permission of pull request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, could you clarify more?
The route is inside
/commitsPathGroup which already does the check using reqRepoReader.Should I add an inline check for read permission?
or did I misunderstand something.
and thank you again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just add a permission check inside the function. The endpoint is special because it needs both code unit permission and pull request unit permission.