Skip to content

Commit

Permalink
Merge pull request #639 from AurorNZ/FixingSha
Browse files Browse the repository at this point in the history
Fixing a bug where sha cannot be resolved for pull requests
  • Loading branch information
Obi-Dann committed May 3, 2023
2 parents 4dbec63 + aa7addd commit 18d54d9
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 58 deletions.
8 changes: 0 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ jobs:
- uses: actions/checkout@v3
- uses: ./

dump-event:
runs-on: ubuntu-latest
steps:
- uses: actions/upload-artifact@v3
with:
name: github-context
path: ${{github.event_path}}

pr-build-test:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'pull_request' }}
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ typings/
# dotenv environment variables file
.env
.env.test
event.json
test-context.json

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down
45 changes: 27 additions & 18 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/src/main.ts",
"runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only", "-r", "dotenv/config"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": ["<node_internals>/**"],
"env": {
"TRY_USE_TEST_CONTEXT": "true"
},
"program": "${workspaceFolder}/src/main.ts",
"runtimeArgs": [
"--nolazy",
"-r",
"ts-node/register/transpile-only",
"-r",
"dotenv/config"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,13 @@ Your action is now published! :rocket:

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


## Debugging

- Create a pull request, ideally with commits that update dependencies
- 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.
- Put the `event.json` file into the root of this repository
- Once the test run is finished, rerun it with debuggin enabled
- 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
- Add the following code to `.env` file
```
GITHUB_EVENT_NAME=pull_request
GITHUB_EVENT_PATH=event.json
INPUT_TOKEN=<YOUR_GITHUB_TOKEN>
```
- Hit F5 in VSCode to start debugging
- Hit F5 in VSCode to start debugging
47 changes: 37 additions & 10 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions src/getRenovateConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export async function getRenovateConfig({
...process.env,
GITHUB_COM_TOKEN: token,
// this might prevent renovate from making changes to the repository
RENOVATE_DRY_RUN: 'true',
RENOVATE_DRY_RUN: 'lookup',
// this prevents renovate from complaining that the onboarding branch does not exist
RENOVATE_REQUIRE_CONFIG: 'ignored',
// this prevents renovate from creating the onboarding branch
Expand All @@ -43,6 +43,9 @@ export async function getRenovateConfig({
globalConfig.separateMajorMinor = false

let config = await globalInitialize(globalConfig)

GlobalConfig.set(config)

config = await getRepositoryConfig(config, `${owner}/${repo}`)

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

const git = simpleGitLib(config.localDir)

GlobalConfig.set(config)

const git = simpleGitLib(config.localDir)

// otherwise initRepo fails
if (githubWorkspacePath) {
await git.fetch(['--depth=1'])
Expand Down
17 changes: 6 additions & 11 deletions src/getRunContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@ export function getRunContext(): RunContext {
case 'pull_request': {
const pullRequestPayload = context.payload

const {pull_request} = pullRequestPayload
if (!pull_request) {
throw new Error(
'Github event is malformed, expected pull_request context on pull_request event'
)
}

const {number: pullRequestNumber, merge_commit_sha} = pull_request
const {
pull_request: {number: pullRequestNumber} = {}
} = pullRequestPayload

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

return {
baseRef: pushPayload.before,
headRef: pushPayload.after,
headRef: context.sha,
repo
}
}
Expand Down
32 changes: 31 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './setupEnvToOverrideDefaultRenovateLogger'
import './setupLogger'
import * as core from '@actions/core'
import {getOctokit} from '@actions/github'
import {context, getOctokit} from '@actions/github'
import {extractAllDependencies} from 'renovate/dist/workers/repository/extract'
import {fetchUpdates} from 'renovate/dist/workers/repository/process/fetch'
import {fetchChangelogs} from './fetchChangelogs'
Expand All @@ -10,6 +10,7 @@ import {getRenovateConfig} from './getRenovateConfig'
import {getUpdatedDependencies} from './getUpdatedDependencies'
import {ensurePrCommentRemoved, upsertPrComment} from './updatePrComment'
import {getRunContext} from './getRunContext'
import fs from 'fs'

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

async function run(): Promise<void> {
try {
if (process.env.TRY_USE_TEST_CONTEXT) {
useContextForTesting()
}

if (core.isDebug()) {
outputGithubContextForTesting()
}

const {baseRef, headRef, pullRequestNumber, repo} = getRunContext()

const token = core.getInput('token')
Expand Down Expand Up @@ -113,4 +122,25 @@ async function run(): Promise<void> {
}
}

function outputGithubContextForTesting(): void {
core.debug('ReportUpdatedDependencies context that can be used for testing:')
core.debug(JSON.stringify(context, null, 2))
}

function useContextForTesting(): void {
try {
const content = fs.readFileSync('./test-context.json', {encoding: 'utf8'})
const json = JSON.parse(content)
for (const propName in context) {
if (Object.hasOwn(json, propName)) {
// @ts-expect-error typescript does not like it :)
context[propName] = json[propName]
}
}
core.info('Successfully load test-context.json')
} catch (ex) {
core.error('Failed to find or read test-context.json')
}
}

run()

0 comments on commit 18d54d9

Please sign in to comment.