Skip to content

Commit

Permalink
Add requested_team tag (#256)
Browse files Browse the repository at this point in the history
* refactor: expandSeriesByValues

* Add `requested_team` tag
  • Loading branch information
int128 authored Feb 26, 2022
1 parent ed5aa20 commit 296de8e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,9 @@ It has the following tags:
- `draft` = `true` or `false`
- `base_ref`
- `head_ref`
- `label` = label(s) of a pull request
- `merged` = `true` or `false`
- `requested_team` = team(s) of requested reviewer(s)
- `label` = label(s) of a pull request

You can set `send-pull-request-labels` to use `label` tag in Datadog.
If a pull request has multiple labels, this action sends the metrics for each label.
Expand Down
8 changes: 4 additions & 4 deletions src/pullRequest/expand.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Series } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-v1/models/Series'

export const expandSeriesByLabels = (series: Series[], labels: { name: string }[]): Series[] => {
if (labels.length === 0) {
export const expandSeriesByValues = (series: Series[], key: string, values: string[]): Series[] => {
if (values.length === 0) {
return series
}
return series.flatMap((s) =>
labels.map((label) => ({
values.map((v) => ({
...s,
tags: [...(s.tags ?? []), `label:${label.name}`],
tags: [...(s.tags ?? []), `${key}:${v}`],
}))
)
}
24 changes: 19 additions & 5 deletions src/pullRequest/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Series } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-v1/models/Series'
import { PullRequestClosedEvent, PullRequestEvent, PullRequestOpenedEvent } from '@octokit/webhooks-types'
import { ClosedPullRequest } from '../queries/closedPullRequest'
import { expandSeriesByLabels } from './expand'
import { expandSeriesByValues } from './expand'

const computeCommonTags = (e: PullRequestEvent): string[] => {
const tags = [
Expand Down Expand Up @@ -136,12 +136,26 @@ export const computePullRequestClosedMetrics = (
)
}

// Datadog treats a tag as combination of values.
// For example, if we send a metric with tags `label:foo` and `label:bar`, Datadog will show `label:foo,bar`.
// Here send a metric for each tag
let expanded: Series[] = series

expanded = expandSeriesByValues(
expanded,
'requested_team',
e.pull_request.requested_teams.map((team) => team.name)
)

if (options.sendPullRequestLabels) {
// Datadog treats a tag as combination of values.
// For example, if we send a metric with tags `label:foo` and `label:bar`, Datadog will show `label:foo,bar`.
return expandSeriesByLabels(series, e.pull_request.labels)
expanded = expandSeriesByValues(
expanded,
'label',
e.pull_request.labels.map((l) => l.name)
)
}
return series

return expanded
}

const unixTime = (s: string): number => Date.parse(s) / 1000
10 changes: 5 additions & 5 deletions tests/pullRequest/expand.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Series } from '@datadog/datadog-api-client/dist/packages/datadog-api-client-v1/models/Series'
import { expandSeriesByLabels } from '../../src/pullRequest/expand'
import { expandSeriesByValues } from '../../src/pullRequest/expand'

test('empty', () => {
expect(expandSeriesByLabels([], [])).toStrictEqual([])
expect(expandSeriesByValues([], 'label', [])).toStrictEqual([])
})

test('expand series by no label', () => {
Expand All @@ -16,7 +16,7 @@ test('expand series by no label', () => {
points: [[1579721588, 2]],
},
]
expect(expandSeriesByLabels(series, [])).toStrictEqual(series)
expect(expandSeriesByValues(series, 'label', [])).toStrictEqual(series)
})

test('expand series by 1 label', () => {
Expand All @@ -30,7 +30,7 @@ test('expand series by 1 label', () => {
points: [[1579721588, 2]],
},
]
expect(expandSeriesByLabels(series, [{ name: 'app' }])).toStrictEqual([
expect(expandSeriesByValues(series, 'label', ['app'])).toStrictEqual([
{
metric: 'github.actions.pull_request_closed.total',
points: [[1579721588, 1]],
Expand All @@ -55,7 +55,7 @@ test('expand series by 2 labels', () => {
points: [[1579721588, 2]],
},
]
expect(expandSeriesByLabels(series, [{ name: 'app' }, { name: 'critical' }])).toStrictEqual([
expect(expandSeriesByValues(series, 'label', ['app', 'critical'])).toStrictEqual([
{
metric: 'github.actions.pull_request_closed.total',
points: [[1579721588, 1]],
Expand Down

0 comments on commit 296de8e

Please sign in to comment.