Skip to content

Commit

Permalink
Merge branch 'master' into lunny/add_close_back
Browse files Browse the repository at this point in the history
  • Loading branch information
techknowlogick authored Jan 6, 2021
2 parents 484a6b3 + 8688c2b commit 0aa8cde
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 14 deletions.
40 changes: 40 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package migrations

import (
"context"
"fmt"
"os"
"reflect"
Expand All @@ -16,6 +17,7 @@ import (
"code.gitea.io/gitea/modules/setting"

"xorm.io/xorm"
"xorm.io/xorm/schemas"
)

const minDBVersion = 70 // Gitea 1.5.3
Expand Down Expand Up @@ -273,6 +275,8 @@ var migrations = []Migration{
NewMigration("Convert topic name from 25 to 50", convertTopicNameFrom25To50),
// v164 -> v165
NewMigration("Add scope and nonce columns to oauth2_grant table", addScopeAndNonceColumnsToOAuth2Grant),
// v165 -> v166
NewMigration("Convert hook task type from char(16) to varchar(16) and trim the column", convertHookTaskTypeToVarcharAndTrim),
}

// GetCurrentDBVersion returns the current db version
Expand Down Expand Up @@ -737,3 +741,39 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin

return nil
}

// modifyColumn will modify column's type or other propertity. SQLITE is not supported
func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error {
var indexes map[string]*schemas.Index
var err error
// MSSQL have to remove index at first, otherwise alter column will fail
// ref. https://sqlzealots.com/2018/05/09/error-message-the-index-is-dependent-on-column-alter-table-alter-column-failed-because-one-or-more-objects-access-this-column/
if x.Dialect().URI().DBType == schemas.MSSQL {
indexes, err = x.Dialect().GetIndexes(x.DB(), context.Background(), tableName)
if err != nil {
return err
}

for _, index := range indexes {
_, err = x.Exec(x.Dialect().DropIndexSQL(tableName, index))
if err != nil {
return err
}
}
}

defer func() {
for _, index := range indexes {
_, err = x.Exec(x.Dialect().CreateIndexSQL(tableName, index))
if err != nil {
log.Error("Create index %s on table %s failed: %v", index.Name, tableName, err)
}
}
}()

alterSQL := x.Dialect().ModifyColumnSQL(tableName, col)
if _, err := x.Exec(alterSQL); err != nil {
return err
}
return nil
}
2 changes: 1 addition & 1 deletion models/migrations/v161.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func convertTaskTypeToString(x *xorm.Engine) error {
}

type HookTask struct {
Typ string `xorm:"char(16) index"`
Typ string `xorm:"VARCHAR(16) index"`
}
if err := x.Sync2(new(HookTask)); err != nil {
return err
Expand Down
66 changes: 66 additions & 0 deletions models/migrations/v165.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"xorm.io/xorm"
"xorm.io/xorm/schemas"
)

func convertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
dbType := x.Dialect().URI().DBType
if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT
return nil
}

type HookTask struct {
Typ string `xorm:"VARCHAR(16) index"`
}

if err := modifyColumn(x, "hook_task", &schemas.Column{
Name: "typ",
SQLType: schemas.SQLType{
Name: "VARCHAR",
},
Length: 16,
Nullable: true, // To keep compatible as nullable
}); err != nil {
return err
}

var hookTaskTrimSQL string
if dbType == schemas.MSSQL {
hookTaskTrimSQL = "UPDATE hook_task SET typ = RTRIM(LTRIM(typ))"
} else {
hookTaskTrimSQL = "UPDATE hook_task SET typ = TRIM(typ)"
}
if _, err := x.Exec(hookTaskTrimSQL); err != nil {
return err
}

type Webhook struct {
Type string `xorm:"VARCHAR(16) index"`
}

if err := modifyColumn(x, "webhook", &schemas.Column{
Name: "type",
SQLType: schemas.SQLType{
Name: "VARCHAR",
},
Length: 16,
Nullable: true, // To keep compatible as nullable
}); err != nil {
return err
}

var webhookTrimSQL string
if dbType == schemas.MSSQL {
webhookTrimSQL = "UPDATE webhook SET type = RTRIM(LTRIM(type))"
} else {
webhookTrimSQL = "UPDATE webhook SET type = TRIM(type)"
}
_, err := x.Exec(webhookTrimSQL)
return err
}
8 changes: 4 additions & 4 deletions models/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ type Webhook struct {
*HookEvent `xorm:"-"`
IsSSL bool `xorm:"is_ssl"`
IsActive bool `xorm:"INDEX"`
Type HookTaskType `xorm:"char(16) 'type'"`
Type HookTaskType `xorm:"VARCHAR(16) 'type'"`
Meta string `xorm:"TEXT"` // store hook-specific attributes
LastStatus HookStatus // Last delivery status

Expand Down Expand Up @@ -641,9 +641,9 @@ type HookTask struct {
RepoID int64 `xorm:"INDEX"`
HookID int64
UUID string
Typ HookTaskType
URL string `xorm:"TEXT"`
Signature string `xorm:"TEXT"`
Typ HookTaskType `xorm:"VARCHAR(16) index"`
URL string `xorm:"TEXT"`
Signature string `xorm:"TEXT"`
api.Payloader `xorm:"-"`
PayloadContent string `xorm:"TEXT"`
HTTPMethod string `xorm:"http_method"`
Expand Down
12 changes: 6 additions & 6 deletions options/locale/locale_zh-TW.ini
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ report_message=如果你確定這是一個 Gitea 的 bug,請去 <a href="https

[startpage]
install=安裝容易
install_desc=簡單地用<a target="_blank" rel="noopener noreferrer" href="https://docs.gitea.io/en-us/install-from-binary/">執行檔</a>來架設平台,或是使用 <a target="_blank" rel="noopener noreferrer" href="https://github.com/go-gitea/gitea/tree/master/docker">Docker</a>,你也可以從<a target="_blank" rel="noopener noreferrer" href="https://docs.gitea.io/en-us/install-from-package/">套件管理員</a>安裝。
install_desc=簡單地執行您平台的<a target="_blank" rel="noopener noreferrer" href="https://docs.gitea.io/en-us/install-from-binary/">二進位檔</a>,或是使用 <a target="_blank" rel="noopener noreferrer" href="https://github.com/go-gitea/gitea/tree/master/docker">Docker</a>,你也可以從<a target="_blank" rel="noopener noreferrer" href="https://docs.gitea.io/en-us/install-from-package/">套件管理員</a>安裝。
platform=跨平台
platform_desc=Gitea 可以在所有能編譯 <a target="_blank" rel="noopener noreferrer" href="http://golang.org/">Go 語言</a>的平台上執行: Windows, macOS, Linux, ARM 等等。挑一個您喜歡的吧!
lightweight=輕量級
Expand All @@ -108,7 +108,7 @@ license_desc=取得 <a target="_blank" rel="noopener noreferrer" href="https://c
[install]
install=安裝頁面
title=初始組態
docker_helper=如果您正在使用 Docker 容器執行 Gitea,請務必先仔細閱讀 <a target="_blank" rel="noopener noreferrer" href="%s">官方文件</a>後再對本頁面進行填寫
docker_helper=如果您在 Docker 中執行 Gitea,請先閱讀<a target="_blank" rel="noopener noreferrer" href="%s">安裝指南</a>再來調整設定
requite_db_desc=Gitea 必須搭配 MySQL、PostgreSQL、MSSQL 或 SQLite3 資料庫使用。
db_title=資料庫設定
db_type=資料庫類型
Expand Down Expand Up @@ -145,7 +145,7 @@ domain_helper=用於 SSH Clone 的域名或主機位置。
ssh_port=SSH 伺服器埠
ssh_port_helper=SSH 伺服器使用的埠號,留空以停用此設定。
http_port=Gitea HTTP 埠
http_port_helper=Giteas web 伺服器將偵聽的埠號
http_port_helper=Gitea 的網頁伺服器要接聽的埠號
app_url=Gitea 基本 URL
app_url_helper=用於 HTTP(S) Clone 和電子郵件通知的基本網址。
log_root_path=日誌路徑
Expand All @@ -155,7 +155,7 @@ optional_title=可選設定
email_title=電子郵件設定
smtp_host=SMTP 主機
smtp_from=電子郵件寄件者
smtp_from_helper=Gitea 將會使用的電子信箱,輸入一個普通的電子信箱或使用 "名稱" <email@example.com> 格式
smtp_from_helper=Gitea 將會使用的電子信箱,直接輸入電子信箱或使用「"名稱" <email@example.com>」的格式
mailer_user=SMTP 帳號
mailer_password=SMTP 密碼
register_confirm=要求註冊時確認電子郵件
Expand Down Expand Up @@ -253,10 +253,10 @@ forgot_password_title=忘記密碼
forgot_password=忘記密碼?
sign_up_now=還沒有帳戶?馬上註冊。
sign_up_successful=帳戶已成功建立。
confirmation_mail_sent_prompt=一封新的確認郵件已發送至 <b>%s</b>。請檢查您的收件箱並在 %s 小時內完成確認註冊操作
confirmation_mail_sent_prompt=一封新的確認信已發送至 <b>%s</b>。請檢查您的收件匣,並在 %s 內完成註冊作業
must_change_password=更新您的密碼
allow_password_change=要求使用者更改密碼 (推薦)
reset_password_mail_sent_prompt=一封確認信已經被發送至 <b>%s</b>。請檢查您的收件匣,並在 %s 小時內完成帳戶救援作業
reset_password_mail_sent_prompt=一封確認信已發送至 <b>%s</b>。請檢查您的收件匣,並在 %s 內完成帳戶救援作業
active_your_account=啟用您的帳戶
account_activated=帳戶已啟用
prohibit_login=禁止登入
Expand Down
6 changes: 3 additions & 3 deletions services/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ var (

// RegisterWebhook registers a webhook
func RegisterWebhook(name string, webhook *webhook) {
webhooks[models.HookTaskType(strings.TrimSpace(name))] = webhook
webhooks[models.HookTaskType(name)] = webhook
}

// IsValidHookTaskType returns true if a webhook registered
func IsValidHookTaskType(name string) bool {
if name == models.GITEA || name == models.GOGS {
return true
}
_, ok := webhooks[models.HookTaskType(strings.TrimSpace(name))]
_, ok := webhooks[models.HookTaskType(name)]
return ok
}

Expand Down Expand Up @@ -147,7 +147,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo

var payloader api.Payloader
var err error
webhook, ok := webhooks[strings.TrimSpace(w.Type)] // NOTICE: w.Type maynot be trimmed before store into database
webhook, ok := webhooks[w.Type]
if ok {
payloader, err = webhook.payloadCreator(p, event, w.Meta)
if err != nil {
Expand Down

0 comments on commit 0aa8cde

Please sign in to comment.