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

feat: add support for Bitbucket Server (SCM + PR) #506

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
55 changes: 49 additions & 6 deletions api/v1alpha1/applicationset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,9 @@ type GitFileGeneratorItem struct {
// SCMProviderGenerator defines a generator that scrapes a SCMaaS API to find candidate repos.
type SCMProviderGenerator struct {
// Which provider to use and config for it.
Github *SCMProviderGeneratorGithub `json:"github,omitempty"`
Gitlab *SCMProviderGeneratorGitlab `json:"gitlab,omitempty"`
Github *SCMProviderGeneratorGithub `json:"github,omitempty"`
Gitlab *SCMProviderGeneratorGitlab `json:"gitlab,omitempty"`
BitbucketServer *SCMProviderGeneratorBitbucketServer `json:"bitbucketServer,omitempty"`
// Filters for which repos should be considered.
Filters []SCMProviderGeneratorFilter `json:"filters,omitempty"`
// Which protocol to use for the SCM URL. Default is provider-specific but ssh if possible. Not all providers
Expand All @@ -304,7 +305,7 @@ type SCMProviderGenerator struct {
Template ApplicationSetTemplate `json:"template,omitempty"`
}

// SCMProviderGeneratorGithub defines a connection info specific to GitHub.
// SCMProviderGeneratorGithub defines connection info specific to GitHub.
type SCMProviderGeneratorGithub struct {
// GitHub org to scan. Required.
Organization string `json:"organization"`
Expand All @@ -316,7 +317,7 @@ type SCMProviderGeneratorGithub struct {
AllBranches bool `json:"allBranches,omitempty"`
}

// SCMProviderGeneratorGitlab defines a connection info specific to Gitlab.
// SCMProviderGeneratorGitlab defines connection info specific to Gitlab.
type SCMProviderGeneratorGitlab struct {
// Gitlab group to scan. Required. You can use either the project id (recommended) or the full namespaced path.
Group string `json:"group"`
Expand All @@ -330,6 +331,18 @@ type SCMProviderGeneratorGitlab struct {
AllBranches bool `json:"allBranches,omitempty"`
}

// SCMProviderGeneratorBitbucketServer defines connection info specific to Bitbucket Server.
type SCMProviderGeneratorBitbucketServer struct {
// Project to scan. Required.
Project string `json:"project"`
// The Bitbucket Server REST API URL to talk to. Required.
API string `json:"api"`
// Credentials for Basic auth
BasicAuth *BasicAuthBitbucketServer `json:"basicAuth,omitempty"`
// Scan all branches instead of just the default branch.
AllBranches bool `json:"allBranches,omitempty"`
}

// SCMProviderGeneratorFilter is a single repository filter.
// If multiple filter types are set on a single struct, they will be AND'd together. All filters must
// pass for a repo to be included.
Expand All @@ -347,13 +360,16 @@ type SCMProviderGeneratorFilter struct {
// PullRequestGenerator defines a generator that scrapes a PullRequest API to find candidate pull requests.
type PullRequestGenerator struct {
// Which provider to use and config for it.
Github *PullRequestGeneratorGithub `json:"github,omitempty"`
Github *PullRequestGeneratorGithub `json:"github,omitempty"`
BitbucketServer *PullRequestGeneratorBitbucketServer `json:"bitbucketServer,omitempty"`
// Filters for which pull requests should be considered.
Filters []PullRequestGeneratorFilter `json:"filters,omitempty"`
// Standard parameters.
RequeueAfterSeconds *int64 `json:"requeueAfterSeconds,omitempty"`
Template ApplicationSetTemplate `json:"template,omitempty"`
}

// PullRequestGenerator defines a connection info specific to GitHub.
// PullRequestGenerator defines connection info specific to GitHub.
type PullRequestGeneratorGithub struct {
// GitHub org or user to scan. Required.
Owner string `json:"owner"`
Expand All @@ -367,6 +383,33 @@ type PullRequestGeneratorGithub struct {
Labels []string `json:"labels,omitempty"`
}

// PullRequestGenerator defines connection info specific to BitbucketServer.
type PullRequestGeneratorBitbucketServer struct {
// Project to scan. Required.
Project string `json:"project"`
// Repo name to scan. Required.
Repo string `json:"repo"`
// The Bitbucket REST API URL to talk to e.g. https://bitbucket.org/rest Required.
API string `json:"api"`
// Credentials for Basic auth
BasicAuth *BasicAuthBitbucketServer `json:"basicAuth,omitempty"`
}

// BasicAuthBitbucketServer defines the username/(password or personal access token) for Basic auth.
type BasicAuthBitbucketServer struct {
// Username for Basic auth
Username string `json:"username"`
// Password (or personal access token) reference.
PasswordRef *SecretRef `json:"passwordRef"`
}

// PullRequestGeneratorFilter is a single pull request filter.
// If multiple filter types are set on a single struct, they will be AND'd together. All filters must
// pass for a pull request to be included.
type PullRequestGeneratorFilter struct {
BranchMatch *string `json:"branchMatch,omitempty"`
}

// ApplicationSetStatus defines the observed state of ApplicationSet
type ApplicationSetStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
97 changes: 97 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

65 changes: 65 additions & 0 deletions docs/Generators-Pull-Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,71 @@ spec:
* `tokenRef`: A `Secret` name and key containing the GitHub access token to use for requests. If not specified, will make anonymous requests which have a lower rate limit and can only see public repositories. (Optional)
* `labels`: Labels is used to filter the PRs that you want to target. (Optional)

## Bitbucket Server

Fetch pull requests from a repo hosted on a Bitbucket Server (not the same as Bitbucket Cloud).

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapps
spec:
generators:
- pullRequest:
bitbucketServer:
mlosicki marked this conversation as resolved.
Show resolved Hide resolved
project: myproject
repo: myrepository
# URL of the Bitbucket Server. Required.
api: https://mycompany.bitbucket.org
# Credentials for Basic authentication. Required for private repositories.
basicAuth:
# The username to authenticate with
username: myuser
# Reference to a Secret containing the password or personal access token.
passwordRef:
secretName: mypassword
key: password
# Labels are not supported by Bitbucket Server, so filtering by label is not possible.
# Filter PRs using the source branch name. (optional)
filters:
- branchMatch: ".*-argocd"
template:
# ...
```

* `project`: Required name of the Bitbucket project
* `repo`: Required name of the Bitbucket repository.
* `api`: Required URL to access the Bitbucket REST API. For the example above, an API request would be made to `https://mycompany.bitbucket.org/rest/api/1.0/projects/myproject/repos/myrepository/pull-requests`
* `branchMatch`: Optional regexp filter which should match the source branch name. This is an alternative to labels which are not supported by Bitbucket Server.

If you want to access a private repository, you must also provide the credentials for Basic auth (this is the only auth supported currently):
* `username`: The username to authenticate with. It only needs read access to the relevant repo.
* `passwordRef`: A `Secret` name and key containing the password or personal access token to use for requests.

## Filters

Filters allow selecting which pull requests to generate for. Each filter can declare one or more conditions, all of which must pass. If multiple filters are present, any can match for a repository to be included. If no filters are specified, all pull requests will be processed.
Currently, only a subset of filters is available when comparing with SCM provider filters.

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapps
spec:
generators:
- scmProvider:
# ...
# Include any pull request ending with "argocd". (optional)
filters:
- branchMatch: ".*-argocd"
template:
# ...
```

* `branchMatch`: A regexp matched against source branch names.

## Template

As with all generators, several keys are available for replacement in the generated application.
Expand Down
41 changes: 41 additions & 0 deletions docs/Generators-SCM-Provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,47 @@ For label filtering, the repository tags are used.

Available clone protocols are `ssh` and `https`.

## Bitbucket Server

Use the Bitbucket Server API (1.0) to scan repos in a project. Note that Bitbucket Server is not to same as Bitbucket Cloud (API 2.0)

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapps
spec:
generators:
- scmProvider:
bitbucketServer:
project: myproject
# URL of the Bitbucket Server. Required.
api: https://mycompany.bitbucket.org
# If true, scan every branch of every repository. If false, scan only the default branch. Defaults to false.
allBranches: true
# Credentials for Basic authentication. Required for private repositories.
basicAuth:
# The username to authenticate with
username: myuser
# Reference to a Secret containing the password or personal access token.
passwordRef:
secretName: mypassword
key: password
# Support for filtering by labels is TODO. Bitbucket server labels are not supported for PRs, but they are for repos
template:
# ...
```

* `project`: Required name of the Bitbucket project
* `api`: Required URL to access the Bitbucket REST api.
* `allBranches`: By default (false) the template will only be evaluated for the default branch of each repo. If this is true, every branch of every repository will be passed to the filters. If using this flag, you likely want to use a `branchMatch` filter.

If you want to access a private repository, you must also provide the credentials for Basic auth (this is the only auth supported currently):
* `username`: The username to authenticate with. It only needs read access to the relevant repo.
* `passwordRef`: A `Secret` name and key containing the password or personal access token to use for requests.

Available clone protocols are `ssh` and `https`.

## Filters

Filters allow selecting which repositories to generate for. Each filter can declare one or more conditions, all of which must pass. If multiple filters are present, any can match for a repository to be included. If no filters are specified, all repositories will be processed.
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/argoproj/argo-cd/v2 v2.3.0-rc5.0.20220206192056-4b04a3918029
github.com/argoproj/gitops-engine v0.5.1-0.20220126184517-b0c5e00ccfa5
github.com/argoproj/pkg v0.11.1-0.20211203175135-36c59d8fafe0
github.com/gfleury/go-bitbucket-v1 v0.0.0-20220125132502-90a950f9bcba
github.com/go-logr/logr v1.2.2
github.com/google/go-github/v35 v35.0.0
github.com/imdario/mergo v0.3.12
Expand Down Expand Up @@ -89,6 +90,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20210610120745-9d4ed1856297 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -97,6 +99,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.11.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20220125132502-90a950f9bcba h1:JstvmY1XmxDNHsCqxzjNLbB+mUJDT/wQIinmCmnU0yM=
github.com/gfleury/go-bitbucket-v1 v0.0.0-20220125132502-90a950f9bcba/go.mod h1:LB3osS9X2JMYmTzcCArHHLrndBAfcVLQAvUddfs+ONs=
github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
Expand Down Expand Up @@ -672,6 +674,7 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/moby/ipvs v1.0.1/go.mod h1:2pngiyseZbIKXNv7hsKj3O9UEz30c53MT9005gt2hxQ=
Expand Down
Loading