Skip to content

Commit

Permalink
feat: Use GITHUB_TOKEN for PR operations
Browse files Browse the repository at this point in the history
This commit introduces the following changes:

- Add a new function `switch_to_github_token_auth` to switch GitHub authentication
- Modify `create_pr` and `create_or_edit_pr` functions to use GITHUB_TOKEN
- Ensure PR operations use GITHUB_TOKEN while other Git operations use SYNC_TOKEN

These changes allow PR creation and editing to be performed using GITHUB_TOKEN,
ensuring that the PR creator appears as github-actions[bot] in the GitHub interface.
Other Git operations continue to use SYNC_TOKEN for authentication.

This enhancement improves the visibility and consistency of automated PR operations
in the GitHub UI while maintaining flexibility for other Git-related tasks.
  • Loading branch information
xesrevinu committed Oct 10, 2024
1 parent bde94d3 commit c2751f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ runs:
shell: bash
id: sync
env:
SYNC_TOKEN: ${{ inputs.sync_token }}
GITHUB_TOKEN: ${{ inputs.github_token }}
SOURCE_REPO_PATH: ${{ inputs.source_repo_path }}
UPSTREAM_BRANCH: ${{ inputs.upstream_branch }}
Expand Down
5 changes: 3 additions & 2 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ fi

DEFAULT_REPO_HOSTNAME="github.com"
SOURCE_REPO_HOSTNAME="${HOSTNAME:-${DEFAULT_REPO_HOSTNAME}}"
SYNC_TOKEN="${SYNC_TOKEN:-${GITHUB_TOKEN}}"
GIT_USER_NAME="${GIT_USER_NAME:-${GITHUB_ACTOR}}"
GIT_USER_EMAIL="${GIT_USER_EMAIL:-github-action@actions-template-sync.noreply.${SOURCE_REPO_HOSTNAME}}"

Expand Down Expand Up @@ -145,7 +146,7 @@ function git_init() {
git config pull.rebase false
git config --add safe.directory /github/workspace

if [[ "${IS_GIT_LFS}" == 'true' ]]; then
if [[ "${IS_GIT_LFS}" == 'true' ]]; then
info "enable git lfs."
git lfs install
fi
Expand All @@ -170,7 +171,7 @@ function git_init() {
if [[ -n "${SSH_PRIVATE_KEY_SRC}" ]] &>/dev/null; then
ssh_setup "${SSH_PRIVATE_KEY_SRC}" "${SOURCE_REPO_HOSTNAME}"
elif [[ "${SOURCE_REPO_HOSTNAME}" != "${DEFAULT_REPO_HOSTNAME}" ]]; then
gh auth login --git-protocol "https" --hostname "${SOURCE_REPO_HOSTNAME}" --with-token <<< "${GITHUB_TOKEN}"
gh auth login --git-protocol "https" --hostname "${SOURCE_REPO_HOSTNAME}" --with-token <<< "${SYNC_TOKEN}"
fi

export SOURCE_REPO="${SOURCE_REPO_PREFIX}${SOURCE_REPO_PATH}"
Expand Down
15 changes: 15 additions & 0 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,15 @@ function push () {
fi
}

#######################################
# Switch GitHub authentication to use GITHUB_TOKEN
#######################################
function switch_to_github_token_auth() {
info "Switching to GITHUB_TOKEN for authentication"
gh auth logout
echo "$GITHUB_TOKEN" | gh auth login --with-token
}

####################################
# creates a pr
# Arguments:
Expand All @@ -311,6 +320,9 @@ function create_pr() {
local labels=$4
local reviewers=$5

# Switch to GITHUB_TOKEN authentication before creating PR
switch_to_github_token_auth

gh pr create \
--title "${title}" \
--body "${body}" \
Expand Down Expand Up @@ -344,6 +356,9 @@ function create_or_edit_pr() {
local reviewers=$5
local pr_branch=$6

# Switch to GITHUB_TOKEN authentication before creating/editing PR
switch_to_github_token_auth

create_pr "${title}" "${body}" "${upstream_branch}" "${labels}" "${reviewers}" || gh pr edit \
--title "${title}" \
--body "${body}" \
Expand Down

0 comments on commit c2751f4

Please sign in to comment.