Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debug job and steps, and don't push br to latest tag #4

Merged
merged 22 commits into from
Oct 4, 2024
Merged
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
18 changes: 17 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
push:
branches:
- main
tags:
- "v*.*.*"

permissions:
contents: read
Expand All @@ -18,6 +20,19 @@ jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Docker Meta
id: meta
uses: docker/metadata-action@v5
with:
images:
${{ vars.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}
tags: |
# branch event
type=ref,enable=true,priority=600,prefix=,suffix=,event=branch
# pull request event
type=ref,enable=true,priority=600,prefix=pr-,suffix=,event=pr
# tags event
type=semver,pattern={{raw}}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3

Expand All @@ -35,4 +50,5 @@ jobs:
with:
platforms: linux/amd64, linux/arm64
push: true
tags: ${{ vars.DOCKERHUB_USERNAME }}/${{ env.IMAGE_NAME }}:latest
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
18 changes: 15 additions & 3 deletions cmd/gh-action/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package main
import (
"fmt"
"os"
"strconv"
"strings"

"github.com/google/go-github/v65/github"
"github.com/jmoiron/sqlx"
)

type configType struct {
dbUri string
dbTable string
runID string
db *sqlx.DB
runID int64
repository string
owner string
repo string
githubToken string
ghClient *github.Client
}

func getConfig() (configType, error) {
Expand All @@ -32,10 +38,15 @@ func getConfig() (configType, error) {
return configType{}, fmt.Errorf("missing env: GITHUB_REPOSITORY")
}

runID := os.Getenv("GH_RUN_ID")
if len(runID) == 0 {
envRunID := os.Getenv("GH_RUN_ID")
var runID int64
if len(envRunID) == 0 {
return configType{}, fmt.Errorf("missing env: GH_RUN_ID")
}
runID, err := strconv.ParseInt(envRunID, 10, 64)
if err != nil {
return configType{}, fmt.Errorf("GH_RUN_ID must be integer, error: %v", err)
}

githubToken := os.Getenv("GH_TOKEN")

Expand All @@ -47,6 +58,7 @@ func getConfig() (configType, error) {
return configType{
dbUri: dbUri,
dbTable: dbTable,
db: nil,
runID: runID,
repository: repository,
owner: repoDetails[0],
Expand Down
87 changes: 84 additions & 3 deletions cmd/gh-action/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,98 @@ package main

import (
"time"

"github.com/google/go-github/v65/github"
)

type WorkflowStat struct {
type WorkflowRunRec struct {
WorkflowId int64
Name string
Status string
Conclusion string
RunId int
RunAttempt int
RunId int64
RunAttempt int64
StartedAt time.Time
UpdatedAt time.Time
RepoName string
Event string
}

func ghWorkflowRunRec(w *github.WorkflowRun) *WorkflowRunRec {
return &WorkflowRunRec{
WorkflowId: w.GetWorkflowID(),
Name: w.GetName(),
Status: w.GetStatus(),
Conclusion: w.GetConclusion(),
RunId: w.GetID(),
RunAttempt: int64(w.GetRunAttempt()),
StartedAt: w.GetCreatedAt().Time,
UpdatedAt: w.GetUpdatedAt().Time,
RepoName: w.GetRepository().GetFullName(),
Event: w.GetEvent(),
}
}

type WorkflowJobRec struct {
JobId int64
RunId int64
NodeID string
HeadBranch string
HeadSHA string
Status string
Conclusion string
CreatedAt time.Time
StartedAt time.Time
CompletedAt time.Time
Name string
RunnerName string
RunnerGroupName string
RunAttempt int64
WorkflowName string
}

func ghWorkflowJobRec(j *github.WorkflowJob) *WorkflowJobRec {
return &WorkflowJobRec{
JobId: j.GetID(),
RunId: j.GetRunID(),
NodeID: j.GetNodeID(),
HeadBranch: j.GetHeadBranch(),
HeadSHA: j.GetHeadSHA(),
Status: j.GetStatus(),
Conclusion: j.GetConclusion(),
CreatedAt: j.GetCreatedAt().Time,
StartedAt: j.GetStartedAt().Time,
CompletedAt: j.GetCompletedAt().Time,
Name: j.GetName(),
RunnerName: j.GetRunnerName(),
RunnerGroupName: j.GetRunnerGroupName(),
RunAttempt: j.GetRunAttempt(),
WorkflowName: j.GetWorkflowName(),
}
}

type WorkflowJobStepRec struct {
JobId int64
RunId int64
RunAttempt int64
Name string
Status string
Conclusion string
Number int64
StartedAt time.Time
CompletedAt time.Time
}

func ghWorkflowJobStepRec(j *github.WorkflowJob, s *github.TaskStep) *WorkflowJobStepRec {
return &WorkflowJobStepRec{
JobId: j.GetID(),
RunId: j.GetRunID(),
RunAttempt: j.GetRunAttempt(),
Name: s.GetName(),
Status: s.GetStatus(),
Conclusion: s.GetConclusion(),
Number: s.GetNumber(),
StartedAt: s.GetStartedAt().Time,
CompletedAt: s.GetCompletedAt().Time,
}
}
114 changes: 108 additions & 6 deletions cmd/gh-action/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import (

"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"

"github.com/google/go-github/v65/github"
)

var (
scheme = `
CREATE TABLE %s (
schemeWorkflowRunsStats = `
CREATE TABLE IF NOT EXISTS %s (
workflowid BIGINT,
name TEXT,
status TEXT,
Expand All @@ -23,36 +25,136 @@ var (
PRIMARY KEY(workflowid, runattempt)
)
`
schemeWorkflowRunAttempts = `
CREATE TABLE IF NOT EXISTS %s (
workflowid BIGINT,
name TEXT,
status TEXT,
conclusion TEXT,
runid BIGINT,
runattempt INT,
startedat TIMESTAMP,
updatedat TIMESTAMP,
reponame TEXT,
event TEXT,
PRIMARY KEY(workflowid, runid, runattempt)
)
`
schemeWorkflowJobs = `
CREATE TABLE IF NOT EXISTS %s (
JobId BIGINT,
RunID BIGINT,
NodeID TEXT,
HeadBranch TEXT,
HeadSHA TEXT,
Status TEXT,
Conclusion TEXT,
CreatedAt TIMESTAMP,
StartedAt TIMESTAMP,
CompletedAt TIMESTAMP,
Name TEXT,
RunnerName TEXT,
RunnerGroupName TEXT,
RunAttempt BIGINT,
WorkflowName TEXT
)`
schemeWorkflowJobsSteps = `
CREATE TABLE IF NOT EXISTS %s (
JobId BIGINT,
RunId BIGINT,
RunAttempt BIGINT,
Name TEXT,
Status TEXT,
Conclusion TEXT,
Number BIGINT,
StartedAt TIMESTAMP,
CompletedAt TIMESTAMP
)
`
)

func initDatabase(conf configType) error {
db, err := sqlx.Connect("postgres", conf.dbUri)
_, err := conf.db.Exec(fmt.Sprintf(schemeWorkflowRunsStats, conf.dbTable))
if err != nil {
return err
}

_, err = conf.db.Exec(fmt.Sprintf(schemeWorkflowRunAttempts, conf.dbTable + "_attempts"))
if err != nil {
return err
}

_, err = conf.db.Exec(fmt.Sprintf(schemeWorkflowJobs, conf.dbTable + "_jobs"))
if err != nil {
return err
}

_, err = db.Exec(fmt.Sprintf(scheme, conf.dbTable))
_, err = conf.db.Exec(fmt.Sprintf(schemeWorkflowJobsSteps, conf.dbTable + "_steps"))
if err != nil {
return err
}
return nil
}

func saveRecords(conf configType, records *WorkflowStat) error {
func connectDB(conf *configType) error {
db, err := sqlx.Connect("postgres", conf.dbUri)
if err != nil {
return err
}
conf.db = db
return nil
}

func saveWorkflowRun(conf configType, record *WorkflowRunRec) error {
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)", conf.dbTable,
"workflowid, name, status, conclusion, runid, runattempt, startedAt, updatedAt, repoName, event",
":workflowid, :name, :status, :conclusion, :runid, :runattempt, :startedat, :updatedat, :reponame, :event",
)

_, err = db.NamedExec(query, *records)
_, err := conf.db.NamedExec(query, *record)

if err != nil {
return err
}
return nil
}

func saveWorkflowRunAttempt(conf configType, workflowRun *github.WorkflowRun) error {
query := fmt.Sprintf("INSERT INTO %s_attempts (%s) VALUES (%s)", conf.dbTable,
"workflowid, name, status, conclusion, runid, runattempt, startedAt, updatedAt, repoName, event",
":workflowid, :name, :status, :conclusion, :runid, :runattempt, :startedat, :updatedat, :reponame, :event",
)

_, err := conf.db.NamedExec(query, ghWorkflowRunRec(workflowRun))
return err
}

func saveJobInfo(conf configType, workflowJob *github.WorkflowJob) error {
query := fmt.Sprintf("INSERT INTO %s_jobs (%s) VALUES (%s)", conf.dbTable,
"jobid, runid, nodeid, headbranch, headsha, status, conclusion, createdat, startedat, completedat, name, runnername, runnergroupname, runattempt, workflowname",
":jobid, :runid, :nodeid, :headbranch, :headsha, :status, :conclusion, :createdat, :startedat, :completedat, :name, :runnername, :runnergroupname, :runattempt, :workflowname",
)

_, err := conf.db.NamedExec(query, ghWorkflowJobRec(workflowJob))
if err != nil {
return err
}

for _, step := range workflowJob.Steps {
err = saveStepInfo(conf, workflowJob, step)
if err != nil {
return err
}
}
return nil
}

func saveStepInfo(conf configType, job *github.WorkflowJob, step *github.TaskStep) error {
query := fmt.Sprintf("INSERT INTO %s_steps (%s) VALUES (%s)", conf.dbTable,
"jobid, runid, runattempt, name, status, conclusion, number, startedat, completedat",
":jobid, :runid, :runattempt, :name, :status, :conclusion, :number, :startedat, :completedat",
)

_, err := conf.db.NamedExec(query, ghWorkflowJobStepRec(job, step))
return err
}
Loading