Skip to content
This repository was archived by the owner on Jul 5, 2023. It is now read-only.

Commit 058a7be

Browse files
committed
Pass repo to the action so it can be used
1 parent 0576c30 commit 058a7be

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
# Required to use access_token, otherwise optional.
3333
access_token_user: 'username'
3434

35+
# Repository for the access token.
36+
# This is required as otherwise we can't set the remote properly.
37+
# Required to use access_token, otherwise optional.
38+
access_token_repository: 'git@github.com:username/repo.git'
39+
3540
# Private key for connecting to remote hosts. To generate private key:
3641
# `ssh-keygen -o -t rsa -C 'action@deployer.org'`.
3742
# Optional.

action.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ inputs:
2323
default: ''
2424
description: The user for the access token.
2525

26+
access-token-repository:
27+
required: false
28+
default: ''
29+
description: The repository for the access token
30+
2631
private-key:
2732
required: false
2833
default: ''

index.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,21 @@ async function ssh() {
5151
async function accessToken() {
5252
let accessToken = core.getInput('access-token')
5353
let accessTokenUser = core.getInput('access-token-user')
54+
let tokenRepository = core.getInput('access-token-repository')
5455

55-
if (accessToken !== '' && accessTokenUser !== '') {
56-
// Get git remote and convert to https
57-
let {stdout} = execa.commandSync('/usr/bin/git config --get remote.origin.url')
58-
let remote = stdout.trim()
59-
if (remote.startsWith('git@')) {
56+
if (accessToken !== '' && accessTokenUser !== '' && tokenRepository !== '') {
57+
// Convert to https if it's a git url
58+
if (tokenRepository.startsWith('git@')) {
6059
// Example: git@github.com:owner/repo.git
61-
remote = remote.replace(/:/, '/') // Replace the colon with a slash
62-
remote = remote.replace(/^git@/, 'https://') // Replace the git@ with https://
60+
tokenRepository = tokenRepository.replace(/:/, '/') // Replace the colon with a slash
61+
tokenRepository = tokenRepository.replace(/^git@/, 'https://') // Replace the git@ with https://
6362
}
6463

6564
// Add access token to remote so https://user:token@github.com/owner/repo.git
66-
remote = remote.replace(/^https:\/\//, `https://${accessTokenUser}:${accessToken}@`)
65+
tokenRepository = tokenRepository.replace(/^https:\/\//, `https://${accessTokenUser}:${accessToken}@`)
6766

68-
// Set remote to new so it will use the token
69-
execa.commandSync(`/usr/bin/git remote set-url origin ${remote}`)
67+
// Set remote, so it will use the token, init is in case it isn't tracked
68+
execa.commandSync(`/usr/bin/git init && /usr/bin/git remote set-url origin ${tokenRepository}`)
7069
}
7170
}
7271

0 commit comments

Comments
 (0)