-
Notifications
You must be signed in to change notification settings - Fork 77
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
Handle multiple types for advanced scenarios #143
Open
mauriziovitale
wants to merge
2
commits into
nrwl:main
Choose a base branch
from
mauriziovitale:feature/142-leverage-multiple-event-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
|
@@ -30,7 +30,7 @@ let BASE_SHA: string; | |
} else { | ||
process.stdout.write("\n"); | ||
process.stdout.write( | ||
`WARNING: Working directory '${workingDirectory}' doesn't exist.\n`, | ||
`WARNING: Working directory '${workingDirectory}' doesn't exist.\n` | ||
); | ||
} | ||
} | ||
|
@@ -50,7 +50,7 @@ let BASE_SHA: string; | |
const baseResult = spawnSync( | ||
"git", | ||
["merge-base", `origin/${mainBranchName}`, mergeBaseRef], | ||
{ encoding: "utf-8" }, | ||
{ encoding: "utf-8" } | ||
); | ||
BASE_SHA = baseResult.stdout; | ||
} catch (e) { | ||
|
@@ -65,7 +65,7 @@ let BASE_SHA: string; | |
owner, | ||
repo, | ||
mainBranchName, | ||
lastSuccessfulEvent, | ||
lastSuccessfulEvent | ||
); | ||
} catch (e) { | ||
core.setFailed(e.message); | ||
|
@@ -79,29 +79,36 @@ let BASE_SHA: string; | |
} else { | ||
process.stdout.write("\n"); | ||
process.stdout.write( | ||
`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n`, | ||
`WARNING: Unable to find a successful workflow run on 'origin/${mainBranchName}', or the latest successful workflow was connected to a commit which no longer exists on that branch (e.g. if that branch was rebased)\n` | ||
); | ||
process.stdout.write( | ||
`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n` | ||
); | ||
process.stdout.write("\n"); | ||
process.stdout.write( | ||
`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n` | ||
); | ||
if (fallbackSHA) { | ||
BASE_SHA = fallbackSHA; | ||
process.stdout.write(`Using provided fallback SHA: ${fallbackSHA}\n`); | ||
} else { | ||
process.stdout.write( | ||
`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n`, | ||
`We are therefore defaulting to use HEAD~1 on 'origin/${mainBranchName}'\n` | ||
); | ||
process.stdout.write("\n"); | ||
process.stdout.write( | ||
`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n`, | ||
`NOTE: You can instead make this a hard error by setting 'error-on-no-successful-workflow' on the action in your workflow.\n` | ||
); | ||
process.stdout.write("\n"); | ||
|
||
const commitCountOutput = spawnSync( | ||
"git", | ||
["rev-list", "--count", `origin/${mainBranchName}`], | ||
{ encoding: "utf-8" }, | ||
{ encoding: "utf-8" } | ||
).stdout; | ||
const commitCount = parseInt( | ||
stripNewLineEndings(commitCountOutput), | ||
10, | ||
10 | ||
); | ||
|
||
const LAST_COMMIT_CMD = `origin/${mainBranchName}${ | ||
|
@@ -117,7 +124,7 @@ let BASE_SHA: string; | |
} else { | ||
process.stdout.write("\n"); | ||
process.stdout.write( | ||
`Found the last successful workflow run on 'origin/${mainBranchName}'\n`, | ||
`Found the last successful workflow run on 'origin/${mainBranchName}'\n` | ||
); | ||
process.stdout.write(`Commit: ${BASE_SHA}\n`); | ||
} | ||
|
@@ -154,7 +161,7 @@ async function findSuccessfulCommit( | |
owner: string, | ||
repo: string, | ||
branch: string, | ||
lastSuccessfulEvent: string, | ||
lastSuccessfulEvent: string | ||
): Promise<string | undefined> { | ||
const octokit = new ProxifiedClient(); | ||
if (!workflow_id) { | ||
|
@@ -168,30 +175,61 @@ async function findSuccessfulCommit( | |
.then(({ data: { workflow_id } }) => workflow_id); | ||
process.stdout.write("\n"); | ||
process.stdout.write( | ||
`Workflow Id not provided. Using workflow '${workflow_id}'\n`, | ||
`Workflow Id not provided. Using workflow '${workflow_id}'\n` | ||
); | ||
} | ||
// fetch all workflow runs on a given repo/branch/workflow with push and success | ||
const shas = await octokit | ||
.request( | ||
`GET /repos/${owner}/${repo}/actions/workflows/${workflow_id}/runs`, | ||
{ | ||
owner, | ||
repo, | ||
// on some workflow runs we do not have branch property | ||
branch: | ||
lastSuccessfulEvent === "push" || | ||
lastSuccessfulEvent === "workflow_dispatch" | ||
? branch | ||
: undefined, | ||
workflow_id, | ||
event: lastSuccessfulEvent, | ||
status: "success", | ||
}, | ||
) | ||
.then(({ data: { workflow_runs } }) => | ||
workflow_runs.map((run: { head_sha: any }) => run.head_sha), | ||
); | ||
let shas = []; | ||
// if there are several events separated by comma | ||
if (lastSuccessfulEvent.includes(",")) { | ||
// find the greatest workflow id among the types and retrieve the sha | ||
const events = lastSuccessfulEvent.split(","); | ||
const workflowIdMap = new Map(); | ||
for (const event of events) { | ||
const workflowRun = await octokit | ||
.request( | ||
`GET /repos/${owner}/${repo}/actions/workflows/${workflow_id}/runs`, | ||
{ | ||
owner, | ||
repo, | ||
// on non-push workflow runs we do not have branch property | ||
branch: | ||
lastSuccessfulEvent === "push" || | ||
lastSuccessfulEvent === "workflow_dispatch" | ||
? branch | ||
: undefined, | ||
event, | ||
workflow_id, | ||
} | ||
) | ||
.then(({ data }) => { | ||
return data.workflow_runs[0]; | ||
}); | ||
workflowIdMap.set(workflowRun.id, workflowRun.head_sha); | ||
} | ||
shas.push(workflowIdMap.get(Math.max(...workflowIdMap.keys()))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it will push all the SHAs from one event, then all of them from another. So they won't be in order in the final array. Which means the call to |
||
} else { | ||
// fetch all workflow runs on a given repo/branch/workflow with push and success | ||
shas = await octokit | ||
.request( | ||
`GET /repos/${owner}/${repo}/actions/workflows/${workflow_id}/runs`, | ||
{ | ||
owner, | ||
repo, | ||
// on non-push workflow runs we do not have branch property | ||
branch: | ||
lastSuccessfulEvent === "push" || | ||
lastSuccessfulEvent === "workflow_dispatch" | ||
? branch | ||
: undefined, | ||
workflow_id, | ||
event: lastSuccessfulEvent, | ||
status: "success", | ||
} | ||
) | ||
.then(({ data: { workflow_runs } }) => | ||
workflow_runs.map((run: { head_sha: any }) => run.head_sha) | ||
); | ||
} | ||
|
||
return await findExistingCommit(octokit, branch, shas); | ||
} | ||
|
@@ -208,7 +246,7 @@ async function findMergeBaseRef(): Promise<string> { | |
function findMergeQueuePr(): string { | ||
const { head_ref, base_sha } = github.context.payload.merge_group; | ||
const result = new RegExp( | ||
`^refs/heads/gh-readonly-queue/${mainBranchName}/pr-(\\d+)-${base_sha}$`, | ||
`^refs/heads/gh-readonly-queue/${mainBranchName}/pr-(\\d+)-${base_sha}$` | ||
).exec(head_ref); | ||
return result ? result.at(1) : undefined; | ||
} | ||
|
@@ -223,7 +261,7 @@ async function findMergeQueueBranch(): Promise<string> { | |
const octokit = new ProxifiedClient(); | ||
const result = await octokit.request( | ||
`GET /repos/${owner}/${repo}/pulls/${pull_number}`, | ||
{ owner, repo, pull_number: +pull_number }, | ||
{ owner, repo, pull_number: +pull_number } | ||
); | ||
return result.data.head.ref; | ||
} | ||
|
@@ -234,7 +272,7 @@ async function findMergeQueueBranch(): Promise<string> { | |
async function findExistingCommit( | ||
octokit: Octokit, | ||
branchName: string, | ||
shas: string[], | ||
shas: string[] | ||
): Promise<string | undefined> { | ||
for (const commitSha of shas) { | ||
if (await commitExists(octokit, branchName, commitSha)) { | ||
|
@@ -250,7 +288,7 @@ async function findExistingCommit( | |
async function commitExists( | ||
octokit: Octokit, | ||
branchName: string, | ||
commitSha: string, | ||
commitSha: string | ||
): Promise<boolean> { | ||
try { | ||
spawnSync("git", ["cat-file", "-e", commitSha], { | ||
|
@@ -273,7 +311,7 @@ async function commitExists( | |
}); | ||
|
||
return commits.data.some( | ||
(commit: { sha: string }) => commit.sha === commitSha, | ||
(commit: { sha: string }) => commit.sha === commitSha | ||
); | ||
} catch { | ||
return false; | ||
|
Oops, something went wrong.
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.
This
if
condition and theelse
code path below seems unnecessary. A string with no commas, e.g."foo"
, willsplit(",")
to["foo"]
and the iteration will work fine over that single element.