-
Notifications
You must be signed in to change notification settings - Fork 654
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(echo): Trigger properly off Github Pull Requests (#609)
* fix(echo): Trigger properly off Github Pull Requests * Add test, add null checks * Refactor * Change mechanism of detecting push, change test * Default to push * Remove null conditional * Fix to string
- Loading branch information
1 parent
0213877
commit 00bf6ae
Showing
5 changed files
with
304 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...ebhooks/src/main/groovy/com/netflix/spinnaker/echo/scm/github/GithubPullRequestEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.echo.scm.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.netflix.spinnaker.echo.model.Event; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class GithubPullRequestEvent implements GithubWebhookEvent { | ||
Repository repository; | ||
|
||
@JsonProperty("pull_request") | ||
PullRequest pullRequest; | ||
|
||
// `.repository.full_name` | ||
@Override | ||
public String getFullRepoName(Event event, Map postedEvent) { | ||
return Optional.of(this) | ||
.map(GithubPullRequestEvent::getRepository) | ||
.map(Repository::getFullName) | ||
.orElse(""); | ||
} | ||
|
||
// `.repository.owner.login` | ||
@Override | ||
public String getRepoProject(Event event, Map postedEvent) { | ||
return Optional.of(this) | ||
.map(GithubPullRequestEvent::getRepository) | ||
.map(Repository::getOwner) | ||
.map(RepositoryOwner::getLogin) | ||
.orElse(""); | ||
} | ||
|
||
// `.repository.name` | ||
@Override | ||
public String getSlug(Event event, Map postedEvent) { | ||
return Optional.of(this) | ||
.map(GithubPullRequestEvent::getRepository) | ||
.map(Repository::getName) | ||
.orElse(""); | ||
} | ||
|
||
// `.pull_request.head.sha` | ||
// TODO | ||
@Override | ||
public String getHash(Event event, Map postedEvent) { | ||
return Optional.of(this) | ||
.map(GithubPullRequestEvent::getPullRequest) | ||
.map(PullRequest::getHead) | ||
.map(PullRequestHead::getSha) | ||
.orElse(""); | ||
} | ||
|
||
// `.pull_request.head.ref` | ||
// TODO | ||
@Override | ||
public String getBranch(Event event, Map postedEvent) { | ||
// Replace on "" still returns "", which is fine | ||
return Optional.of(this) | ||
.map(GithubPullRequestEvent::getPullRequest) | ||
.map(PullRequest::getHead) | ||
.map(PullRequestHead::getRef) | ||
.orElse(""); | ||
} | ||
|
||
@Data | ||
private static class Repository { | ||
RepositoryOwner owner; | ||
String name; | ||
|
||
@JsonProperty("full_name") | ||
String fullName; | ||
} | ||
|
||
@Data | ||
private static class RepositoryOwner { | ||
String login; | ||
} | ||
|
||
@Data | ||
private static class PullRequest { | ||
PullRequestHead head; | ||
} | ||
|
||
@Data | ||
private static class PullRequestHead { | ||
String ref; | ||
String sha; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
echo-webhooks/src/main/groovy/com/netflix/spinnaker/echo/scm/github/GithubPushEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.echo.scm.github; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.netflix.spinnaker.echo.model.Event; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class GithubPushEvent implements GithubWebhookEvent { | ||
Repository repository; | ||
String after; | ||
String ref; | ||
|
||
// `.repository.full_name` | ||
@Override | ||
public String getFullRepoName(Event event, Map postedEvent) { | ||
return Optional.of(this) | ||
.map(GithubPushEvent::getRepository) | ||
.map(Repository::getFullName) | ||
.orElse(""); | ||
} | ||
|
||
// `.repository.owner.name` | ||
@Override | ||
public String getRepoProject(Event event, Map postedEvent) { | ||
return Optional.of(this) | ||
.map(GithubPushEvent::getRepository) | ||
.map(Repository::getOwner) | ||
.map(RepositoryOwner::getName) | ||
.orElse(""); | ||
} | ||
|
||
// `.repository.name` | ||
@Override | ||
public String getSlug(Event event, Map postedEvent) { | ||
return Optional.of(this) | ||
.map(GithubPushEvent::getRepository) | ||
.map(Repository::getName) | ||
.orElse(""); | ||
} | ||
|
||
// `.after` | ||
@Override | ||
public String getHash(Event event, Map postedEvent) { | ||
return Optional.of(this).map(GithubPushEvent::getAfter).orElse(""); | ||
} | ||
|
||
// `.ref`, remove `refs/heads/` | ||
@Override | ||
public String getBranch(Event event, Map postedEvent) { | ||
// Replace on "" still returns "", which is fine | ||
return Optional.of(this).map(GithubPushEvent::getRef).orElse("").replace("refs/heads/", ""); | ||
} | ||
|
||
@Data | ||
private static class Repository { | ||
RepositoryOwner owner; | ||
String name; | ||
|
||
@JsonProperty("full_name") | ||
String fullName; | ||
} | ||
|
||
@Data | ||
private static class RepositoryOwner { | ||
String name; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
echo-webhooks/src/main/groovy/com/netflix/spinnaker/echo/scm/github/GithubWebhookEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2019 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.spinnaker.echo.scm.github; | ||
|
||
import com.netflix.spinnaker.echo.model.Event; | ||
import java.util.Map; | ||
|
||
public interface GithubWebhookEvent { | ||
String getFullRepoName(Event event, Map postedEvent); | ||
|
||
String getRepoProject(Event event, Map postedEvent); | ||
|
||
String getSlug(Event event, Map postedEvent); | ||
|
||
String getHash(Event event, Map postedEvent); | ||
|
||
String getBranch(Event event, Map postedEvent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters