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

feat: add file matching to PR generator #19678

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"old_path": "README",
"new_path": "README",
"a_mode": "100644",
"b_mode": "100644",
"diff": "",
"new_file": false,
"renamed_file": false,
"deleted_file": false,
"generated_file": false
},
{
"old_path": "VERSION",
"new_path": "VERSION",
"a_mode": "100644",
"b_mode": "100644",
"diff": "",
"new_file": false,
"renamed_file": false,
"deleted_file": false,
"generated_file": false
}
]
19 changes: 19 additions & 0 deletions applicationset/services/pull_request/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func (g *GiteaService) List(ctx context.Context) ([]*PullRequest, error) {
}
list := []*PullRequest{}
for _, pr := range prs {
var changeFiles []string
changeFiles, err = g.listChangedFiles(pr.Index)
if err != nil {
return nil, err
}
list = append(list, &PullRequest{
Number: int(pr.Index),
Title: pr.Title,
Expand All @@ -63,11 +68,25 @@ func (g *GiteaService) List(ctx context.Context) ([]*PullRequest, error) {
HeadSHA: pr.Head.Sha,
Labels: getGiteaPRLabelNames(pr.Labels),
Author: pr.Poster.UserName,
ChangedFiles: changeFiles,
})
}
return list, nil
}

func (g *GiteaService) listChangedFiles(prNumber int64) ([]string, error) {
filesChanged := []string{}

files, _, err := g.client.ListPullRequestFiles(g.owner, g.repo, prNumber, gitea.ListPullRequestFilesOptions{})
if err != nil {
return nil, err
}
for _, file := range files {
filesChanged = append(filesChanged, file.Filename)
}
return filesChanged, nil
}

// Get the Gitea pull request label names.
func getGiteaPRLabelNames(giteaLabels []*gitea.Label) []string {
var labelNames []string
Expand Down
22 changes: 20 additions & 2 deletions applicationset/services/pull_request/gitea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ func giteaMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
if err != nil {
t.Fail()
}
case "/api/v1/repos/test-argocd/pr-test/pulls/1/files?limit=0&page=1":
_, err := io.WriteString(w, `[{
"sha": "7bbaf62d92ddfafd9cc8b340c619abaec32bc09f",
"filename": "test",
"status": "added",
"additions": 0,
"deletions": 0,
"changes": 0,
"blob_url": "https://gitea.com/test-argocd/pr-test/raw/7bbaf62d92ddfafd9cc8b340c619abaec32bc09f/test",
"raw_url": "https://gitea.com/test-argocd/pr-test/raw/7bbaf62d92ddfafd9cc8b340c619abaec32bc09f/test",
"contents_url": "https://gitea.com/api/v1/repos/test-argocd/pr-test/contents/test?ref=7bbaf62d92ddfafd9cc8b340c619abaec32bc09f",
"patch": "@@ -0,0 +1 @@\n+test\n"
}]`)
if err != nil {
t.Fail()
}
case "/api/v1/repos/test-argocd/pr-test/pulls?limit=0&page=1&state=open":
_, err := io.WriteString(w, `[{
"id": 50721,
Expand Down Expand Up @@ -250,9 +266,11 @@ func TestGiteaList(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
giteaMockHandler(t)(w, r)
}))
host, err := NewGiteaService(context.Background(), "", ts.URL, "test-argocd", "pr-test", false)
svc, err := NewGiteaService(context.Background(), "", ts.URL, "test-argocd", "pr-test", false)
require.NoError(t, err)
prs, err := ListPullRequests(context.Background(), svc, nil)

require.NoError(t, err)
prs, err := host.List(context.Background())
require.NoError(t, err)
assert.Len(t, prs, 1)
assert.Equal(t, 1, prs[0].Number)
Expand Down
30 changes: 30 additions & 0 deletions applicationset/services/pull_request/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ func NewGithubService(ctx context.Context, token, url, owner, repo string, label
}, nil
}

func (g *GithubService) listChangedFiles(ctx context.Context, number int) ([]string, error) {
filesChanged := []string{}
opts := &github.ListOptions{
PerPage: 100,
}

for {
commitChanges, resp, err := g.client.PullRequests.ListFiles(ctx, g.owner, g.repo, number, opts)
fmt.Println("commitChanges", commitChanges)
for _, commitChange := range commitChanges {
filesChanged = append(filesChanged, *commitChange.Filename)
}
if err != nil {
return nil, fmt.Errorf("error listing files for pull request %d: %w", number, err)
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return filesChanged, nil
}

func (g *GithubService) List(ctx context.Context) ([]*PullRequest, error) {
opts := &github.PullRequestListOptions{
ListOptions: github.ListOptions{
Expand All @@ -61,9 +84,15 @@ func (g *GithubService) List(ctx context.Context) ([]*PullRequest, error) {
return nil, fmt.Errorf("error listing pull requests for %s/%s: %w", g.owner, g.repo, err)
}
for _, pull := range pulls {
filesChanged, err := g.listChangedFiles(ctx, *pull.Number)
if err != nil {
return nil, err
}

if !containLabels(g.labels, pull.Labels) {
continue
}

pullRequests = append(pullRequests, &PullRequest{
Number: *pull.Number,
Title: *pull.Title,
Expand All @@ -72,6 +101,7 @@ func (g *GithubService) List(ctx context.Context) ([]*PullRequest, error) {
HeadSHA: *pull.Head.SHA,
Labels: getGithubPRLabelNames(pull.Labels),
Author: *pull.User.Login,
ChangedFiles: filesChanged,
})
}
if resp.NextPage == 0 {
Expand Down
Loading
Loading