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

@ -4,4 +4,8 @@ variable "gitea_url" {
variable "gitea_token" { variable "gitea_token" {
}
variable "gitea_mirror_token" {
} }

View File

@ -9,30 +9,42 @@ import (
) )
const ( const (
repoOwner string = "username" repoOwner string = "username"
repoName string = "name" repoName string = "name"
repoDescription string = "description" repoDescription string = "description"
repoPrivateFlag string = "private" repoPrivateFlag string = "private"
repoIssueLabels string = "issue_labels" repoIssueLabels string = "issue_labels"
repoAutoInit string = "auto_init" repoAutoInit string = "auto_init"
repoTemplate string = "repo_template" repoTemplate string = "repo_template"
repoGitignores string = "gitignores" repoGitignores string = "gitignores"
repoLicense string = "license" repoLicense string = "license"
repoReadme string = "readme" repoReadme string = "readme"
repoDefaultBranch string = "default_branch" repoDefaultBranch string = "default_branch"
repoWebsite string = "website" repoWebsite string = "website"
repoIssues string = "has_issues" repoIssues string = "has_issues"
repoWiki string = "has_wiki" repoWiki string = "has_wiki"
repoPrs string = "has_pull_requests" repoPrs string = "has_pull_requests"
repoProjects string = "has_projects" repoProjects string = "has_projects"
repoIgnoreWhitespace string = "ignore_whitespace_conflicts" repoIgnoreWhitespace string = "ignore_whitespace_conflicts"
repoAllowMerge string = "allow_merge_commits" repoAllowMerge string = "allow_merge_commits"
repoAllowRebase string = "allow_rebase" repoAllowRebase string = "allow_rebase"
repoAllowRebaseMerge string = "allow_rebase_explicit" repoAllowRebaseMerge string = "allow_rebase_explicit"
repoAllowSquash string = "allow_squash_merge" repoAllowSquash string = "allow_squash_merge"
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,20 +72,55 @@ func resourceRepoCreate(d *schema.ResourceData, meta interface{}) (err error) {
var repo *gitea.Repository var repo *gitea.Repository
opts := gitea.CreateRepoOption{ if (d.Get(repoMirror)).(bool) {
Name: d.Get(repoName).(string), opts := gitea.MigrateRepoOption{
Description: d.Get(repoDescription).(string), RepoName: d.Get(repoName).(string),
Private: d.Get(repoPrivateFlag).(bool), RepoOwner: d.Get(repoOwner).(string),
IssueLabels: d.Get(repoIssueLabels).(string), CloneAddr: d.Get(migrationCloneAddress).(string),
AutoInit: d.Get(repoAutoInit).(bool), Service: gitea.GitServiceType(d.Get(migrationService).(string)),
Template: d.Get(repoTemplate).(bool), Mirror: d.Get(repoMirror).(bool),
Gitignores: d.Get(repoGitignores).(string), Private: d.Get(repoPrivateFlag).(bool),
License: d.Get(repoLicense).(string), Description: d.Get(repoDescription).(string),
Readme: d.Get(repoReadme).(string), Wiki: d.Get(repoWiki).(bool),
DefaultBranch: d.Get(repoDefaultBranch).(string), Milestones: d.Get(migrationMilestones).(bool),
TrustModel: "default", 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{
Name: d.Get(repoName).(string),
Description: d.Get(repoDescription).(string),
Private: d.Get(repoPrivateFlag).(bool),
IssueLabels: d.Get(repoIssueLabels).(string),
AutoInit: d.Get(repoAutoInit).(bool),
Template: d.Get(repoTemplate).(bool),
Gitignores: d.Get(repoGitignores).(string),
License: d.Get(repoLicense).(string),
Readme: d.Get(repoReadme).(string),
DefaultBranch: d.Get(repoDefaultBranch).(string),
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",
} }