Skip to content

Commit

Permalink
Revert "handle pull_request events separately (#10)" (#11)
Browse files Browse the repository at this point in the history
This reverts commit 8076f3c.
  • Loading branch information
Jim Sheldon authored Dec 6, 2019
1 parent 8076f3c commit 7c25f57
Showing 1 changed file with 5 additions and 37 deletions.
42 changes: 5 additions & 37 deletions plugin/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ package plugin

import (
"context"
"regexp"
"strconv"

"github.com/drone/drone-go/drone"

"github.com/google/go-github/github"
"golang.org/x/oauth2"
)

// regular expression to extract the pull request number
// from the git ref (e.g. refs/pulls/{d}/head)
var re = regexp.MustCompile("\\d+")

func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string, error) {
newctx := context.Background()
ts := oauth2.StaticTokenSource(
Expand All @@ -25,44 +19,18 @@ func getFilesChanged(repo drone.Repo, build drone.Build, token string) ([]string
client := github.NewClient(tc)

var commitFiles []github.CommitFile

if build.Event == "pull_request" {
s := re.FindString(build.Ref)
d, _ := strconv.Atoi(s)

listOpts := &github.ListOptions{
Page: 1,
PerPage: 300,
}
allFiles := []*github.CommitFile{}

for {
cfs, resp, err := client.PullRequests.ListFiles(newctx, repo.Namespace, repo.Name, d, listOpts)
if err != nil {
return nil, err
}

allFiles = append(allFiles, cfs...)
if resp.NextPage == 0 {
break
}
listOpts.Page = resp.NextPage
}
for _, file := range allFiles {
commitFiles = append(commitFiles, *file)
}
} else if build.Before == "" || build.Before == "0000000000000000000000000000000000000000" {
rc, _, err := client.Repositories.GetCommit(newctx, repo.Namespace, repo.Name, build.After)
if build.Before == "" || build.Before == "0000000000000000000000000000000000000000" {
response, _, err := client.Repositories.GetCommit(newctx, repo.Namespace, repo.Name, build.After)
if err != nil {
return nil, err
}
commitFiles = rc.Files
commitFiles = response.Files
} else {
cc, _, err := client.Repositories.CompareCommits(newctx, repo.Namespace, repo.Name, build.Before, build.After)
response, _, err := client.Repositories.CompareCommits(newctx, repo.Namespace, repo.Name, build.Before, build.After)
if err != nil {
return nil, err
}
commitFiles = cc.Files
commitFiles = response.Files
}

var files []string
Expand Down

0 comments on commit 7c25f57

Please sign in to comment.