name: versioning-and-changelog on: pull_request: types: - closed branches: - master jobs: versioning_and_changelog: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Flutter uses: subosito/flutter-action@v2 with: channel: stable flutter-version-file: pubspec.yaml - name: Find commits from merged pull request id: get_commits run: | # Concatène les commits en une seule ligne, séparés par ";;" COMMITS=$(git log HEAD^..HEAD --oneline --no-merges | paste -sd ";;" -) if [ -z "$COMMITS" ]; then echo "No commits found in the merged PR." exit 0 fi echo "Commits from merged PR:" echo "$COMMITS" # On exporte la variable en une seule ligne echo "COMMITS=$COMMITS" >> "$GITHUB_ENV" - name: Extract current version id: versioning run: | PUBSPEC_FILE="pubspec.yaml" if [ ! -f "$PUBSPEC_FILE" ]; then echo "Error: $PUBSPEC_FILE not found." exit 1 fi CURRENT_VERSION=$(grep 'version:' "$PUBSPEC_FILE" | awk -F ': ' '{print $2}') VERSION=$(echo "$CURRENT_VERSION" | cut -d'+' -f1) BUILD=$(echo "$CURRENT_VERSION" | cut -d'+' -f2) echo "Current version: $VERSION+$BUILD" echo "CURRENT_VERSION=$CURRENT_VERSION" >> "$GITHUB_ENV" echo "VERSION=$VERSION" >> "$GITHUB_ENV" echo "BUILD=$BUILD" >> "$GITHUB_ENV" - name: Determine increment type from commits id: increment_type run: | # Récupère la variable exportée COMMITS="${{ env.COMMITS }}" INCREMENT_TYPE="patch" if echo "$COMMITS" | grep -iq "breaking:"; then INCREMENT_TYPE="major" elif echo "$COMMITS" | grep -iq "feat:"; then INCREMENT_TYPE="minor" elif echo "$COMMITS" | grep -iq "fix:"; then INCREMENT_TYPE="patch" elif echo "$COMMITS" | grep -iq "refactor:"; then INCREMENT_TYPE="patch" elif echo "$COMMITS" | grep -iq "style:"; then INCREMENT_TYPE="patch" elif echo "$COMMITS" | grep -iq "chore:"; then INCREMENT_TYPE="patch" else INCREMENT_TYPE="patch" fi echo "Determined increment type: $INCREMENT_TYPE" echo "INCREMENT_TYPE=$INCREMENT_TYPE" >> "$GITHUB_ENV" - name: Increment version and update pubspec.yaml id: increment_version run: | increment_version() { local version=$1 local index=$2 IFS='.' read -r -a parts <<< "$version" if [[ ${#parts[@]} -le $index ]]; then echo "Error: Invalid version format or index." exit 1 fi parts[$index]=$((parts[$index] + 1)) for ((i = index + 1; i < ${#parts[@]}; i++)); do parts[$i]=0 done echo "${parts[*]}" | tr ' ' '.' } CURRENT_VERSION="${{ env.CURRENT_VERSION }}" VERSION="${{ env.VERSION }}" BUILD="${{ env.BUILD }}" INCREMENT_TYPE="${{ env.INCREMENT_TYPE }}" NEW_BUILD=$((BUILD + 1)) echo "Incrementing version using type: $INCREMENT_TYPE" echo "Old Version: $CURRENT_VERSION" case $INCREMENT_TYPE in major) NEW_VERSION=$(increment_version "$VERSION" 0) ;; minor) NEW_VERSION=$(increment_version "$VERSION" 1) ;; patch) NEW_VERSION=$(increment_version "$VERSION" 2) ;; *) echo "Error: Unknown increment type: $INCREMENT_TYPE" exit 1 ;; esac sed -i.bak "s/^version: .*/version: $NEW_VERSION+$NEW_BUILD/" pubspec.yaml echo "NEW_VERSION=$NEW_VERSION" >> "$GITHUB_ENV" echo "NEW_BUILD=$NEW_BUILD" >> "$GITHUB_ENV" echo "New Version: $NEW_VERSION+$NEW_BUILD" - name: Generate Changelog for merged PR run: | echo "DEBUG: Raw COMMITS = [${{ env.COMMITS }}]" # Reconstruire la liste des commits en remplaçant ";;" par des retours à la ligne COMMITS=$(echo "${{ env.COMMITS }}" | sed 's/;;/\n/g') echo "DEBUG: COMMITS after conversion = [$COMMITS]" CHANGELOG_FILE="CHANGELOG.md" DATE=$(date +"%Y-%m-%d") NEW_VERSION="${{ env.NEW_VERSION }}" NEW_BUILD="${{ env.NEW_BUILD }}" # Créer le fichier changelog s'il n'existe pas if [ ! -f "$CHANGELOG_FILE" ]; then echo "# Changelog" > "$CHANGELOG_FILE" fi TEMP_CHANGELOG="CHANGELOG_TEMP.md" { echo "## $NEW_VERSION+$NEW_BUILD ($DATE)" echo "" echo "### Breaking Changes" BREAKING=$(echo "$COMMITS" | grep -i "breaking:" || true | sed -E 's/^[[:space:]]*([a-f0-9]+)[[:space:]]+breaking:[[:space:]]+/- (#\1) /g') echo "${BREAKING:-No breaking changes found.}" echo "" echo "### Features" FEATURES=$(echo "$COMMITS" | grep -i "feat:" || true | sed -E 's/^[[:space:]]*([a-f0-9]+)[[:space:]]+feat:[[:space:]]+/- (#\1) /g') echo "${FEATURES:-No features found.}" echo "" echo "### Bug Fixes" BUG_FIXES=$(echo "$COMMITS" | grep -i "fix:" || true | sed -E 's/^[[:space:]]*([a-f0-9]+)[[:space:]]+fix:[[:space:]]+/- (#\1) /g') echo "${BUG_FIXES:-No bug fixes found.}" echo "" echo "### Refactors" REFACTOR=$(echo "$COMMITS" | grep -i "refactor:" || true | sed -E 's/^[[:space:]]*([a-f0-9]+)[[:space:]]+refactor:[[:space:]]+/- (#\1) /g') echo "${REFACTOR:-No refactors found.}" echo "" echo "### Style Changes" STYLE=$(echo "$COMMITS" | grep -i "style:" || true | sed -E 's/^[[:space:]]*([a-f0-9]+)[[:space:]]+style:[[:space:]]+/- (#\1) /g') echo "${STYLE:-No style changes found.}" echo "" echo "### Chores" CHORES=$(echo "$COMMITS" | grep -i "chore:" || true | sed -E 's/^[[:space:]]*([a-f0-9]+)[[:space:]]+chore:[[:space:]]+/- (#\1) /g') echo "${CHORES:-No chores found.}" echo "" echo "### Miscellaneous" MISC=$(echo "$COMMITS" | grep -ivE "breaking:|feat:|fix:|refactor:|style:|chore:" || true | sed -E 's/^[[:space:]]*([a-f0-9]+)[[:space:]]+/- (#\1) /g') echo "${MISC:-No miscellaneous changes found.}" echo "" cat "$CHANGELOG_FILE" } > "$TEMP_CHANGELOG" mv "$TEMP_CHANGELOG" "$CHANGELOG_FILE" echo "Changelog updated." - name: Set remote URL run: | # Remplacer (ou mettre à jour) l'origin avec l'URL incluant le token git remote set-url origin https://${{ secrets.GITEAACCESSTOKEN }}@${{ secrets.GITEAURL#https:// }}/mobiles/flutter-mobile.git git remote -v - name: Commit and Push Changes run: | git config --global user.email "gitea-actions@bot.com" git config --global user.name "Gitea Actions" git add pubspec.yaml CHANGELOG.md git commit -m "chore: increment version $NEW_VERSION+$NEW_BUILD and update changelog" || echo "No changes to commit" echo "🔗 Pushing changes..." git push origin HEAD:master