added possibility to create migrations as well
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Tobias Trabelsi 2022-06-04 22:43:23 +02:00
parent 9c2fa1e3b9
commit f9d36151fc
Signed by: lerentis
GPG Key ID: FF0C2839718CAF2E
4 changed files with 194 additions and 41 deletions

View File

@ -40,6 +40,18 @@ Handling Repository resources
- `ignore_whitespace_conflicts` (Boolean) - `ignore_whitespace_conflicts` (Boolean)
- `issue_labels` (String) - `issue_labels` (String)
- `license` (String) - `license` (String)
- `migration_clone_addresse` (String)
- `migration_issue_labels` (Boolean)
- `migration_lfs` (Boolean)
- `migration_lfs_endpoint` (String)
- `migration_milestones` (Boolean)
- `migration_mirror_interval` (String) valid time units are 'h', 'm', 's'. 0 to disable automatic sync
- `migration_releases` (Boolean)
- `migration_service` (String) git/github/gitlab/gitea/gogs
- `migration_service_auth_password` (String, Sensitive)
- `migration_service_auth_token` (String, Sensitive)
- `migration_service_auth_username` (String)
- `mirror` (Boolean)
- `private` (Boolean) - `private` (Boolean)
- `readme` (String) - `readme` (String)
- `repo_template` (Boolean) - `repo_template` (Boolean)

View File

@ -6,3 +6,13 @@ resource "gitea_repository" "test" {
license = "MIT" license = "MIT"
gitignores = "Go" gitignores = "Go"
} }
resource "gitea_repository" "mirror" {
username = "lerentis"
name = "terraform-provider-gitea-mirror"
description = "Mirror of Terraform Provider"
mirror = true
migration_clone_addresse = "https://git.uploadfilter24.eu/lerentis/terraform-provider-gitea.git"
migration_service = "gitea"
migration_service_auth_token = var.gitea_mirror_token
}

View File

@ -5,3 +5,7 @@ variable "gitea_url" {
variable "gitea_token" { variable "gitea_token" {
} }
variable "gitea_mirror_token" {
}

View File

@ -33,6 +33,18 @@ const (
repoAchived string = "archived" repoAchived string = "archived"
repoAllowManualMerge string = "allow_manual_merge" repoAllowManualMerge string = "allow_manual_merge"
repoAutodetectManualMerge string = "autodetect_manual_merge" repoAutodetectManualMerge string = "autodetect_manual_merge"
repoMirror string = "mirror"
migrationCloneAddress string = "migration_clone_addresse"
migrationService string = "migration_service"
migrationServiceAuthName string = "migration_service_auth_username"
migrationServiceAuthPassword string = "migration_service_auth_password"
migrationServiceAuthToken string = "migration_service_auth_token"
migrationMilestones string = "migration_milestones"
migrationReleases string = "migration_releases"
migrationIssueLabels string = "migration_issue_labels"
migrationMirrorInterval string = "migration_mirror_interval"
migrationLFS string = "migration_lfs"
migrationLFSEndpoint string = "migration_lfs_endpoint"
) )
func resourceRepoRead(d *schema.ResourceData, meta interface{}) (err error) { func resourceRepoRead(d *schema.ResourceData, meta interface{}) (err error) {
@ -60,6 +72,40 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) {
var repo *gitea.Repository var repo *gitea.Repository
if (d.Get(repoMirror)).(bool) {
opts := gitea.MigrateRepoOption{
RepoName: d.Get(repoName).(string),
RepoOwner: d.Get(repoOwner).(string),
CloneAddr: d.Get(migrationCloneAddress).(string),
Service: gitea.GitServiceType(d.Get(migrationService).(string)),
Mirror: d.Get(repoMirror).(bool),
Private: d.Get(repoPrivateFlag).(bool),
Description: d.Get(repoDescription).(string),
Wiki: d.Get(repoWiki).(bool),
Milestones: d.Get(migrationMilestones).(bool),
Labels: d.Get(migrationIssueLabels).(bool),
Issues: d.Get(repoIssues).(bool),
PullRequests: d.Get(repoPrs).(bool),
Releases: d.Get(migrationReleases).(bool),
MirrorInterval: d.Get(migrationMirrorInterval).(string),
LFS: d.Get(migrationLFS).(bool),
LFSEndpoint: d.Get(migrationLFSEndpoint).(string),
}
if d.Get(migrationServiceAuthName).(string) != "" {
opts.AuthUsername = d.Get(migrationServiceAuthName).(string)
}
if d.Get(migrationServiceAuthPassword).(string) != "" {
opts.AuthPassword = d.Get(migrationServiceAuthPassword).(string)
}
if d.Get(migrationServiceAuthToken).(string) != "" {
opts.AuthToken = d.Get(migrationServiceAuthToken).(string)
}
repo, _, err = client.MigrateRepo(opts)
} else {
opts := gitea.CreateRepoOption{ opts := gitea.CreateRepoOption{
Name: d.Get(repoName).(string), Name: d.Get(repoName).(string),
Description: d.Get(repoDescription).(string), Description: d.Get(repoDescription).(string),
@ -74,6 +120,7 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) {
TrustModel: "default", TrustModel: "default",
} }
repo, _, err = client.CreateRepo(opts) repo, _, err = client.CreateRepo(opts)
}
if err != nil { if err != nil {
return return
@ -91,7 +138,7 @@ func resourceRepoUpdate(d *schema.ResourceData, meta interface{}) (err error) {
var name string = d.Get(repoName).(string) var name string = d.Get(repoName).(string)
var description string = d.Get(repoDescription).(string) var description string = d.Get(repoDescription).(string)
var website string = d.Get(repoDescription).(string) var website string = d.Get(repoWebsite).(string)
var private bool = d.Get(repoPrivateFlag).(bool) var private bool = d.Get(repoPrivateFlag).(bool)
var template bool = d.Get(repoTemplate).(bool) var template bool = d.Get(repoTemplate).(bool)
var hasIssues bool = d.Get(repoIssues).(bool) var hasIssues bool = d.Get(repoIssues).(bool)
@ -104,7 +151,6 @@ func resourceRepoUpdate(d *schema.ResourceData, meta interface{}) (err error) {
var allowRebase bool = d.Get(repoAllowRebase).(bool) var allowRebase bool = d.Get(repoAllowRebase).(bool)
var allowRebaseMerge bool = d.Get(repoAllowRebaseMerge).(bool) var allowRebaseMerge bool = d.Get(repoAllowRebaseMerge).(bool)
var allowSquash bool = d.Get(repoAllowSquash).(bool) var allowSquash bool = d.Get(repoAllowSquash).(bool)
var archived bool = d.Get(repoAchived).(bool)
var allowManualMerge bool = d.Get(repoAllowManualMerge).(bool) var allowManualMerge bool = d.Get(repoAllowManualMerge).(bool)
var autodetectManualMerge bool = d.Get(repoAutodetectManualMerge).(bool) var autodetectManualMerge bool = d.Get(repoAutodetectManualMerge).(bool)
@ -124,11 +170,18 @@ func resourceRepoUpdate(d *schema.ResourceData, meta interface{}) (err error) {
AllowRebase: &allowRebase, AllowRebase: &allowRebase,
AllowRebaseMerge: &allowRebaseMerge, AllowRebaseMerge: &allowRebaseMerge,
AllowSquash: &allowSquash, AllowSquash: &allowSquash,
Archived: &archived,
AllowManualMerge: &allowManualMerge, AllowManualMerge: &allowManualMerge,
AutodetectManualMerge: &autodetectManualMerge, AutodetectManualMerge: &autodetectManualMerge,
} }
if d.Get(repoMirror).(bool) {
var mirrorInterval string = d.Get(migrationMirrorInterval).(string)
opts.MirrorInterval = &mirrorInterval
} else {
var archived bool = d.Get(repoAchived).(bool)
opts.Archived = &archived
}
repo, _, err = client.EditRepo(d.Get(repoOwner).(string), d.Get(repoName).(string), opts) repo, _, err = client.EditRepo(d.Get(repoOwner).(string), d.Get(repoName).(string), opts)
if err != nil { if err != nil {
@ -153,7 +206,6 @@ func setRepoResourceData(repo *gitea.Repository, d *schema.ResourceData) (err er
d.Set("name", repo.Name) d.Set("name", repo.Name)
d.Set("description", repo.Description) d.Set("description", repo.Description)
d.Set("full_name", repo.FullName) d.Set("full_name", repo.FullName)
d.Set("description", repo.Description)
d.Set("private", repo.Private) d.Set("private", repo.Private)
d.Set("fork", repo.Fork) d.Set("fork", repo.Fork)
d.Set("mirror", repo.Mirror) d.Set("mirror", repo.Mirror)
@ -348,6 +400,81 @@ func resourceGiteaRepository() *schema.Resource {
Optional: true, Optional: true,
Default: true, Default: true,
}, },
"mirror": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: false,
},
"migration_clone_addresse": {
Type: schema.TypeString,
Required: false,
Optional: true,
ForceNew: true,
},
"migration_service": {
Type: schema.TypeString,
Required: false,
ForceNew: true,
Optional: true,
Description: "git/github/gitlab/gitea/gogs",
},
"migration_service_auth_username": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
},
"migration_service_auth_password": {
Type: schema.TypeString,
Required: false,
Optional: true,
Sensitive: true,
Default: "",
},
"migration_service_auth_token": {
Type: schema.TypeString,
Required: false,
Optional: true,
Sensitive: true,
Default: "",
},
"migration_milestones": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"migration_releases": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"migration_issue_labels": {
Type: schema.TypeBool,
Required: false,
Optional: true,
Default: true,
},
"migration_mirror_interval": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "8h0m0s",
Description: "valid time units are 'h', 'm', 's'. 0 to disable automatic sync",
},
"migration_lfs": {
Type: schema.TypeBool,
Required: false,
Optional: true,
},
"migration_lfs_endpoint": {
Type: schema.TypeString,
Required: false,
Optional: true,
Default: "",
},
}, },
Description: "Handling Repository resources", Description: "Handling Repository resources",
} }