Skip to content

Improve onboarding for storing badges on an orphan branch #4

Description

@chris48s

This is probably the ideal solution for where to store the badges. Currently this is just handled as a documentation point
https://github.com/action-badges/core/blob/main/docs/github-action.md#storing-your-badges

I tried this naive approach:

async function createOrphanBranch(client, { owner, repo, branch }) {
  const SHA1_EMPTY_TREE = "4b825dc642cb6eb9a060e54bf8d69288fbee4904";
  const res = await client.request("POST /repos/{owner}/{repo}/git/commits", {
    owner,
    repo,
    message: "init orphan branch",
    tree: SHA1_EMPTY_TREE,
    parents: [],
  });
  await client.request("POST /repos/{owner}/{repo}/git/refs", {
    owner,
    repo,
    ref: `refs/heads/${branch}`,
    sha: res.data.sha,
  });
}

async function initOrphanBranch(client, { owner, repo, branch }) {
  try {
    await client.rest.repos.getBranch({ owner, repo, branch });
  } catch (e) {
    await createOrphanBranch(client, { owner, repo, branch });
  }
}

but it isn't concurrency-safe: If there are multiple workflows generating badges some of them will probably fail on the first run.
How can I do this in a concurrency-safe way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions