Skip to content

Commit 18d54d9

Browse files
authored
Merge pull request #639 from AurorNZ/FixingSha
Fixing a bug where sha cannot be resolved for pull requests
2 parents 4dbec63 + aa7addd commit 18d54d9

File tree

8 files changed

+112
-58
lines changed

8 files changed

+112
-58
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ jobs:
2424
- uses: actions/checkout@v3
2525
- uses: ./
2626

27-
dump-event:
28-
runs-on: ubuntu-latest
29-
steps:
30-
- uses: actions/upload-artifact@v3
31-
with:
32-
name: github-context
33-
path: ${{github.event_path}}
34-
3527
pr-build-test:
3628
runs-on: ubuntu-latest
3729
if: ${{ github.event_name == 'pull_request' }}

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typings/
6868
# dotenv environment variables file
6969
.env
7070
.env.test
71-
event.json
71+
test-context.json
7272

7373
# parcel-bundler cache (https://parceljs.org/)
7474
.cache

.vscode/launch.json

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
"version": "0.2.0",
6-
"configurations": [
7-
{
8-
"type": "pwa-node",
9-
"request": "launch",
10-
"name": "Launch Program",
11-
"skipFiles": ["<node_internals>/**"],
12-
"program": "${workspaceFolder}/src/main.ts",
13-
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only", "-r", "dotenv/config"],
14-
"console": "integratedTerminal",
15-
"internalConsoleOptions": "neverOpen"
16-
}
17-
]
18-
}
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "pwa-node",
9+
"request": "launch",
10+
"name": "Launch Program",
11+
"skipFiles": ["<node_internals>/**"],
12+
"env": {
13+
"TRY_USE_TEST_CONTEXT": "true"
14+
},
15+
"program": "${workspaceFolder}/src/main.ts",
16+
"runtimeArgs": [
17+
"--nolazy",
18+
"-r",
19+
"ts-node/register/transpile-only",
20+
"-r",
21+
"dotenv/config"
22+
],
23+
"console": "integratedTerminal",
24+
"internalConsoleOptions": "neverOpen"
25+
}
26+
]
27+
}

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,13 @@ Your action is now published! :rocket:
5959

6060
See the [versioning documentation](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)
6161

62-
6362
## Debugging
63+
6464
- Create a pull request, ideally with commits that update dependencies
65-
- Download the `github-context` artifact from Github Actions run for the PR. That artifact should have the JSON file with the context of the Github event.
66-
- Put the `event.json` file into the root of this repository
65+
- Once the test run is finished, rerun it with debuggin enabled
66+
- Take the JSON context from debug logs (after `ReportUpdatedDependencies context that can be used for testing:`) and save it as test-context.json into the root of this repository
6767
- Add the following code to `.env` file
6868
```
69-
GITHUB_EVENT_NAME=pull_request
70-
GITHUB_EVENT_PATH=event.json
7169
INPUT_TOKEN=<YOUR_GITHUB_TOKEN>
7270
```
73-
- Hit F5 in VSCode to start debugging
71+
- Hit F5 in VSCode to start debugging

dist/index.js

Lines changed: 37 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/getRenovateConfig.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export async function getRenovateConfig({
2222
...process.env,
2323
GITHUB_COM_TOKEN: token,
2424
// this might prevent renovate from making changes to the repository
25-
RENOVATE_DRY_RUN: 'true',
25+
RENOVATE_DRY_RUN: 'lookup',
2626
// this prevents renovate from complaining that the onboarding branch does not exist
2727
RENOVATE_REQUIRE_CONFIG: 'ignored',
2828
// this prevents renovate from creating the onboarding branch
@@ -43,6 +43,9 @@ export async function getRenovateConfig({
4343
globalConfig.separateMajorMinor = false
4444

4545
let config = await globalInitialize(globalConfig)
46+
47+
GlobalConfig.set(config)
48+
4649
config = await getRepositoryConfig(config, `${owner}/${repo}`)
4750

4851
let githubWorkspacePath = process.env['GITHUB_WORKSPACE']
@@ -60,10 +63,10 @@ export async function getRenovateConfig({
6063
config.localDir = repositoryPath
6164
}
6265

63-
const git = simpleGitLib(config.localDir)
64-
6566
GlobalConfig.set(config)
6667

68+
const git = simpleGitLib(config.localDir)
69+
6770
// otherwise initRepo fails
6871
if (githubWorkspacePath) {
6972
await git.fetch(['--depth=1'])

src/getRunContext.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,15 @@ export function getRunContext(): RunContext {
1616
case 'pull_request': {
1717
const pullRequestPayload = context.payload
1818

19-
const {pull_request} = pullRequestPayload
20-
if (!pull_request) {
21-
throw new Error(
22-
'Github event is malformed, expected pull_request context on pull_request event'
23-
)
24-
}
25-
26-
const {number: pullRequestNumber, merge_commit_sha} = pull_request
19+
const {
20+
pull_request: {number: pullRequestNumber} = {}
21+
} = pullRequestPayload
2722

2823
return {
2924
// github actions usually checkout merge commits when PR is merge into the target branch
3025
// `${context.sha}^` will get the first parent commit ßwhich will me the head of target branch for this PR
31-
baseRef: `${merge_commit_sha}^`,
32-
headRef: merge_commit_sha,
26+
baseRef: `${context.sha}^`,
27+
headRef: context.sha,
3328
pullRequestNumber,
3429
repo
3530
}
@@ -40,7 +35,7 @@ export function getRunContext(): RunContext {
4035

4136
return {
4237
baseRef: pushPayload.before,
43-
headRef: pushPayload.after,
38+
headRef: context.sha,
4439
repo
4540
}
4641
}

src/main.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import './setupEnvToOverrideDefaultRenovateLogger'
22
import './setupLogger'
33
import * as core from '@actions/core'
4-
import {getOctokit} from '@actions/github'
4+
import {context, getOctokit} from '@actions/github'
55
import {extractAllDependencies} from 'renovate/dist/workers/repository/extract'
66
import {fetchUpdates} from 'renovate/dist/workers/repository/process/fetch'
77
import {fetchChangelogs} from './fetchChangelogs'
@@ -10,6 +10,7 @@ import {getRenovateConfig} from './getRenovateConfig'
1010
import {getUpdatedDependencies} from './getUpdatedDependencies'
1111
import {ensurePrCommentRemoved, upsertPrComment} from './updatePrComment'
1212
import {getRunContext} from './getRunContext'
13+
import fs from 'fs'
1314

1415
// Github actions can only run on Node 16
1516
// but renovate requires Node 18
@@ -24,6 +25,14 @@ if (!('structuredClone' in globalThis)) {
2425

2526
async function run(): Promise<void> {
2627
try {
28+
if (process.env.TRY_USE_TEST_CONTEXT) {
29+
useContextForTesting()
30+
}
31+
32+
if (core.isDebug()) {
33+
outputGithubContextForTesting()
34+
}
35+
2736
const {baseRef, headRef, pullRequestNumber, repo} = getRunContext()
2837

2938
const token = core.getInput('token')
@@ -113,4 +122,25 @@ async function run(): Promise<void> {
113122
}
114123
}
115124

125+
function outputGithubContextForTesting(): void {
126+
core.debug('ReportUpdatedDependencies context that can be used for testing:')
127+
core.debug(JSON.stringify(context, null, 2))
128+
}
129+
130+
function useContextForTesting(): void {
131+
try {
132+
const content = fs.readFileSync('./test-context.json', {encoding: 'utf8'})
133+
const json = JSON.parse(content)
134+
for (const propName in context) {
135+
if (Object.hasOwn(json, propName)) {
136+
// @ts-expect-error typescript does not like it :)
137+
context[propName] = json[propName]
138+
}
139+
}
140+
core.info('Successfully load test-context.json')
141+
} catch (ex) {
142+
core.error('Failed to find or read test-context.json')
143+
}
144+
}
145+
116146
run()

0 commit comments

Comments
 (0)