Skip to content

Commit

Permalink
Merge pull request #513 from penguin-statistics/dev
Browse files Browse the repository at this point in the history
Release v3.14.7
  • Loading branch information
GalvinGao authored Nov 27, 2023
2 parents 62415d7 + 7f21565 commit 2ab981f
Show file tree
Hide file tree
Showing 21 changed files with 452 additions and 738 deletions.
112 changes: 57 additions & 55 deletions .github/workflows/pr-auto-upsert.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,73 +23,75 @@ jobs:
# fetch all history so that git log can get all commit messages
fetch-depth: 0

- id: get_summary
name: Get summary from GPT
continue-on-error: true
run: |
# get all commit messages from last release tag to HEAD
git log $(git describe --tags --abbrev=0)..HEAD --pretty=format:%s > /tmp/commit_messages.txt
# replace newlines with \n
sed -i ':a;N;$!ba;s/\n/\\n/g' /tmp/commit_messages.txt
echo "commit messages: $(cat /tmp/commit_messages.txt)"
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${{ secrets.OPENAI_API_KEY }}" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are now a git commit message summarizer. By analyzing commit messages, you should provide a concise summary, highlighting key changes and updates made in each commit. This can save time and improve clarity for developers working in collaborative projects.\n\nCommits are typically constructed using the Conventional Commit standard.\n\nYour summarized message should consist of 1-3 sentences that are compacted into sentences for each of the categories (e.g. Added, Fixed, Removed). You should NOT use bullet points."
},
{
"role": "user",
"content": "feat: remove deleted required flag\nbuild: add workflow_dispatch\nfeat: add rowsAffected & fix json struct tag\nchore: add logs"
},
{
"role": "assistant",
"content": "Added: `workflow_dispatch`, `rowsAffected`, logs.\nFixed: JSON struct tag.\nRemoved: required flag of `deleted`."
},
{
"role": "user",
"content": "$(cat /tmp/commit_messages.txt)"
}
],
"temperature": 1,
"max_tokens": 256,
"top_p": 1,
"frequency_penalty": 0.1,
"presence_penalty": 0.2
}' | jq -r '.choices[0].message.content' > /tmp/summary.txt
# replace newlines with spaces
sed -i ':a;N;$!ba;s/\n/ /g' /tmp/summary.txt
# replace " with \"
sed -i 's/"/\\"/g' /tmp/summary.txt
echo "summary: $(cat /tmp/summary.txt)"
echo "summary=$(cat /tmp/summary.txt)" >> $GITHUB_OUTPUT
# create a PR from dev to main, with title in form: Release <semver>
# where, <semver> is the next version number to be released, based on the last release in git tag
- name: Create PR
- name: Upsert PR Content
uses: actions/github-script@v6
with:
github-token: ${{ secrets.PAT_FOR_RELEASE_TAGGER }}
script: |
const https = require('https')
const commitMessages = await github.rest.repos.compareCommits({
owner: context.repo.owner,
repo: context.repo.repo,
base: 'main',
head: 'dev'
}).then(res => res.data.commits.map(commit => commit.commit.message))
const summary = await new Promise((resolve, reject) => {
const req = https.request({
hostname: 'api.openai.com',
path: '/v1/chat/completions',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${{ secrets.OPENAI_API_KEY }}`
}
}, res => {
let data = ''
res.on('data', chunk => data += chunk)
res.on('end', () => {
resolve(JSON.parse(data).choices[0].message.content)
})
})
req.on('error', reject)
req.write(JSON.stringify({
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are now a git commit message summarizer. By analyzing commit messages, you should provide a concise summary, highlighting key changes and updates made in each commit. This can save time and improve clarity for developers working in collaborative projects.\n\nCommits are typically constructed using the Conventional Commit standard.\n\nYour summarized message should consist of 1-3 sentences that are short, concise and includes the overall intent of the PR. You should NOT use bullet points. You MAY omit merge commits in your response."
},
{
"role": "user",
"content": "feat: remove deleted required flag\nbuild: add workflow_dispatch\nfeat: add rowsAffected & fix json struct tag\nchore: add logs"
},
{
"role": "assistant",
"content": "Added: `workflow_dispatch`, `rowsAffected`, logs.\nFixed: JSON struct tag.\nRemoved: required flag of `deleted`."
},
{
"role": "user",
"content": commitMessages.join('\n')
}
],
temperature: 1,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0.1,
presence_penalty: 0.2
}))
req.end()
})
const { data: { tag_name: lastRelease } } = await github.rest.repos.getLatestRelease({
owner: context.repo.owner,
repo: context.repo.repo
})
const nextRelease = lastRelease.replace(/(\d+)$/, (match, p1) => Number(p1) + 1)
const prTitle = `Release ${nextRelease}`
let body = `> *This PR is automatically created by actions defined in this repository. To see the run log of this action, please click [here](/${{ github.repository }}/actions/runs/${{ github.run_id }})*\n\n---\n\n## Summary\n\n`
body += "${{ steps.get_summary.outputs.summary || '(no summary)' }}"
let body = `> *This PR is automatically created by actions defined in this repository. To see the run log of this action, please click [here](/${{ github.repository }}/actions/runs/${{ github.run_id }})*\n\n---\n\n## Summary\n\n${summary.replace(/\n/g, '\n\n')}`
const existedPR = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/meta/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
type AdminController struct {
fx.In

DB *bun.DB
PatternRepo *repo.DropPattern
PatternElementRepo *repo.DropPatternElement
RecognitionDefectRepo *repo.RecognitionDefect
Expand Down Expand Up @@ -474,7 +475,7 @@ func (c *AdminController) RejectRulesReevaluationApply(ctx *fiber.Ctx) error {

changeSet := evaluation.ChangeSet()

err = c.DropReportRepo.DB.RunInTx(ctx.UserContext(), nil, func(ictx context.Context, tx bun.Tx) error {
err = c.DB.RunInTx(ctx.UserContext(), nil, func(ictx context.Context, tx bun.Tx) error {
chunks := lo.Chunk(changeSet, 100)
for _, changeChunk := range chunks {
log.Debug().
Expand Down
68 changes: 22 additions & 46 deletions internal/repo/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@ package repo

import (
"context"
"database/sql"
"time"

"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/uptrace/bun"

"exusiai.dev/backend-next/internal/model"
"exusiai.dev/backend-next/internal/pkg/pgerr"
"exusiai.dev/backend-next/internal/pkg/pgid"
"exusiai.dev/backend-next/internal/repo/selector"
)

const AccountMaxRetries = 100

type Account struct {
db *bun.DB
db *bun.DB
sel selector.S[model.Account]
}

func NewAccount(db *bun.DB) *Account {
return &Account{db: db}
return &Account{
db: db,
sel: selector.New[model.Account](db),
}
}

func (c *Account) CreateAccountWithRandomPenguinId(ctx context.Context) (*model.Account, error) {
func (r *Account) CreateAccountWithRandomPenguinId(ctx context.Context) (*model.Account, error) {
// retry if account already exists
for i := 0; i < AccountMaxRetries; i++ {
account := &model.Account{
PenguinID: pgid.New(),
CreatedAt: time.Now(),
}

_, err := c.db.NewInsert().
_, err := r.db.NewInsert().
Model(account).
Returning("account_id").
Exec(ctx)
Expand All @@ -58,52 +61,25 @@ func (c *Account) CreateAccountWithRandomPenguinId(ctx context.Context) (*model.
return nil, pgerr.ErrInternalError.Msg("failed to create account")
}

func (c *Account) GetAccountById(ctx context.Context, accountId string) (*model.Account, error) {
var account model.Account

err := c.db.NewSelect().
Model(&account).
Column("account_id").
Where("account_id = ?", accountId).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}

return &account, nil
func (r *Account) GetAccountById(ctx context.Context, accountId string) (*model.Account, error) {
return r.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("account_id = ?", accountId)
})
}

func (c *Account) GetAccountByPenguinId(ctx context.Context, penguinId string) (*model.Account, error) {
var account model.Account

err := c.db.NewSelect().
Model(&account).
Where("penguin_id = ?", penguinId).
Scan(ctx)

if errors.Is(err, sql.ErrNoRows) {
return nil, pgerr.ErrNotFound
} else if err != nil {
return nil, err
}

return &account, nil
func (r *Account) GetAccountByPenguinId(ctx context.Context, penguinId string) (*model.Account, error) {
return r.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Where("penguin_id = ?", penguinId)
})
}

func (c *Account) IsAccountExistWithId(ctx context.Context, accountId int) bool {
var account model.Account

err := c.db.NewSelect().
Model(&account).
Column("account_id").
Where("account_id = ?", accountId).
Scan(ctx, &account)
func (r *Account) IsAccountExistWithId(ctx context.Context, accountId int) bool {
account, err := r.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Column("account_id").Where("account_id = ?", accountId)
})
if err != nil {
return false
}

return account.AccountID > 0
return account != nil
}
26 changes: 10 additions & 16 deletions internal/repo/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,27 @@ import (
"github.com/uptrace/bun"

"exusiai.dev/backend-next/internal/model"
"exusiai.dev/backend-next/internal/repo/selector"
)

type Activity struct {
DB *bun.DB
db *bun.DB
sel selector.S[model.Activity]
}

func NewActivity(db *bun.DB) *Activity {
return &Activity{DB: db}
return &Activity{db: db, sel: selector.New[model.Activity](db)}
}

func (c *Activity) GetActivities(ctx context.Context) ([]*model.Activity, error) {
var activities []*model.Activity
err := c.DB.NewSelect().
Model(&activities).
Order("activity_id ASC").
Scan(ctx)

if err != nil && !errors.Is(err, sql.ErrNoRows) {
return nil, err
}

return activities, nil
func (r *Activity) GetActivities(ctx context.Context) ([]*model.Activity, error) {
return r.sel.SelectMany(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Order("activity_id ASC")
})
}

func (c *Activity) GetActivityById(ctx context.Context, activityId int) (*model.Activity, error) {
func (r *Activity) GetActivityById(ctx context.Context, activityId int) (*model.Activity, error) {
var activity model.Activity
err := c.DB.NewSelect().
err := r.db.NewSelect().
Model(&activity).
Where("activity_id = ?", activityId).
Scan(ctx)
Expand Down
Loading

0 comments on commit 2ab981f

Please sign in to comment.