-
Notifications
You must be signed in to change notification settings - Fork 14
154 lines (139 loc) · 5.82 KB
/
version_bump.yml
File metadata and controls
154 lines (139 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#name: "Version Increment"
#
#on:
# # Run this action on any Pull Request raised against ARM
# # pull_request:
# # Ensure changes re-run the version script (i.e. title change)
# types: [opened, edited, synchronize]
# # Don't run on changes to the below paths
# paths-ignore:
# - 'arm_wiki/**'
# - '.github/**'
#
# # Allows you to run this workflow manually from the Actions tab
# workflow_dispatch:
permissions:
contents: write
pull-requests: write
jobs:
version:
runs-on: ubuntu-latest
steps:
- name: Get branch name
id: branch-name
uses: tj-actions/branch-names@v9
- name: Checkout code
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Fetch main branch
run: |
git fetch origin main
MAIN_VERSION=$(git show origin/main:VERSION)
echo "MAIN_VERSION=$MAIN_VERSION" >> $GITHUB_ENV
- name: Determine new version
id: determine-version
env:
# Safely get title (avoid injection attacks)
PR_TITLE: ${{ github.event.pull_request.title }}
run: |
# Set Title environment flag to false
TITLE_FLAG="false"
VERSION_TYPE="false"
echo "PR Title: $PR_TITLE"
if [[ "$PR_TITLE" == *"[FEATURE]"* ]]; then
VERSION_TYPE="minor"
echo "PR set to FEATURE updating minor version"
TITLE_FLAG="true"
elif [[ "$PR_TITLE" == *"[BUGFIX]"* ]]; then
VERSION_TYPE="patch"
echo "PR set to BUGFIX updating patch version"
TITLE_FLAG="true"
else
echo "No version bump flag found in PR title. Exiting."
echo "Edit your PR title to include either FEATURE or BUGFIX"
fi
# If Feature or Bugfix update the version
if [[ "$TITLE_FLAG" == "true" ]]; then
# Abort script if version file missing
if [[ ! -f VERSION ]]; then
echo "VERSION file not found! Aborting."
exit 1
fi
# Get branch version
BRANCH_VERSION=$(cat VERSION)
echo "Branch Version: $BRANCH_VERSION"
# Get the main version and split into VERSION_PARTS
IFS='.' read -r -a VERSION_PARTS <<< "$MAIN_VERSION"
if [[ "$VERSION_TYPE" == "minor" ]]; then
VERSION_PARTS[1]=$((VERSION_PARTS[1]+1))
VERSION_PARTS[2]=0
elif [[ "$VERSION_TYPE" == "patch" ]]; then
VERSION_PARTS[2]=$((VERSION_PARTS[2]+1))
fi
NEW_VERSION="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV
echo "New Version: " $NEW_VERSION
fi
# Set variables to global environment variables for later
echo "TITLE_FLAG=$TITLE_FLAG" >> $GITHUB_ENV
echo "PR_TITLE=$PR_TITLE" >> $GITHUB_ENV
echo "VERSION_TYPE=$VERSION_TYPE" >> $GITHUB_ENV
echo "BRANCH_VERSION=$BRANCH_VERSION" >> $GITHUB_ENV
- name: Comment on PR with no spam
uses: actions/github-script@v8
with:
github-token: ${{ secrets.VERSION_BOT }}
script: |
// Retrieve variables from the environment
const titleFlag = process.env.TITLE_FLAG;
const prNumber = context.issue.number;
const prTitle = process.env.PR_TITLE;
const newVersion = process.env.NEW_VERSION;
const currentVersion = process.env.MAIN_VERSION;
const branchVersion = process.env.BRANCH_VERSION;
const versionType = process.env.VERSION_TYPE;
// Prepare the message based on the title flag
let prBody;
if (titleFlag === "false") {
prBody = `ARM Version Bot\n **PR title:** ${prTitle}\n **No valid version flag found**. PR title must include either [FEATURE] or [BUGFIX] to auto-increment the version.\n **Please update the PR title** and re-run the workflow.`;
} else {
prBody = `ARM Version Bot:\n **PR title:** ${prTitle}\n **Current version:** ${currentVersion}\n **Required Version:** ${newVersion}\n **PR Version:** ${branchVersion}\n **Release version:** ${versionType}`;
}
// Get existing comments
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
per_page: 100
});
// Check for existing version bot comments
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Version Bot')
);
// Generate PR comment if not already there
if (botComment) {
// Check if comment aligns with expected output, otherwise update
if (botComment.body !== prBody) {
// Update the existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: prBody
});
console.log('Updated existing Version Bot comment.');
} else {
console.log('No comment update needed.');
}
} else {
// Creating new comment, no previous comments found
console.log('Creating new comment');
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: prBody
})
}