http response prometheus1m /api/v1/status/flags 200 {"status":"success","data":{"storage.tsdb.retention.time": "1d"}}
http response prometheus1m /api/v1/metadata 200 {"status":"success","data":{}}
http response prometheus1m /api/v1/status/config 200 {"status":"success","data":{"yaml":"global:\n  scrape_interval: 1m\n"}}
http response prometheus1m /api/v1/query_range 200 {"status":"success","data":{"resultType":"matrix","result":[]}}
http response prometheus1m /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[]}}
http start prometheus1m 127.0.0.1:1094

http response prometheus5m /api/v1/status/flags 200 {"status":"success","data":{"storage.tsdb.retention.time": "1d"}}
http response prometheus5m /api/v1/metadata 200 {"status":"success","data":{}}
http response prometheus5m /api/v1/status/config 200 {"status":"success","data":{"yaml":"global:\n  scrape_interval: 5m\n"}}
http response prometheus5m /api/v1/query_range 200 {"status":"success","data":{"resultType":"matrix","result":[]}}
http response prometheus5m /api/v1/query 200 {"status":"success","data":{"resultType":"vector","result":[]}}
http start prometheus5m 127.0.0.1:2094

http response bitbucket /plugins/servlet/applinks/whoami 200 pint
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests 200 {"size":1,"isLastPage":true,"values":[{"id":1,"open":true,"fromRef":{"id":"refs/heads/v2","latestCommit":"fake-commit-id"},"toRef":{"id":"refs/heads/main","latestCommit":"fake-commit-id"}}]}
http response bitbucket /rest/api/latest/projects/prometheus/repos/rules/pull-requests/1/activities 200 {"size":0,"isLastPage":true,"values":[]}
http response bitbucket /rest/api/1.0/projects/prometheus/repos/rules/pull-requests/1/comments 200 {}
http start bitbucket 127.0.0.1:6094

mkdir testrepo
cd testrepo
exec git init --initial-branch=main .

cp ../src/v1.yml rules.yml
exec ln -s rules.yml symlink.yml
exec ln -s symlink.yml double.yml
exec ln -s rules.yml ../ignored.yml
cp ../src/.pint.hcl .
env GIT_AUTHOR_NAME=pint
env GIT_AUTHOR_EMAIL=pint@example.com
env GIT_COMMITTER_NAME=pint
env GIT_COMMITTER_EMAIL=pint@example.com
exec git add .
exec git commit -am 'import rules and config'

exec git checkout -b v2
cp ../src/v2.yml rules.yml
exec git commit -am 'v2'

env BITBUCKET_AUTH_TOKEN="12345"
! exec pint -s -l debug -d promql/series --no-color ci
! stdout .

stderr 'path=rules.yml rule=rule1'
stderr 'path=rules.yml rule=rule2'
stderr 'path=symlink.yml rule=rule1'
stderr 'path=symlink.yml rule=rule2'
stderr 'path=double.yml rule=rule1'
stderr 'path=double.yml rule=rule2'
! stderr 'path=ignored.yml'

stderr 'level=INFO msg="Problems found" Bug=4'

! stderr '---> rules.yml:5 Bug: Duration for `rate'
! stderr '---> rules.yml:7 Bug: Duration for `rate'
stderr '---> symlink.yml ~> rules.yml:5'
stderr '---> symlink.yml ~> rules.yml:7'
stderr '---> double.yml ~> rules.yml:5'
stderr '---> double.yml ~> rules.yml:7'

cmp bitbucket.got ../bitbucket.expected

-- src/v1.yml --
groups:
- name: foo
  rules:
  - alert: rule1
    expr: rate(errors_total[5m]) > 0
  - alert: rule2
    expr: rate(errors_total[5m]) > 0

-- src/v2.yml --
groups:
- name: foo
  rules:
  - alert: rule1
    expr: rate(errors_total[2m]) > 0
  - alert: rule2
    expr: rate(errors_total[2m]) > 0

-- src/.pint.hcl --
ci {
  baseBranch = "main"
}
repository {
  bitbucket {
    uri        = "http://127.0.0.1:6094"
    timeout    = "10s"
    project    = "prometheus"
    repository = "rules"
  }
}
prometheus "1m" {
  uri      = "http://127.0.0.1:1094"
  timeout  = "5s"
  required = true
  include = ["rules.yml"]
}
prometheus "5m" {
  uri      = "http://127.0.0.1:2094"
  timeout  = "5s"
  required = true
  include = ["symlink.yml", "double.yml"]
}

-- bitbucket.expected --
GET /rest/api/1.0/projects/prometheus/repos/rules/commits/.*/pull-requests
  Accept-Encoding: gzip
  Authorization: Bearer "12345"
  Content-Type: application/json

GET /plugins/servlet/applinks/whoami
  Accept-Encoding: gzip
  Authorization: Bearer "12345"
  Content-Type: application/json

GET /rest/api/latest/projects/prometheus/repos/rules/pull-requests/1/activities
  Accept-Encoding: gzip
  Authorization: Bearer "12345"
  Content-Type: application/json

POST /rest/api/1.0/projects/prometheus/repos/rules/pull-requests/1/comments
  Accept-Encoding: gzip
  Authorization: Bearer "12345"
  Content-Type: application/json
--- BODY ---
text: |
    :stop_sign: **Bug** reported by [pint](https://cloudflare.github.io/pint/) **promql/rate** check.

    <details>
    <summary>duration too small</summary>

    ```yaml
    5 |     expr: rate(errors_total[2m]) > 0
                  ^^^^^^^^^^^^^^^^^^^^^^
    ```

    Duration for `rate()` must be at least 2 x scrape_interval, `5m` Prometheus server at http://127.0.0.1:2094 is using `5m` scrape_interval.

    Using [rate](https://prometheus.io/docs/prometheus/latest/querying/functions/#rate) and [irate](https://prometheus.io/docs/prometheus/latest/querying/functions/#irate) function comes with a few requirements:

    - The metric you calculate (i)rate from must be a counter or a native histogram.
    - The time window of the (i)rate function must have at least 2 samples.

    The type of your metric is defined by the application that exports that metric.
    The number of samples depends on how often your application is being scraped by Prometheus.
    Each scrape produces a sample, so if your application is scraped every minute then the minimal time window you can use is two minutes.

    </details>

    :leftwards_arrow_with_hook: This problem was detected on a symlinked file `double.yml`.

    ------

    :information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/promql/rate.html).
severity: NORMAL
anchor:
    path: rules.yml
    lineType: ADDED
    fileType: TO
    diffType: EFFECTIVE
    line: 5
--- END ---

POST /rest/api/1.0/projects/prometheus/repos/rules/pull-requests/1/comments
  Accept-Encoding: gzip
  Authorization: Bearer "12345"
  Content-Type: application/json
--- BODY ---
text: |
    :stop_sign: **Bug** reported by [pint](https://cloudflare.github.io/pint/) **promql/rate** check.

    <details>
    <summary>duration too small</summary>

    ```yaml
    7 |     expr: rate(errors_total[2m]) > 0
                  ^^^^^^^^^^^^^^^^^^^^^^
    ```

    Duration for `rate()` must be at least 2 x scrape_interval, `5m` Prometheus server at http://127.0.0.1:2094 is using `5m` scrape_interval.

    Using [rate](https://prometheus.io/docs/prometheus/latest/querying/functions/#rate) and [irate](https://prometheus.io/docs/prometheus/latest/querying/functions/#irate) function comes with a few requirements:

    - The metric you calculate (i)rate from must be a counter or a native histogram.
    - The time window of the (i)rate function must have at least 2 samples.

    The type of your metric is defined by the application that exports that metric.
    The number of samples depends on how often your application is being scraped by Prometheus.
    Each scrape produces a sample, so if your application is scraped every minute then the minimal time window you can use is two minutes.

    </details>

    :leftwards_arrow_with_hook: This problem was detected on a symlinked file `double.yml`.

    ------

    :information_source: To see documentation covering this check and instructions on how to resolve it [click here](https://cloudflare.github.io/pint/checks/promql/rate.html).
severity: NORMAL
anchor:
    path: rules.yml
    lineType: ADDED
    fileType: TO
    diffType: EFFECTIVE
    line: 7
--- END ---

