1616 with :
1717 fetch-depth : 0
1818
19+ # Generate commit history and calculate suggested merge date
20+ - name : Generate commit history and schedule
21+ id : generate-commits
22+ run : |
23+ # Get the commit history between master and dev
24+ COMMIT_LIST=$(git log --pretty=format:"* %s (%h) by %an" origin/master..origin/dev)
25+
26+ # Handle empty commit list (branches are identical)
27+ if [ -z "$COMMIT_LIST" ]; then
28+ COMMIT_LIST="No new commits - branches may be identical"
29+ fi
30+
31+ # Calculate suggested merge date based on release policy
32+ # By default, suggesting next Monday as merge date - adjust according to your policy
33+ MERGE_DATE=$(date -d "next Monday" +"%A, %B %d, %Y")
34+
35+ # Save commit list to a file (to handle multi-line output)
36+ echo "$COMMIT_LIST" > commit_list.txt
37+
38+ # Create a PR body with the commit list and suggested merge date
39+ echo "PR_BODY<<EOF" >> $GITHUB_ENV
40+ echo "This is an automated PR to promote changes from \`dev\` to \`master\`." >> $GITHUB_ENV
41+ echo "" >> $GITHUB_ENV
42+ echo "## Suggested Merge Schedule" >> $GITHUB_ENV
43+ echo "According to our release policy, this PR is expected to be merged on: **$MERGE_DATE**" >> $GITHUB_ENV
44+ echo "" >> $GITHUB_ENV
45+ echo "## Commits to be merged:" >> $GITHUB_ENV
46+ echo "\`\`\`" >> $GITHUB_ENV
47+ cat commit_list.txt >> $GITHUB_ENV
48+ echo "\`\`\`" >> $GITHUB_ENV
49+ echo "" >> $GITHUB_ENV
50+ echo "Please review the changes carefully before merging." >> $GITHUB_ENV
51+ echo "EOF" >> $GITHUB_ENV
52+
1953 # Use a fixed branch name for the PR
2054 - name : Update dev-to-master branch
2155 run : |
3064 git checkout -b $BRANCH_NAME
3165 git push --force origin $BRANCH_NAME
3266
33- # Create or update PR using GitHub API
67+ # Create or update PR using GitHub API with retry logic
3468 - name : Create or Update Pull Request
3569 uses : actions/github-script@v6
3670 with :
@@ -39,41 +73,69 @@ jobs:
3973 const { repo, owner } = context.repo;
4074 const prBranch = "automated-dev-to-master-branch";
4175 const prTitle = "Promote dev changes to master";
42- const prBody = "This is an automated PR to promote changes from `dev` to `master`.\n\nPlease review the changes carefully before merging.";
76+ const prBody = process.env.PR_BODY;
77+
78+ // Retry helper function
79+ async function retryOperation(operation, maxRetries = 3, delay = 5000) {
80+ let lastError;
81+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
82+ try {
83+ return await operation();
84+ } catch (error) {
85+ lastError = error;
86+ console.log(`Attempt ${attempt}/${maxRetries} failed: ${error.message}`);
87+ if (attempt < maxRetries) {
88+ console.log(`Waiting ${delay/1000} seconds before retry...`);
89+ await new Promise(resolve => setTimeout(resolve, delay));
90+ }
91+ }
92+ }
93+ throw lastError;
94+ }
4395
4496 // Check if PR already exists
45- const prs = await github.rest.pulls.list({
46- owner,
47- repo,
48- head: `${owner}:${prBranch}`,
49- base: 'master',
50- state: 'open'
97+ const prs = await retryOperation(async () => {
98+ return github.rest.pulls.list({
99+ owner,
100+ repo,
101+ head: `${owner}:${prBranch}`,
102+ base: 'master',
103+ state: 'open'
104+ });
51105 });
52106
53107 if (prs.data.length > 0) {
54108 // Update existing PR
55109 console.log(`Updating existing PR #${prs.data[0].number}`);
56- await github.rest.pulls.update({
57- owner,
58- repo,
59- pull_number: prs.data[0].number,
60- title: prTitle,
61- body: prBody
110+ await retryOperation(async () => {
111+ return github.rest.pulls.update({
112+ owner,
113+ repo,
114+ pull_number: prs.data[0].number,
115+ title: prTitle,
116+ body: prBody
117+ });
62118 });
119+ console.log(`PR #${prs.data[0].number} updated successfully.`);
63120 } else {
64121 // Create new PR
65122 try {
66- const result = await github.rest.pulls.create({
67- owner,
68- repo,
69- title: prTitle,
70- body: prBody,
71- head: prBranch,
72- base: 'master'
123+ const result = await retryOperation(async () => {
124+ return github.rest.pulls.create({
125+ owner,
126+ repo,
127+ title: prTitle,
128+ body: prBody,
129+ head: prBranch,
130+ base: 'master'
131+ });
73132 });
74133 console.log(`PR created: ${result.data.html_url}`);
75134 } catch (error) {
76- console.log(`Error creating PR: ${error.message}`);
135+ console.log(`All attempts to create PR failed: ${error.message}`);
136+
137+ // As a fallback, output command for manual PR creation
138+ console.log(`To create the PR manually, visit: https://github.com/${owner}/${repo}/compare/master...${prBranch}`);
77139 throw error;
78140 }
79141 }
0 commit comments