-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
165 lines (152 loc) · 7.78 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
name: "Submodules Alternative"
description: "Adds/updates all repositories added by using the Submodules Alternative tool."
author: "Panquesito7"
branding:
icon: git-pull-request
color: purple
inputs:
repos_filename:
description: "The filename used to obtain the configuration and necessary repository data."
required: true
default: "repos"
use_pr:
description: "Whether to add/update repositories via a PR or direct push."
required: true
default: "true"
branch_name:
description: "The name of the branch to push to (only if `use_pr` is enabled). Not applicable when multiple PRs are created!"
required: false
default: "repo-update"
commit_message:
description: "The commit/PR message to use when adding new subtrees."
required: false
default: 'Add the given subtrees'
commit_message_update:
description: "The commit/PR message to use when updating all the subtrees. Not applicable when multiple PRs are created!"
required: false
default: 'Bump subtrees to their latest version'
add_repos:
description: "Runs the `fetch-repos` script if enabled to clone all the subtrees."
required: true
default: "false"
update_repos:
description: "Whether to update the subtrees or not."
required: true
default: "true"
squash_commits:
description: "Whether to squash all commits into one or not."
required: false
default: "false"
one_pr:
description: "Creates one single PR for updating all the subtrees. This does not apply to the `fetch-repos` script. Doesn't work together with `squash_commits`."
required: false
default: "false"
runs:
using: "composite"
steps:
- name: Make sure the configurations are valid
shell: bash
run: |
repos_filename=${{ inputs.repos_filename }}
if [[ ${{ inputs.add_repos }} == false ]] && [[ ${{ inputs.update_repos }} == false ]]; then
echo "Both 'add_repos' and 'update_repos' are disabled. Please enable at least one of them."
exit 1
fi
if [[ ${{ inputs.squash_commits }} == true ]] && [[ ${{ inputs.one_pr }} == false ]]; then
echo "The 'one_pr' option doesn't work together with 'squash_commits' enabled. Please adjust accordingly."
exit 1
fi
if [[ $repos_filename == *.lua ]]; then
echo "Warning: 'repos_filename' contains '.lua' at the end. It is recommended to remove it."
repos_filename=$(echo ${{ inputs.repos_filename }} | sed 's/\.lua//g')
fi
echo "REPOS_FILENAME=$repos_filename" >> $GITHUB_ENV
- name: Setup Git configurations
shell: bash
run: |
git config --global user.name github-actions[bot]
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
- name: Change to the correct branch
shell: bash
run: |
if git ls-remote --heads origin ${{ inputs.branch_name }} | grep -q ${{ inputs.branch_name }}; then
git checkout ${{ inputs.branch_name }}
else
git checkout -b ${{ inputs.branch_name }}
fi
- name: Update the repositories file
shell: bash
run: |
if [[ $(git diff --name-only origin/${GITHUB_REF##*/} ${{ env.REPOS_FILENAME }}.lua) ]]; then
git rm ${{ env.REPOS_FILENAME }}.lua || true
default_branch=$(git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)
wget https://raw.githubusercontent.com/${{ github.repository }}/${default_branch}/${{ env.REPOS_FILENAME }}.lua
fi
- name: Setup Lua
shell: bash
run: |
sudo apt-get -qq update
sudo apt-get -qq install lua5.3
- name: Run fetch-repos script
shell: bash
run: |
if [[ ${{ inputs.add_repos }} == true ]]; then
lua ${{ github.action_path }}/src/fetch-repos.lua '${{ env.REPOS_FILENAME }}' ${{ inputs.squash_commits }} '${{ inputs.commit_message }}'
fi
- name: Run update-repos script
shell: bash
run: |
if [[ ${{ inputs.update_repos }} == true ]]; then
lua ${{ github.action_path }}/src/update-repos.lua '${{ env.REPOS_FILENAME }}' ${{ inputs.squash_commits }} ${{ inputs.one_pr }} '${{ inputs.commit_message_update }}'
fi
- name: Push changes and create PR
shell: bash
run: |
if [[ ${{ inputs.use_pr }} == true ]]; then
if [[ ${{ inputs.one_pr }} == true ]]; then # One PR for everything: fetching and updating.
git push origin ${{ inputs.branch_name }}:${{ inputs.branch_name }} || true
pr_body=$(bash ${{ github.action_path }}/src/tools/markdown_pr_body.sh "all" "false" "${{ github.action_path }}")
gh pr create --base ${GITHUB_REF##*/} --head ${{ inputs.branch_name }} --title '${{ inputs.commit_message }}' --body "$pr_body" || true
else # The One PR option is not available when adding the repositories.
# The code below creates one PR for fetching all the subtrees.
if [[ ${{ inputs.add_repos }} == true ]]; then
git checkout ${{ inputs.branch_name }} || true
git push origin ${{ inputs.branch_name }}:${{ inputs.branch_name }} || true
# Obtain the PR body and the necessary labels.
PR_BODY=$(bash ${{ github.action_path }}/src/tools/markdown_pr_body.sh "added" "false" "${{ github.action_path }}")
LABELS_FETCH=$(bash ${{ github.action_path }}/src/tools/fetch_labels.sh "${{ env.REPOS_FILENAME }}" "fetch")
# Create the pull request with the given data.
gh pr create --base ${GITHUB_REF##*/} --head ${{ inputs.branch_name }} --title '${{ inputs.commit_message }}' --body "Repositories were added or updated using the Submodules Alternative tool." || true
# Edit the newly created PR by adding the specific labels with the given PR number.
if [[ $(gh pr list --base ${GITHUB_REF##*/} --head ${{ inputs.branch_name }}) ]]; then
PR_NUMBER=$(bash ${{ github.action_path }}/src/tools/pr_number.sh ${GITHUB_REF##*/} "${{ inputs.branch_name }}")
gh pr edit $PR_NUMBER --add-label $LABELS_FETCH || true
fi
elif [[ ${{ inputs.update_repos }} == true ]] && [[ ${{ inputs.add_repos }} == false ]]; then
# The One PR option is available when updating the repositories.
# Get the branches that will be used to create the PRs.
BRANCHES=$(lua -e 'local repos = require("${{ env.REPOS_FILENAME }}").repos; dofile("${{ github.action_path }}/src/helper-functions.lua"); get_repo_branches(repos)')
LABELS_UPDATE=$(bash ${{ github.action_path }}/src/tools/fetch_labels.sh "${{ env.REPOS_FILENAME }}" "update")
for branch in ${BRANCHES[@]}; do
# Obtain the PR body and adjust it to the updated subtree.
REPO_NAME=${branch%-update}
PR_BODY=$(bash ${{ github.action_path }}/src/tools/markdown_pr_body.sh "updated" "true" "${{ github.action_path }}" "$REPO_NAME")
# Create the PR with the given data.
gh pr create --base ${GITHUB_REF##*/} --head $branch --title 'Bump `'$REPO_NAME'` to its latest commit' --body "$PR_BODY" || true
# Edit the newly created PR by adding the specific labels.
if [[ $(gh pr list --base ${GITHUB_REF##*/} --head $branch) ]]; then
PR_NUMBER=$(bash ${{ github.action_path }}/src/tools/pr_number.sh ${GITHUB_REF##*/} $branch)
gh pr edit $PR_NUMBER --add-label "$LABELS_UPDATE" || true
fi
done
fi
fi
else
git push || true
fi
env:
GH_TOKEN: ${{ github.token }}
- name: Delete temporary files
shell: bash
run: |
rm updated_subtrees.txt added_subtrees.txt || true