Skip to content

Commit

Permalink
Add gitlab support to the PR resource.
Browse files Browse the repository at this point in the history
This refactors the PR init container a bit to add support for multiple SCM providers.
This PR also adds support for gitlab, as the second supported SCM provider. To do this,
we needed a few other changes:
- Switch from CombinedStatus to Statuses
- Switch from the Issues service to the PullRequests service for labels and comments. These
are equivalent for Github, but not Gitlan and other providers.
- Update go-scm
  • Loading branch information
dlorenc committed Dec 3, 2019
1 parent de4fe10 commit 7978c12
Show file tree
Hide file tree
Showing 30 changed files with 3,007 additions and 237 deletions.
8 changes: 5 additions & 3 deletions Gopkg.lock

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

43 changes: 0 additions & 43 deletions cmd/pullrequest-init/github.go

This file was deleted.

29 changes: 0 additions & 29 deletions cmd/pullrequest-init/github_test.go

This file was deleted.

21 changes: 15 additions & 6 deletions cmd/pullrequest-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,32 @@ import (
"context"
"flag"
"fmt"
"go.uber.org/zap"
"os"

"github.com/tektoncd/pipeline/pkg/pullrequest"

"knative.dev/pkg/logging"
)

var (
prURL = flag.String("url", "", "The url of the pull request to initialize.")
path = flag.String("path", "", "Path of directory under which PR will be copied")
mode = flag.String("mode", "download", "Whether to operate in download or upload mode")
prURL = flag.String("url", "", "The url of the pull request to initialize.")
path = flag.String("path", "", "Path of directory under which PR will be copied")
mode = flag.String("mode", "download", "Whether to operate in download or upload mode")
provider = flag.String("provider", "", "The SCM provider to use. Optional")
)

func main() {
flag.Parse()
logger, _ := logging.NewLogger("", "pullrequest-init")
logger = logger.With(
zap.String("resource_type", "pullrequest"),
zap.String("mode", *mode))
defer logger.Sync()
ctx := context.Background()

client, err := NewGitHubHandler(ctx, logger, *prURL)
token := os.Getenv("AUTH_TOKEN")
client, err := pullrequest.NewSCMHandler(logger, *prURL, *provider, token)
if err != nil {
logger.Fatalf("error creating GitHub client: %v", err)
}
Expand All @@ -48,13 +57,13 @@ func main() {
fmt.Println(err)
logger.Fatal(err)
}
if err := ToDisk(pr, *path); err != nil {
if err := pullrequest.ToDisk(pr, *path); err != nil {
logger.Fatal(err)
}

case "upload":
logger.Info("RUNNING UPLOAD!")
r, err := FromDisk(*path)
r, err := pullrequest.FromDisk(*path)
if err != nil {
logger.Fatal(err)
}
Expand Down
10 changes: 6 additions & 4 deletions docs/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ spec:
- name: url
value: https://github.com/wizzbangcorp/wizzbang/pulls/1
secrets:
- fieldName: githubToken
- fieldName: authToken
secretName: github-secrets
secretKey: token
---
Expand All @@ -439,16 +439,18 @@ data:
Params that can be added are the following:

1. `url`: represents the location of the pull request to fetch.
1. `provider`: represents the SCM provider to use. This will be "guessed" based on the url if not set.
Valid values are `github` or `gitlab` today.

#### Statuses

The following status codes are available to use for the Pull Request resource:
https://godoc.org/github.com/jenkins-x/go-scm/scm#State

#### GitHub
#### Pull Request

The `pullRequest` resource will look for GitHub OAuth authentication tokens in
spec secrets with a field name called `githubToken`.
The `pullRequest` resource will look for GitHub or Gitlab OAuth authentication tokens in
spec secrets with a field name called `authToken`.

URLs should be of the form: https://github.com/tektoncd/pipeline/pull/1

Expand Down
26 changes: 17 additions & 9 deletions pkg/apis/pipeline/v1alpha1/pull_request_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
)

const (
prSource = "pr-source"
githubTokenField = "githubToken"
prSource = "pr-source"
authTokenField = "authToken"
// nolint: gosec
githubTokenEnv = "GITHUB_TOKEN"
authTokenEnv = "AUTH_TOKEN"
)

// PullRequestResource is an endpoint from which to get data which is required
Expand All @@ -37,9 +37,11 @@ type PullRequestResource struct {
Name string `json:"name"`
Type PipelineResourceType `json:"type"`

// GitHub URL pointing to the pull request.
// URL pointing to the pull request.
// Example: https://github.com/owner/repo/pulls/1
URL string `json:"url"`
// SCM provider (github or gitlab today). This will be guessed from URL if not set.
Provider string `json:"provider"`
// Secrets holds a struct to indicate a field name and corresponding secret name to populate it.
Secrets []SecretParam `json:"secrets"`

Expand All @@ -60,6 +62,8 @@ func NewPullRequestResource(prImage string, r *PipelineResource) (*PullRequestRe
for _, param := range r.Spec.Params {
if strings.EqualFold(param.Name, "URL") {
prResource.URL = param.Value
} else if strings.EqualFold(param.Name, "Provider") {
prResource.Provider = param.Value
}
}

Expand All @@ -84,9 +88,10 @@ func (s *PullRequestResource) GetURL() string {
// Replacements is used for template replacement on a PullRequestResource inside of a Taskrun.
func (s *PullRequestResource) Replacements() map[string]string {
return map[string]string{
"name": s.Name,
"type": string(s.Type),
"url": s.URL,
"name": s.Name,
"type": string(s.Type),
"url": s.URL,
"provider": s.Provider,
}
}

Expand All @@ -106,12 +111,15 @@ func (s *PullRequestResource) GetOutputTaskModifier(ts *TaskSpec, sourcePath str

func (s *PullRequestResource) getSteps(mode string, sourcePath string) []Step {
args := []string{"-url", s.URL, "-path", sourcePath, "-mode", mode}
if s.Provider != "" {
args = append(args, []string{"-provider", s.Provider}...)
}

evs := []corev1.EnvVar{}
for _, sec := range s.Secrets {
if strings.EqualFold(sec.FieldName, githubTokenField) {
if strings.EqualFold(sec.FieldName, authTokenField) {
ev := corev1.EnvVar{
Name: githubTokenEnv,
Name: authTokenEnv,
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Expand Down
24 changes: 13 additions & 11 deletions pkg/apis/pipeline/v1alpha1/pull_request_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ func TestPullRequest_NewResource(t *testing.T) {
url := "https://github.com/tektoncd/pipeline/pulls/1"
pr := tb.PipelineResource("foo", "default", tb.PipelineResourceSpec(
v1alpha1.PipelineResourceTypePullRequest,
tb.PipelineResourceSpecParam("type", "github"),
tb.PipelineResourceSpecParam("url", url),
tb.PipelineResourceSpecSecretParam("githubToken", "test-secret-key", "test-secret-name"),
tb.PipelineResourceSpecParam("provider", "github"),
tb.PipelineResourceSpecSecretParam("authToken", "test-secret-key", "test-secret-name"),
))
got, err := v1alpha1.NewPullRequestResource("override-with-pr:latest", pr)
if err != nil {
t.Fatalf("Error creating storage resource: %s", err.Error())
}

want := &v1alpha1.PullRequestResource{
Name: pr.Name,
Type: v1alpha1.PipelineResourceTypePullRequest,
URL: url,
Secrets: pr.Spec.SecretParams,
PRImage: "override-with-pr:latest",
Name: pr.Name,
Type: v1alpha1.PipelineResourceTypePullRequest,
URL: url,
Provider: "github",
Secrets: pr.Spec.SecretParams,
PRImage: "override-with-pr:latest",
}
if diff := cmp.Diff(want, got); diff != "" {
t.Error(diff)
Expand Down Expand Up @@ -85,20 +86,21 @@ func containerTestCases(mode string) []testcase {
Name: "creds",
URL: "https://example.com",
Secrets: []v1alpha1.SecretParam{{
FieldName: "githubToken",
FieldName: "authToken",
SecretName: "github-creds",
SecretKey: "token",
}},
PRImage: "override-with-pr:latest",
PRImage: "override-with-pr:latest",
Provider: "github",
},
out: []v1alpha1.Step{{Container: corev1.Container{
Name: "pr-source-creds-mz4c7",
Image: "override-with-pr:latest",
WorkingDir: v1alpha1.WorkspaceDir,
Command: []string{"/ko-app/pullrequest-init"},
Args: []string{"-url", "https://example.com", "-path", "/workspace", "-mode", mode},
Args: []string{"-url", "https://example.com", "-path", "/workspace", "-mode", mode, "-provider", "github"},
Env: []corev1.EnvVar{{
Name: "GITHUB_TOKEN",
Name: "AUTH_TOKEN",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Expand Down
Loading

0 comments on commit 7978c12

Please sign in to comment.