Skip to content

Conversation

@markomirosavljev
Copy link

@markomirosavljev markomirosavljev commented Dec 27, 2025

Closes #25804

Summary

This PR adds 2 additional filters in applicationSet PR generator, createdWithin and updatedWithin which allow PR filtering based on time after they are created, or last time updated.

Motivation

  • In cases we have lots of applicationsets creating review envs using PR generator, we can end up having lots of apps that are created based off stale PRs on repositories that are not being worked on atm. This filtering can prevent ArgoCD from creating apps for PRs older than specific duration, or that are not updated within specific time duration.

Implementation

  • Added createdAt and updatedAt field to pull request parameters - both parsed from values returned by SCM APIs and will also be available for templating
  • Added createdWithin and updatedWithin filters on applicationset CRD and updated filtering functions to parse time.Duration from CRD while compiling and applying filters to applicationset.
  • Updated test cases to cover new fields in PR parameters, and extended test cases with filtering cases.

Manifests will look like this:

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      ....
  creationTimestamp: "2025-12-27T10:55:06Z"
  generation: 2
  name: pr-preview-apps
  namespace: argocd
  resourceVersion: "48678"
  uid: 5d376305-51f7-4fd3-8ee4-3e4c8b47daae
spec:
  generators:
  - pullRequest:
      filters:
      - createdWithin: 168h
      - updatedWithin: 24h
      github:
        labels:
        - preview
        owner: ownerorg
        repo: core
        tokenRef:
          key: password
          secretName: creds-3812896623
      requeueAfterSeconds: 120
  template:
    metadata:
      annotations:
        pr-createdat: '{{created_at}}'
        pr-updatedat: '{{updated_at}}'
      labels:
        app-type: pr-preview
        pr-number: '{{number}}'
      name: pr-{{number}}-preview
    spec:
      destination:
        namespace: pr-{{number}}
        server: https://kubernetes.default.svc
      project: default
      source:
        path: charts/core
        repoURL: https://github.com/ownerorg/core.git
        targetRevision: '{{head_sha}}'
      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
        - CreateNamespace=true

Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • The title of the PR states what changed and the related issues number (used for the release note).
  • The title of the PR conforms to the Title of the PR
  • I've included "Closes [ISSUE #]" or "Fixes [ISSUE #]" in the description to automatically close the associated issue.
  • I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • Does this PR require documentation updates?
  • I've updated documentation as required by this PR.
  • I have signed off all my commits as required by DCO
  • I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • My build is green (troubleshooting builds).
  • My new feature complies with the feature status guidelines.
  • I have added a brief description of why this PR is necessary and/or what this PR solves.
  • Optional. My organization is added to USERS.md.
  • Optional. For bug fixes, I've indicated what older releases this fix should be cherry-picked into (this may or may not happen depending on risk/complexity).

@markomirosavljev markomirosavljev requested a review from a team as a code owner December 27, 2025 11:42
@bunnyshell
Copy link

bunnyshell bot commented Dec 27, 2025

❗ Preview Environment stop on Bunnyshell failed

See: Environment Details | Pipeline Logs

Available commands (reply to this comment):

  • 🔴 /bns:stop to stop again the environment
  • 🔵 /bns:start to start the environment
  • 🚀 /bns:deploy to redeploy the environment
  • /bns:delete to remove the environment

Labels: getGithubPRLabelNames(pull.Labels),
Author: *pull.User.Login,
CreatedAt: pull.CreatedAt.Time,
UpdatedAt: pull.UpdatedAt.Time,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there no tests where we can add these fields? I see that we have tests for other providers that you edited to add the timestamp field but not for GitHub.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct. I've just added additional test cases for Github PR generator service.

Copy link
Member

@nitishfy nitishfy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the conflicts.

@markomirosavljev
Copy link
Author

Please fix the conflicts.

Conflicts resolved @nitishfy

Copy link
Contributor

@ppapapetrou76 ppapapetrou76 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a small log/event when a PR is skipped due to filtering (so users can debug why apps were not created).

Please make sure time comparisons use the time format Since/UTC properly to avoid DST/timezone surprises.

updatedTime = "2015-10-15T17:38:55.491628+00:00"
)

func parseBitbucketCloudTimeFromString(t string) time.Time {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function already exists above in another test

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this function from gitea_test.go and reused this one for both gitea and bitbucket_cloud

if filter.TitleMatch != nil && !filter.TitleMatch.MatchString(pullRequest.Title) {
return false
}
if filter.CreatedWithin != nil && pullRequest.CreatedAt.Before(time.Now().Add(-*filter.CreatedWithin)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createdWithin takes precedence over updatedWithin — please make this explicit in both code comments and user docs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mentioned this in the docs for createdWithin filter.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've mentioned this in the docs for createdWithin filter.

Can we also add a note in the CLI argument description. Most users will check the CLI output first rather than the dcos.
WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking if this comments and documentation updates are needed since this is pretty much the behavior for all conditions if they are provided within a single filter like this:

- branchMatch: "test-*"
  createdWithin: "12h"
  updatedWithin: "6h"

In this case filter wants all conditions to be true so if any of these conditions fail, PR will be filtered out and there will be no difference if 1st, 2nd or 3rd condition failed and any other passed.

I think it is my false assumption that there is "precedence" while I was initially working on this PR and I apologize, I will update PR description to remove that.

That is already mentioned in the docs here.

@markomirosavljev
Copy link
Author

Consider adding a small log/event when a PR is skipped due to filtering (so users can debug why apps were not created).

Please make sure time comparisons use the time format Since/UTC properly to avoid DST/timezone surprises.

@ppapapetrou76 That will be useful for debugging. I've added log that will show properties of pull request and filters for each PR that is filtered out during appset reconciliation.

Also, I've updated tests and date parsing methods for SCM providers to use UTC.

@markomirosavljev
Copy link
Author

Also, with this PR ApplicationSet CRD has issues when manifests are installed without --server-side flag. As I saw, this is already updated in developer guide docs as probably people had similar issues.

}
if filter.UpdatedWithin != nil && pullRequest.UpdatedAt.Before(time.Now().UTC().Add(-*filter.UpdatedWithin)) {
return false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make this code a little bit more readable by moving these lines in PullRequest struct methods ( interface.go ) file

You can have something like

func (p PullRequest) IsCreatedWithin(t *time.Duration) bool {
	return t != nil && p.CreatedAt.Before(time.Now().UTC().Add(-*t))
} 

Same for UpdatedWithin

WDYT?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, what do you think about moving all checks to methods?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but for now let's do the checks related to the context of this PR

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've created methods for checking if PR is created or updated within specified time duration.

Comment on lines 37 to 50
if filter.CreatedWithin != nil {
createdWithin, err := time.ParseDuration(*filter.CreatedWithin)
if err != nil {
return nil, fmt.Errorf("error parsing CreatedWithin duration %s: %w", *filter.CreatedWithin, err)
}
outFilter.CreatedWithin = &createdWithin
}
if filter.UpdatedWithin != nil {
updatedWithin, err := time.ParseDuration(*filter.UpdatedWithin)
if err != nil {
return nil, fmt.Errorf("error parsing UpdatedWithin duration %s: %w", *filter.UpdatedWithin, err)
}
outFilter.UpdatedWithin = &updatedWithin
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you think we can remove duplication by adding a function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we might want to do that for other filters too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's stick to time-based filter for now in this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated both filters to use function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ApplicationSet PR generator time based filters

3 participants