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

348 lines
10 KiB
Bash

#!/usr/bin/env bash
# ─────────────────────────────────────────────────────────────────────
# Scenario 3: One-Click Project Initialization
# Flow: Input description → Create repo → README/LICENSE/CI → Issues → Release
#
# Commands/Skills chained:
# 1. repo +create -- create repository
# 2. wiki +create -- create README wiki page
# 3. wiki +create -- create CONTRIBUTING guide
# 4. issue +create -- create initial issues
# 5. branch +protect -- protect default branch
# 6. release +create -- create initial release
# ─────────────────────────────────────────────────────────────────────
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/lib/common.sh"
usage() {
echo "Usage: $0 --owner OWNER --name REPO_NAME --description DESC [--lang LANG] [--private] [--dry-run]"
echo ""
echo " --owner OWNER Repository owner (org or user)"
echo " --name REPO_NAME Repository name"
echo " --description DESC Repository description"
echo " --lang LANG Primary language: go|python|node|java (default: go)"
echo " --private Make repository private"
echo " --dry-run Preview actions without executing"
exit 1
}
PROJ_LANG="go"
DRY_RUN=false
OWNER=""
REPO_NAME=""
DESCRIPTION=""
PRIVATE="false"
while [[ $# -gt 0 ]]; do
case "$1" in
--owner) OWNER="$2"; shift 2 ;;
--name) REPO_NAME="$2"; shift 2 ;;
--description) DESCRIPTION="$2"; shift 2 ;;
--lang) PROJ_LANG="$2"; shift 2 ;;
--private) PRIVATE="true"; shift ;;
--dry-run) DRY_RUN="true"; shift ;;
--help|-h) usage ;;
*) log_err "Unknown arg: $1"; usage ;;
esac
done
if [[ -z "$OWNER" || -z "$REPO_NAME" || -z "$DESCRIPTION" ]]; then
log_err "Missing required parameters: --owner, --name, --description"
usage
fi
check_auth
# ─────────────────────────────────────────────────────────────────────
log_title "Project Initialization: $OWNER/$REPO_NAME"
# ─────────────────────────────────────────────────────────────────────
echo " Owner: $OWNER"
echo " Name: $REPO_NAME"
echo " Description: $DESCRIPTION"
echo " Language: $PROJ_LANG"
echo " Private: $PRIVATE"
divider
# ── Step 1: Create Repository ────────────────────────────────────────
log_step "Creating repository..."
REPO_RESULT=$(gl_check repo +create --owner "$OWNER" --name "$REPO_NAME" --description "$DESCRIPTION" --private "$PRIVATE")
REPO_ID=$(echo "$REPO_RESULT" | jq -r '.data.id // .data.project_id // empty')
log_ok "Repository created: $OWNER/$REPO_NAME (id: $REPO_ID)"
# ── Step 2: Create README ────────────────────────────────────────────
log_step "Creating README wiki page..."
# Wait for repo to be fully initialized
sleep 2
README_CONTENT="# $REPO_NAME
$DESCRIPTION
## Getting Started
### Prerequisites"
case "$PROJ_LANG" in
go)
README_CONTENT+="
- Go 1.21+
- Git
### Installation
\`\`\`bash
git clone https://gitlink.org.cn/$OWNER/$REPO_NAME.git
cd $REPO_NAME
go mod download
go build ./...
\`\`\`
### Usage
\`\`\`bash
go run main.go
\`\`\`
### Testing
\`\`\`bash
go test ./...
\`\`\`"
;;
python)
README_CONTENT+="
- Python 3.9+
- pip
### Installation
\`\`\`bash
git clone https://gitlink.org.cn/$OWNER/$REPO_NAME.git
cd $REPO_NAME
pip install -r requirements.txt
\`\`\`
### Usage
\`\`\`bash
python main.py
\`\`\`
### Testing
\`\`\`bash
pytest
\`\`\`"
;;
node)
README_CONTENT+="
- Node.js 18+
- npm or yarn
### Installation
\`\`\`bash
git clone https://gitlink.org.cn/$OWNER/$REPO_NAME.git
cd $REPO_NAME
npm install
\`\`\`
### Usage
\`\`\`bash
npm start
\`\`\`
### Testing
\`\`\`bash
npm test
\`\`\`"
;;
java)
README_CONTENT+="
- JDK 17+
- Maven 3.8+
### Installation
\`\`\`bash
git clone https://gitlink.org.cn/$OWNER/$REPO_NAME.git
cd $REPO_NAME
mvn clean install
\`\`\`
### Usage
\`\`\`bash
mvn exec:java
\`\`\`
### Testing
\`\`\`bash
mvn test
\`\`\`"
;;
esac
README_CONTENT+="
## Contributing
See [CONTRIBUTING](./CONTRIBUTING) for guidelines.
## License
This project is licensed under the MIT License."
# Retry wiki creation up to 3 times
WIKI_OK=false
for attempt in 1 2 3; do
WIKI_RESULT=$(gl_run wiki +create --owner "$OWNER" --repo "$REPO_NAME" \
--title "README" --content "$README_CONTENT" 2>&1) || true
if [[ "$(json_ok "$WIKI_RESULT")" == "true" ]]; then
log_ok "README created"
WIKI_OK=true
break
fi
[[ $attempt -lt 3 ]] && sleep 2
done
[[ "$WIKI_OK" == "false" ]] && log_warn "README wiki creation may have failed"
# ── Step 3: Create CONTRIBUTING Guide ────────────────────────────────
log_step "Creating CONTRIBUTING guide..."
CONTRIB_CONTENT="# Contributing to $REPO_NAME
Thank you for your interest in contributing!
## How to Contribute
1. Fork the repository
2. Create a feature branch: \`git checkout -b feature/my-feature\`
3. Make your changes
4. Run tests to ensure everything passes
5. Commit your changes: \`git commit -m 'feat: add my feature'\`
6. Push to your fork: \`git push origin feature/my-feature\`
7. Create a Pull Request
## Code Style
- Follow the existing code style
- Write meaningful commit messages
- Add tests for new features
- Update documentation as needed
## Reporting Issues
- Use the issue tracker
- Include reproduction steps
- Include environment details
## Code of Conduct
Please be respectful and constructive in all interactions."
# Retry wiki creation up to 3 times
WIKI_OK=false
for attempt in 1 2 3; do
WIKI_CONTRIB=$(gl_run wiki +create --owner "$OWNER" --repo "$REPO_NAME" \
--title "CONTRIBUTING" --content "$CONTRIB_CONTENT" 2>&1) || true
if [[ "$(json_ok "$WIKI_CONTRIB")" == "true" ]]; then
log_ok "CONTRIBUTING guide created"
WIKI_OK=true
break
fi
[[ $attempt -lt 3 ]] && sleep 2
done
[[ "$WIKI_OK" == "false" ]] && log_warn "CONTRIBUTING wiki creation may have failed"
# ── Step 4: Create Initial Issues ────────────────────────────────────
log_step "Creating initial issues..."
ISSUES_TO_CREATE=(
"Setup CI/CD Pipeline|Configure continuous integration and deployment for the project.|feature"
"Write Project Documentation|Complete project documentation including API docs and architecture guide.|documentation"
"Setup Code Review Process|Establish code review guidelines and automation.|enhancement"
"Add Unit Tests|Add comprehensive unit test coverage for core modules.|enhancement"
"Setup Dependency Management|Configure dependency scanning and updates.|security"
)
for entry in "${ISSUES_TO_CREATE[@]}"; do
IFS='|' read -r title body label <<< "$entry"
ISSUE_RESULT=$(gl_run issue +create --owner "$OWNER" --repo "$REPO_NAME" \
--title "$title" --body "$body" 2>&1) || true
ISSUE_NUM=$(echo "$ISSUE_RESULT" | jq -r '.data.id // .data.number // empty')
if [[ -n "$ISSUE_NUM" ]]; then
# Add label
gl_run issue +label-add --owner "$OWNER" --repo "$REPO_NAME" --number "$ISSUE_NUM" --labels "$label" > /dev/null 2>&1 || true
log_ok "Issue created: #$ISSUE_NUM - $title"
else
log_warn "Issue creation may have failed: $title"
fi
done
# ── Step 5: Protect Default Branch ───────────────────────────────────
log_step "Protecting master branch..."
PROTECT_RESULT=$(gl_run branch +protect --owner "$OWNER" --repo "$REPO_NAME" --name master 2>&1) || true
if [[ "$(json_ok "$PROTECT_RESULT")" == "true" ]]; then
log_ok "Branch 'master' protected"
else
log_warn "Branch protection may have failed (may require admin permissions)"
fi
# ── Step 6: Create Initial Release ───────────────────────────────────
log_step "Creating initial release v0.1.0..."
RELEASE_BODY="# v0.1.0 - Initial Release
## What's New
- Project initialized with $PROJ_LANG template
- README and CONTRIBUTING guides created
- CI/CD pipeline issues filed
- Branch protection enabled
## Next Steps
- [ ] Setup CI/CD pipeline
- [ ] Write comprehensive tests
- [ ] Complete documentation
- [ ] First feature implementation
---
*Auto-initialized by gitlink-cli project-init workflow*"
RELEASE_RESULT=$(gl_run release +create --owner "$OWNER" --repo "$REPO_NAME" \
--tag "v0.1.0" --name "Initial Release" --body "$RELEASE_BODY" 2>&1) || true
if [[ "$(json_ok "$RELEASE_RESULT")" == "true" ]]; then
log_ok "Release v0.1.0 created"
else
log_warn "Release creation may have failed"
fi
# ─────────────────────────────────────────────────────────────────────
log_title "Project Initialization Complete"
# ─────────────────────────────────────────────────────────────────────
echo -e "${GREEN}Created:${NC}"
echo " Repository: $OWNER/$REPO_NAME"
echo " README: Wiki page"
echo " CONTRIBUTING: Wiki page"
echo " Issues: ${#ISSUES_TO_CREATE[@]} initial issues"
echo " Branch: master (protected)"
echo " Release: v0.1.0"
echo ""
echo -e "${CYAN}Next steps:${NC}"
echo " 1. Clone: git clone https://gitlink.org.cn/$OWNER/$REPO_NAME.git"
echo " 2. Add your code and push"
echo " 3. Setup CI/CD by closing the first issue"
echo ""