Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
Use CommittedDate as a fallback if PushedDate is not available.
Browse files Browse the repository at this point in the history
The fix for #22 makes github-pr-resource skip PRs if the the latest
commit in a PR pushed has a later date than one in PR being pushed.
This fix works around this by using `PushedDate` by default and
resorting to `CommitDate` iff `PushedDate` is not available.

This should bring the best of both worlds -- checks for all PRs on
master and a chance for non-master-based PRs to be checked.

Signed-off-by: Milan Plzik <[email protected]>
  • Loading branch information
Milan Plzik committed May 15, 2019
1 parent b8d9dde commit edca4f5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Loop:
continue
}
// Filter out commits that are too old.
if !p.Tip.CommittedDate.Time.After(request.Version.CommittedDate) {
if !p.Age().After(request.Version.ChangedDate) {
continue
}

Expand Down Expand Up @@ -166,7 +166,7 @@ func (r CheckResponse) Len() int {
}

func (r CheckResponse) Less(i, j int) bool {
return r[j].CommittedDate.After(r[i].CommittedDate)
return r[j].ChangedDate.After(r[i].ChangedDate)
}

func (r CheckResponse) Swap(i, j int) {
Expand Down
14 changes: 7 additions & 7 deletions in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ func TestGet(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.GetParameters{},
pullRequest: createTestPR(1, "master", false, false),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
versionString: `{"pr":"pr1","commit":"commit1","changed":"0001-01-01T00:00:00Z"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"}]`,
},
{
Expand All @@ -52,11 +52,11 @@ func TestGet(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.GetParameters{},
pullRequest: createTestPR(1, "master", false, false),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
versionString: `{"pr":"pr1","commit":"commit1","changed":"0001-01-01T00:00:00Z"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"}]`,
},
{
Expand All @@ -69,11 +69,11 @@ func TestGet(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.GetParameters{},
pullRequest: createTestPR(1, "master", false, false),
versionString: `{"pr":"pr1","commit":"commit1","committed":"0001-01-01T00:00:00Z"}`,
versionString: `{"pr":"pr1","commit":"commit1","changed":"0001-01-01T00:00:00Z"}`,
metadataString: `[{"name":"pr","value":"1"},{"name":"url","value":"pr1 url"},{"name":"head_name","value":"pr1"},{"name":"head_sha","value":"oid1"},{"name":"base_name","value":"master"},{"name":"base_sha","value":"sha"},{"name":"message","value":"commit message1"},{"name":"author","value":"login1"}]`,
},
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestGetSkipDownload(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.GetParameters{SkipDownload: true},
},
Expand Down
14 changes: 12 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ type MetadataField struct {
type Version struct {
PR string `json:"pr"`
Commit string `json:"commit"`
CommittedDate time.Time `json:"committed,omitempty"`
ChangedDate time.Time `json:"changed,omitempty"`
}

// NewVersion constructs a new Version.
func NewVersion(p *PullRequest) Version {
return Version{
PR: strconv.Itoa(p.Number),
Commit: p.Tip.OID,
CommittedDate: p.Tip.CommittedDate.Time,
ChangedDate: p.Age(),
}
}

Expand All @@ -85,6 +85,15 @@ type PullRequest struct {
Tip CommitObject
}

// Age ...
func (p *PullRequest) Age() time.Time {
if p.Tip.PushedDate != nil {
return p.Tip.PushedDate.Time
}

return p.Tip.CommittedDate.Time
}

// PullRequestObject represents the GraphQL commit node.
// https://developer.github.com/v4/object/pullrequest/
type PullRequestObject struct {
Expand All @@ -106,6 +115,7 @@ type CommitObject struct {
ID string
OID string
CommittedDate githubv4.DateTime
PushedDate *githubv4.DateTime
Message string
Author struct {
User struct {
Expand Down
8 changes: 4 additions & 4 deletions out_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestPut(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.PutParameters{},
pullRequest: createTestPR(1, "master", false, false),
Expand All @@ -44,7 +44,7 @@ func TestPut(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.PutParameters{
Status: "success",
Expand All @@ -61,7 +61,7 @@ func TestPut(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.PutParameters{
Status: "failure",
Expand All @@ -79,7 +79,7 @@ func TestPut(t *testing.T) {
version: resource.Version{
PR: "pr1",
Commit: "commit1",
CommittedDate: time.Time{},
ChangedDate: time.Time{},
},
parameters: resource.PutParameters{
Comment: "comment",
Expand Down

0 comments on commit edca4f5

Please sign in to comment.