-
Notifications
You must be signed in to change notification settings - Fork 0
chore: add Action for generating diff tour links #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+162
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da88b9a
chore: add logic for generating diff tours
buenos-nachos 08398f0
refactor: recreate local types
buenos-nachos 666fc4e
docs: update comments
buenos-nachos 18db8e3
revert: un-JavaScript the codebase
buenos-nachos ed44ac6
diff-tour: document closed trigger and fork/merge guard in usage
marcleblanc2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # `diff-tour` | ||
|
|
||
| Generates a link to the Diff Tour for a PR and posts it as a PR comment. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```yaml | ||
| on: | ||
| pull_request: | ||
| # `closed` fires on merge, so the comment is updated to link the merge commit | ||
| types: [opened, synchronize, reopened, closed] | ||
|
|
||
| permissions: | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| diff-tour: | ||
| runs-on: ubuntu-latest | ||
| # Diff Tour resolves branch names against the base repo, so skip fork PRs. | ||
| # Closed PRs only need an update when they were merged. | ||
| if: > | ||
| github.event.pull_request.head.repo.full_name == github.repository | ||
| && (github.event.action != 'closed' || github.event.pull_request.merged) | ||
| steps: | ||
| - uses: sourcegraph/actions/diff-tour@main | ||
| ``` | ||
|
|
||
| ## Inputs | ||
|
|
||
| This action has no inputs. | ||
|
|
||
| ## What it does | ||
|
|
||
| 1. Builds a Diff Tour URL for the PR — a commit link if the PR is merged, or a branch compare link if it's still open | ||
| 2. Creates a PR comment with the link, or updates the existing one if it has already posted a comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| name: Diff Tour link | ||
| description: Generate a link to the Diff Tour based on a PR. | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | ||
| with: | ||
| script: | | ||
| const script = require(`${{ github.action_path }}/generateTourLink.cjs`) | ||
| const result = await script({ github, context }) | ||
| console.log(result) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /** | ||
| * @file This file is written in plain JavaScript to remove an extra build step, but we should use the @ts-check | ||
| * directive and JSDoc comments to make sure the logic is still type-safe. | ||
| * | ||
| * @todo 2026-09-10 - This file recreates local versions of the `AsyncFunctionArguments` type from | ||
| * `@actions/github-script`.so that we can keep this file fully self-contained and type-safe without having to bring in | ||
| * typical JS/TS tooling. If this repo gets to the point where it needs multiple JS scripts, consider ripping out these | ||
| * local types in favor of bringing in the package straight from GitHub. | ||
| */ | ||
| //@ts-check | ||
|
|
||
| /** @type {{log: (...args: unknown[]) => void}} */ | ||
| const console = /** @type {any} */ (globalThis).console | ||
|
|
||
| /** | ||
| * @typedef {object} PullRequest | ||
| * @property {number} number | ||
| * @property {boolean} merged | ||
| * @property {string} merge_commit_sha | ||
| * @property {{ref: string}} base | ||
| * @property {{ref: string}} head | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {object} Context | ||
| * @property {{pull_request?: PullRequest}} payload | ||
| * @property {{owner: string, repo: string}} repo | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {object} Comment | ||
| * @property {number} id | ||
| * @property {string | null | undefined} body | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {object} Github | ||
| * @property {object} rest | ||
| * @property {object} rest.issues | ||
| * @property {(params: {owner: string, repo: string, issue_number: number}) => Promise<{data: Comment[]}>} rest.issues.listComments | ||
| * @property {(params: {owner: string, repo: string, comment_id: number, body: string}) => Promise<unknown>} rest.issues.updateComment | ||
| * @property {(params: {owner: string, repo: string, issue_number: number, body: string}) => Promise<unknown>} rest.issues.createComment | ||
| */ | ||
|
|
||
| /** | ||
| * @typedef {object} GenerateTourLinkArgs | ||
| * @property {Github} github | ||
| * @property {Context} context | ||
| */ | ||
|
|
||
| const instance = "https://sourcegraph.sourcegraph.com" | ||
| const diffTourCommentMarker = "<!-- difftour-link -->" | ||
|
|
||
| /** @param {GenerateTourLinkArgs} args */ | ||
| async function generateTourLink({ github, context }) { | ||
| const pullRequest = context.payload.pull_request | ||
| if (pullRequest === undefined) { | ||
| return | ||
| } | ||
|
|
||
| /** @type {string} */ | ||
| let url | ||
| const { repo, owner } = context.repo | ||
| const repoLink = `${owner}/${repo}` | ||
| if (pullRequest.merged) { | ||
| const commit = encodeURIComponent(pullRequest.merge_commit_sha) | ||
| url = `${instance}/r/${repoLink}/-/commit/${commit}?mode=Tour` | ||
| } else { | ||
| const base = encodeURIComponent(pullRequest.base.ref) | ||
| const head = encodeURIComponent(pullRequest.head.ref) | ||
| url = `${instance}/r/${repoLink}/-/compare/${base}...${head}?mode=Tour` | ||
| } | ||
|
|
||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner, | ||
| repo, | ||
| issue_number: pullRequest.number, | ||
| }) | ||
|
|
||
| const markedComments = comments.filter( | ||
| (c) => c.body?.includes(diffTourCommentMarker) ?? false, | ||
| ) | ||
| if (markedComments.length > 1) { | ||
| console.log( | ||
| "Found multiple comments with the Diff Tour marker. Updating only the first", | ||
| ) | ||
| } | ||
|
|
||
| const commentContent = `${diffTourCommentMarker}\n[Open the Diff Tour for this PR in Sourcegraph](${url})` | ||
| const first = markedComments[0] | ||
| if (first !== undefined) { | ||
| await github.rest.issues.updateComment({ | ||
| owner, | ||
| repo, | ||
| comment_id: first.id, | ||
| body: commentContent, | ||
| }) | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner, | ||
| repo, | ||
| issue_number: pullRequest.number, | ||
| body: commentContent, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| module.exports = generateTourLink |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should rather just have the contents of this script right here. There is a lot of fluff around this which I don't think we quite need yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you be okay with keeping the separate
.jsfile but getting rid of everything else in the project?I can totally see everything else being too much ceremony when we're only adding a single new action, but I think there's still benefits to having the logic in a separate file, at least for local dev. My worry is that if everything's inlined, we'll basically be recreating the problem with Bash scripts, just in a different language
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an explicit file would be fine yeah!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. Just ripped out the other stuff