Skip to content

Commit 8608147

Browse files
Update auto-pr-merge.yml
1 parent aadf931 commit 8608147

File tree

1 file changed

+15
-211
lines changed

1 file changed

+15
-211
lines changed
Lines changed: 15 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -1,217 +1,21 @@
1-
name: Auto-merge PRs
1+
name: Auto-Merge PR
2+
23
on:
3-
pull_request_target:
4-
types: [opened, synchronize]
5-
paths:
6-
- 'Contributors.md' # <- only run if only contributors file changed
7-
4+
pull_request:
5+
types:
6+
- synchronize # Trigger on any changes to the PR
7+
88
jobs:
99
auto-merge:
1010
runs-on: ubuntu-latest
11-
permissions:
12-
contents: write
13-
pull-requests: write
14-
issues: write
15-
16-
steps:
17-
# Check out the repository code
18-
- name: Checkout code
19-
uses: actions/checkout@v2
20-
with:
21-
ref: ${{ github.event.pull_request.head.sha }}
22-
fetch-depth: 2
23-
24-
- name: Check if PR only modifies Contributors.md
25-
id: is_only_contributors_file_changed
26-
run: |
27-
# Get a list of files changed in the pull request
28-
PR_FILES=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
29-
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" | \
30-
jq -r '.[].filename')
31-
FILES_CHANGED=$(echo $PR_FILES | tr '\n' ' ')
32-
33-
echo "files_changed=$FILES_CHANGED" >> $GITHUB_ENV
34-
35-
if [[ "${FILES_CHANGED// /}" == "Contributors.md" ]]; then
36-
37-
echo "only_contributors=true" >> $GITHUB_ENV
38-
else
39-
echo "only_contributors=false" >> $GITHUB_ENV
40-
fi
41-
42-
- name: Check if PR has only one line change
43-
run: |
44-
ADDITIONS=${{ github.event.pull_request.additions }}
45-
DELETIONS=${{ github.event.pull_request.deletions }}
4611

47-
echo "additions=$ADDITIONS" >> $GITHUB_ENV
48-
echo "deletions=$DELETIONS" >> $GITHUB_ENV
49-
50-
if [[ $ADDITIONS == 1 && $DELETIONS == 0 ]]; then
51-
echo "one_line_change=true" >> $GITHUB_ENV
52-
elif [[ $ADDITIONS == 2 && $DELETIONS == 1 ]]; then
53-
echo "one_line_change=true" >> $GITHUB_ENV
54-
else
55-
echo "one_line_change=false" >> $GITHUB_ENV
56-
fi
57-
58-
# Merge the pull request if it only modifies the Contributors.md file or if it fail to do then drop failure message as post
12+
steps:
5913
- name: Merge PR
60-
id: merge_pr
61-
if: env.only_contributors == 'true' && env.one_line_change == 'true'
62-
uses: actions/github-script@v5
63-
with:
64-
github-token: ${{ secrets.GITHUB_TOKEN }}
65-
script: |
66-
try {
67-
// Attempt to merge the pull request using the squash method
68-
const response = await github.rest.pulls.merge({
69-
owner: context.repo.owner,
70-
repo: context.repo.repo,
71-
pull_number: context.issue.number,
72-
merge_method: "squash"
73-
})
74-
75-
// Check if the merge was successful by checking the status code of the response
76-
if (response.status === 200) {
77-
78-
const celebrationGifs = [
79-
'https://c.tenor.com/ZCq4SwgCfxAAAAAC/snoopy-peanuts.gif',
80-
'https://c.tenor.com/Z0ojZS2kpO0AAAAC/milk-and-mocha-happy.gif',
81-
'https://c.tenor.com/LffD4a8ET9AAAAAC/heart-celebrate.gif',
82-
'https://c.tenor.com/HJ0iSKwIG28AAAAC/yes-baby.gif',
83-
'https://c.tenor.com/4blWuIh5MIYAAAAC/baby-yoda.gif',
84-
'https://c.tenor.com/B_zYdea4l-4AAAAC/yay-minions.gif',
85-
'https://media1.giphy.com/media/artj92V8o75VPL7AeQ/giphy.gif',
86-
'https://media2.giphy.com/media/IwAZ6dvvvaTtdI8SD5/giphy.gif',
87-
'https://media0.giphy.com/media/z8gtBVdZVrH20/giphy.gif',
88-
'https://media2.giphy.com/media/26gN16cJ6gy4LzZSw/giphy.gif',
89-
'https://media1.giphy.com/media/LZElUsjl1Bu6c/giphy.gif',
90-
'https://media1.giphy.com/media/gHnwTttExPf4nwOWm7/giphy.gif',
91-
]
92-
93-
const getRandomGif = () => celebrationGifs[Math.floor(Math.random() * celebrationGifs.length)]
94-
95-
// social media links
96-
const web_url = 'https://firstcontributions.github.io';
97-
const slack_invite_url = 'https://join.slack.com/t/firstcontributors/shared_invite/zt-1n4y7xnk0-DnLVTaN6U9xLU79H5Hi62w'
98-
const twitter_tweet_share = 'https://twitter.com/intent/tweet?text=Yay%21%20I%20just%20made%20my%20first%20open%20source%20contribution%20with%20@1stcontribution.%20You%20can%20too%20at%20https%3A//goo.gl/66Axwe%0A&hashtags=OpenSource,CodeNewbie'
99-
const fb_share_link = 'https://www.facebook.com/sharer/sharer.php?u=https://roshanjossey.github.io/first-contributions&quote=Yay%21%20I%20just%20made%20my%20first%20open%20source%20contribution%20with%20First%20Contributions.%20You%20can%20too,%20by%20following%20a%20simple%20tutorial%20at%20https%3A//goo.gl/66Axwe&hashtag=%23OpenSource'
100-
const reddit_link = 'https://www.reddit.com/submit?url=https%3A%2F%2Fgithub.com%2Ffirstcontributions%2Ffirst-contributions&title=Learn%20how%20to%20contribute%20to%20open%20source%20projects%20in%205%20minutes'
101-
const linkedin_share_link = 'https://www.linkedin.com/sharing/share-offsite/?url=https://github.com/firstcontributions/first-contributions';
102-
const dev_share_link = "https://dev.to/new?prefill=---%0Atitle%3A%20First%20Contributions%3A%20learn%20how%20to%20contribute%20to%20open%20source%20projects%0Apublished%3A%20true%0Atags%3A%20opensource%2C%20beginners%2C%20tutorial%0A---%0A%0AI%20followed%20the%20hands-on%20tutorial%20in%20the%20Readme%20of%20first%20contributions%20and%20made%20my%20first%20pull%20request%20to%20the%20same%20repo.%0A%0A%0A%7B%25%20embed%20https%3A%2F%2Fgithub.com%2Ffirstcontributions%2Ffirst-contributions%20%25%7D";
103-
const hackernews_share_link = 'https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fgithub.com%2Ffirstcontributions%2Ffirst-contributions&t=Show%20HN%3A%20Hands%20on%20tutorial%20for%20open%20source%20contribution'
104-
105-
// social logo
106-
const repo_logo = "https://avatars0.githubusercontent.com/u/65761570?s=88&u=640f39b808c75c6b86460aa907dd030bcca2f3c7&v=4"
107-
const slack_logo = "https://edent.github.io/SuperTinyIcons/images/svg/slack.svg"
108-
const twitter_logo = "https://edent.github.io/SuperTinyIcons/images/svg/twitter.svg"
109-
const fb_logo = "https://edent.github.io/SuperTinyIcons/images/svg/facebook.svg"
110-
const reddit_logo = "https://edent.github.io/SuperTinyIcons/images/svg/reddit.svg"
111-
const linkedin_logo = "https://edent.github.io/SuperTinyIcons/images/svg/linkedin.svg";
112-
const dev_logo = "https://edent.github.io/SuperTinyIcons/images/svg/dev_to.svg";
113-
const hackernews_logo = "https://edent.github.io/SuperTinyIcons/images/svg/hackernews.svg";
114-
115-
116-
117-
const getMergeMessage = (username) => {
118-
const greeting = `Hello @${username}, congratulations! You've successfully submitted a pull request. 🎉`;
119-
const starRepoMessage = `If you liked the tutorial, please star this repo by clicking the star button on the top right of this page. <img alt="star screenshot" title="star button" src="https://firstcontributions.github.io/assets/star.png">`;
120-
121-
const nextSteps = `# Next steps \n - Continue contributing: If you're looking for projects to contribute to, checkout our [<img src="${repo_logo}" width="22" title="web app" /> webapp](${web_url}). \n - Join our Slack group: We have a community to help/support contributors. [<img src="${slack_logo}" width="22" title="Slack" /> Join slack group](${slack_invite_url}). \n - Share on social media: You can share this content to help more people. [ <img alt="twitter" title="twitter" src="${twitter_logo}" width="22"> tweet](${twitter_tweet_share}). [<img alt="twitter" title="twitter" src="${fb_logo}" width="22"> share](${fb_share_link}). [ <img alt="reddit" title="reddit" src="${reddit_logo}" width="22"> share](${reddit_link}). [<img alt="linkedin" title="linkedin" src="${linkedin_logo}" width="22"> post](${linkedin_share_link}). [<img alt="devio" title="devio" src="${dev_logo}" width="22"> publish](${dev_share_link}). [<img src="${hackernews_logo}" width="22" title="HackerNews" /> Post on HackerNews](${hackernews_share_link}).`;
122-
const feedbackMessage = `We'd love to hear your thoughts about this project. Let us know how we can improve by commenting or opening an issue here.`;
123-
124-
const gif = `![celebration gif](${getRandomGif()})`;
125-
126-
return `${greeting}\n\n${starRepoMessage}\n\n${nextSteps}\n\n${feedbackMessage}\n\n${gif}`;
127-
}
128-
129-
// Generate the merge message using the getMergeMessage function
130-
const message = getMergeMessage(context.payload.pull_request.user.login);
131-
132-
// post a comment
133-
await github.rest.issues.createComment({
134-
owner: context.repo.owner,
135-
repo: context.repo.repo,
136-
issue_number: context.issue.number,
137-
body: message
138-
})
139-
140-
} else {
141-
142-
// Post a comment on the pull request using the createComment method
143-
await github.rest.issues.createComment({
144-
owner: context.repo.owner,
145-
repo: context.repo.repo,
146-
issue_number: context.issue.number,
147-
body: "Something went wrong while attempting to merge this pull request. Please check the GitHub Actions log for more information."
148-
})
149-
}
150-
} catch (error) {
151-
152-
let errMsg = "";
153-
console.error("Error merging pull request:", error.message);
154-
155-
// Handle specific error cases based on status code
156-
if (error.status === 405 && error.response.data.message === "Pull Request is not mergeable") {
157-
158-
errMsg = `Hello @${context.payload.pull_request.user.login}, thank you for your pull request. We appreciate your contribution to the project. However, before we can merge it, there is a merge conflict with the target branch. \n\n No worries! You can follow [this guide](https://github.com/firstcontributions/first-contributions/blob/main/additional-material/git_workflow_scenarios/resolving-merge-conflicts.md) on resolving merge conflicts.
159-
Once you've fixed the conflicts and pushed your changes, the repository will check the changes you made and proceed with the merge if everything looks good. \n\n If you have any questions or need further assistance, don't hesitate to reach out. We're here to help!`
160-
161-
} else if (error.status === 409) {
162-
console.error("The pull request has conflicts with the target branch. Resolve the conflicts before merging.");
163-
errMsg = "The pull request has conflicts with the target branch. Resolve the conflicts before merging.";
164-
165-
} else {
166-
console.error("Something went wrong while merging the pull request.");
167-
errMsg = "Something went wrong while merging the pull request.";
168-
}
169-
170-
// Post a comment on the pull request using the createComment method
171-
await github.rest.issues.createComment({
172-
owner: context.repo.owner,
173-
repo: context.repo.repo,
174-
issue_number: context.issue.number,
175-
body: errMsg
176-
})
177-
178-
// Set GitHub Action as failed
179-
core.setFailed(error.message);
180-
}
181-
182-
# Post a comment on the pull request if it was not merged automatically
183-
- name: Post comment on PR if not merged automatically
184-
# Check if the pull request only modifies the CONTRIBUTORS.md file
185-
if: env.only_contributors != 'true'
186-
uses: actions/github-script@v5
187-
with:
188-
script: |
189-
// get the existing comments.
190-
const {data: comments} = await github.rest.issues.listComments({
191-
owner: context.repo.owner,
192-
repo: context.repo.repo,
193-
issue_number: context.payload.number,
194-
})
195-
196-
// find any comment already made by the bot.
197-
const botComment = comments.find(comment => comment.user.login === 'github-actions[bot]')
198-
199-
const body = `Thank you for your pull request. This pull request contains changes in files which requires review. The following files were changed:\n\n ${process.env.files_changed.trim() ? `\n\n${process.env.files_changed.trim().split(' ').map(file => `- ${file}`).join('\n')}` : ''}`
200-
201-
if (botComment) {
202-
await github.rest.issues.updateComment({
203-
owner: context.repo.owner,
204-
repo: context.repo.repo,
205-
comment_id: botComment.id,
206-
body: body
207-
})
208-
} else {
209-
await github.rest.issues.createComment({
210-
owner: context.repo.owner,
211-
repo: context.repo.repo,
212-
issue_number: context.issue.number,
213-
body: body
214-
});
215-
}
216-
217-
github-token: ${{ secrets.GITHUB_TOKEN }}
14+
run: |
15+
git config user.name "${{ github.actor }}"
16+
git config user.email "${{ github.actor }}@users.noreply.github.com"
17+
git checkout -B auto-merge
18+
git pull "${{ github.event.pull_request.head.repo.clone_url }}" "${{ github.event.pull_request.head.ref }}"
19+
git push "${{ github.event.pull_request.head.repo.clone_url }}" auto-merge
20+
env:
21+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)