Skip to content

Commit 3247499

Browse files
alexvanderberkelAlex Esseling
andauthored
feat(#598): enable possibility to have source repository in other organisation
- fix: 🐛 issue with AUTHN and AUTHZ - feat: ✨ preparations to be able to have target repository in another org/repository --------- Signed-off-by: Andy Augustin <[email protected]> Signed-off-by: Alex <[email protected]> Co-authored-by: Alex Esseling <[email protected]>
1 parent 4d7b02e commit 3247499

File tree

8 files changed

+178
-22
lines changed

8 files changed

+178
-22
lines changed

.github/workflows/test_all.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ jobs:
1515
permissions:
1616
contents: write
1717
pull-requests: write
18+
call_github_app_test_target_org:
19+
uses: ./.github/workflows/test_github_app _target_org.yml
20+
secrets: inherit
21+
permissions:
22+
contents: write
23+
pull-requests: write
24+
call_github_app_test:
25+
uses: ./.github/workflows/test_github_app.yml
26+
secrets: inherit
27+
permissions:
28+
contents: write
29+
pull-requests: write
1830
call_test_ssh_gitlab:
1931
uses: ./.github/workflows/test_ssh_gitlab.yml
2032
secrets: inherit
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: test-github_app
2+
3+
on:
4+
push:
5+
# branches:
6+
# - "!main"
7+
# pull_request:
8+
workflow_call:
9+
workflow_dispatch:
10+
11+
jobs:
12+
test-implementation-job:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: write
16+
pull-requests: write
17+
steps:
18+
# To use this repository's private action, you must check out the repository
19+
- name: token-generation
20+
uses: actions/create-github-app-token@v1
21+
id: source-app-token
22+
with:
23+
app-id: ${{ secrets.TEST_GITHUB_APP_ID }}
24+
private-key: ${{ secrets.TEST_APP_PEM_FILE }}
25+
owner: alexvanderberkel
26+
repositories: private-test-repo
27+
28+
- name: Checkout
29+
# https://github.com/actions/checkout#usage
30+
uses: actions/checkout@v4
31+
with:
32+
token: ${{ steps.source-app-token.outputs.token }}
33+
persist-credentials: false # Don't set this to true as otherwise the token will be stored in the local git config and the run will fail
34+
35+
- name: Test action step
36+
uses: ./ # Uses an action in the root directory
37+
env:
38+
MY_VAR: "bar"
39+
with:
40+
source_repo_path: alexvanderberkel/private-test-repo
41+
source_gh_token: ${{ steps.source-app-token.outputs.token }}
42+
# target_gh_token: ${{ github.token }}
43+
upstream_branch: main
44+
is_dry_run: false
45+
is_allow_hooks: true
46+
47+
48+

.github/workflows/test_ssh.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
if: github.repository_owner == 'AndreasAugustin'
2323
uses: ./ # Uses an action in the root directory
2424
with:
25-
github_token: ${{ secrets.GITHUB_TOKEN }}
2625
source_repo_path: ${{ secrets.SOURCE_REPO_PATH_TEST }} # <owner/repo>, should be within secrets
2726
source_repo_ssh_private_key: ${{ secrets.SOURCE_REPO_SSH_PRIVATE_KEY }} # contains the private ssh key of the private repository
2827
is_dry_run: true

.github/workflows/test_ssh_gitlab.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ jobs:
2323
uses: ./ # Uses an action in the root directory
2424
with:
2525
hostname: ${{ secrets.SOURCE_REPO_GITLAB_HOSTNAME }}
26-
github_token: ${{ secrets.GITHUB_TOKEN }}
2726
source_repo_path: ${{ secrets.SOURCE_REPO_GITLAB_PATH }} # <owner/repo>, should be within secrets
2827
source_repo_ssh_private_key: ${{ secrets.SOURCE_REPO_GITLAB_SSH_PRIVATE_KEY }} # contains the private ssh key of the private repository
2928
is_dry_run: true

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ jobs:
144144
- name: actions-template-sync
145145
uses: AndreasAugustin/actions-template-sync@v2
146146
with:
147-
github_token: ${{ steps.generate_token.outputs.token }}
147+
source_gh_token: ${{ steps.generate_token.outputs.token }}
148148
source_repo_path: <owner/repo>
149149
upstream_branch: <target_branch> # defaults to main
150150
pr_labels: <label1>,<label2>[,...] # defaults to template_sync
@@ -181,7 +181,7 @@ jobs:
181181
- name: actions-template-sync
182182
uses: AndreasAugustin/actions-template-sync@v2
183183
with:
184-
github_token: ${{ secrets.GITHUB_TOKEN }}
184+
source_gh_token: ${{ secrets.GITHUB_TOKEN }}
185185
source_repo_path: ${{ secrets.SOURCE_REPO_PATH }} # <owner/repo>, should be within secrets
186186
upstream_branch: ${{ secrets.TARGET_BRANCH }} #<target_branch> # defaults to main
187187
pr_labels: <label1>,<label2>[,...] # defaults to template_sync
@@ -235,15 +235,17 @@ jobs:
235235
- name: Test action step PAT
236236
uses: AndreasAugustin/actions-template-sync@v2
237237
with:
238-
github_token: ${{ secrets.CUSTOM_GITHUB_PAT }}
238+
source_gh_token: ${{ secrets.CUSTOM_GITHUB_PAT }}
239239
source_repo_path: ${{ secrets.SOURCE_REPO_PATH }} # <owner/repo>, should be within secrets
240240
```
241241

242242
### Action Inputs
243243

244244
| Variable | Description | Required | Default |
245245
|-----------------------------|---------------------------------------------------------------------------------------------------------------|----------|-----------------------------------------------------------------------|
246-
| github_token | Token for the repo. Can be passed in using `${{ secrets.GITHUB_TOKEN }}` | `true` | `${{ github.token }}` |
246+
| github_token | :warning: [Deprecated] please use `source_gh_token` instead to have a declarative name. Token for the repo. Can be passed in using `${{ secrets.GITHUB_TOKEN }}` | `true` | `${{ github.token }}` |
247+
| source_gh_token | `[optional]` used for the source github repo token. Can be passed in using `${{ secrets.GITHUB_TOKEN }}` | `false` | `${{ github.token }}` |
248+
| target_gh_token | `[optional]` used for the source github repo token. Can be passed in using `${{ secrets.GITHUB_TOKEN }}` | `false` | `${{ github.token }}` |
247249
| source_repo_path | Repository path of the template | `true` | |
248250
| upstream_branch | The target branch | `false` | The remote's default (usually `main`) |
249251
| source_repo_ssh_private_key | `[optional]` private ssh key for the source repository. [see](#private-template-repository) | `false` | |
@@ -267,7 +269,7 @@ jobs:
267269
| git_user_email | `[optional]` set the committer git user.email | `false` | `[email protected].${SOURCE_REPO_HOSTNAME}` |
268270
| git_remote_pull_params | `[optional]` set remote pull parameters | `false` | `--allow-unrelated-histories --squash --strategy=recursive -X theirs` |
269271
| gpg_private_key | `[optional]` set if you want to sign commits | `false` | |
270-
| gpg_passphrase | `[optional]` set if your optionial gpg private key has a passphrase | `false` | |
272+
| gpg_passphrase | `[optional]` set if your optional gpg private key has a passphrase | `false` | |
271273
| steps | `[optional] add the steps you want to execute within the action` | `false` | all steps will be executed |
272274
| template_sync_ignore_file_path | `[optional] set the path to the ignore file.` | false |`.templatesyncignore` |
273275
| is_with_tags | `[optional]` set to `true` if tags should be synced | `false` | `false` |
@@ -394,7 +396,7 @@ jobs:
394396
- name: actions-template-sync
395397
uses: AndreasAugustin/actions-template-sync@v2
396398
with:
397-
github_token: ${{ secrets.GITHUB_TOKEN }}
399+
source_gh_token: ${{ secrets.GITHUB_TOKEN }}
398400
source_repo_path: <owner/repo>
399401
git_user_name: # add the gpg username
400402
git_user_email: # add the gpg email
@@ -642,7 +644,7 @@ The idea is to use the [docker action][action-docker]
642644
- name: actions-template-sync
643645
uses: AndreasAugustin/actions-template-sync@v2
644646
with:
645-
github_token: ${{ secrets.GITHUB_TOKEN }}
647+
source_gh_token: ${{ secrets.GITHUB_TOKEN }}
646648
source_repo_path: <owner/repo>
647649
upstream_branch: <target_branch> # defaults to main
648650
pr_labels: <label1>,<label2>[,...] # optional, no default

action.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,18 @@ branding:
55
icon: cloud
66
color: green
77
inputs:
8-
github_token:
9-
description: 'Token for the repo. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}'
10-
required: true
8+
source_gh_token:
9+
description: 'GitHub Token for the repo to be synced from. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}'
10+
required: false
1111
default: ${{ github.token }}
12+
target_gh_token:
13+
description: 'GitHub Token for the repo to be synced to. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}'
14+
required: false
15+
default: ${{ github.token }}
16+
github_token:
17+
deprecationMessage: 'please use source_gh_token instead to have a declarative name'
18+
description: 'GitHub Token for the repo to be synced from. Can be passed in using $\{{ secrets.GITHUB_TOKEN }}'
19+
required: false
1220
source_repo_path:
1321
description: "Repository path of the template"
1422
required: true
@@ -95,7 +103,11 @@ runs:
95103
shell: bash
96104
id: sync
97105
env:
106+
SOURCE_GH_TOKEN: ${{ inputs.source_gh_token}}
107+
TARGET_GH_TOKEN: ${{ inputs.target_gh_token }}
108+
# GITHUB_TOKEN is deprecated and will be removed soon
98109
GITHUB_TOKEN: ${{ inputs.github_token }}
110+
#
99111
SOURCE_REPO_PATH: ${{ inputs.source_repo_path }}
100112
UPSTREAM_BRANCH: ${{ inputs.upstream_branch }}
101113
SSH_PRIVATE_KEY_SRC: ${{ inputs.source_repo_ssh_private_key }}

src/entrypoint.sh

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ source "${SCRIPT_DIR}/sync_common.sh"
1212
# Precheks
1313
##########################################
1414

15-
if [[ -z "${GITHUB_TOKEN}" ]]; then
16-
err "Missing input 'github_token: \${{ secrets.GITHUB_TOKEN }}'.";
15+
if [[ -z "${TARGET_GH_TOKEN}" ]]; then
16+
err "Missing input 'target_gh_token': \${{ secrets.GITHUB_TOKEN }}'.";
17+
exit 1;
18+
fi
19+
20+
if [[ -z "${SOURCE_GH_TOKEN}" ]]; then
21+
err "Missing input 'source_gh_token': \${{ secrets.GITHUB_TOKEN }}'.";
1722
exit 1;
1823
fi
1924

@@ -27,6 +32,10 @@ if [[ -z "${HOME}" ]]; then
2732
exit 1
2833
fi
2934

35+
if [[ -z "${GITHUB_SERVER_URL}" ]]; then
36+
err "Missing env variable 'GITHUB_SERVER_URL' of the target github server. E.g. https://github.com"
37+
fi
38+
3039
if ! [ -x "$(command -v gh)" ]; then
3140
err "github-cli gh is not installed. 'https://github.com/cli/cli'";
3241
exit 1;
@@ -37,7 +46,7 @@ fi
3746
############################################
3847

3948
DEFAULT_REPO_HOSTNAME="github.com"
40-
SOURCE_REPO_HOSTNAME="${HOSTNAME:-${DEFAULT_REPO_HOSTNAME}}"
49+
export SOURCE_REPO_HOSTNAME="${HOSTNAME:-${DEFAULT_REPO_HOSTNAME}}"
4150
GIT_USER_NAME="${GIT_USER_NAME:-${GITHUB_ACTOR}}"
4251
GIT_USER_EMAIL="${GIT_USER_EMAIL:-github-action@actions-template-sync.noreply.${SOURCE_REPO_HOSTNAME}}"
4352

@@ -161,6 +170,42 @@ function git_init() {
161170
ssh-keyscan -t rsa "${source_repo_hostname}" >> "${HOME}"/.ssh/known_hosts
162171
else
163172
info "the source repository is located within GitHub."
173+
fi
174+
echo "::endgroup::"
175+
}
176+
177+
#######################################
178+
# doing the login to the source repository using gh cli
179+
# Arguments:
180+
# source_repo_hostname
181+
#######################################
182+
function gh_login_src_github() {
183+
echo "::group::login src github"
184+
local source_repo_hostname=$1
185+
# GITHUB_TOKEN is deprecated and can be removed in the future
186+
if [[ -n "${SOURCE_GH_TOKEN}" ]] || [[ -n "${GITHUB_TOKEN}" ]] &>/dev/null; then
187+
################################
188+
if [[ -n "${GITHUB_TOKEN}" ]] &>/dev/null; then
189+
warn "github_token parameter is deprecated please use source_gh_token."
190+
info "setting SOURCE_GH_TOKEN"
191+
export SOURCE_GH_TOKEN="${GITHUB_TOKEN}"
192+
unset GITHUB_TOKEN
193+
fi
194+
###############################
195+
if [[ -z "${SOURCE_GH_TOKEN}" ]] &>/dev/null; then
196+
err "Missing input 'source_gh_token: \${{ secrets.GITHUB_TOKEN }}'.";
197+
exit 1;
198+
fi
199+
info "source server url: ${source_repo_hostname}"
200+
info "logging out"
201+
gh auth logout --hostname "${source_repo_hostname}" || debug "not logged in"
202+
info "login to the source git repository"
203+
gh auth login --git-protocol "https" --hostname "${source_repo_hostname}" --with-token <<< "${SOURCE_GH_TOKEN}"
204+
gh auth status
205+
gh auth setup-git --hostname "${source_repo_hostname}"
206+
gh auth status --hostname "${source_repo_hostname}"
207+
else
208+
info "default token to be used"
164209
gh auth setup-git --hostname "${source_repo_hostname}"
165210
gh auth status --hostname "${source_repo_hostname}"
166211
fi
@@ -171,17 +216,17 @@ function git_init() {
171216
# Logic
172217
###################################################
173218

219+
git_init "${GIT_USER_EMAIL}" "${GIT_USER_NAME}" "${SOURCE_REPO_HOSTNAME}"
220+
174221
# Forward to /dev/null to swallow the output of the private key
175222
if [[ -n "${SSH_PRIVATE_KEY_SRC}" ]] &>/dev/null; then
176223
ssh_setup "${SSH_PRIVATE_KEY_SRC}" "${SOURCE_REPO_HOSTNAME}"
177-
elif [[ "${SOURCE_REPO_HOSTNAME}" != "${DEFAULT_REPO_HOSTNAME}" ]]; then
178-
gh auth login --git-protocol "https" --hostname "${SOURCE_REPO_HOSTNAME}" --with-token <<< "${GITHUB_TOKEN}"
224+
else
225+
gh_login_src_github "${SOURCE_REPO_HOSTNAME}"
179226
fi
180227

181228
export SOURCE_REPO="${SOURCE_REPO_PREFIX}${SOURCE_REPO_PATH}"
182229

183-
git_init "${GIT_USER_EMAIL}" "${GIT_USER_NAME}" "${SOURCE_REPO_HOSTNAME}"
184-
185230
if [[ -n "${GPG_PRIVATE_KEY}" ]] &>/dev/null; then
186231
gpg_setup "${GPG_PRIVATE_KEY}" "${GIT_USER_EMAIL}"
187232
fi

src/sync_template.sh

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ if [[ -z "${TEMPLATE_SYNC_IGNORE_FILE_PATH}" ]]; then
3333
exit 1;
3434
fi
3535

36+
if [[ -z "${GITHUB_SERVER_URL}" ]]; then
37+
err "Missing env variable 'GITHUB_SERVER_URL' of the target github server. E.g. https://github.com"
38+
fi
39+
40+
info "prechecks passed"
3641
########################################################
3742
# Variables
3843
########################################################
@@ -83,13 +88,39 @@ debug "PR_BODY ${PR_BODY}"
8388
# Check if the Ignore File exists inside .github folder or if it doesn't exist at all
8489
if [[ -f ".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}" || ! -f "${TEMPLATE_SYNC_IGNORE_FILE_PATH}" ]]; then
8590
debug "using ignore file as in .github folder"
86-
TEMPLATE_SYNC_IGNORE_FILE_PATH=".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}"
91+
TEMPLATE_SYNC_IGNORE_FILE_PATH=".github/${TEMPLATE_SYNC_IGNORE_FILE_PATH}"
8792
fi
8893

94+
info "variables done"
95+
8996
#####################################################
9097
# Functions
9198
#####################################################
9299

100+
#######################################
101+
# doing the login to the source repository using gh cli
102+
# Arguments:
103+
# github_server url
104+
#######################################
105+
function gh_login_target_github() {
106+
echo "::group::login target github"
107+
local github_server_url=$1
108+
109+
if [[ -n "${TARGET_GH_TOKEN}" ]]; then
110+
target_repo_hostname=$(echo "${github_server_url}" | cut -d '/' -f 3)
111+
info "target server url: ${target_repo_hostname}"
112+
info "logging out of the target if logged in"
113+
gh auth logout --hostname "${target_repo_hostname}" || debug "not logged in"
114+
unset GH_TOKEN
115+
info "login to the target git repository"
116+
gh auth login --git-protocol "https" --hostname "${target_repo_hostname}" --with-token <<< "${TARGET_GH_TOKEN}"
117+
gh auth setup-git --hostname "${target_repo_hostname}"
118+
gh auth status --hostname "${target_repo_hostname}"
119+
fi
120+
121+
echo "::endgroup::"
122+
}
123+
93124
#######################################
94125
# set the gh action outputs if run with github action.
95126
# Arguments:
@@ -243,11 +274,16 @@ function pull_source_changes() {
243274

244275
eval "git pull ${source_repo} --tags ${git_remote_pull_params}" || pull_has_issues=true
245276

277+
info "finished pulling from the source."
278+
info "logging out from source ${SOURCE_REPO_HOSTNAME}."
279+
246280
if [ "$pull_has_issues" == true ] ; then
247281
warn "There had been some git pull issues."
248282
warn "Maybe a merge issue."
249283
warn "We go on but it is likely that you need to fix merge issues within the created PR."
250284
fi
285+
286+
gh_login_target_github "${GITHUB_SERVER_URL}"
251287
}
252288

253289
#######################################
@@ -294,6 +330,8 @@ function eventual_create_labels () {
294330
##############################
295331
function push () {
296332
info "push changes"
333+
334+
297335
local branch=$1
298336
local is_force=$2
299337
local is_with_tags=$3
@@ -308,9 +346,10 @@ function push () {
308346
if [ "$is_with_tags" == true ] ; then
309347
warn "include tags."
310348
args+=(--tags)
311-
fi
312-
349+
fi
350+
313351
git push "${args[@]}"
352+
314353
}
315354

316355
####################################

0 commit comments

Comments
 (0)