Skip to content

Commit

Permalink
Merge pull request #128 from holger-wg2/holger/add-changed-files
Browse files Browse the repository at this point in the history
[Feature/ChangedFiles]#125 Add support for getting changedFiles from PR as resource
  • Loading branch information
Kristian authored Jul 16, 2019
2 parents fa91027 + 14ecb63 commit 0015816
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 13 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ generate notifications over the webhook. So if you have a repository with little

#### `get`

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

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

The information in `metadata.json` is also available as individual files in the `.git/resource` directory, e.g. the `base_sha`
is available as `.git/resource/base_sha`. For a complete list of available (individual) metadata files, please check the code
Expand Down
80 changes: 80 additions & 0 deletions fakes/fake_github.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions github.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Github interface {
ListModifiedFiles(int) ([]string, error)
PostComment(string, string) error
GetPullRequest(string, string) (*PullRequest, error)
GetChangedFiles(string, string) ([]ChangedFileObject, error)
UpdateCommitStatus(string, string, string, string, string, string) error
}

Expand Down Expand Up @@ -198,6 +199,62 @@ func (m *GithubClient) PostComment(prNumber, comment string) error {
return err
}

// GetChangedFiles ...
func (m *GithubClient) GetChangedFiles(prNumber string, commitRef string) ([]ChangedFileObject, error) {
pr, err := strconv.Atoi(prNumber)
if err != nil {
return nil, fmt.Errorf("failed to convert pull request number to int: %s", err)
}

var cfo []ChangedFileObject

var filequery struct {
Repository struct {
PullRequest struct {
Files struct {
Edges []struct {
Node struct {
ChangedFileObject
}
} `graphql:"edges"`
PageInfo struct {
EndCursor githubv4.String
HasNextPage bool
} `graphql:"pageInfo"`
} `graphql:"files(first:$changedFilesFirst, after: $changedFilesEndCursor)"`
} `graphql:"pullRequest(number:$prNumber)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}

offset := ""

for {
vars := map[string]interface{}{
"repositoryOwner": githubv4.String(m.Owner),
"repositoryName": githubv4.String(m.Repository),
"prNumber": githubv4.Int(pr),
"changedFilesFirst": githubv4.Int(100),
"changedFilesEndCursor": githubv4.String(offset),
}

if err := m.V4.Query(context.TODO(), &filequery, vars); err != nil {
return nil, err
}

for _, f := range filequery.Repository.PullRequest.Files.Edges {
cfo = append(cfo, ChangedFileObject{Path: f.Node.Path})
}

if !filequery.Repository.PullRequest.Files.PageInfo.HasNextPage {
break
}

offset = string(filequery.Repository.PullRequest.Files.PageInfo.EndCursor)
}

return cfo, nil
}

// GetPullRequest ...
func (m *GithubClient) GetPullRequest(prNumber, commitRef string) (*PullRequest, error) {
pr, err := strconv.Atoi(prNumber)
Expand Down Expand Up @@ -231,6 +288,7 @@ func (m *GithubClient) GetPullRequest(prNumber, commitRef string) (*PullRequest,
if err := m.V4.Query(context.TODO(), &query, vars); err != nil {
return nil, err
}

for _, c := range query.Repository.PullRequest.Commits.Edges {
if c.Node.Commit.OID == commitRef {
// Return as soon as we find the correct ref.
Expand Down
26 changes: 23 additions & 3 deletions in.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,25 @@ func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResp
if err := ioutil.WriteFile(filepath.Join(path, filename), content, 0644); err != nil {
return nil, fmt.Errorf("failed to write metadata file %s: %s", filename, err)
}

}

if request.Params.ListChangedFiles {
cfol, err := github.GetChangedFiles(request.Version.PR, request.Version.Commit)
if err != nil {
return nil, fmt.Errorf("failed to fetch list of changed files: %s", err)
}

var fl []byte

for _, v := range cfol {
fl = append(fl, []byte(v.Path+"\n")...)
}

// Create List with changed files
if err := ioutil.WriteFile(filepath.Join(path, "changed_files"), fl, 0644); err != nil {
return nil, fmt.Errorf("failed to write file list: %s", err)
}
}

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

// GetParameters ...
type GetParameters struct {
SkipDownload bool `json:"skip_download"`
IntegrationTool string `json:"integration_tool"`
GitDepth int `json:"git_depth"`
SkipDownload bool `json:"skip_download"`
IntegrationTool string `json:"integration_tool"`
GitDepth int `json:"git_depth"`
ListChangedFiles bool `json:"list_changed_files"`
}

// GetRequest ...
Expand Down
Loading

0 comments on commit 0015816

Please sign in to comment.