Skip to content

Commit e889dc4

Browse files
a7medevHeshamMegid
authored andcommitted
ci: automate snapshot generation (#1021)
Jira ID: MOB-12896
1 parent b5c6af9 commit e889dc4

File tree

4 files changed

+130
-20
lines changed

4 files changed

+130
-20
lines changed

.circleci/config.yml

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ orbs:
55
advanced-checkout: vsco/[email protected]
66
node: circleci/[email protected]
77

8+
references:
9+
release_dependencies: &release_dependencies
10+
- danger
11+
- lint
12+
- test_module
13+
- test_android
14+
- validate_shell_files
15+
- sync_generated_files
16+
- test_ios
17+
- e2e_ios
18+
- e2e_android
19+
820
commands:
921
install_node_modules:
1022
parameters:
@@ -48,6 +60,15 @@ commands:
4860
- run:
4961
name: Search and Replace in << parameters.file >>
5062
command: sed -i '<< parameters.replace-pattern >>' << parameters.file >>
63+
notify_github:
64+
parameters:
65+
data:
66+
type: string
67+
description: The data to be passed to the GitHub API for creating the comment.
68+
steps:
69+
- run:
70+
name: Post comment on GitHub
71+
command: ./scripts/notify-github.sh "<< parameters.data >>"
5172

5273
jobs:
5374
danger:
@@ -287,6 +308,47 @@ jobs:
287308
working_directory: project
288309
command: Escape react-native publish
289310

311+
generate_snapshot:
312+
executor:
313+
name: node/default
314+
steps:
315+
- advanced-checkout/shallow-checkout
316+
- install_node_modules
317+
- run: yarn build
318+
- run: yarn remove @instabug/danger-plugin-coverage
319+
- run:
320+
name: Remove build files from .gitignore
321+
command: sed -i '/dist/d' .gitignore && sed -i '/bin/d' .gitignore
322+
- run:
323+
name: Get snapshot branch name
324+
command: |
325+
source scripts/snapshot-branch.sh
326+
echo "export SNAPSHOT_BRANCH=$SNAPSHOT_BRANCH" >> "$BASH_ENV"
327+
- run:
328+
name: Setup Git
329+
command: |
330+
git config --global user.name "Instabug-CP-CI"
331+
git config --global user.email [email protected]
332+
- run:
333+
name: Create snapshot branch
334+
command: git checkout -b $SNAPSHOT_BRANCH
335+
- run:
336+
name: Commit changes
337+
command: |
338+
git add .
339+
git commit -m "chore: add generate files"
340+
- run:
341+
name: Push snapshot
342+
command: git push --force origin $SNAPSHOT_BRANCH
343+
- run:
344+
name: Install jq
345+
command: sudo apt-get update && sudo apt-get install -y jq
346+
- run:
347+
name: Replace snapshot branch in comment template
348+
command: sed -i "s|{BRANCH}|$SNAPSHOT_BRANCH|g" scripts/snapshot-comment.md
349+
- notify_github:
350+
data: "$(jq -Rcs '{ body: . }' scripts/snapshot-comment.md)"
351+
290352
workflows:
291353
publish:
292354
jobs:
@@ -303,32 +365,20 @@ workflows:
303365
- test_ios
304366
- e2e_ios
305367
- e2e_android
306-
- hold_publish:
368+
- hold_generate_snapshot:
369+
type: approval
370+
requires: *release_dependencies
371+
- generate_snapshot:
307372
requires:
308-
- danger
309-
- lint
310-
- test_module
311-
- test_android
312-
- validate_shell_files
313-
- sync_generated_files
314-
- test_ios
315-
- e2e_ios
316-
- e2e_android
373+
- hold_generate_snapshot
374+
- hold_publish:
375+
requires: *release_dependencies
317376
type: approval
318377
filters:
319378
branches:
320379
only: master
321380
- hold_release_nn:
322-
requires:
323-
- danger
324-
- lint
325-
- test_module
326-
- test_android
327-
- validate_shell_files
328-
- sync_generated_files
329-
- test_ios
330-
- e2e_ios
331-
- e2e_android
381+
requires: *release_dependencies
332382
type: approval
333383
filters:
334384
branches:

scripts/notify-github.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
pr_url="https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls?head=$CIRCLE_PROJECT_USERNAME:$CIRCLE_BRANCH&state=open"
4+
pr_response=$(curl --location --request GET "$pr_url" --header "Authorization: Bearer $RELEASE_GITHUB_TOKEN")
5+
6+
if [ $(echo "$pr_response" | jq length) -eq 0 ]; then
7+
echo "No PR found to update"
8+
else
9+
pr_comment_url=$(echo "$pr_response" | jq -r ".[]._links.comments.href")
10+
11+
curl --location --request POST "$pr_comment_url" \
12+
--header 'Content-Type: application/json' \
13+
--header "Authorization: Bearer $RELEASE_GITHUB_TOKEN" \
14+
--data-raw "$1"
15+
fi

scripts/snapshot-branch.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
pr_url="https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls?head=$CIRCLE_PROJECT_USERNAME:$CIRCLE_BRANCH&state=open"
4+
pr_response=$(curl --location --request GET "$pr_url" --header "Authorization: Bearer $RELEASE_GITHUB_TOKEN")
5+
6+
if [ $(echo "$pr_response" | jq length) -eq 0 ]; then
7+
echo "No PR found, proceeding with branch name instead"
8+
9+
SNAPSHOT_BRANCH="snapshot/$CIRCLE_BRANCH"
10+
else
11+
pr_description=$(echo "$pr_response" | jq -r '.[].body')
12+
13+
# The `sed "s/\r//g"` is used to remove the carriage return character \r from the end of the string
14+
SNAPSHOT_BRANCH=$(echo -E "$pr_description" | grep 'Snapshot name:' | cut -d ':' -f 2 | xargs echo -n | sed "s/\r//g" || echo -n)
15+
16+
if [ -z "$SNAPSHOT_BRANCH" ]; then
17+
echo "No custom snapshot name found, proceeding with default snapshot naming convention"
18+
19+
version=$(jq -r '.version' package.json)
20+
jira_id=$(echo -E "$pr_description" | grep 'Jira ID:' | grep -Eo '[A-Z]+-[0-9]+' || echo -n)
21+
22+
if [ -z "$jira_id" ]; then
23+
echo "No Jira ID found, proceeding with branch name instead"
24+
25+
SNAPSHOT_BRANCH="snapshot/$CIRCLE_BRANCH"
26+
else
27+
SNAPSHOT_BRANCH="snapshot/$version-$jira_id"
28+
fi
29+
fi
30+
fi

scripts/snapshot-comment.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Your snapshot has been generated! :rocket:
2+
3+
### Installation
4+
5+
You can install the snapshot through NPM:
6+
7+
```sh
8+
npm install https://github.com/Instabug/Instabug-React-Native\#{BRANCH}
9+
```
10+
11+
or Yarn:
12+
13+
```sh
14+
yarn add https://github.com/Instabug/Instabug-React-Native\#{BRANCH}
15+
```

0 commit comments

Comments
 (0)