3030 git checkout -b $BRANCH_NAME
3131 git push --force origin $BRANCH_NAME
3232
33- # Create or update PR using GitHub API
33+ # Create or update PR using GitHub API with retry logic
3434 - name : Create or Update Pull Request
3535 uses : actions/github-script@v6
3636 with :
@@ -41,39 +41,67 @@ jobs:
4141 const prTitle = "Promote dev changes to master";
4242 const prBody = "This is an automated PR to promote changes from `dev` to `master`.\n\nPlease review the changes carefully before merging.";
4343
44+ // Retry helper function
45+ async function retryOperation(operation, maxRetries = 3, delay = 5000) {
46+ let lastError;
47+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
48+ try {
49+ return await operation();
50+ } catch (error) {
51+ lastError = error;
52+ console.log(`Attempt ${attempt}/${maxRetries} failed: ${error.message}`);
53+ if (attempt < maxRetries) {
54+ console.log(`Waiting ${delay/1000} seconds before retry...`);
55+ await new Promise(resolve => setTimeout(resolve, delay));
56+ }
57+ }
58+ }
59+ throw lastError;
60+ }
61+
4462 // 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'
63+ const prs = await retryOperation(async () => {
64+ return github.rest.pulls.list({
65+ owner,
66+ repo,
67+ head: `${owner}:${prBranch}`,
68+ base: 'master',
69+ state: 'open'
70+ });
5171 });
5272
5373 if (prs.data.length > 0) {
5474 // Update existing PR
5575 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
76+ await retryOperation(async () => {
77+ return github.rest.pulls.update({
78+ owner,
79+ repo,
80+ pull_number: prs.data[0].number,
81+ title: prTitle,
82+ body: prBody
83+ });
6284 });
85+ console.log(`PR #${prs.data[0].number} updated successfully.`);
6386 } else {
6487 // Create new PR
6588 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'
89+ const result = await retryOperation(async () => {
90+ return github.rest.pulls.create({
91+ owner,
92+ repo,
93+ title: prTitle,
94+ body: prBody,
95+ head: prBranch,
96+ base: 'master'
97+ });
7398 });
7499 console.log(`PR created: ${result.data.html_url}`);
75100 } catch (error) {
76- console.log(`Error creating PR: ${error.message}`);
101+ console.log(`All attempts to create PR failed: ${error.message}`);
102+
103+ // As a fallback, output command for manual PR creation
104+ console.log(`To create the PR manually, visit: https://github.com/${owner}/${repo}/compare/master...${prBranch}`);
77105 throw error;
78106 }
79107 }
0 commit comments