Skip to content
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

fix: conflict on run id #88

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pkg.pr.new uses `npm pack --json` under the hood, in case you face issues, you c

<img width="100%" src="https://github.com/stackblitz-labs/pkg.pr.new/assets/37929992/2fc03b94-ebae-4c47-a271-03a4ad5d2449" />

pkg.pr.new is not available in your local environment and it only works in workflows.
pkg.pr.new is not available in your local environment and it only works in workflows since it is not a js registry.

### Examples

Expand Down
1 change: 1 addition & 0 deletions e2e/publish.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ for (const [{ payload }, pr] of [
GITHUB_ACTOR_ID: payload.sender.id,
GITHUB_SHA: payload.workflow_job.head_sha,
GITHUB_ACTION: payload.workflow_job.id,
GITHUB_JOB: payload.workflow_job.name,
GITHUB_REF_NAME: pr
? `${pr.payload.number}/merge`
: payload.workflow_job.head_branch,
Expand Down
17 changes: 11 additions & 6 deletions packages/backend/server/routes/webhook.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const prMarkEvents: PullRequestEvent["action"][] = [
"opened",
"reopened",
"synchronize",
"enqueued",
"dequeued",
];

export default eventHandler(async (event) => {
Expand All @@ -25,12 +27,19 @@ export default eventHandler(async (event) => {
const [owner, repo] = payload.repository.full_name.split("/");

const metadata = {
url: payload.workflow_job.html_url.split("/job/")[0], // run url: (https://github.com/stackblitz-labs/pkg.pr.new/actions/runs/8390507718)/job/23004786296
owner,
repo,
job: payload.workflow_job.name,
runId: payload.workflow_job.run_id,
attempt: payload.workflow_job.run_attempt,
actor: payload.sender.id,
};
const hashKey = hash(metadata);
if (payload.action === "queued") {

if (payload.action === "completed") {
// Publishing is not available anymore
await workflowsBucket.removeItem(hashKey);
} else {
const prData: PullRequestData = {
owner,
repo,
Expand All @@ -52,9 +61,6 @@ export default eventHandler(async (event) => {

// Publishing is only available throughout the lifetime of a workflow_job
await workflowsBucket.setItem(hashKey, data);
} else if (payload.action === "completed") {
// Publishing is not available anymore
await workflowsBucket.removeItem(hashKey);
}
};

Expand All @@ -71,7 +77,6 @@ export default eventHandler(async (event) => {
const prDataHash = hash(key);
if (prMarkEvents.includes(payload.action)) {
await pullRequestNumbersBucket.setItem(prDataHash, payload.number);
// TODO: send comment here if the workflow was run before (for in repo pull requests)
} else if (payload.action === "closed") {
await pullRequestNumbersBucket.removeItem(prDataHash);

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/environments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ declare global {
GITHUB_EVENT_PATH: string;
// A unique number for each workflow run within a repository. This number does not change if you re-run the workflow run. For example, 1658821493.
GITHUB_RUN_ID: string;
// The job_id of the current job. For example, greeting_job.
GITHUB_JOB: string;
// A unique number for each attempt of a particular workflow run in a repository. This number begins at 1 for the workflow run's first attempt, and increments with each re-run. For example, 3.
GITHUB_RUN_ATTEMPT: string;
}
Expand Down
18 changes: 11 additions & 7 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineCommand, runMain, parseArgs } from "citty";
import { defineCommand, runMain } from "citty";
import assert from "node:assert";
import path from "path";
import ezSpawn from "@jsdevtools/ez-spawn";
Expand Down Expand Up @@ -71,28 +71,32 @@ const main = defineCommand({
);
process.exit(1);
}
const octokit = new Octokit();

new Octokit(); // gh authentication

const {
GITHUB_SERVER_URL,
GITHUB_REPOSITORY,
GITHUB_RUN_ID,
GITHUB_RUN_ATTEMPT,
GITHUB_ACTOR_ID,
GITHUB_JOB
} = process.env;

const [owner, repo] = GITHUB_REPOSITORY.split("/");

// Note: If you need to use a workflow run's URL from within a job, you can combine these variables: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID
const url = `${GITHUB_SERVER_URL}/${owner}/${repo}/actions/runs/${GITHUB_RUN_ID}`;

const metadata = {
url,
owner,
repo,
job: GITHUB_JOB,
runId: Number(GITHUB_RUN_ID),
attempt: Number(GITHUB_RUN_ATTEMPT),
actor: Number(GITHUB_ACTOR_ID),
};

console.log('metadata', metadata)
console.log(process.env)
const key = hash(metadata);
console.log('key', key)

const checkResponse = await fetch(new URL("/check", API_URL), {
method: "POST",
Expand Down