Browse Source

build-script find function; oauth creds of the Repository owning users are used

master
Stefan Naumann 5 months ago
parent
commit
2d1067c529
4 changed files with 51 additions and 38 deletions
  1. +44
    -33
      build/build.go
  2. +2
    -2
      core/config.go
  3. +4
    -2
      hook/gitea.go
  4. +1
    -1
      web/api.go

+ 44
- 33
build/build.go View File

@@ -5,18 +5,18 @@ package build

import (
"time"
"errors"
"mvoCI/core"
. "mvoCI/core"

"github.com/jinzhu/gorm"
)

// build a repository now (the latest commit, default branch)
func BuildNow ( r core.Repository, db *gorm.DB ) uint {
var bs core.BuildScript
db.Where("repository_id=? AND event_type = ?", r.ID, "push" ).First (&bs );

if bs.ID <= 0 {
// fallback: push event
bs, err := FindBuildScript ( db, r, "push" );
if err != nil {
Console.Warn("Build Script finding failed: ", err.Error());
return 0;
}
b := core.Build { Log: "", CommitSha: "", FinishedAt: time.Unix(0,0),
@@ -27,20 +27,39 @@ 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 {
func FindBuildScript ( db *gorm.DB, r core.Repository, event string ) (core.BuildScript, error) {
var bs core.BuildScript
db.Where("repository_id=? AND event_type = ?", r.ID, event ).First (&bs );

if bs.ID <= 0 {
// fallback: push event
db.Where("repository_id=? AND event_type = ?", r.ID, event ).First ( &bs );
if bs.ID <= 0 || len(bs.ShellScript) <= 0 {
if event != "push" {
db.Where("repository_id=? AND event_type = ?", r.ID, "push" ).First (&bs );
if bs.ID <= 0 {
return 0;
}
db.Where("repository_id=? AND event_type = ?", r.ID, "push" ).First ( &bs );
}
}

if bs.ID > 0 && len(bs.ShellScript) > 0 {
return bs, nil;
} else {
return core.BuildScript{}, errors.New("No BuildScript found");
}
}

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 {

bs, err := FindBuildScript ( db, r, "push" );
if err != nil {
Console.Warn("Build Script finding failed: ", err.Error());
return 0;
}

b := core.Build { Log: "", CommitSha: sha, FinishedAt: time.Unix(0,0),
Branch: branch, Repository: r, Status : "enqueued",
CommitAuthor: author, CommitUrl: url, CommitMessage: message,
@@ -51,18 +70,12 @@ func BuildRelease ( db *gorm.DB, r core.Repository, branch string, sha string, a
}

func BuildSpecific2 ( db *gorm.DB, r core.Repository, branch string, sha string, author string, url string, message string, event string ) uint {
var bs core.BuildScript
db.Where("repository_id=? AND event_type = ?", r.ID, event ).First (&bs );

if bs.ID <= 0 {
// fallback: push event
if event != "push" {
db.Where("repository_id=? AND event_type = ?", r.ID, "push" ).First (&bs );
if bs.ID <= 0 {
return 0;
}
}
bs, err := FindBuildScript ( db, r, "push" );
if err != nil {
Console.Warn("Build Script finding failed: ", err.Error());
return 0;
}

b := core.Build { Log: "", CommitSha: sha, FinishedAt: time.Unix(0,0),
Branch: branch, Repository: r, Status : "enqueued",
CommitAuthor: author, CommitUrl: url, CommitMessage: message,
@@ -72,12 +85,10 @@ func BuildSpecific2 ( db *gorm.DB, r core.Repository, branch string, sha string,
return b.ID;
}

func BuildSpecific ( db *gorm.DB, r core.Repository, branch string, sha string ) uint {
var bs core.BuildScript
db.Where("repository_id=? AND event_type = ?", r.ID, "push" ).First (&bs );

if bs.ID <= 0 {
// fallback: push event
func BuildSpecific3 ( db *gorm.DB, r core.Repository, branch string, sha string, u core.User ) uint {
bs, err := FindBuildScript ( db, r, "push" );
if err != nil {
Console.Warn("Build Script finding failed: ", err.Error());
return 0;
}

@@ -88,13 +99,13 @@ func BuildSpecific ( db *gorm.DB, r core.Repository, branch string, sha string )
return b.ID;
}

func BuildHookedFromSecret ( db *gorm.DB, secret string, branch string, sha string ) uint {
func BuildHookedFromSecret3 ( db *gorm.DB, secret string, branch string, sha string ) uint {
var r core.Repository
db.Where("secret = ?", secret).First ( &r )
if r.ID <= 0 {
return 0;
}
return BuildSpecific ( db, r, branch, sha )
return BuildSpecific3 ( db, r, branch, sha, core.User{} )
}

// rebuild a given build


+ 2
- 2
core/config.go View File

@@ -53,7 +53,7 @@ type ConfigApi struct {
ListingEnable bool
}

type ConfigGiteaHook struct {
type ConfigOauth struct {
OauthClientId string
OauthClientSecret string
}
@@ -80,7 +80,7 @@ type Config struct {
PublicEnable bool
Api ConfigApi
Build ConfigBuild
GiteaApi ConfigGiteaHook
GiteaApi ConfigOauth
}

func ConfigDefault () Config {


+ 4
- 2
hook/gitea.go View File

@@ -18,8 +18,10 @@ func GiteaPushRelease ( b *core.Build, url string, zip string, cfg *core.Config,

var cred core.OauthToken
var tok oauth2.Token;
db.Where("api = ? AND url LIKE ? AND state = ?", "gitea", baseurl, "finished" ).First ( &cred );

db.Where("api = ? AND url LIKE ? AND state = ? AND user_id = ?", "gitea", baseurl, "finished", b.Repository.UserID ).First ( &cred );
if cred.ID <= 0 {
return errors.New("No matching OAuth-Credentials found");
}
err := json.Unmarshal ( []byte(cred.ClientToken), &tok );
if err != nil {
return err;


+ 1
- 1
web/api.go View File

@@ -483,7 +483,7 @@ func apiRepoBuildHandler ( endpoint string, user core.User, ctx echo.Context ) e
branch = r.DefaultBranch;
}

b := build.BuildSpecific ( s.db, r, branch, hash )
b := build.BuildSpecific2 ( s.db, r, branch, hash, "", "", "", "API Build" )
ctx.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
var bx core.Build;
s.db.Where ("id = ?", b).First ( &bx );


Loading…
Cancel
Save