Skip to content

Commit 8aea760

Browse files
authored
ci: add branch input to Format Code workflow (#326)
Enables manual triggering of the Format Code workflow against any branch via `workflow_dispatch` input. ## Changes - **Added `branch` input**: Optional parameter in `workflow_dispatch` trigger that accepts branch names - **Updated checkout**: Uses `inputs.branch` when provided, falls back to `github.ref` otherwise - **Fixed branch detection**: Modified default branch check, format commits step, and force push step to use the actual target branch (from input or context) ## Behavior ```yaml # Manual trigger via GitHub UI or API inputs: branch: 'feature/my-branch' # Optional - omit to use current branch ``` When `branch` is specified: - Checks out the specified branch - Validates it's not the default branch - Formats all commits on that branch - Force-pushes formatted commits back to the same branch When omitted, maintains existing behavior using the triggered branch context. <!-- START COPILOT CODING AGENT SUFFIX --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Support passing a branch to the Format Code workflow</issue_title> > <issue_description>Add a branch input to the Format Code `workflow_dispatch` workflow.</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> - Fixes #325 <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).
2 parents 5d4ff03 + 8255e59 commit 8aea760

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

.github/workflows/format.yml

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ name: Format Code
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
branch:
7+
description: 'Branch to format (defaults to current branch if not specified)'
8+
required: false
9+
type: string
510

611
permissions:
712
contents: write # needed to amend commits and force push
@@ -17,6 +22,8 @@ jobs:
1722
fetch-depth: 0
1823
# Use token that allows force push
1924
token: ${{ secrets.GITHUB_TOKEN }}
25+
# Use the branch input if provided, otherwise use the default branch
26+
ref: ${{ inputs.branch || github.ref }}
2027

2128
- name: Check if running on default branch
2229
id: check-branch
@@ -25,8 +32,17 @@ jobs:
2532
DEFAULT_BRANCH=$(gh repo view --json defaultBranchRef --jq '.defaultBranchRef.name')
2633
echo "default-branch=$DEFAULT_BRANCH" >> $GITHUB_OUTPUT
2734
35+
# Determine the branch being processed
36+
# If a branch was specified via workflow input, use that
37+
# Otherwise, use the branch from GITHUB_REF
38+
if [ -n "${{ inputs.branch }}" ]; then
39+
BRANCH="${{ inputs.branch }}"
40+
else
41+
BRANCH="${GITHUB_REF#refs/heads/}"
42+
fi
43+
2844
# Prevent running on the default branch
29-
if [ "$GITHUB_REF" == "refs/heads/$DEFAULT_BRANCH" ]; then
45+
if [ "$BRANCH" == "$DEFAULT_BRANCH" ]; then
3046
echo "Error: This workflow cannot run on the default branch ($DEFAULT_BRANCH)"
3147
exit 1
3248
fi
@@ -49,7 +65,13 @@ jobs:
4965
DEFAULT_BRANCH="${{ steps.check-branch.outputs.default-branch }}"
5066
5167
# Get the current branch name
52-
CURRENT_BRANCH="${GITHUB_REF#refs/heads/}"
68+
# If a branch was specified via workflow input, use that
69+
# Otherwise, use the branch from GITHUB_REF
70+
if [ -n "${{ inputs.branch }}" ]; then
71+
CURRENT_BRANCH="${{ inputs.branch }}"
72+
else
73+
CURRENT_BRANCH="${GITHUB_REF#refs/heads/}"
74+
fi
5375
5476
# Check if this branch has an open pull request and use its target branch
5577
BASE_BRANCH=$(gh pr view "$CURRENT_BRANCH" --json baseRefName --jq '.baseRefName' 2>/dev/null || echo "")
@@ -213,8 +235,17 @@ jobs:
213235

214236
- name: Force push formatted commits
215237
run: |
238+
# Determine the branch name to push to
239+
# If a branch was specified via workflow input, use that
240+
# Otherwise, use the branch from github.ref_name
241+
if [ -n "${{ inputs.branch }}" ]; then
242+
BRANCH_NAME="${{ inputs.branch }}"
243+
else
244+
BRANCH_NAME="${{ github.ref_name }}"
245+
fi
246+
216247
# Force push the reformatted branch
217248
# Use --force-with-lease for safety (protects against concurrent updates)
218249
# Note: --force-if-includes requires Git 2.30+, so we use just --force-with-lease
219-
git push --force-with-lease origin "HEAD:${{ github.ref_name }}"
220-
echo "✓ Successfully pushed formatted commits to ${{ github.ref_name }}"
250+
git push --force-with-lease origin "HEAD:$BRANCH_NAME"
251+
echo "✓ Successfully pushed formatted commits to $BRANCH_NAME"

0 commit comments

Comments
 (0)