Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions .github/workflows/updateChangelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ jobs:
- uses: actions/checkout@v4
- name: get merge commit message
id: pull
run: echo message="$(git log --pretty="format:%b")" >> $GITHUB_OUTPUT
run: |
pull_number="$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")"
commit_message="$(git log --pretty="format:%b")"
echo message="$commit_message [GH-$pull_number]" >> $GITHUB_OUTPUT
# check-for-changelog-entry
changelog-entry:
# if contains to check for bug, enhancement, breaking change, feature
if: ${{ contains(needs.pull-commit-message.outputs.message, '[BUG]') || contains(needs.pull-commit-message.outputs.message, '[ENHANCEMENT]') || contains(needs.pull-commit-message.outputs.message, '[FEATURE]') || contains(needs.pull-commit-message.outputs.message, '[BREAKING]')}}
# if contains to check for bug, enhancement, feature
if: ${{ contains(needs.pull-commit-message.outputs.message, '[BUG]') || contains(needs.pull-commit-message.outputs.message, '[ENHANCEMENT]') || contains(needs.pull-commit-message.outputs.message, '[FEATURE]') }}
runs-on: ubuntu-latest
needs: pull-commit-message
outputs:
Expand Down Expand Up @@ -58,7 +61,7 @@ jobs:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH=automated-changelog
#CHANGE API CALL BELOW TO MATCH AZURE REPO
#TODO CHANGE API CALL BELOW TO MATCH AZURE REPO
if gh api repos/rcskosir/Issues_GitHubActions/branches/$BRANCH > /dev/null 2>&1; then
echo "Branch exists on remote..."
git fetch origin $BRANCH
Expand All @@ -85,13 +88,16 @@ jobs:
((minor++))
patch=$(echo $patch | sed 's/ (.*)//')
new_version="${major}.$minor.${patch} (Unreleased)"
headers="${new_version}\n\nBREAKING CHANGES:\n\nENHANCEMENTS:\n\nFEATURES:\n\nBUG FIXES:\n"
headers="${new_version}\n\nENHANCEMENTS:\n\nFEATURES:\n\nBUG FIXES:\n"
temp_file=$(mktemp)
echo -e "$headers" > "$temp_file"
cat "$FILE" >> "$temp_file"
mv "$temp_file" "$FILE"
echo "File has been updated."

major=$(echo $major | sed 's/## //')
RELEASENUM="${major}.$minor.${patch}"

git add CHANGELOG.md
git commit -m "staring new changelog PR"
git push --set-upstream origin automated-changelog
Expand All @@ -101,13 +107,13 @@ jobs:
--base main \
--head automated-changelog \
-l "changelog" \
-t "CHANGELOG.md updates" \
-b "changelog for next release"
-t "CHANGELOG.md for $RELEASENUM" \
-b "Automated changelog for next release, $RELEASENUM"

- name: Set up Python
uses: actions/setup-python@v4 # Set up Python environment
- name: Set up Go
uses: actions/setup-go@v3 # Set up go
with:
python-version: '3.9'
go-version: '1.20'

- name: Add commit message to changelog pull request
# at this point a PR is opened for sure, now add entry
Expand All @@ -117,7 +123,10 @@ jobs:
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"

python .github/workflows/update_changelog.py '${{ needs.changelog-entry.outputs.entry }}'
ls -R


go run .github/workflows/update_changelog.go '${{ needs.changelog-entry.outputs.entry }}'

git add CHANGELOG.md
git commit -m "Update changelog"
Expand Down
129 changes: 129 additions & 0 deletions .github/workflows/update_changelog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"bufio"
"fmt"
"os"
"regexp"
"sort"
"strings"
)

func updateChangelog(inputString string, changelogFile string) {
// Step 1: Identify the type of entry and clean the input string
re := regexp.MustCompile(`^\[(BUG|ENHANCEMENT|FEATURE)\](.*)`)
match := re.FindStringSubmatch(strings.TrimSpace(inputString))
if match == nil {
fmt.Println("Error: Input string must start with '[BUG]' or '[ENHANCEMENT]' or '[FEATURE]'.")
return
}

changeType := match[1]
cleanedString := strings.TrimSpace(match[2])

// Step 2: Determine the appropriate header
var header string
switch changeType {
case "BUG":
header = "BUG FIXES:"
case "ENHANCEMENT":
header = "ENHANCEMENTS:"
case "FEATURE":
header = "FEATURES:"
}

// Step 3: Read the existing changelog file and look for the header
file, err := os.Open(changelogFile)
if err != nil {
fmt.Println("Error: Unable to open the changelog file.")
return
}
defer file.Close()

var lines []string
var sectionStartIdx, sectionEndIdx int
var inSection bool
scanner := bufio.NewScanner(file)
for i := 0; scanner.Scan(); i++ {
line := scanner.Text()
lines = append(lines, line)

// Check if we have found the header and begin collecting the section
if !inSection && strings.Contains(line, header) {
sectionStartIdx = i
inSection = true
}
// If we're in the section, keep track of where the section ends
if inSection && (strings.TrimSpace(line) == "") && sectionEndIdx == 0 {
sectionEndIdx = i
break
}
}

// Step 4: Insert the entry under the header in alphabetical order
var newLines []string
headerFound := false
if sectionStartIdx != 0 && sectionEndIdx != 0 {
// Found the section, insert the new entry in sorted order
var sectionEntries []string
for _, line := range lines[sectionStartIdx+1 : sectionEndIdx] {
sectionEntries = append(sectionEntries, line)
}

// Insert the new entry in the correct position (alphabetically)
sectionEntries = append(sectionEntries, cleanedString)
sort.Strings(sectionEntries) // Sort the entries alphabetically

// Rebuild the lines with the sorted entries
for i := 0; i < sectionStartIdx; i++ {
newLines = append(newLines, lines[i])
}
newLines = append(newLines, fmt.Sprintf("### %s", header))
for _, entry := range sectionEntries {
newLines = append(newLines, entry)
}
for i := sectionEndIdx; i < len(lines); i++ {
newLines = append(newLines, lines[i])
}
headerFound = true
}

// If the header is not found, add it to the top with the new entry
if !headerFound {
fmt.Printf("Warning: '%s' not found in the changelog. Appending the entry at the top.\n", header)
newLines = append(newLines, fmt.Sprintf("\n### %s\n", header), cleanedString+"\n")
} else {
fmt.Println("Changes were added in alphabetical order.")
}

// Step 5: Write the updated content back to the file
fileOut, err := os.Create(changelogFile)
if err != nil {
fmt.Println("Error: Unable to create the changelog file.")
return
}
defer fileOut.Close()

writer := bufio.NewWriter(fileOut)
for _, line := range newLines {
_, err := writer.WriteString(line + "\n")
if err != nil {
fmt.Println("Error writing to the file.")
return
}
}

writer.Flush()

fmt.Printf("The change has been added to the changelog under %s.\n", header)
}

func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide the input string as a command-line argument.")
return
}

inputString := os.Args[1] // Get the entry from the command line
updateChangelog(inputString, "CHANGELOG.md")
}
7 changes: 3 additions & 4 deletions .github/workflows/update_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

def update_changelog(input_string, changelog_file='CHANGELOG.md'):
# Step 1: Identify the type of entry and clean the input string
match = re.match(r'^\[(BUG|ENHANCEMENT|FEATURE|BREAKING)\](.*)', input_string.strip())
match = re.match(r'^\[(BUG|ENHANCEMENT|FEATURE)\](.*)', input_string.strip())
if not match:
print("Error: Input string must start with '[BUG]' or '[ENHANCEMENT]' or '[FEATURE]' or '[BREAKING]'.")
print("Error: Input string must start with '[BUG]' or '[ENHANCEMENT]' or '[FEATURE]'.")
return

change_type = match.group(1)
Expand All @@ -18,8 +18,7 @@ def update_changelog(input_string, changelog_file='CHANGELOG.md'):
header = 'ENHANCEMENTS:'
elif change_type == 'FEATURE':
header = 'FEATURES:'
elif change_type == 'BREAKING':
header = 'BREAKING CHANGES:'


# Step 3: Read the existing changelog file and look for the header
with open(changelog_file, 'r') as file:
Expand Down
58 changes: 0 additions & 58 deletions update_changelog.py

This file was deleted.

20 changes: 0 additions & 20 deletions workflows/issue-opened.yaml

This file was deleted.

34 changes: 0 additions & 34 deletions workflows/project_custom_field_filler.yaml

This file was deleted.

Loading