Skip to content

Commit

Permalink
get all changed files (#742)
Browse files Browse the repository at this point in the history
* get all changed files in PR

Former-commit-id: b890d18
  • Loading branch information
motatoes authored Nov 8, 2023
1 parent 0eacb2b commit 746b97e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
28 changes: 19 additions & 9 deletions libs/orchestrator/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
)

func NewGitHubService(ghToken string, repoName string, owner string) GithubService {
client := github.NewTokenClient(context.Background(), ghToken)
client := github.NewClient(nil)
if ghToken != "" {
client = client.WithAuthToken(ghToken)
}

return GithubService{
Client: client,
RepoName: repoName,
Expand Down Expand Up @@ -47,15 +51,21 @@ func (svc *GithubService) GetUserTeams(organisation string, user string) ([]stri
}

func (svc *GithubService) GetChangedFiles(prNumber int) ([]string, error) {
files, _, err := svc.Client.PullRequests.ListFiles(context.Background(), svc.Owner, svc.RepoName, prNumber, nil)
if err != nil {
log.Fatalf("error getting pull request files: %v", err)
}

fileNames := make([]string, len(files))
var fileNames []string
opts := github.ListOptions{PerPage: 100}
for {
files, resp, err := svc.Client.PullRequests.ListFiles(context.Background(), svc.Owner, svc.RepoName, prNumber, &opts)
if err != nil {
log.Fatalf("error getting pull request files: %v", err)
}

for i, file := range files {
fileNames[i] = *file.Filename
for _, file := range files {
fileNames = append(fileNames, *file.Filename)
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return fileNames, nil
}
Expand Down
6 changes: 6 additions & 0 deletions libs/orchestrator/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,9 @@ func TestFindAllProjectsDependantOnImpactedProjects(t *testing.T) {
assert.NotContains(t, projectNames, "k")
assert.NotContains(t, projectNames, "b")
}

func TestFindAllChangedFilesOfPR(t *testing.T) {
githubPrService := NewGitHubService("", "digger", "diggerhq")
files, _ := githubPrService.GetChangedFiles(98)
assert.Equal(t, 45, len(files))
}

0 comments on commit 746b97e

Please sign in to comment.