2019-11-24 20:57:52 +03:00
// Copyright 2019 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 models
import (
2021-04-20 01:25:08 +03:00
"bufio"
"bytes"
2021-09-23 18:45:36 +03:00
"context"
2019-11-24 20:57:52 +03:00
"strings"
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2021-12-10 04:27:50 +03:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-10 08:13:16 +03:00
"code.gitea.io/gitea/models/webhook"
2019-11-24 20:57:52 +03:00
"code.gitea.io/gitea/modules/log"
2019-11-30 09:54:47 +03:00
"github.com/gobwas/glob"
2019-11-24 20:57:52 +03:00
)
2019-11-25 08:17:51 +03:00
// GenerateRepoOptions contains the template units to generate
type GenerateRepoOptions struct {
2022-03-27 05:56:28 +03:00
Name string
DefaultBranch string
Description string
Private bool
GitContent bool
Topics bool
GitHooks bool
Webhooks bool
Avatar bool
IssueLabels bool
2019-11-25 08:17:51 +03:00
}
// IsValid checks whether at least one option is chosen for generation
func ( gro GenerateRepoOptions ) IsValid ( ) bool {
return gro . GitContent || gro . Topics || gro . GitHooks || gro . Webhooks || gro . Avatar || gro . IssueLabels // or other items as they are added
}
2019-11-30 09:54:47 +03:00
// GiteaTemplate holds information about a .gitea/template file
type GiteaTemplate struct {
Path string
Content [ ] byte
globs [ ] glob . Glob
}
// Globs parses the .gitea/template globs or returns them if they were already parsed
func ( gt GiteaTemplate ) Globs ( ) [ ] glob . Glob {
if gt . globs != nil {
return gt . globs
}
gt . globs = make ( [ ] glob . Glob , 0 )
2021-04-20 01:25:08 +03:00
scanner := bufio . NewScanner ( bytes . NewReader ( gt . Content ) )
for scanner . Scan ( ) {
line := strings . TrimSpace ( scanner . Text ( ) )
2019-11-30 09:54:47 +03:00
if line == "" || strings . HasPrefix ( line , "#" ) {
continue
}
g , err := glob . Compile ( line , '/' )
if err != nil {
log . Info ( "Invalid glob expression '%s' (skipped): %v" , line , err )
continue
}
gt . globs = append ( gt . globs , g )
}
return gt . globs
}
2019-11-24 20:57:52 +03:00
// GenerateWebhooks generates webhooks from a template repository
2021-12-10 04:27:50 +03:00
func GenerateWebhooks ( ctx context . Context , templateRepo , generateRepo * repo_model . Repository ) error {
2021-11-10 08:13:16 +03:00
templateWebhooks , err := webhook . ListWebhooksByOpts ( & webhook . ListWebhookOptions { RepoID : templateRepo . ID } )
2019-11-24 20:57:52 +03:00
if err != nil {
return err
}
for _ , templateWebhook := range templateWebhooks {
2021-11-10 08:13:16 +03:00
generateWebhook := & webhook . Webhook {
2020-12-09 20:20:13 +03:00
RepoID : generateRepo . ID ,
URL : templateWebhook . URL ,
HTTPMethod : templateWebhook . HTTPMethod ,
ContentType : templateWebhook . ContentType ,
Secret : templateWebhook . Secret ,
HookEvent : templateWebhook . HookEvent ,
IsActive : templateWebhook . IsActive ,
Type : templateWebhook . Type ,
OrgID : templateWebhook . OrgID ,
Events : templateWebhook . Events ,
Meta : templateWebhook . Meta ,
2019-11-24 20:57:52 +03:00
}
2021-11-10 08:13:16 +03:00
if err := webhook . CreateWebhook ( ctx , generateWebhook ) ; err != nil {
2019-11-24 20:57:52 +03:00
return err
}
}
return nil
}
2019-11-25 08:17:51 +03:00
// GenerateIssueLabels generates issue labels from a template repository
2021-12-10 04:27:50 +03:00
func GenerateIssueLabels ( ctx context . Context , templateRepo , generateRepo * repo_model . Repository ) error {
2021-09-24 14:32:56 +03:00
templateLabels , err := getLabelsByRepoID ( db . GetEngine ( ctx ) , templateRepo . ID , "" , db . ListOptions { } )
2019-11-25 08:17:51 +03:00
if err != nil {
return err
}
for _ , templateLabel := range templateLabels {
generateLabel := & Label {
RepoID : generateRepo . ID ,
Name : templateLabel . Name ,
Description : templateLabel . Description ,
Color : templateLabel . Color ,
}
2022-03-29 10:23:45 +03:00
if err := db . Insert ( ctx , generateLabel ) ; err != nil {
2019-11-25 08:17:51 +03:00
return err
}
}
return nil
}