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

Feat/pull request generator #366

Merged
merged 6 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions api/v1alpha1/applicationset_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type ApplicationSetGenerator struct {
Matrix *MatrixGenerator `json:"matrix,omitempty"`
SCMProvider *SCMProviderGenerator `json:"scmProvider,omitempty"`
ClusterDecisionResource *DuckTypeGenerator `json:"clusterDecisionResource,omitempty"`
PullRequest *PullRequestGenerator `json:"pullRequest,omitempty"`
jgwest marked this conversation as resolved.
Show resolved Hide resolved
}

// ApplicationSetBaseGenerator include list item info
Expand All @@ -88,6 +89,7 @@ type ApplicationSetBaseGenerator struct {
Git *GitGenerator `json:"git,omitempty"`
SCMProvider *SCMProviderGenerator `json:"scmProvider,omitempty"`
ClusterDecisionResource *DuckTypeGenerator `json:"clusterDecisionResource,omitempty"`
PullRequest *PullRequestGenerator `json:"pullRequest,omitempty"`
}

// ListGenerator include items info
Expand Down Expand Up @@ -203,6 +205,29 @@ type SCMProviderGeneratorFilter struct {
BranchMatch *string `json:"branchMatch,omitempty"`
}

// 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"`
// Standard parameters.
RequeueAfterSeconds *int64 `json:"requeueAfterSeconds,omitempty"`
Template ApplicationSetTemplate `json:"template,omitempty"`
}

// PullRequestGenerator defines a connection info specific to GitHub.
type PullRequestGeneratorGithub struct {
// GitHub org or user to scan. Required.
Owner string `json:"owner"`
// GitHub repo name to scan. Required.
Repo string `json:"repo"`
// The GitHub API URL to talk to. If blank, use https://api.github.com/.
API string `json:"api,omitempty"`
// Authentication token reference.
TokenRef *SecretRef `json:"tokenRef,omitempty"`
// Labels is used to filter the PRs that you want to target
Labels []string `json:"labels,omitempty"`
}

// ApplicationSetStatus defines the observed state of ApplicationSet
type ApplicationSetStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand Down
61 changes: 61 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.

88 changes: 88 additions & 0 deletions docs/Generators-Pull-Request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Pull Request Generator

The Pull Request generator uses the API of an SCMaaS provider (eg GitHub/GitLab) to automatically discover open pull requests within an repository. This fits well with the style of building a test environment when you create a pull request.


```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapps
spec:
generators:
- pullRequest:
# See below for provider specific options.
github:
# ...
```

## GitHub

Specify the repository from which to fetch the Github Pull requests.

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapps
spec:
generators:
- pullRequest:
github:
# The GitHub organization or user.
owner: myorg
# The Github repository
repo: myrepository
# For GitHub Enterprise (optional)
api: https://git.example.com/
# Reference to a Secret containing an access token. (optional)
tokenRef:
secretName: github-token
key: token
# Labels is used to filter the PRs that you want to target. (optional)
labels:
- preview
template:
# ...
```

* `owner`: Required name of the GitHub organization or user.
* `repo`: Required name of the Github repositry.
* `api`: If using GitHub Enterprise, the URL to access it. (Optional)
* `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)

## Template

As with all generators, several keys are available for replacement in the generated application.

```yaml
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapps
spec:
generators:
- pullRequest:
# ...
template:
metadata:
name: 'myapp-{{ branch }}-{{ number }}'
spec:
source:
repoURL: 'https://github.com/myorg/myrepo.git'
targetRevision: '{{ head_sha }}'
path: kubernetes/
helm:
parameters:
- name: "image.tag"
value: "pull-{{ head_sha }}"
project: default
destination:
server: https://kubernetes.default.svc
namespace: default
```

* `number`: The ID number of the pull request.
* `branch`: The name of the branch of the pull request head.
* `head_sha`: This is the SHA of the head of the pull request.
3 changes: 2 additions & 1 deletion docs/Generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ As of this writing there are six generators:
- [Matrix generator](Generators-Matrix.md): The Matrix generator may be used to combine the generated parameters of two separate generators.
- [SCM Provider generator](Generators-SCM-Provider.md): The SCM Provider generator uses the API of an SCM provider (eg GitHub) to automatically discover repositories within an organization.
- [Cluster Decision Resource generator](Generators-Cluster-Decision-Resource.md): The Cluster Decision Resource generator is used to interface with Kubernetes custom resources that use custom resource-specific logic to decide which set of Argo CD clusters to deploy to.
- [Pull Request generator](Generators-Pull-Request.md): The Pull Request generator uses the API of an SCMaaS provider (eg GitHub) to automatically discover open pull requests within an repository.

If you are new to generators, begin with the **List** and **Cluster** generators. For more advanced use cases, see the documentation for the remaining generators above.
If you are new to generators, begin with the **List** and **Cluster** generators. For more advanced use cases, see the documentation for the remaining generators above.
40 changes: 40 additions & 0 deletions examples/pull-request-generator/pull-request-example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
name: myapp
spec:
generators:
- pullRequest:
github:
# The GitHub organization or user.
owner: myorg
# The Github repository
repo: myrepo
# For GitHub Enterprise. (optional)
api: https://git.example.com/
# Reference to a Secret containing an access token. (optional)
tokenRef:
secretName: github-token
key: token
# Labels is used to filter the PRs that you want to target. (optional)
labels:
- preview
template:
metadata:
name: 'myapp-{{ branch }}-{{ number }}'
spec:
source:
repoURL: 'https://github.com/myorg/myrepo.git'
targetRevision: '{{ head_sha }}'
path: helm-guestbook
helm:
parameters:
- name: "image.tag"
value: "pull-{{ head_sha }}"
project: default
destination:
server: https://kubernetes.default.svc
namespace: "{{ branch }}-{{ number }}"
syncPolicy:
syncOptions:
- CreateNamespace=true
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func main() {
"Git": generators.NewGitGenerator(services.NewArgoCDService(argoCDDB, argocdRepoServer)),
"SCMProvider": generators.NewSCMProviderGenerator(mgr.GetClient()),
"ClusterDecisionResource": generators.NewDuckTypeGenerator(context.Background(), dynClient, k8s, namespace),
"PullRequest": generators.NewPullRequestGenerator(mgr.GetClient()),
}

combineGenerators := map[string]generators.Generator{
Expand Down
Loading