diff --git a/models/marts/engineering/pull_requests.sql b/models/marts/engineering/pull_requests.sql new file mode 100644 index 0000000..1a885f6 --- /dev/null +++ b/models/marts/engineering/pull_requests.sql @@ -0,0 +1,52 @@ + +with + +pull_requests as ( + select * from {{ ref('stg_github__pull_requests') }} +), + +repositories as ( + select * from {{ ref('stg_github__repositories') }} +), + +issues as ( + select * from {{ ref('stg_github__issues') }} +), + +issues_merged as ( + select * from {{ ref('stg_github__issues_merged') }} +), + +final as ( + select + pull_requests.pull_request_id, + repositories.name as repo_name, + issues.number as pull_request_number, + + -- TODO: find out how to label these PRs + cast(null as string) as type, -- (bug, eng, feature), + + case + when pull_requests.is_draft then 'draft' + when issues_merged.merged_at is not null then 'merged' + when issues.closed_at is not null then 'closed_without_merge' + else 'open' + end as state, + + issues.created_at as opened_at, + issues_merged.merged_at, + round(date_diff(issues_merged.merged_at, issues.created_at, hour) / 24.0, 2) as days_open_to_merge + + from pull_requests + + left join repositories + on pull_requests.head_repo_id = repositories.repo_id + + left join issues + on pull_requests.issue_id = issues.issue_id + + left join issues_merged + on pull_requests.issue_id = issues_merged.issue_id +) + +select * from final diff --git a/models/staging/github/src_github.yml b/models/staging/github/src_github.yml new file mode 100644 index 0000000..012c2f8 --- /dev/null +++ b/models/staging/github/src_github.yml @@ -0,0 +1,35 @@ +version: 2 + +sources: + - name: github + database: analytics-engineers-club + tables: + - name: pull_request + columns: + - name: id + description: "This is **not** the pull request number" + tests: + - unique + - not_null + + - name: issue + description: "This table contains both issues AND pull requests" + columns: + - name: id + tests: + - unique + - not_null + + - name: issue_merged + columns: + - name: issue_id + tests: + - unique + - not_null + + - name: repository + columns: + - name: id + tests: + - unique + - not_null diff --git a/models/staging/github/stg_github__issues.sql b/models/staging/github/stg_github__issues.sql new file mode 100644 index 0000000..2a28ace --- /dev/null +++ b/models/staging/github/stg_github__issues.sql @@ -0,0 +1,29 @@ +with source as ( + + select * from {{ source('github', 'issue') }} + +), + +renamed as ( + + select + id as issue_id, + _fivetran_synced, + body, + closed_at, + created_at, + locked, + milestone_id, + number, + pull_request, + repository_id, + state, + title, + updated_at, + user_id + + from source + +) + +select * from renamed diff --git a/models/staging/github/stg_github__issues_merged.sql b/models/staging/github/stg_github__issues_merged.sql new file mode 100644 index 0000000..04b6634 --- /dev/null +++ b/models/staging/github/stg_github__issues_merged.sql @@ -0,0 +1,20 @@ +with source as ( + select * from {{ source('github', 'issue_merged') }} +), + +renamed as ( + select + issue_id, + actor_id as merge_user_id, + + commit_sha, + + -- timestamps + merged_at, + + -- excluded columns + -- _fivetran_synced, + from source +) + +select * from renamed diff --git a/models/staging/github/stg_github__pull_requests.sql b/models/staging/github/stg_github__pull_requests.sql new file mode 100644 index 0000000..bcafee4 --- /dev/null +++ b/models/staging/github/stg_github__pull_requests.sql @@ -0,0 +1,33 @@ +with source as ( + + select * from {{ source('github', 'pull_request') }} + +), + +renamed as ( + + select + id as pull_request_id, + issue_id, + + base_label, + base_ref, + base_repo_id, + base_sha, + base_user_id, + draft as is_draft, + head_label, + head_ref, + head_repo_id, + head_sha, + head_user_id, + merge_commit_sha, + + -- excluded + -- _fivetran_synced, + + from source + +) + +select * from renamed diff --git a/models/staging/github/stg_github__repositories.sql b/models/staging/github/stg_github__repositories.sql new file mode 100644 index 0000000..6ac3739 --- /dev/null +++ b/models/staging/github/stg_github__repositories.sql @@ -0,0 +1,32 @@ +with source as ( + select * from {{ source('github', 'repository') }} +), + +renamed as ( + select + id as repo_id, + owner_id as owner_user_id, + + archived as is_archived, + + default_branch, + description, + fork as is_fork, + full_name, + homepage, + language, + name, + + private as is_private, + -- timestamps + created_at, + + + -- excluded + -- _fivetran_synced, + + from source + +) + +select * from renamed diff --git a/packages.yml b/packages.yml new file mode 100644 index 0000000..d91e39e --- /dev/null +++ b/packages.yml @@ -0,0 +1,3 @@ +packages: + - package: dbt-labs/codegen + version: 0.4.0