Skip to content
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

Add API endpoint to get changed files of a PR #18228

Closed
wants to merge 4 commits into from

Conversation

anbraten
Copy link
Contributor

closes #654

@lunny lunny added the modifies/api This PR adds API routes or modifies them label Jan 11, 2022
@lunny
Copy link
Member

lunny commented Jan 11, 2022

We need more fields here.

{
    "sha": "bbcd538c8e72b8c175046e27cc8f907076331401",
    "filename": "file1.txt",
    "status": "added",
    "additions": 103,
    "deletions": 21,
    "changes": 124,
    "blob_url": "https://github.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
    "raw_url": "https://github.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/file1.txt",
    "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/file1.txt?ref=6dcb09b5b57875f334f61aebed695e2e4193db5e",
    "patch": "@@ -132,7 +132,7 @@ module Test @@ -1000,7 +1000,7 @@ module Test"
}

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 11, 2022
Copy link
Contributor

@Gusted Gusted left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few touches on the code - also to addition on lunny, it should have more fields to become more useful. Also I'm not sure which applications might need this, so this could be really irrelevant. But it could be that applications might not need the patch field(which could become quite large on big PR's) so a option to not send that back would be nice in order to reduce network bandwidth for those who don't need it, but only need the other fields.

return
}

var prInfo *git.CompareInfo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this to Line 1321(after defer baseGitRepo.Close())

&gitdiff.DiffOptions{
BeforeCommitID: startCommitID,
AfterCommitID: endCommitID,
SkipTo: ctx.FormString("skip-to"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skip-to isn't mentioned as query in the swagger description.

MaxLines: maxLines,
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
MaxFiles: maxFiles,
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will always be empty as this ctx.Data is set by SetWhitespaceBehavior middleware.

WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
}, ctx.FormStrings("files")...)
if err != nil {
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx.ServerError("GetDiffRangeWithWhitespaceBehavior", err)
ctx.ServerError("GetDiff", err)

start, end := listOptions.GetStartEnd()

if end > totalNumberOfFiles {
end = totalNumberOfFiles
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could cause that it becomes. start > end In which a panic will be caused on line 1368 panic: runtime error: makeslice: cap out of range. Maybe add a check if start is still smaller than end?

Comment on lines +1370 to +1371
apiFile := convert.ToChangedFile(diff.Files[i])
apiFiles = append(apiFiles, apiFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
apiFile := convert.ToChangedFile(diff.Files[i])
apiFiles = append(apiFiles, apiFile)
apiFiles = append(apiFiles, convert.ToChangedFile(diff.Files[i]))

@6543 6543 added the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Jan 14, 2022
@@ -930,6 +930,7 @@ func Routes(sessioner func(http.Handler) http.Handler) *web.Route {
m.Get(".{diffType:diff|patch}", repo.DownloadPullDiffOrPatch)
m.Post("/update", reqToken(), repo.UpdatePullRequest)
m.Get("/commits", repo.GetPullRequestCommits)
m.Get("/files", repo.GetPullRequestFiles)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use context.ReferencesGitRepo(false), to get gitRepo injected into ctx

@zeripath zeripath changed the title add api endpoint to get changed files of a pr Add API endpoint to get changed files of a PR Jan 19, 2022
@zeripath zeripath removed the type/feature Completely new functionality. Can only be merged if feature freeze is not active. label Jan 19, 2022
@wxiaoguang wxiaoguang added this to the 1.17.0 milestone Jan 20, 2022
@codecov-commenter
Copy link

Codecov Report

❗ No coverage uploaded for pull request base (main@1ddfa59). Click here to learn what that means.
The diff coverage is 1.51%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #18228   +/-   ##
=======================================
  Coverage        ?   46.02%           
=======================================
  Files           ?      838           
  Lines           ?    92791           
  Branches        ?        0           
=======================================
  Hits            ?    42704           
  Misses          ?    43303           
  Partials        ?     6784           
Impacted Files Coverage Δ
modules/convert/convert.go 78.22% <0.00%> (ø)
routers/api/v1/repo/pull.go 25.68% <0.00%> (ø)
routers/api/v1/api.go 78.22% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1ddfa59...30a9ed6. Read the comment docs.

@lafriks
Copy link
Member

lafriks commented Jan 26, 2022

Would be nice if it would return structure same as in https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files even if it would return only filenames so that it could be extended easier in the future

@6543
Copy link
Member

6543 commented Feb 2, 2022

☝️

@charlesmelby
Copy link

Any progress on this? It looks like this is needed for several CI-related integrations.

@anbraten
Copy link
Contributor Author

I haven't looked at this PR for a while. You are right its required for some CI systems that's the reason I started it 😉. My general plan was to create the most basic version for now just a list of paths of all changed files with a structure similar to the one of Github. This way it would be easily extendable in the future if needed.

@techknowlogick techknowlogick modified the milestones: 1.17.0, 1.18.0 Jun 8, 2022
@d0x7
Copy link

d0x7 commented Aug 13, 2022

Any update? Do you maybe wanna integrate the requested changes and if the team is willing to, it could be released in this state, and when needed expanded feature-wise? I think it's somewhat important for Woodpeeker, Drone and other CIs. Including myself - a bot I'm writing for a internal project would somewhat rely on this existing.

@anbraten
Copy link
Contributor Author

seems like #21177 is winning the race 🏇🏾 😃

@anbraten anbraten closed this Sep 25, 2022
@lunny
Copy link
Member

lunny commented Sep 26, 2022

Please join the discussion there. :)

@lunny lunny removed this from the 1.18.0 milestone Sep 26, 2022
6543 added a commit that referenced this pull request Sep 29, 2022
This adds an api endpoint `/files` to PRs that allows to get a list of changed files.

built upon #18228, reviews there are included
closes #654

Co-authored-by: Anton Bracke <anton@ju60.de>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API to GET all changed files in Pull Request