Skip to content

Commit 0015816

Browse files
author
Kristian
authored
Merge pull request #128 from holger-wg2/holger/add-changed-files
[Feature/ChangedFiles]#125 Add support for getting changedFiles from PR as resource
2 parents fa91027 + 14ecb63 commit 0015816

File tree

6 files changed

+262
-13
lines changed

6 files changed

+262
-13
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,12 @@ generate notifications over the webhook. So if you have a repository with little
6060

6161
#### `get`
6262

63-
| Parameter | Required | Example | Description |
64-
|--------------------|----------|----------|------------------------------------------------------------------------------------|
65-
| `skip_download` | No | `true` | Use with `get_params` in a `put` step to do nothing on the implicit get. |
66-
| `integration_tool` | No | `rebase` | The integration tool to use, `merge`, `rebase` or `checkout`. Defaults to `merge`. |
67-
| `git_depth` | No | `1` | Shallow clone the repository using the `--depth` Git option |
63+
| Parameter | Required | Example | Description |
64+
|-----------------------|----------|----------|------------------------------------------------------------------------------------|
65+
| `skip_download` | No | `true` | Use with `get_params` in a `put` step to do nothing on the implicit get. |
66+
| `integration_tool` | No | `rebase` | The integration tool to use, `merge`, `rebase` or `checkout`. Defaults to `merge`. |
67+
| `git_depth` | No | `1` | Shallow clone the repository using the `--depth` Git option |
68+
| `list_changed_files` | No | `true` | Generate a list of changed files and save alongside metadata |
6869

6970
Clones the base (e.g. `master` branch) at the latest commit, and merges the pull request at the specified commit
7071
into master. This ensures that we are both testing and setting status on the exact commit that was requested in
@@ -73,6 +74,7 @@ input. Because the base of the PR is not locked to a specific commit in versions
7374
requested version and the metadata emitted by `get` are available to your tasks as JSON:
7475
- `.git/resource/version.json`
7576
- `.git/resource/metadata.json`
77+
- `.git/resource/changed_files` (if enabled by `list_changed_files`)
7678

7779
The information in `metadata.json` is also available as individual files in the `.git/resource` directory, e.g. the `base_sha`
7880
is available as `.git/resource/base_sha`. For a complete list of available (individual) metadata files, please check the code

fakes/fake_github.go

Lines changed: 80 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Github interface {
2424
ListModifiedFiles(int) ([]string, error)
2525
PostComment(string, string) error
2626
GetPullRequest(string, string) (*PullRequest, error)
27+
GetChangedFiles(string, string) ([]ChangedFileObject, error)
2728
UpdateCommitStatus(string, string, string, string, string, string) error
2829
}
2930

@@ -198,6 +199,62 @@ func (m *GithubClient) PostComment(prNumber, comment string) error {
198199
return err
199200
}
200201

202+
// GetChangedFiles ...
203+
func (m *GithubClient) GetChangedFiles(prNumber string, commitRef string) ([]ChangedFileObject, error) {
204+
pr, err := strconv.Atoi(prNumber)
205+
if err != nil {
206+
return nil, fmt.Errorf("failed to convert pull request number to int: %s", err)
207+
}
208+
209+
var cfo []ChangedFileObject
210+
211+
var filequery struct {
212+
Repository struct {
213+
PullRequest struct {
214+
Files struct {
215+
Edges []struct {
216+
Node struct {
217+
ChangedFileObject
218+
}
219+
} `graphql:"edges"`
220+
PageInfo struct {
221+
EndCursor githubv4.String
222+
HasNextPage bool
223+
} `graphql:"pageInfo"`
224+
} `graphql:"files(first:$changedFilesFirst, after: $changedFilesEndCursor)"`
225+
} `graphql:"pullRequest(number:$prNumber)"`
226+
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
227+
}
228+
229+
offset := ""
230+
231+
for {
232+
vars := map[string]interface{}{
233+
"repositoryOwner": githubv4.String(m.Owner),
234+
"repositoryName": githubv4.String(m.Repository),
235+
"prNumber": githubv4.Int(pr),
236+
"changedFilesFirst": githubv4.Int(100),
237+
"changedFilesEndCursor": githubv4.String(offset),
238+
}
239+
240+
if err := m.V4.Query(context.TODO(), &filequery, vars); err != nil {
241+
return nil, err
242+
}
243+
244+
for _, f := range filequery.Repository.PullRequest.Files.Edges {
245+
cfo = append(cfo, ChangedFileObject{Path: f.Node.Path})
246+
}
247+
248+
if !filequery.Repository.PullRequest.Files.PageInfo.HasNextPage {
249+
break
250+
}
251+
252+
offset = string(filequery.Repository.PullRequest.Files.PageInfo.EndCursor)
253+
}
254+
255+
return cfo, nil
256+
}
257+
201258
// GetPullRequest ...
202259
func (m *GithubClient) GetPullRequest(prNumber, commitRef string) (*PullRequest, error) {
203260
pr, err := strconv.Atoi(prNumber)
@@ -231,6 +288,7 @@ func (m *GithubClient) GetPullRequest(prNumber, commitRef string) (*PullRequest,
231288
if err := m.V4.Query(context.TODO(), &query, vars); err != nil {
232289
return nil, err
233290
}
291+
234292
for _, c := range query.Repository.PullRequest.Commits.Edges {
235293
if c.Node.Commit.OID == commitRef {
236294
// Return as soon as we find the correct ref.

in.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,25 @@ func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResp
9999
if err := ioutil.WriteFile(filepath.Join(path, filename), content, 0644); err != nil {
100100
return nil, fmt.Errorf("failed to write metadata file %s: %s", filename, err)
101101
}
102+
103+
}
104+
105+
if request.Params.ListChangedFiles {
106+
cfol, err := github.GetChangedFiles(request.Version.PR, request.Version.Commit)
107+
if err != nil {
108+
return nil, fmt.Errorf("failed to fetch list of changed files: %s", err)
109+
}
110+
111+
var fl []byte
112+
113+
for _, v := range cfol {
114+
fl = append(fl, []byte(v.Path+"\n")...)
115+
}
116+
117+
// Create List with changed files
118+
if err := ioutil.WriteFile(filepath.Join(path, "changed_files"), fl, 0644); err != nil {
119+
return nil, fmt.Errorf("failed to write file list: %s", err)
120+
}
102121
}
103122

104123
return &GetResponse{
@@ -109,9 +128,10 @@ func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResp
109128

110129
// GetParameters ...
111130
type GetParameters struct {
112-
SkipDownload bool `json:"skip_download"`
113-
IntegrationTool string `json:"integration_tool"`
114-
GitDepth int `json:"git_depth"`
131+
SkipDownload bool `json:"skip_download"`
132+
IntegrationTool string `json:"integration_tool"`
133+
GitDepth int `json:"git_depth"`
134+
ListChangedFiles bool `json:"list_changed_files"`
115135
}
116136

117137
// GetRequest ...

0 commit comments

Comments
 (0)