Skip to content

ci: add /squash reusable workflow#3173

Open
waveywaves wants to merge 7 commits intotektoncd:mainfrom
waveywaves:ci/add-squash-command
Open

ci: add /squash reusable workflow#3173
waveywaves wants to merge 7 commits intotektoncd:mainfrom
waveywaves:ci/add-squash-command

Conversation

@waveywaves
Copy link
Member

@waveywaves waveywaves commented Feb 24, 2026

Changes

Add a /squash slash command to the Tekton chatops system, following the same three-layer architecture as the existing /rebase command (PR tektoncd/pipeline#9375):

  1. _squash-command.yaml (new) — Reusable workflow containing the squash logic
  2. _slash.yml (modified) — Register /squash in the shared slash command dispatch

How it works

When a maintainer comments /squash on a PR:

  1. slash.yml in the consuming repo dispatches a squash-command repository event
  2. The caller workflow delegates to this reusable workflow
  3. The workflow validates the PR state, performs git reset --soft to squash all commits into one using the first commit's message, and force pushes with --force-with-lease

Edge cases handled

  • Fork PRs: Rejected with error (no write access to contributor forks)
  • Single-commit PRs: No-op with informational comment
  • Merge commits in history: Warning posted, squash proceeds
  • Commit trailers: Co-authored-by and Signed-off-by trailers preserved from all commits
  • Concurrent pushes: --force-with-lease prevents overwriting

Why git reset --soft instead of git rebase -i

Interactive rebase requires editor hacks (GIT_SEQUENCE_EDITOR) in CI and is fragile with merge commits. git reset --soft $(git merge-base ...) is simpler, fully non-interactive, and produces identical results for the squash use case.

Follow-up

A corresponding PR for tektoncd/pipeline will add the caller workflow (squash-command.yaml), slash.yml entry, and CONTRIBUTING.md documentation once this is merged and a SHA is available for pinning.

/kind feature

Submitter Checklist

See the contribution guide
for more details.

@tekton-robot tekton-robot requested a review from abayer February 24, 2026 19:52
@tekton-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign savitaashture after the PR has been reviewed.
You can assign the PR to them by writing /assign @savitaashture in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot requested a review from wlynch February 24, 2026 19:52
@tekton-robot tekton-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 24, 2026
Add a reusable workflow that squashes all commits on a PR branch into
a single commit when triggered by the /squash slash command. Uses
git reset --soft for reliable non-interactive squashing in CI.

Edge cases handled:
- Fork PRs rejected (no write access to contributor repos)
- Single-commit PRs produce informational no-op
- Merge commits in branch history trigger warning
- Co-authored-by and Signed-off-by trailers preserved
- --force-with-lease prevents overwriting concurrent pushes
Add squash command entry to the _slash.yml reusable slash command
config so repos using the shared dispatch get the /squash command
automatically.
@waveywaves waveywaves force-pushed the ci/add-squash-command branch from ca302d0 to 7caff61 Compare February 24, 2026 19:55
Add fork PR support to the squash workflow. For fork PRs where
"Allow edits from maintainers" is enabled, the workflow now checks
maintainerCanModify via the GitHub API, checks out the fork
repository, adds the upstream as a remote for merge-base
computation, and pushes back to the fork branch.

If the PR author has not enabled maintainer edits, the command
reports a clear error asking them to enable it.
Support optional arguments for the /squash slash command:
- /squash <message>: use a custom commit message instead of
  the first commit's message
- /squash --last N: squash only the last N commits instead
  of all commits on the branch
- /squash --last N <message>: combine both options

Arguments are parsed by slash-command-dispatch v5 from the
PR comment text after the /squash command.
Parse the full comment body for multiline commit messages.
Lines after the /squash command line become the extended
commit body, following standard git title/body convention
with a blank line separator.
When using the default commit message (first commit's %B), trailers
like Signed-off-by were duplicated because %B already includes them
and the trailer collection loop appended them again. Add a grep-based
dedup check to skip trailers already present in the commit message.
slash-command-dispatch uses key=value format for named args, not
--key value. Parse --last N from the unnamed args string and strip
it from the custom message. Supports both --last N and last=N syntax.
@waveywaves
Copy link
Member Author

Tested on a fork with all features working: waveywaves/tekton-pipeline#5

  • /squash — squash all commits, first commit message preserved, trailers deduped
  • /squash <custom message> — custom commit message
  • /squash --last N — partial squash (last N commits only)

@waveywaves
Copy link
Member Author

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the custom commit message be multi-line ?

#
# Security Notes:
# - Only users with "write" permission can trigger this command (enforced in slash.yml)
# - Supports fork PRs when "Allow edits from maintainers" is enabled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mean the "bot user" need to be maintainer right ? It has write access for sure, not sure if it has maintainer role.

Copy link
Member Author

@waveywaves waveywaves Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, write (push) access is sufficient: maintainer role is not required.

The GitHub docs say: "People with push access to the upstream repository of a fork in a personal account can commit to the forked branches."

The Write role grants push access, so any user/bot with Write access can push to fork branches when the PR author enables "Allow edits from maintainers".

One important caveat: this only works with PATs, not with GITHUB_TOKEN or GitHub App installation tokens. GitHub intentionally restricts GITHUB_TOKEN from pushing to forks. Since CHATOPS_TOKEN is a PAT with write access, the fork push works. This is the same mechanism used by cirrus-actions/rebase (500+ stars) which pushes to forks using x-access-token:$TOKEN in the git URL.

Also note: this only works for user-owned forks, not org-owned forks (GitHub doesn't allow maintainer_can_modify for org forks).

@waveywaves
Copy link
Member Author

waveywaves commented Feb 25, 2026

Can the custom commit message be multi-line?

Yes! Lines after the /squash line become the commit body. For example:

/squash Fix the auth bug

This resolves the token refresh issue that caused
intermittent 401 errors on long-running pipelines.

This produces a commit with:

  • Title: Fix the auth bug
  • Body: This resolves the token refresh issue...

The multiline parsing extracts everything after line 1 of the comment body via tail -n +2. This is documented in the header comment (lines 13-17) and implemented in the "Determine commit message" section of the squash step.

cc @vdemeester

@waveywaves
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants