Browse Source

gitea-hook: first working asset push to gitea on release-hook; intermediate state!

master
Stefan Naumann 12 months ago
parent
commit
1f8c67bfac
  1. 3
      Makefile
  2. 10
      build/build.go
  3. 18
      build/worker.go
  4. 1
      core/config.go
  5. 2
      core/models.go
  6. 72
      hook/gitea.go
  7. 6
      hook/gitlab.go
  8. 33
      web/webhook.go

3
Makefile

@ -48,3 +48,6 @@ dep:
doc:
godoc -http=localhost:6060
ctags:
ctags -R --language-force=go .

10
build/build.go

@ -20,6 +20,16 @@ func BuildNow ( r core.Repository, db *gorm.DB ) uint {
return b.ID
}
func BuildRelease ( db *gorm.DB, r core.Repository, branch string, sha string, author string, url string, message string, event string, api string, apiUrl string ) uint {
b := core.Build { Log: "", CommitSha: sha, FinishedAt: time.Unix(0,0),
Branch: branch, Repository: r, Status : "enqueued",
CommitAuthor: author, CommitUrl: url, CommitMessage: message,
Event: event, Api: api, ApiUrl: apiUrl }
db.Create ( &b )
EnqueueBuild ( b )
return b.ID;
}
func BuildSpecific2 ( db *gorm.DB, r core.Repository, branch string, sha string, author string, url string, message string, event string ) uint {
b := core.Build { Log: "", CommitSha: sha, FinishedAt: time.Unix(0,0),
Branch: branch, Repository: r, Status : "enqueued",

18
build/worker.go

@ -16,6 +16,7 @@ import (
"strconv"
"io/ioutil"
"mvoCI/core"
"mvoCI/hook"
"github.com/jinzhu/gorm"
)
@ -238,6 +239,10 @@ func WorkerGoZip ( b *core.Build, log *mockerBuilder ) (*mockerBuilder, bool) {
}
b.Zip = ZipName
if b.Api == "gitea" && b.Event == "release" {
// only gitea has release-api event implemented, atm. (2020-06-27)
return WorkerPushRelease ( b, log );
}
} else {
b.Zip = ""
log.append ("> Skipping zipping because of repo-local configuration");
@ -245,6 +250,19 @@ func WorkerGoZip ( b *core.Build, log *mockerBuilder ) (*mockerBuilder, bool) {
return WorkerGoCleanup ( b, log )
}
func WorkerPushRelease ( b *core.Build, log *mockerBuilder ) (*mockerBuilder, bool ) {
switch b.Api {
case "gitea":
err := hook.GiteaPushRelease ( b.ApiUrl, c.cfg.Directory.Build + "/" + b.Zip, c.cfg.GiteaApi );
if err != nil {
core.Console.Log("Error pushing the artifact: ", err );
} else {
core.Console.Log("Pushing artifact should have worked. Maybe.");
}
}
return WorkerGoCleanup ( b, log );
}
// removes the repo directory
func WorkerGoCleanup ( b *core.Build, log *mockerBuilder ) (*mockerBuilder, bool) {
log.append ( "> Cleaning up build directory\n" )

1
core/config.go

@ -75,6 +75,7 @@ type Config struct {
PublicEnable bool
Api ConfigApi
Build ConfigBuild
GiteaApi string
}
func ConfigDefault () Config {

2
core/models.go

@ -89,6 +89,8 @@ type Build struct {
Zip string // keeps the name of the zipped Build
Repository Repository // link to the Repository
RepositoryID uint
Api string
ApiUrl string
}
type WebHookLog struct {

72
hook/gitea.go

@ -1,6 +1,61 @@
package hook
import "time"
import (
"fmt"
"io"
"os"
"bytes"
"time"
"errors"
"strconv"
"net/http"
"io/ioutil"
"path/filepath"
"mime/multipart"
)
func GiteaPushRelease ( url string, zip string, GiteaApiKey string ) error {
url2 := url + "/assets?access_token=" + GiteaApiKey
fmt.Println("\n", url2, "\n");
formData := map[string]string{
"authorization": "token " + GiteaApiKey,
};
file, err := os.Open( zip )
if err != nil {
return err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter( body )
part, err := writer.CreateFormFile( "attachment", filepath.Base( zip ) )
if err != nil {
return err
}
_, err = io.Copy( part, file )
if err != nil {
return err
}
for key, val := range formData {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return err
}
client := &http.Client{};
req, _ := http.NewRequest( "POST", url2, body )
req.Header.Set("Content-Type", writer.FormDataContentType())
resp, err := client.Do ( req )
if resp.StatusCode != 200 {
body, _ := ioutil.ReadAll ( resp.Body );
fmt.Println ("Message: ", string(body) );
return errors.New("Statuscode ("+strconv.Itoa(resp.StatusCode)+") != 200");
}
return nil;
}
type GiteaPayloadCommitUser struct {
Name string
@ -47,6 +102,19 @@ type GiteaPayloadRepository struct {
Updated_at time.Time
}
type GiteaPayloadRelease struct {
Id int
TagName string `json:"tag_name"`
TargetCommitish string `json:"target_commitish"`
Name string
Body string
Url string
HtmlUrl string `json:"html_url"`
Draft bool
Prerelease bool
Author GiteaPayloadUser
}
type GiteaPayload struct {
Secret string
Ref string
@ -57,5 +125,5 @@ type GiteaPayload struct {
Repository GiteaPayloadRepository
Pusher GiteaPayloadUser
Sender GiteaPayloadUser
Release GiteaPayloadRelease
}

6
hook/gitlab.go

@ -5,9 +5,9 @@ type GitlabPayloadProject struct {
Name string
Description string
WebUrl string `json:"web_url"`
AvatarUrl string `json:avatar_url"`
GitSshUrl string `json:git_ssh_url"`
GitHttpUrl string `json:git_http_url"`
AvatarUrl string `json:"avatar_url"`
GitSshUrl string `json:"git_ssh_url"`
GitHttpUrl string `json:"git_http_url"`
}
type GitlabPayloadCommit struct {

33
web/webhook.go

@ -137,7 +137,7 @@ func webhook ( ctx echo.Context ) error {
}
case "gitea":
event = ctx.Request().Header["X-Gitea-Event"][0]
if event == "push" {
if event == "push" || event == "release" {
var pl GiteaPayload
err = GenericJSONDecode ( rd, &pl )
if err != nil {
@ -150,15 +150,30 @@ func webhook ( ctx echo.Context ) error {
HookLog.Repository = r;
sig := ctx.Request().Header["X-Gitea-Signature"][0]
if SecretCheck ( &bodyBuff, r.Secret, sig, "sha256" ) {
author = pl.Pusher.Full_Name;
if len(author) < 1 {
author = pl.Pusher.Login
if event == "push" {
author = pl.Pusher.Full_Name;
if len(author) < 1 {
author = pl.Pusher.Login
}
if len(pl.Commits) > 0 {
message = pl.Commits[0].Message
url = pl.Commits[0].Url
bID = build.BuildSpecific2 ( s.db, r, branchFromRef ( pl.Ref ), pl.After, author, url, message, event )
}
} else if event == "release" {
author = pl.Release.Author.Full_Name;
if len(author) < 1 {
author = pl.Release.Author.Login
}
message = pl.Release.TagName + " - " + pl.Release.Name
url = pl.Release.HtmlUrl
apiUrl := pl.Release.Url
bID = build.BuildRelease ( s.db, r, pl.Release.TargetCommitish, pl.Release.TagName, author, url, message, event, api, apiUrl );
}
message = pl.Commits[0].Message
url = pl.Commits[0].Url
bID = build.BuildSpecific2 ( s.db, r, branchFromRef ( pl.Ref ), pl.After, author, url, message, event )
} else {
ResponseBody = "Signature check failed"
}

Loading…
Cancel
Save