Skip to content

Commit e6857e4

Browse files
authored
Merge branch 'UIU-Developers-Hub:dev' into dev
2 parents ded2efd + e928e48 commit e6857e4

2 files changed

Lines changed: 215 additions & 39 deletions

File tree

.github/workflows/auto-merge-contributorsList.yml

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
name: Auto-merge PRs
2+
on:
3+
pull_request_target:
4+
types: [opened, synchronize]
5+
paths:
6+
- 'contributorsList.md' # <- only run if only contributors file changed
7+
8+
jobs:
9+
auto-merge:
10+
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@v4
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 }}
46+
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
59+
- name: Merge PR
60+
id: merge_pr
61+
if: env.only_contributors == 'true' && env.one_line_change == 'true'
62+
uses: actions/github-script@v6
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 codeContributionsLink = 'https://github.com/roshanjossey/code-contributions'
98+
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'
99+
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'
100+
const linkedin_share_link = 'https://www.linkedin.com/sharing/share-offsite/?url=https://github.com/firstcontributions/first-contributions';
101+
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";
102+
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'
103+
const bluesky_share_link = 'https://bsky.app/intent/compose?text=Yay%21%20I%20just%20made%20my%20first%20open%20source%20contribution%20with%20%40FirstContributions.%20You%20can%20too%20by%20following%20a%20simple%20tutorial%20at%20https%3A%2F%2Fgoo.gl%2F66Axwe%20%23OpenSource%20%23FirstContribution%20%23Coding%20%23DevCommunity%20%23GitHub%20%23LearnToCode';
104+
105+
// social logo
106+
const repo_logo = "https://avatars0.githubusercontent.com/u/65761570?s=88&u=640f39b808c75c6b86460aa907dd030bcca2f3c7&v=4"
107+
const fb_logo = "https://edent.github.io/SuperTinyIcons/images/svg/facebook.svg"
108+
const reddit_logo = "https://edent.github.io/SuperTinyIcons/images/svg/reddit.svg"
109+
const linkedin_logo = "https://edent.github.io/SuperTinyIcons/images/svg/linkedin.svg";
110+
const dev_logo = "https://edent.github.io/SuperTinyIcons/images/svg/dev_to.svg";
111+
const hackernews_logo = "https://edent.github.io/SuperTinyIcons/images/svg/hackernews.svg";
112+
const bluesky_logo = "https://edent.github.io/SuperTinyIcons/images/svg/bluesky.svg";
113+
114+
115+
const getMergeMessage = (username) => {
116+
const greeting = `Hello @${username}, congratulations! You've successfully submitted a pull request. 🎉`;
117+
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">`;
118+
119+
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 - If you want more practice checkout [code contributions](${codeContributionsLink}). \n - Share on social media: You can share this content to help more people.\n - [<img alt="bluesky" title="bluesky" src="${bluesky_logo}" width="22"> Post on Bluesky](${bluesky_share_link}).\n - [<img alt="facebook" title="facebook" src="${fb_logo}" width="22"> share](${fb_share_link}).\n - [ <img alt="reddit" title="reddit" src="${reddit_logo}" width="22"> share](${reddit_link}).\n - [<img alt="linkedin" title="linkedin" src="${linkedin_logo}" width="22"> post](${linkedin_share_link}).\n - [<img alt="devio" title="devio" src="${dev_logo}" width="22"> publish](${dev_share_link}).\n - [<img src="${hackernews_logo}" width="22" title="HackerNews" /> Post on HackerNews](${hackernews_share_link}).`;
120+
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.`;
121+
122+
const gif = `![celebration gif](${getRandomGif()})`;
123+
124+
return `${greeting}\n\n${starRepoMessage}\n\n${nextSteps}\n\n${feedbackMessage}\n\n${gif}`;
125+
}
126+
127+
// Generate the merge message using the getMergeMessage function
128+
const message = getMergeMessage(context.payload.pull_request.user.login);
129+
130+
// post a comment
131+
await github.rest.issues.createComment({
132+
owner: context.repo.owner,
133+
repo: context.repo.repo,
134+
issue_number: context.issue.number,
135+
body: message
136+
})
137+
138+
} else {
139+
140+
// Post a comment on the pull request using the createComment method
141+
await github.rest.issues.createComment({
142+
owner: context.repo.owner,
143+
repo: context.repo.repo,
144+
issue_number: context.issue.number,
145+
body: "Something went wrong while attempting to merge this pull request. Please check the GitHub Actions log for more information."
146+
})
147+
}
148+
} catch (error) {
149+
150+
let errMsg = "";
151+
console.error("Error merging pull request:", error.message);
152+
153+
// Handle specific error cases based on status code
154+
if (error.status === 405 && error.response.data.message === "Pull Request is not mergeable") {
155+
156+
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/docs/additional-material/git_workflow_scenarios/resolving-merge-conflicts.md) on resolving merge conflicts.
157+
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!`
158+
159+
} else if (error.status === 409) {
160+
console.error("The pull request has conflicts with the target branch. Resolve the conflicts before merging.");
161+
errMsg = "The pull request has conflicts with the target branch. Resolve the conflicts before merging.";
162+
163+
} else {
164+
console.error("Something went wrong while merging the pull request.");
165+
errMsg = "Something went wrong while merging the pull request.";
166+
}
167+
168+
// Post a comment on the pull request using the createComment method
169+
await github.rest.issues.createComment({
170+
owner: context.repo.owner,
171+
repo: context.repo.repo,
172+
issue_number: context.issue.number,
173+
body: errMsg
174+
})
175+
176+
// Set GitHub Action as failed
177+
core.setFailed(error.message);
178+
}
179+
180+
# Post a comment on the pull request if it was not merged automatically
181+
- name: Post comment on PR if not merged automatically
182+
# Check if the pull request only modifies the CONTRIBUTORS.md file
183+
if: env.only_contributors != 'true'
184+
uses: actions/github-script@v6
185+
with:
186+
script: |
187+
// get the existing comments.
188+
const {data: comments} = await github.rest.issues.listComments({
189+
owner: context.repo.owner,
190+
repo: context.repo.repo,
191+
issue_number: context.payload.number,
192+
})
193+
194+
// find any comment already made by the bot.
195+
const botComment = comments.find(comment => comment.user.login === 'github-actions[bot]')
196+
197+
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')}` : ''}`
198+
199+
if (botComment) {
200+
await github.rest.issues.updateComment({
201+
owner: context.repo.owner,
202+
repo: context.repo.repo,
203+
comment_id: botComment.id,
204+
body: body
205+
})
206+
} else {
207+
await github.rest.issues.createComment({
208+
owner: context.repo.owner,
209+
repo: context.repo.repo,
210+
issue_number: context.issue.number,
211+
body: body
212+
});
213+
}
214+
215+
github-token: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)