From 34e7c1648f93d92f90edd2172f217906f35a5010 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sat, 10 Mar 2018 20:09:14 +0100 Subject: [PATCH 1/3] Add context to gitea status Signed-off-by: Jonas Franz --- cmd/drone-server/server.go | 12 +++++++++--- cmd/drone-server/setup.go | 7 ++++--- remote/gitea/gitea.go | 11 +++++++---- remote/gitea/gitea_test.go | 8 +++++--- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/cmd/drone-server/server.go b/cmd/drone-server/server.go index 389d019a..fe2032a1 100644 --- a/cmd/drone-server/server.go +++ b/cmd/drone-server/server.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -310,6 +310,12 @@ var flags = []cli.Flag{ Usage: "gitea server address", Value: "https://try.gitea.io", }, + cli.StringFlag{ + EnvVar: "DRONE_GITEA_CONTEXT", + Name: "gitea-context", + Usage: "gitea status context", + Value: "continuous-integration/drone", + }, cli.StringFlag{ EnvVar: "DRONE_GITEA_GIT_USERNAME", Name: "gitea-git-username", diff --git a/cmd/drone-server/setup.go b/cmd/drone-server/setup.go index e1dff3ec..248b49a4 100644 --- a/cmd/drone-server/setup.go +++ b/cmd/drone-server/setup.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -115,6 +115,7 @@ func setupGogs(c *cli.Context) (remote.Remote, error) { func setupGitea(c *cli.Context) (remote.Remote, error) { return gitea.New(gitea.Opts{ URL: c.String("gitea-server"), + Context: c.String("gitea-context"), Username: c.String("gitea-git-username"), Password: c.String("gitea-git-password"), PrivateMode: c.Bool("gitea-private-mode"), diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index 929b907e..1773007f 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -29,6 +29,7 @@ import ( // Opts defines configuration options. type Opts struct { URL string // Gitea server url. + Context string // Context to display in status check Username string // Optional machine account username. Password string // Optional machine account password. PrivateMode bool // Gitea is running in private mode. @@ -37,6 +38,7 @@ type Opts struct { type client struct { URL string + Context string Machine string Username string Password string @@ -111,6 +113,7 @@ func New(opts Opts) (remote.Remote, error) { } return &client{ URL: opts.URL, + Context: opts.Context, Machine: url.Host, Username: opts.Username, Password: opts.Password, @@ -266,7 +269,7 @@ func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link strin State: status, TargetURL: link, Description: desc, - Context: "", + Context: c.Context, }, ) diff --git a/remote/gitea/gitea_test.go b/remote/gitea/gitea_test.go index 3af78647..17f3610f 100644 --- a/remote/gitea/gitea_test.go +++ b/remote/gitea/gitea_test.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -44,12 +44,14 @@ func Test_gitea(t *testing.T) { g.It("Should return client with specified options", func() { remote, _ := New(Opts{ URL: "http://localhost:8080", + Context: "continuous-integration/test", Username: "someuser", Password: "password", SkipVerify: true, PrivateMode: true, }) g.Assert(remote.(*client).URL).Equal("http://localhost:8080") + g.Assert(remote.(*client).Context).Equal("continuous-integration/test") g.Assert(remote.(*client).Machine).Equal("localhost") g.Assert(remote.(*client).Username).Equal("someuser") g.Assert(remote.(*client).Password).Equal("password") From 6a32935011fc5dd63b1ff5883a754ac31863b774 Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sat, 10 Mar 2018 20:09:14 +0100 Subject: [PATCH 2/3] Add context to gitea status Signed-off-by: Jonas Franz --- cmd/drone-server/server.go | 12 +++++++++--- cmd/drone-server/setup.go | 7 ++++--- remote/gitea/gitea.go | 11 +++++++---- remote/gitea/gitea_test.go | 8 +++++--- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/cmd/drone-server/server.go b/cmd/drone-server/server.go index 389d019a..fe2032a1 100644 --- a/cmd/drone-server/server.go +++ b/cmd/drone-server/server.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -310,6 +310,12 @@ var flags = []cli.Flag{ Usage: "gitea server address", Value: "https://try.gitea.io", }, + cli.StringFlag{ + EnvVar: "DRONE_GITEA_CONTEXT", + Name: "gitea-context", + Usage: "gitea status context", + Value: "continuous-integration/drone", + }, cli.StringFlag{ EnvVar: "DRONE_GITEA_GIT_USERNAME", Name: "gitea-git-username", diff --git a/cmd/drone-server/setup.go b/cmd/drone-server/setup.go index e1dff3ec..248b49a4 100644 --- a/cmd/drone-server/setup.go +++ b/cmd/drone-server/setup.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -115,6 +115,7 @@ func setupGogs(c *cli.Context) (remote.Remote, error) { func setupGitea(c *cli.Context) (remote.Remote, error) { return gitea.New(gitea.Opts{ URL: c.String("gitea-server"), + Context: c.String("gitea-context"), Username: c.String("gitea-git-username"), Password: c.String("gitea-git-password"), PrivateMode: c.Bool("gitea-private-mode"), diff --git a/remote/gitea/gitea.go b/remote/gitea/gitea.go index 929b907e..1773007f 100644 --- a/remote/gitea/gitea.go +++ b/remote/gitea/gitea.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -29,6 +29,7 @@ import ( // Opts defines configuration options. type Opts struct { URL string // Gitea server url. + Context string // Context to display in status check Username string // Optional machine account username. Password string // Optional machine account password. PrivateMode bool // Gitea is running in private mode. @@ -37,6 +38,7 @@ type Opts struct { type client struct { URL string + Context string Machine string Username string Password string @@ -111,6 +113,7 @@ func New(opts Opts) (remote.Remote, error) { } return &client{ URL: opts.URL, + Context: opts.Context, Machine: url.Host, Username: opts.Username, Password: opts.Password, @@ -266,7 +269,7 @@ func (c *client) Status(u *model.User, r *model.Repo, b *model.Build, link strin State: status, TargetURL: link, Description: desc, - Context: "", + Context: c.Context, }, ) diff --git a/remote/gitea/gitea_test.go b/remote/gitea/gitea_test.go index 3af78647..17f3610f 100644 --- a/remote/gitea/gitea_test.go +++ b/remote/gitea/gitea_test.go @@ -1,11 +1,11 @@ // Copyright 2018 Drone.IO Inc. -// +// // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -44,12 +44,14 @@ func Test_gitea(t *testing.T) { g.It("Should return client with specified options", func() { remote, _ := New(Opts{ URL: "http://localhost:8080", + Context: "continuous-integration/test", Username: "someuser", Password: "password", SkipVerify: true, PrivateMode: true, }) g.Assert(remote.(*client).URL).Equal("http://localhost:8080") + g.Assert(remote.(*client).Context).Equal("continuous-integration/test") g.Assert(remote.(*client).Machine).Equal("localhost") g.Assert(remote.(*client).Username).Equal("someuser") g.Assert(remote.(*client).Password).Equal("password") From 00f72ef20668d9d048edc32dcd54c96006038a54 Mon Sep 17 00:00:00 2001 From: Fernando Barbosa Date: Tue, 27 Mar 2018 19:19:22 -0300 Subject: [PATCH 3/3] Fix EventStreamSSE Memory Leak The `Config.Services.Pubsub.Subscribe` is being initialized with the global Gin Context. This causes the publisher object to [hang at line 58](https://github.com/cncd/pubsub/blob/master/pub.go#L58) and the goroutine remains opened indefinetly, which can be a source of memory leak. Setting it to the locally defined ctx object correctly causes the goroutine to close when the function exits due to the deferred close. See https://discourse.drone.io/t/memory-leak-on-drone-server/1884 for further information. --- server/stream.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/stream.go b/server/stream.go index 2e202989..6dd19a9b 100644 --- a/server/stream.go +++ b/server/stream.go @@ -78,7 +78,7 @@ func EventStreamSSE(c *gin.Context) { go func() { // TODO remove this from global config - Config.Services.Pubsub.Subscribe(c, "topic/events", func(m pubsub.Message) { + Config.Services.Pubsub.Subscribe(ctx, "topic/events", func(m pubsub.Message) { name := m.Labels["repo"] priv := m.Labels["private"] if repo[name] || priv == "false" {