-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add github pkg to webhook #1230
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,77 @@ | ||||||
/* | ||||||
Copyright 2018 The Skaffold Authors | ||||||
|
||||||
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 github | ||||||
|
||||||
import ( | ||||||
"context" | ||||||
"log" | ||||||
"os" | ||||||
|
||||||
"github.com/GoogleContainerTools/skaffold/pkg/webhook/constants" | ||||||
"github.com/google/go-github/github" | ||||||
"github.com/pkg/errors" | ||||||
"golang.org/x/oauth2" | ||||||
) | ||||||
|
||||||
// Client provides the context and client with necessary auth | ||||||
// for interacting with the Github API | ||||||
type Client struct { | ||||||
ctx context.Context | ||||||
*github.Client | ||||||
} | ||||||
|
||||||
// NewClient returns a github client with the necessary auth | ||||||
func NewClient() *Client { | ||||||
githubToken := os.Getenv(constants.GithubAccessToken) | ||||||
// Setup the token for github authentication | ||||||
ts := oauth2.StaticTokenSource( | ||||||
&oauth2.Token{AccessToken: githubToken}, | ||||||
) | ||||||
tc := oauth2.NewClient(context.Background(), ts) | ||||||
|
||||||
// Return a client instance from github | ||||||
client := github.NewClient(tc) | ||||||
return &Client{ | ||||||
Client: client, | ||||||
ctx: context.Background(), | ||||||
} | ||||||
} | ||||||
|
||||||
// CommentOnPR comments message on the PR | ||||||
func (g *Client) CommentOnPR(pr *github.PullRequestEvent, message string) error { | ||||||
comment := &github.IssueComment{ | ||||||
Body: &[]string{message}[0], | ||||||
} | ||||||
|
||||||
log.Printf("Creating comment on PR %d", pr.PullRequest.GetNumber()) | ||||||
_, _, err := g.Client.Issues.CreateComment(g.ctx, constants.GithubOwner, constants.GithubRepo, pr.PullRequest.GetNumber(), comment) | ||||||
if err != nil { | ||||||
return errors.Wrap(err, "creating github comment") | ||||||
} | ||||||
log.Printf("Succesfully commented on PR %d.", pr.GetNumber()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return nil | ||||||
} | ||||||
|
||||||
// RemoveLabelFromPR removes label from pr | ||||||
func (g *Client) RemoveLabelFromPR(pr *github.PullRequestEvent, label string) error { | ||||||
_, err := g.Client.Issues.DeleteLabel(g.ctx, constants.GithubOwner, constants.GithubRepo, label) | ||||||
if err != nil { | ||||||
return errors.Wrap(err, "deleting label") | ||||||
} | ||||||
log.Printf("Successfully deleted label from PR %d", pr.GetNumber()) | ||||||
return nil | ||||||
} |
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
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 | ||||
---|---|---|---|---|---|---|
|
@@ -18,15 +18,17 @@ package main | |||||
|
||||||
import ( | ||||||
"encoding/json" | ||||||
"fmt" | ||||||
"log" | ||||||
"net/http" | ||||||
|
||||||
"github.com/GoogleContainerTools/skaffold/pkg/webhook/kubernetes" | ||||||
"github.com/GoogleContainerTools/skaffold/pkg/webhook/labels" | ||||||
"github.com/google/go-github/github" | ||||||
"github.com/pkg/errors" | ||||||
|
||||||
"github.com/GoogleContainerTools/skaffold/pkg/webhook/constants" | ||||||
"github.com/google/go-github/github" | ||||||
pkggithub "github.com/GoogleContainerTools/skaffold/pkg/webhook/github" | ||||||
) | ||||||
|
||||||
const ( | ||||||
|
@@ -69,7 +71,7 @@ func handlePullRequestEvent(event *github.PullRequestEvent) error { | |||||
|
||||||
prNumber := event.GetNumber() | ||||||
|
||||||
if event.PullRequest.GetMerged() || event.PullRequest.ClosedAt == nil { | ||||||
if event.PullRequest.GetState() != constants.OpenState { | ||||||
log.Printf("Pull request %d is either merged or closed, skipping docs deployment", prNumber) | ||||||
return nil | ||||||
} | ||||||
|
@@ -79,6 +81,12 @@ func handlePullRequestEvent(event *github.PullRequestEvent) error { | |||||
return nil | ||||||
} | ||||||
|
||||||
// If a PR was relabeled, we need to first cleanup preexisting deployments | ||||||
if err := kubernetes.CleanupDeployment(event); err != nil { | ||||||
return errors.Wrap(err, "cleaning up deployment") | ||||||
} | ||||||
|
||||||
// Create service for the PR and get the associated external IP | ||||||
log.Printf("Label %s found on PR %d, creating service", constants.DocsLabel, prNumber) | ||||||
svc, err := kubernetes.CreateService(event) | ||||||
if err != nil { | ||||||
|
@@ -90,14 +98,26 @@ func handlePullRequestEvent(event *github.PullRequestEvent) error { | |||||
return errors.Wrap(err, "getting external IP") | ||||||
} | ||||||
|
||||||
// Create a deployment which maps to the service | ||||||
log.Printf("Creating deployment for pull request %d", prNumber) | ||||||
deployment, err := kubernetes.CreateDeployment(event, svc, ip) | ||||||
if err != nil { | ||||||
return errors.Wrap(err, "creating deployment") | ||||||
return errors.Wrapf(err, "creating deployment for PR %d", prNumber) | ||||||
} | ||||||
if err := kubernetes.WaitForDeploymentToStabilize(deployment); err != nil { | ||||||
return errors.Wrapf(err, "waiting for deployment %s to stabilize", deployment.Name) | ||||||
} | ||||||
// TODO: priyawadhwa@ to add logic for commenting on Github once the deployment is ready | ||||||
|
||||||
// Now, comment on the PR and remove the docs-modifications label | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
githubClient := pkggithub.NewClient() | ||||||
baseURL := kubernetes.BaseURL(ip) | ||||||
msg := fmt.Sprintf("Please visit [%s](%s) to view changes to the docs.", baseURL, baseURL) | ||||||
if err := githubClient.CommentOnPR(event, msg); err != nil { | ||||||
return errors.Wrapf(err, "comenting on PR %d", prNumber) | ||||||
} | ||||||
if err := githubClient.RemoveLabelFromPR(event, constants.DocsLabel); err != nil { | ||||||
return errors.Wrapf(err, "removing %s label from PR %d", constants.DocsLabel, prNumber) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks weird... why isn't this just
message
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoops my bad, Body requires a string pointer and previously I was setting "message" directly in the array. fixed now!