gitlink-cli/workflows/03-project-init.ps1

232 lines
11 KiB
PowerShell

# ----------------------------------------------------------------
# Scenario 3: One-Click Project Initialization
# Flow: Input description -> Create repo -> README/CONTRIBUTING/CI config ->
# Initial Issues -> Branch protection -> Initial Release
#
# Commands chained:
# 1. repo +create -- create repository
# 2. wiki +create -- create README wiki page
# 3. wiki +create -- create CONTRIBUTING guide
# 4. wiki +create -- create CI/CD config guide
# 5. issue +create -- create initial issues
# 6. branch +protect -- protect master branch
# 7. release +create -- create initial release
# ----------------------------------------------------------------
#Requires -Version 5.1
param(
[string]$Owner = "",
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$true)]
[string]$Description,
[string]$Lang = "go",
[switch]$Private,
[switch]$DryRun,
[switch]$Help
)
$ErrorActionPreference = "Stop"
Import-Module "$PSScriptRoot/lib/common.psm1" -Force
if ($Help) {
Write-Host "Usage: powershell 03-project-init.ps1 -Owner OWNER -Name REPO_NAME -Description DESC [-Lang go|python|node|java] [-Private] [-DryRun]"
exit 0
}
Check-Auth
if (-not $Owner) {
$detected = Detect-OwnerRepo
$Owner = $detected.Owner
}
# ----------------------------------------------------------------
Log-Title "Project Initialization: $Owner/$Name"
# ----------------------------------------------------------------
Write-Host " Owner: $Owner"
Write-Host " Name: $Name"
Write-Host " Description: $Description"
Write-Host " Language: $Lang"
Write-Host " Private: $($Private.IsPresent)"
Divider
# -- Step 1: Create Repository --
Log-Step "Creating repository..."
$privateStr = if ($Private) { "true" } else { "false" }
$repoResult = Invoke-GLCheck repo,+create,--owner,$Owner,--name,$Name,--description,$Description,--private,$privateStr
if ($repoResult) {
Log-Ok "Repository created: $Owner/$Name"
} else {
Log-Err "Repository creation failed"
exit 1
}
# -- Step 2: Create README --
Log-Step "Creating README wiki page..."
Start-Sleep -Seconds 2
$langSection = switch ($Lang) {
"go" {
"### Prerequisites`n- Go 1.21+`n- Git`n`n### Installation`n``````bash`ngit clone https://gitlink.org.cn/$Owner/$Name.git`ncd $Name`ngo mod download`ngo build ./...`n```````n`n### Usage`n``````bash`ngo run main.go`n```````n`n### Testing`n``````bash`ngo test ./...`n``````"
}
"python" {
"### Prerequisites`n- Python 3.9+`n- pip`n`n### Installation`n``````bash`ngit clone https://gitlink.org.cn/$Owner/$Name.git`ncd $Name`npip install -r requirements.txt`n```````n`n### Usage`n``````bash`npython main.py`n```````n`n### Testing`n``````bash`npytest`n``````"
}
"node" {
"### Prerequisites`n- Node.js 18+`n- npm or yarn`n`n### Installation`n``````bash`ngit clone https://gitlink.org.cn/$Owner/$Name.git`ncd $Name`nnpm install`n```````n`n### Usage`n``````bash`nnpm start`n```````n`n### Testing`n``````bash`nnpm test`n``````"
}
"java" {
"### Prerequisites`n- JDK 17+`n- Maven 3.8+`n`n### Installation`n``````bash`ngit clone https://gitlink.org.cn/$Owner/$Name.git`ncd $Name`nmvn clean install`n```````n`n### Usage`n``````bash`nmvn exec:java`n```````n`n### Testing`n``````bash`nmvn test`n``````"
}
default { "" }
}
$readmeContent = "# $Name" + "`n`n"
$readmeContent += "$Description" + "`n`n"
$readmeContent += "## Getting Started" + "`n`n"
$readmeContent += $langSection + "`n`n"
$readmeContent += "## Contributing" + "`n`n"
$readmeContent += "See [CONTRIBUTING](./CONTRIBUTING) for guidelines." + "`n`n"
$readmeContent += "## License" + "`n`n"
$readmeContent += "This project is licensed under the MIT License."
$wikiOk = $false
for ($attempt = 1; $attempt -le 3; $attempt++) {
$wikiResult = Invoke-GL wiki,+create,--owner,$Owner,--repo,$Name,--title,"README",--body,$readmeContent
if ($wikiResult -and (Get-JsonOk ($wikiResult | ConvertFrom-Json))) {
Log-Ok "README created"
$wikiOk = $true
break
}
if ($attempt -lt 3) { Start-Sleep -Seconds 2 }
}
if (-not $wikiOk) { Log-Warn "README wiki creation may have failed" }
# -- Step 3: Create CONTRIBUTING Guide --
Log-Step "Creating CONTRIBUTING guide..."
$contribContent = "# Contributing to $Name" + "`n`n"
$contribContent += "Thank you for your interest in contributing!" + "`n`n"
$contribContent += "## How to Contribute" + "`n`n"
$contribContent += "1. Fork the repository" + "`n"
$contribContent += "2. Create a feature branch: ``git checkout -b feature/my-feature```n"
$contribContent += "3. Make your changes" + "`n"
$contribContent += "4. Run tests to ensure everything passes" + "`n"
$contribContent += "5. Commit your changes: ``git commit -m 'feat: add my feature'```n"
$contribContent += "6. Push to your fork: ``git push origin feature/my-feature```n"
$contribContent += "7. Create a Pull Request" + "`n`n"
$contribContent += "## Code Style" + "`n`n"
$contribContent += "- Follow the existing code style" + "`n"
$contribContent += "- Write meaningful commit messages" + "`n"
$contribContent += "- Add tests for new features" + "`n"
$contribContent += "- Update documentation as needed" + "`n`n"
$contribContent += "## Reporting Issues" + "`n`n"
$contribContent += "- Use the issue tracker" + "`n"
$contribContent += "- Include reproduction steps" + "`n"
$contribContent += "- Include environment details"
$wikiOk = $false
for ($attempt = 1; $attempt -le 3; $attempt++) {
$wikiContrib = Invoke-GL wiki,+create,--owner,$Owner,--repo,$Name,--title,"CONTRIBUTING",--body,$contribContent
if ($wikiContrib -and (Get-JsonOk ($wikiContrib | ConvertFrom-Json))) {
Log-Ok "CONTRIBUTING guide created"
$wikiOk = $true
break
}
if ($attempt -lt 3) { Start-Sleep -Seconds 2 }
}
if (-not $wikiOk) { Log-Warn "CONTRIBUTING wiki creation may have failed" }
# -- Step 4: Create CI Config Guide --
Log-Step "Creating CI/CD configuration guide..."
$ciContent = "# CI/CD Configuration" + "`n`n"
$ciContent += "## GitLink CI Setup" + "`n`n"
$ciContent += "This project uses GitLink CI for continuous integration." + "`n`n"
$ciContent += "### Pipeline Stages" + "`n`n"
$ciContent += "1. **Test**: Run unit tests" + "`n"
$ciContent += "2. **Build**: Build the project" + "`n"
$ciContent += "3. **Deploy**: Deploy to staging (master branch only)" + "`n`n"
$ciContent += "### Configuration" + "`n`n"
$ciContent += "Create a ``.gitlink-ci.yml`` file in the repository root."
Invoke-GL wiki,+create,--owner,$Owner,--repo,$Name,--title,"CI/CD Configuration",--body,$ciContent | Out-Null
Log-Ok "CI/CD configuration guide created"
# -- Step 5: Create Initial Issues --
Log-Step "Creating initial issues..."
$issuesToCreate = @(
@{ Title = "Setup CI/CD Pipeline"; Body = "Configure continuous integration and deployment for the project.`n`n## Tasks`n- [ ] Create .gitlink-ci.yml configuration`n- [ ] Setup test stage`n- [ ] Setup build stage`n- [ ] Setup deploy stage`n- [ ] Add status badge to README"; Label = "feature" },
@{ Title = "Write Project Documentation"; Body = "Complete project documentation including API docs and architecture guide.`n`n## Tasks`n- [ ] Write API documentation`n- [ ] Create architecture diagram`n- [ ] Add usage examples`n- [ ] Document configuration options"; Label = "documentation" },
@{ Title = "Setup Code Review Process"; Body = "Establish code review guidelines and automation.`n`n## Tasks`n- [ ] Define review checklist`n- [ ] Setup branch protection rules`n- [ ] Configure required reviewers`n- [ ] Document review process"; Label = "enhancement" },
@{ Title = "Add Unit Tests"; Body = "Add comprehensive unit test coverage for core modules.`n`n## Tasks`n- [ ] Setup test framework`n- [ ] Write tests for core modules`n- [ ] Achieve 80 percent code coverage`n- [ ] Add CI test integration"; Label = "enhancement" },
@{ Title = "Setup Dependency Management"; Body = "Configure dependency scanning and updates.`n`n## Tasks`n- [ ] Setup dependency scanner`n- [ ] Configure automatic updates`n- [ ] Add license compliance check`n- [ ] Document dependency policy"; Label = "security" }
)
foreach ($entry in $issuesToCreate) {
$issueResult = Invoke-GL issue,+create,--owner,$Owner,--repo,$Name,--title,$entry.Title,--body,$entry.Body
if ($issueResult) {
try {
$issueJson = $issueResult | ConvertFrom-Json
$issueNum = if ($issueJson.data.id) { $issueJson.data.id } elseif ($issueJson.data.number) { $issueJson.data.number } else { $null }
if ($issueNum) {
Invoke-GL issue,+label-add,--owner,$Owner,--repo,$Name,--number,$issueNum,--labels,$entry.Label | Out-Null
Log-Ok "Issue created: #$issueNum - $($entry.Title)"
}
} catch {
Log-Warn "Issue creation may have failed: $($entry.Title)"
}
}
}
# -- Step 6: Protect Default Branch --
Log-Step "Protecting master branch..."
$protectResult = Invoke-GL branch,+protect,--owner,$Owner,--repo,$Name,--name,master
if ($protectResult -and (Get-JsonOk ($protectResult | ConvertFrom-Json))) {
Log-Ok "Branch 'master' protected"
} else {
Log-Warn "Branch protection may have failed (may require admin permissions)"
}
# -- Step 7: Create Initial Release --
Log-Step "Creating initial release v0.1.0..."
$releaseBody = "# v0.1.0 - Initial Release" + "`n`n"
$releaseBody += "## What's New" + "`n"
$releaseBody += "- Project initialized with $Lang template" + "`n"
$releaseBody += "- README and CONTRIBUTING guides created" + "`n"
$releaseBody += "- CI/CD configuration guide created" + "`n"
$releaseBody += "- 5 initial issues filed" + "`n"
$releaseBody += "- Branch protection enabled" + "`n`n"
$releaseBody += "## Next Steps" + "`n"
$releaseBody += "- [ ] Setup CI/CD pipeline" + "`n"
$releaseBody += "- [ ] Write comprehensive tests" + "`n"
$releaseBody += "- [ ] Complete documentation" + "`n"
$releaseBody += "- [ ] First feature implementation" + "`n`n"
$releaseBody += "---`n*Auto-initialized by gitlink-cli project-init workflow*"
$releaseResult = Invoke-GL release,+create,--owner,$Owner,--repo,$Name,--tag,"v0.1.0",--name,"Initial Release",--body,$releaseBody
if ($releaseResult -and (Get-JsonOk ($releaseResult | ConvertFrom-Json))) {
Log-Ok "Release v0.1.0 created"
} else {
Log-Warn "Release creation may have failed"
}
# ----------------------------------------------------------------
Log-Title "Project Initialization Complete"
# ----------------------------------------------------------------
Write-Host " Repository: $Owner/$Name" -ForegroundColor Green
Write-Host " README: Wiki page" -ForegroundColor Green
Write-Host " CONTRIBUTING: Wiki page" -ForegroundColor Green
Write-Host " CI/CD Guide: Wiki page" -ForegroundColor Green
Write-Host " Issues: 5 initial issues" -ForegroundColor Green
Write-Host " Branch: master (protected)" -ForegroundColor Green
Write-Host " Release: v0.1.0" -ForegroundColor Green
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host " 1. Clone: git clone https://gitlink.org.cn/$Owner/$Name.git"
Write-Host " 2. Add your code and push"
Write-Host " 3. Setup CI/CD by closing the first issue"