Repository navigation
Expand file tree
/
Copy pathaction.yml
More file actions
141 lines (133 loc) · 5.55 KB
/
Copy pathaction.yml
File metadata and controls
141 lines (133 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
name: git-sim pull request check
description: >
Comment on a pull request with what merging it would do: git-sim's
pre-flight report (risk, facts, a text commit graph) and, as a workflow
artifact, the interactive before / after graph of the merge.
author: Initial Commit
branding:
icon: git-merge
color: orange
inputs:
mode:
description: "merge (default) or rebase: which operation to check"
required: false
default: merge
comment:
description: "Post the report as a pull request comment (needs pull-requests: write)"
required: false
default: "true"
artifact:
description: "Upload the interactive graph as a workflow artifact"
required: false
default: "true"
python-version:
description: "Python version to install git-sim with"
required: false
default: "3.12"
token:
description: "Token for the comment (defaults to the workflow's GITHUB_TOKEN)"
required: false
default: ${{ github.token }}
package:
description: "What pip installs: git-sim from PyPI, a pinned version (git-sim==0.4.0), or a Git URL"
required: false
default: git-sim
runs:
using: composite
steps:
- uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
# skia draws the graph and loads these, which GitHub's Ubuntu runners lack
- name: Install the libraries skia needs on Linux
if: runner.os == 'Linux'
shell: bash
run: sudo apt-get update -qq && sudo apt-get install -y -qq libegl1 libgl1 libfontconfig1 > /dev/null
- name: Install git-sim
shell: bash
env:
PACKAGE: ${{ inputs.package }}
run: python -m pip install --quiet "$PACKAGE"
- name: Fetch the pull request and its base
id: fetch
shell: bash
env:
BASE: ${{ github.event.pull_request.base.ref }}
NUMBER: ${{ github.event.pull_request.number }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
run: |
set -euo pipefail
# The pull request's commits as a local branch named like its own, so
# the report says "merge feature"; pr-<number> when that name can't be
# used (a fork's main into main, or a name git won't take)
BRANCH="$HEAD_REF"
if [ -z "$BRANCH" ] || [ "$BRANCH" = "$BASE" ] || ! git check-ref-format --branch "$BRANCH" > /dev/null 2>&1; then
BRANCH="pr-$NUMBER"
fi
# The base is fetched apart from it: on pull_request_target the checkout
# is the base branch itself, which git refuses to fetch into.
git fetch -q origin "+refs/pull/$NUMBER/head:refs/heads/$BRANCH"
git fetch -q origin "$BASE"
git checkout -q -B "$BASE" "origin/$BASE"
echo "branch=$BRANCH" >> "$GITHUB_OUTPUT"
- name: Pre-flight and simulate
id: sim
shell: bash
env:
MODE: ${{ inputs.mode }}
BRANCH: ${{ steps.fetch.outputs.branch }}
BASE: ${{ github.event.pull_request.base.ref }}
git_sim_auto_open: "false"
run: |
set -euo pipefail
mkdir -p git-sim-out
if [ "$MODE" = "rebase" ]; then
git checkout -q "$BRANCH"
CMD=(rebase "$BASE")
else
CMD=(merge "$BRANCH")
fi
git-sim preflight --markdown "${CMD[@]}" > git-sim-out/report.md || true
PAGE=$(git-sim -d --media-dir git-sim-out --output-only-path "${CMD[@]}" 2>/dev/null | tail -n 1 || true)
if [ -n "$PAGE" ] && [ -f "$PAGE" ]; then
cp "$PAGE" git-sim-out/simulation.html
echo "page=git-sim-out/simulation.html" >> "$GITHUB_OUTPUT"
fi
echo "report=git-sim-out/report.md" >> "$GITHUB_OUTPUT"
- name: Upload the interactive graph
if: inputs.artifact == 'true' && steps.sim.outputs.page != ''
uses: actions/upload-artifact@v4
with:
name: git-sim-pr-${{ github.event.pull_request.number }}
path: ${{ steps.sim.outputs.page }}
- name: Comment on the pull request
if: inputs.comment == 'true'
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
NUMBER: ${{ github.event.pull_request.number }}
REPORT: ${{ steps.sim.outputs.report }}
HAS_PAGE: ${{ steps.sim.outputs.page }}
MODE: ${{ inputs.mode }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
set -euo pipefail
{
echo "<!-- git-sim-pr-check -->"
cat "$REPORT"
if [ -n "$HAS_PAGE" ]; then
echo
echo "The interactive before / after graph is attached to [this run]($RUN_URL) as the artifact \`git-sim-pr-$NUMBER\`."
fi
echo
if [ "$MODE" = "rebase" ]; then OP=rebase; else OP=merge; fi
echo "<sub>What does \`git $OP\` do? [See it explained on Initial Commit](https://initialcommit.com/learn/git/go?cmd=$OP&from=github-action).</sub><br>"
echo "<sub>Computed from the repository by [git-sim](https://initialcommit.com/tools/git-sim?ref=git-sim-github-action); nothing was merged or changed.</sub>"
} > git-sim-out/comment.md
# one comment per pull request, updated on later runs
existing=$(gh api --paginate "repos/${GITHUB_REPOSITORY}/issues/$NUMBER/comments" --jq '.[] | select(.body | startswith("<!-- git-sim-pr-check -->")) | .id' | head -n 1 || true)
if [ -n "$existing" ]; then
gh api -X PATCH "repos/${GITHUB_REPOSITORY}/issues/comments/$existing" -F body=@git-sim-out/comment.md > /dev/null
else
gh pr comment "$NUMBER" --body-file git-sim-out/comment.md
fi