Skip to content
Open
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
14 changes: 14 additions & 0 deletions backend/plugins/gh-copilot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,22 @@ It follows the same structure/patterns as other DevLake data-source plugins (not
- `GET /orgs/{org}/copilot/billing/seats`
- `GET /orgs/{org}/copilot/metrics`

**AI credit usage endpoints** (GitHub Billing Usage REST API, version `2026-03-10`):

- `GET /enterprises/{enterprise}/settings/billing/ai_credit/usage`
- `GET /organizations/{org}/settings/billing/ai_credit/usage` (note: `organizations/`, not `orgs/`)

These are collected **daily** (one request per day, iterating `year`/`month`/`day`) and report Copilot
**AI credit consumption** broken down by product, SKU and model. Quantities and amounts are floats and the
`unit_type` is `ai-credits`.

**Stored data (tool layer)**:

- `_tool_copilot_org_metrics` (daily aggregates)
- `_tool_copilot_language_metrics` (editor/language breakdown)
- `_tool_copilot_seats` (seat assignments)
- `_tool_copilot_ai_credit_usage` (daily AI credit consumption: gross/net quantity and amount per
product/sku/model, at `enterprise` or `organization` level)

## Data flow (high level)

Expand Down Expand Up @@ -61,6 +72,9 @@ flowchart LR

- GitHub Copilot Business or Enterprise enabled for the target organization
- A token that can access GitHub Copilot billing/metrics (classic PAT with `manage_billing:copilot` works)
- For **AI credit usage**, the token additionally needs billing read access:
`manage_billing:copilot` (or `admin:enterprise` for the enterprise endpoint / `admin:org` for the
organization endpoint)

### 1) Create a connection

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
id,params,data,url,input,created_at
1,"{""ConnectionId"": 1, ""ScopeId"": ""octodemo"", ""Organization"": ""octodemo"", ""Endpoint"": ""https://api.github.com""}","{""timePeriod"": {""year"": 2026, ""month"": 6, ""day"": 8}, ""organization"": ""octodemo"", ""usageItems"": [{""product"": ""Copilot"", ""sku"": ""Copilot AI Credits"", ""model"": ""Claude Opus 4.8"", ""unitType"": ""ai-credits"", ""pricePerUnit"": 0.01, ""grossQuantity"": 5445.77595, ""grossAmount"": 54.4577595, ""discountQuantity"": 5445.77595, ""discountAmount"": 54.4577595, ""netQuantity"": 0.0, ""netAmount"": 0.0}, {""product"": ""Copilot"", ""sku"": ""Copilot AI Credits"", ""model"": ""Claude Sonnet 4.6"", ""unitType"": ""ai-credits"", ""pricePerUnit"": 0.01, ""grossQuantity"": 176.97417, ""grossAmount"": 1.7697417, ""discountQuantity"": 175.3533, ""discountAmount"": 1.753533, ""netQuantity"": 1.62087, ""netAmount"": 0.0162087}]}",https://api.github.com/organizations/octodemo/settings/billing/ai_credit/usage?year=2026&month=6&day=8,null,2026-06-09 00:00:00.000
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
connection_id,scope_id,level,year,month,day,product,sku,model,unit_type,cost_center_id,date,enterprise,organization,user_login,cost_center_name,price_per_unit,gross_quantity,gross_amount,discount_quantity,discount_amount,net_quantity,net_amount
1,octodemo,organization,2026,6,8,Copilot,Copilot AI Credits,Claude Opus 4.8,ai-credits,,2026-06-08T00:00:00.000+00:00,,octodemo,,,0.01,5445.77595,54.4577595,5445.77595,54.4577595,0,0
1,octodemo,organization,2026,6,8,Copilot,Copilot AI Credits,Claude Sonnet 4.6,ai-credits,,2026-06-08T00:00:00.000+00:00,,octodemo,,,0.01,176.97417,1.7697417,175.3533,1.753533,1.62087,0.0162087
8 changes: 8 additions & 0 deletions backend/plugins/gh-copilot/e2e/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ func TestCopilotMetricsDataFlow(t *testing.T) {

dataflowTester.ImportCsvIntoRawTable("./metrics/raw_tables/_raw_copilot_metrics.csv", "_raw_copilot_org_metrics")
dataflowTester.ImportCsvIntoRawTable("./metrics/raw_tables/_raw_copilot_seats.csv", "_raw_copilot_seats")
dataflowTester.ImportCsvIntoRawTable("./metrics/raw_tables/_raw_copilot_ai_credit_usage_org.csv", "_raw_copilot_ai_credit_usage_org")

dataflowTester.FlushTabler(&models.GhCopilotSeat{})
dataflowTester.FlushTabler(&models.GhCopilotEnterpriseDailyMetrics{})
dataflowTester.FlushTabler(&models.GhCopilotMetricsByLanguageFeature{})
dataflowTester.FlushTabler(&models.GhCopilotAiCreditUsage{})

dataflowTester.Subtask(tasks.ExtractSeatsMeta, taskData)
dataflowTester.Subtask(tasks.ExtractOrgMetricsMeta, taskData)
dataflowTester.Subtask(tasks.ExtractOrgAiCreditUsageMeta, taskData)

dataflowTester.VerifyTableWithOptions(&models.GhCopilotEnterpriseDailyMetrics{}, e2ehelper.TableOptions{
CSVRelPath: "./metrics/snapshot_tables/_tool_copilot_enterprise_daily_metrics.csv",
Expand All @@ -84,4 +87,9 @@ func TestCopilotMetricsDataFlow(t *testing.T) {
CSVRelPath: "./metrics/snapshot_tables/_tool_copilot_metrics_by_language_feature.csv",
IgnoreTypes: []interface{}{common.NoPKModel{}},
})

dataflowTester.VerifyTableWithOptions(&models.GhCopilotAiCreditUsage{}, e2ehelper.TableOptions{
CSVRelPath: "./metrics/snapshot_tables/_tool_copilot_ai_credit_usage.csv",
IgnoreTypes: []interface{}{common.NoPKModel{}},
})
}
76 changes: 76 additions & 0 deletions backend/plugins/gh-copilot/models/ai_credit_usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package models

import (
"time"

"github.com/apache/incubator-devlake/core/models/common"
)

// AI credit usage "level" identifies which billing endpoint a usage row came from.
const (
AiCreditUsageLevelEnterprise = "enterprise"
AiCreditUsageLevelOrganization = "organization"
)

// GhCopilotAiCreditUsage stores a single AI credit billing usage line item for a
// time period, sourced from the GitHub Billing Usage "AI credit usage" reports
// (enterprise and organization endpoints). Each row corresponds to one usageItem
// (a product/sku/model/unitType combination) within a single day.
type GhCopilotAiCreditUsage struct {
ConnectionId uint64 `gorm:"primaryKey" json:"connectionId"`
ScopeId string `gorm:"primaryKey;type:varchar(255)" json:"scopeId"`
// Level distinguishes enterprise vs organization sourced usage.
Level string `gorm:"primaryKey;type:varchar(20)" json:"level"`
// Time period of the usage line (daily granularity).
Year int `gorm:"primaryKey" json:"year"`
Month int `gorm:"primaryKey" json:"month"`
Day int `gorm:"primaryKey" json:"day"`
// Usage line dimensions.
Product string `gorm:"primaryKey;type:varchar(100)" json:"product"`
Sku string `gorm:"primaryKey;type:varchar(150)" json:"sku"`
Model string `gorm:"primaryKey;type:varchar(150)" json:"model"`
UnitType string `gorm:"primaryKey;type:varchar(50)" json:"unitType"`
CostCenterId string `gorm:"primaryKey;type:varchar(100)" json:"costCenterId"`

// Date is the time period expressed as a date (year-month-day) for easier
// time-series querying in dashboards.
Date time.Time `gorm:"type:date;index" json:"date"`

// Top-level context (may be empty for the unfiltered aggregate report).
Enterprise string `gorm:"type:varchar(100)" json:"enterprise"`
Organization string `gorm:"type:varchar(100)" json:"organization"`
UserLogin string `gorm:"type:varchar(255)" json:"userLogin"`
CostCenterName string `gorm:"type:varchar(255)" json:"costCenterName"`

// Billing metrics. GitHub returns fractional values, so these are float64.
PricePerUnit float64 `json:"pricePerUnit" gorm:"comment:Price per unit for the line item"`
GrossQuantity float64 `json:"grossQuantity" gorm:"comment:Gross quantity consumed (e.g. credits)"`
GrossAmount float64 `json:"grossAmount" gorm:"comment:Gross billed amount"`
DiscountQuantity float64 `json:"discountQuantity" gorm:"comment:Discounted quantity"`
DiscountAmount float64 `json:"discountAmount" gorm:"comment:Discounted amount"`
NetQuantity float64 `json:"netQuantity" gorm:"comment:Net quantity after discounts"`
NetAmount float64 `json:"netAmount" gorm:"comment:Net billed amount after discounts"`

common.NoPKModel
}

func (GhCopilotAiCreditUsage) TableName() string {
return "_tool_copilot_ai_credit_usage"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package migrationscripts

import (
"time"

"github.com/apache/incubator-devlake/core/context"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models/migrationscripts/archived"
"github.com/apache/incubator-devlake/helpers/migrationhelper"
)

type addCopilotAiCreditUsage struct{}

type aiCreditUsage20260701 struct {
ConnectionId uint64 `gorm:"primaryKey"`
ScopeId string `gorm:"primaryKey;type:varchar(255)"`
Level string `gorm:"primaryKey;type:varchar(20)"`
Year int `gorm:"primaryKey"`
Month int `gorm:"primaryKey"`
Day int `gorm:"primaryKey"`
Product string `gorm:"primaryKey;type:varchar(100)"`
Sku string `gorm:"primaryKey;type:varchar(150)"`
Model string `gorm:"primaryKey;type:varchar(150)"`
UnitType string `gorm:"primaryKey;type:varchar(50)"`
CostCenterId string `gorm:"primaryKey;type:varchar(100)"`

Date time.Time `gorm:"type:date;index"`

Enterprise string `gorm:"type:varchar(100)"`
Organization string `gorm:"type:varchar(100)"`
UserLogin string `gorm:"type:varchar(255)"`
CostCenterName string `gorm:"type:varchar(255)"`

PricePerUnit float64
GrossQuantity float64
GrossAmount float64
DiscountQuantity float64
DiscountAmount float64
NetQuantity float64
NetAmount float64

archived.NoPKModel
}

func (aiCreditUsage20260701) TableName() string {
return "_tool_copilot_ai_credit_usage"
}

func (script *addCopilotAiCreditUsage) Up(basicRes context.BasicRes) errors.Error {
return migrationhelper.AutoMigrateTables(basicRes,
&aiCreditUsage20260701{},
)
}

func (*addCopilotAiCreditUsage) Version() uint64 {
return 20260701000000
}

func (*addCopilotAiCreditUsage) Name() string {
return "Add Copilot AI credit usage table"
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func All() []plugin.MigrationScript {
new(addPRFieldsToEnterpriseMetrics),
new(addOrganizationIdToUserMetrics),
new(addCopilotMetricsGaps),
new(addCopilotAiCreditUsage),
}
}
2 changes: 2 additions & 0 deletions backend/plugins/gh-copilot/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ func GetTablesInfo() []dal.Tabler {
&GhCopilotSeat{},
// User-team mappings
&GhCopilotUserTeam{},
// AI credit billing usage
&GhCopilotAiCreditUsage{},
}
}
Loading
Loading