Skip to content

Commit

Permalink
WIP: 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.
  • Loading branch information
dlorenc committed Nov 11, 2019
1 parent 98dbc0b commit ecd785c
Show file tree
Hide file tree
Showing 24 changed files with 2,419 additions and 92 deletions.
5 changes: 4 additions & 1 deletion Gopkg.lock

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

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

This file was deleted.

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

This file was deleted.

15 changes: 9 additions & 6 deletions cmd/pullrequest-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ import (
"flag"
"fmt"

"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() {
Expand All @@ -35,7 +38,7 @@ func main() {
defer logger.Sync()
ctx := context.Background()

client, err := NewGitHubHandler(logger, *prURL)
client, err := pullrequest.NewSCMHandler(logger, *prURL, *provider)
if err != nil {
logger.Fatalf("error creating GitHub client: %v", err)
}
Expand All @@ -48,13 +51,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
8 changes: 4 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 @@ -445,10 +445,10 @@ Params that can be added are the following:
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
22 changes: 15 additions & 7 deletions pkg/apis/pipeline/v1alpha1/pull_request_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

const (
prSource = "pr-source"
githubTokenEnv = "githubToken"
prSource = "pr-source"
authTokenEnv = "authToken"
)

// PullRequestResource is an endpoint from which to get data which is required
Expand All @@ -35,9 +35,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 @@ -58,6 +60,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 @@ -82,9 +86,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 @@ -104,10 +109,13 @@ 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, githubTokenEnv) {
if strings.EqualFold(sec.FieldName, authTokenEnv) {
ev := corev1.EnvVar{
Name: strings.ToUpper(sec.FieldName),
ValueFrom: &corev1.EnvVarSource{
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: "GITHUBTOKEN",
Name: "AUTHTOKEN",
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Expand Down
2 changes: 1 addition & 1 deletion cmd/pullrequest-init/api.go → pkg/pullrequest/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package pullrequest

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package pullrequest

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion cmd/pullrequest-init/disk.go → pkg/pullrequest/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package pullrequest

import (
"encoding/gob"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package main
package pullrequest

import (
"encoding/json"
Expand Down
Loading

0 comments on commit ecd785c

Please sign in to comment.