Skip to content

Commit b4876d3

Browse files
author
Magnus Lindvall
committed
Merge remote-tracking branch 'upstream/master'
2 parents aff1346 + e64aa18 commit b4876d3

File tree

101 files changed

+1828
-359
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+1828
-359
lines changed

.dockerignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

Dockerfile

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
1+
2+
###################################
3+
#Build stage
4+
FROM golang:1.10-alpine3.7 AS build-env
5+
6+
ARG GITEA_VERSION
7+
ARG TAGS="sqlite"
8+
ENV TAGS "bindata $TAGS"
9+
10+
#Build deps
11+
RUN apk --no-cache add build-base git
12+
13+
#Setup repo
14+
COPY . ${GOPATH}/src/code.gitea.io/gitea
15+
WORKDIR ${GOPATH}/src/code.gitea.io/gitea
16+
17+
#Checkout version if set
18+
RUN if [ -n "${GITEA_VERSION}" ]; then git checkout "${GITEA_VERSION}"; fi \
19+
&& make clean generate build
20+
121
FROM alpine:3.7
2-
LABEL maintainer="The Gitea Authors"
22+
LABEL maintainer="maintainers@gitea.io"
323

424
EXPOSE 22 3000
525

626
RUN apk --no-cache add \
7-
su-exec \
8-
ca-certificates \
9-
sqlite \
1027
bash \
28+
ca-certificates \
29+
curl \
30+
gettext \
1131
git \
1232
linux-pam \
13-
s6 \
14-
curl \
1533
openssh \
16-
gettext \
34+
s6 \
35+
sqlite \
36+
su-exec \
1737
tzdata
38+
1839
RUN addgroup \
1940
-S -g 1000 \
2041
git && \
@@ -29,12 +50,11 @@ RUN addgroup \
2950

3051
ENV USER git
3152
ENV GITEA_CUSTOM /data/gitea
32-
ENV GODEBUG=netdns=go
3353

3454
VOLUME ["/data"]
3555

3656
ENTRYPOINT ["/usr/bin/entrypoint"]
3757
CMD ["/bin/s6-svscan", "/etc/s6"]
3858

3959
COPY docker /
40-
COPY gitea /app/gitea/gitea
60+
COPY --from=build-env /go/src/code.gitea.io/gitea/gitea /app/gitea/gitea

MAINTAINERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ David Schneiderbauer <dschneiderbauer@gmail.com> (@daviian)
2121
Peter Žeby <morlinest@gmail.com> (@morlinest)
2222
Matti Ranta <matti@mdranta.net> (@techknowlogick)
2323
Michael Lustfield <mtecknology@debian.org> (@MTecknology)
24+
Jonas Franz <info@jonasfranz.software> (@JonasFranzDEV)

docker/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)
77

88
.PHONY: docker
99
docker:
10-
docker run -ti --rm -v $(CURDIR):/srv/app/src/code.gitea.io/gitea -w /srv/app/src/code.gitea.io/gitea -e TAGS="bindata $(TAGS)" webhippie/golang:edge make clean generate build
1110
docker build --disable-content-trust=false -t $(DOCKER_REF) .
11+
# support also build args docker build --build-arg GITEA_VERSION=v1.2.3 --build-arg TAGS="bindata sqlite" .
12+
13+
.PHONY: docker-build
14+
docker-build:
15+
docker run -ti --rm -v $(CURDIR):/srv/app/src/code.gitea.io/gitea -w /srv/app/src/code.gitea.io/gitea -e TAGS="bindata $(TAGS)" webhippie/golang:edge make clean generate build

models/attachment.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ import (
1111
"os"
1212
"path"
1313

14-
gouuid "github.com/satori/go.uuid"
15-
14+
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/setting"
1716
"code.gitea.io/gitea/modules/util"
17+
api "code.gitea.io/sdk/gitea"
18+
19+
"github.com/go-xorm/xorm"
20+
gouuid "github.com/satori/go.uuid"
1821
)
1922

2023
// Attachment represent a attachment of issue/comment/release.
@@ -39,6 +42,20 @@ func (a *Attachment) IncreaseDownloadCount() error {
3942
return nil
4043
}
4144

45+
// APIFormat converts models.Attachment to api.Attachment
46+
func (a *Attachment) APIFormat() *api.Attachment {
47+
size, _ := a.Size()
48+
return &api.Attachment{
49+
ID: a.ID,
50+
Name: a.Name,
51+
Created: a.CreatedUnix.AsTime(),
52+
DownloadCount: a.DownloadCount,
53+
Size: size,
54+
UUID: a.UUID,
55+
DownloadURL: a.DownloadURL(),
56+
}
57+
}
58+
4259
// AttachmentLocalPath returns where attachment is stored in local file
4360
// system based on given UUID.
4461
func AttachmentLocalPath(uuid string) string {
@@ -50,6 +67,30 @@ func (a *Attachment) LocalPath() string {
5067
return AttachmentLocalPath(a.UUID)
5168
}
5269

70+
// Size returns the file's size of the attachment
71+
func (a *Attachment) Size() (int64, error) {
72+
fi, err := os.Stat(a.LocalPath())
73+
if err != nil {
74+
return 0, err
75+
}
76+
return fi.Size(), nil
77+
}
78+
79+
// MustSize returns the result of a.Size() by ignoring errors
80+
func (a *Attachment) MustSize() int64 {
81+
size, err := a.Size()
82+
if err != nil {
83+
log.Error(4, "size: %v", err)
84+
return 0
85+
}
86+
return size
87+
}
88+
89+
// DownloadURL returns the download url of the attached file
90+
func (a *Attachment) DownloadURL() string {
91+
return fmt.Sprintf("%sattachments/%s", setting.AppURL, a.UUID)
92+
}
93+
5394
// NewAttachment creates a new attachment object.
5495
func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment, err error) {
5596
attach := &Attachment{
@@ -81,6 +122,22 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment,
81122
return attach, nil
82123
}
83124

125+
// GetAttachmentByID returns attachment by given id
126+
func GetAttachmentByID(id int64) (*Attachment, error) {
127+
return getAttachmentByID(x, id)
128+
}
129+
130+
func getAttachmentByID(e Engine, id int64) (*Attachment, error) {
131+
attach := &Attachment{ID: id}
132+
133+
if has, err := e.Get(attach); err != nil {
134+
return nil, err
135+
} else if !has {
136+
return nil, ErrAttachmentNotExist{ID: id, UUID: ""}
137+
}
138+
return attach, nil
139+
}
140+
84141
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
85142
attach := &Attachment{UUID: uuid}
86143
has, err := e.Get(attach)
@@ -180,3 +237,20 @@ func DeleteAttachmentsByComment(commentID int64, remove bool) (int, error) {
180237

181238
return DeleteAttachments(attachments, remove)
182239
}
240+
241+
// UpdateAttachment updates the given attachment in database
242+
func UpdateAttachment(atta *Attachment) error {
243+
return updateAttachment(x, atta)
244+
}
245+
246+
func updateAttachment(e Engine, atta *Attachment) error {
247+
var sess *xorm.Session
248+
if atta.ID != 0 && atta.UUID == "" {
249+
sess = e.ID(atta.ID)
250+
} else {
251+
// Use uuid only if id is not set and uuid is set
252+
sess = e.Where("uuid = ?", atta.UUID)
253+
}
254+
_, err := sess.Cols("name", "issue_id", "release_id", "comment_id", "download_count").Update(atta)
255+
return err
256+
}

models/attachment_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,32 @@ func TestDeleteAttachments(t *testing.T) {
5858
assert.True(t, IsErrAttachmentNotExist(err))
5959
assert.Nil(t, attachment)
6060
}
61+
62+
func TestGetAttachmentByID(t *testing.T) {
63+
assert.NoError(t, PrepareTestDatabase())
64+
65+
attach, err := GetAttachmentByID(1)
66+
assert.NoError(t, err)
67+
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
68+
}
69+
70+
func TestAttachment_DownloadURL(t *testing.T) {
71+
attach := &Attachment{
72+
UUID: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11",
73+
ID: 1,
74+
}
75+
assert.Equal(t, "https://try.gitea.io/attachments/a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.DownloadURL())
76+
}
77+
78+
func TestUpdateAttachment(t *testing.T) {
79+
assert.NoError(t, PrepareTestDatabase())
80+
81+
attach, err := GetAttachmentByID(1)
82+
assert.NoError(t, err)
83+
assert.Equal(t, "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11", attach.UUID)
84+
85+
attach.Name = "new_name"
86+
assert.NoError(t, UpdateAttachment(attach))
87+
88+
AssertExistsAndLoadBean(t, &Attachment{Name: "new_name"})
89+
}

models/error.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,21 @@ func (err ErrBranchNameConflict) Error() string {
785785
return fmt.Sprintf("branch conflicts with existing branch [name: %s]", err.BranchName)
786786
}
787787

788+
// ErrNotAllowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it
789+
type ErrNotAllowedToMerge struct {
790+
Reason string
791+
}
792+
793+
// IsErrNotAllowedToMerge checks if an error is an ErrNotAllowedToMerge.
794+
func IsErrNotAllowedToMerge(err error) bool {
795+
_, ok := err.(ErrNotAllowedToMerge)
796+
return ok
797+
}
798+
799+
func (err ErrNotAllowedToMerge) Error() string {
800+
return fmt.Sprintf("not allowed to merge [reason: %s]", err.Reason)
801+
}
802+
788803
// ErrTagAlreadyExists represents an error that tag with such name already exists
789804
type ErrTagAlreadyExists struct {
790805
TagName string

models/issue_label.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@ import (
1919
var labelColorPattern = regexp.MustCompile("#([a-fA-F0-9]{6})")
2020

2121
// GetLabelTemplateFile loads the label template file by given name,
22-
// then parses and returns a list of name-color pairs.
23-
func GetLabelTemplateFile(name string) ([][2]string, error) {
22+
// then parses and returns a list of name-color pairs and optionally description.
23+
func GetLabelTemplateFile(name string) ([][3]string, error) {
2424
data, err := getRepoInitFile("label", name)
2525
if err != nil {
2626
return nil, fmt.Errorf("getRepoInitFile: %v", err)
2727
}
2828

2929
lines := strings.Split(string(data), "\n")
30-
list := make([][2]string, 0, len(lines))
30+
list := make([][3]string, 0, len(lines))
3131
for i := 0; i < len(lines); i++ {
3232
line := strings.TrimSpace(lines[i])
3333
if len(line) == 0 {
3434
continue
3535
}
3636

37-
fields := strings.SplitN(line, " ", 2)
37+
parts := strings.SplitN(line, ";", 2)
38+
39+
fields := strings.SplitN(parts[0], " ", 2)
3840
if len(fields) != 2 {
3941
return nil, fmt.Errorf("line is malformed: %s", line)
4042
}
@@ -43,8 +45,14 @@ func GetLabelTemplateFile(name string) ([][2]string, error) {
4345
return nil, fmt.Errorf("bad HTML color code in line: %s", line)
4446
}
4547

48+
var description string
49+
50+
if len(parts) > 1 {
51+
description = strings.TrimSpace(parts[1])
52+
}
53+
4654
fields[1] = strings.TrimSpace(fields[1])
47-
list = append(list, [2]string{fields[1], fields[0]})
55+
list = append(list, [3]string{fields[1], fields[0], description})
4856
}
4957

5058
return list, nil
@@ -55,6 +63,7 @@ type Label struct {
5563
ID int64 `xorm:"pk autoincr"`
5664
RepoID int64 `xorm:"INDEX"`
5765
Name string
66+
Description string
5867
Color string `xorm:"VARCHAR(7)"`
5968
NumIssues int
6069
NumClosedIssues int

models/migrations/migrations.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/modules/generate"
2424
"code.gitea.io/gitea/modules/log"
2525
"code.gitea.io/gitea/modules/setting"
26+
"code.gitea.io/gitea/vendor/gopkg.in/ini.v1"
2627
)
2728

2829
const minDBVersion = 4
@@ -169,6 +170,8 @@ var migrations = []Migration{
169170
// v57 -> v58
170171
NewMigration("add closed_unix column for issues", addIssueClosedTime),
171172
// v58 -> v59
173+
NewMigration("add label descriptions", addLabelsDescriptions),
174+
// v59 -> v60
172175
NewMigration("add login source id column for public_key table", addLoginSourceIDToPublicKeyTable),
173176
}
174177

@@ -219,6 +222,66 @@ Please try to upgrade to a lower version (>= v0.6.0) first, then upgrade to curr
219222
return nil
220223
}
221224

225+
func dropTableColumns(x *xorm.Engine, tableName string, columnNames ...string) (err error) {
226+
if tableName == "" || len(columnNames) == 0 {
227+
return nil
228+
}
229+
230+
switch {
231+
case setting.UseSQLite3:
232+
log.Warn("Unable to drop columns in SQLite")
233+
case setting.UseMySQL, setting.UseTiDB, setting.UsePostgreSQL:
234+
cols := ""
235+
for _, col := range columnNames {
236+
if cols != "" {
237+
cols += ", "
238+
}
239+
cols += "DROP COLUMN `" + col + "`"
240+
}
241+
if _, err := x.Exec(fmt.Sprintf("ALTER TABLE `%s` %s", tableName, cols)); err != nil {
242+
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
243+
}
244+
case setting.UseMSSQL:
245+
sess := x.NewSession()
246+
defer sess.Close()
247+
248+
if err = sess.Begin(); err != nil {
249+
return err
250+
}
251+
252+
cols := ""
253+
for _, col := range columnNames {
254+
if cols != "" {
255+
cols += ", "
256+
}
257+
cols += "`" + strings.ToLower(col) + "`"
258+
}
259+
sql := fmt.Sprintf("SELECT Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('%[1]s') AND PARENT_COLUMN_ID IN (SELECT column_id FROM sys.columns WHERE lower(NAME) IN (%[2]s) AND object_id = OBJECT_ID('%[1]s'))",
260+
tableName, strings.Replace(cols, "`", "'", -1))
261+
constraints := make([]string, 0)
262+
if err := sess.SQL(sql).Find(&constraints); err != nil {
263+
sess.Rollback()
264+
return fmt.Errorf("Find constraints: %v", err)
265+
}
266+
for _, constraint := range constraints {
267+
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP CONSTRAINT `%s`", tableName, constraint)); err != nil {
268+
sess.Rollback()
269+
return fmt.Errorf("Drop table `%s` constraint `%s`: %v", tableName, constraint, err)
270+
}
271+
}
272+
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` DROP COLUMN %s", tableName, cols)); err != nil {
273+
sess.Rollback()
274+
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
275+
}
276+
277+
return sess.Commit()
278+
default:
279+
log.Fatal(4, "Unrecognized DB")
280+
}
281+
282+
return nil
283+
}
284+
222285
func fixLocaleFileLoadPanic(_ *xorm.Engine) error {
223286
cfg, err := ini.Load(setting.CustomConf)
224287
if err != nil {

0 commit comments

Comments
 (0)