From 296de8e28d37b5a9b1e387957f6b6816aac3fded Mon Sep 17 00:00:00 2001 From: Hidetake Iwata Date: Sat, 26 Feb 2022 18:06:57 +0900 Subject: [PATCH] Add `requested_team` tag (#256) * refactor: expandSeriesByValues * Add `requested_team` tag --- README.md | 3 ++- src/pullRequest/expand.ts | 8 ++++---- src/pullRequest/metrics.ts | 24 +++++++++++++++++++----- tests/pullRequest/expand.test.ts | 10 +++++----- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 0c17f96c..c7ea5b7a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/pullRequest/expand.ts b/src/pullRequest/expand.ts index ef1dad9a..9b9a49b1 100644 --- a/src/pullRequest/expand.ts +++ b/src/pullRequest/expand.ts @@ -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}`], })) ) } diff --git a/src/pullRequest/metrics.ts b/src/pullRequest/metrics.ts index 717cb5c8..d8a1d74f 100644 --- a/src/pullRequest/metrics.ts +++ b/src/pullRequest/metrics.ts @@ -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 = [ @@ -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 diff --git a/tests/pullRequest/expand.test.ts b/tests/pullRequest/expand.test.ts index 5d3aef8c..8fdd6d24 100644 --- a/tests/pullRequest/expand.test.ts +++ b/tests/pullRequest/expand.test.ts @@ -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', () => { @@ -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', () => { @@ -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]], @@ -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]],