Open Factory Flooropen source

Guides

The Open Factory Floor API

On this page

How a factory describes itself, its state and its history, as plain data. The app is built on it and on nothing else, and so can anything else be: a script, an agent, another page.

Version 1, served by python3 -m factory.app on your own machine (http://127.0.0.1:8787 unless you say otherwise). Every answer is JSON. It only reads, unless the server was started to allow deciding (Deciding): then it has five requests that write.

#The three questions

QuestionRequestAnswered from
What are you?GET /api/v1/projects/OWNER/REPO/describeThe factory file alone. The same until the file changes
What are you doing now?GET /api/v1/projects/OWNER/REPO/stateGitHub, as the status report reads it. Kept for five minutes; ?fresh=1 reads again, paying only for what changed (how)
What has happened?GET /api/v1/projects/OWNER/REPO/historyThe event record of the same reading, as steps a picture can replay
What did it cost?GET /api/v1/projects/OWNER/REPO/costGitHub, as the cost report reads it. Worked out only for this request, one at a time, and kept for an hour (Cost)

And two about the server itself:

Request
Who is asking, and what may they do?GET /api/v1/me
Which projects do you answer for?GET /api/v1/projects
What may I do on this project?GET /api/v1/projects/OWNER/REPO/access

#Describe

The factory's shape. version is 1; a reader should check it before drawing anything.

KeyWhat it is
project, factory, branchWhich repository, the name of the group it belongs to, and the branch it releases from
mergeHow the app merges a change for a person, when the factory file says: commit, squash or rebase. Null when it does not say
front_doorThe intake stations in order. Each has id and kind: checks (with requires, the check names), review (with standard, automatic_reviewer, automatic_passes, judged_by_the_repository) or other
risk_areasEach named area of risk and why it matters
linesEach release line: id, feeds, workflow, watch, has_gate, unattended, and stations in order. A station is {id, kind: station} or {id: gate, kind: gate, gate}
a gateunattended (may it release without a person), holds_when (each rule: when, plain text, and paths or kind), always_holds_when (what holds whatever the settings say), not_in_release
hoppersEach hopper: id, label, pick_up (person or agent), takes_leftovers
bin, builderThe bin's settings and the builder's, or null
lines_error, hoppers_errorWhen that part of the factory file cannot be used: why, in Open Factory Floor's own words. The rest is still described
told_when_a_person_is_neededThe accounts named for a hold

The same answer on its own: python3 -m factory.describe.

#State

Everything the status report says, as data (python3 -m factory.status --json gives the same), plus stands: the words for each place an open change can stand, in order.

KeyWhat it is
as_of, daysWhen it was read, and how far back "lately" goes
openEach open change: number, title, url, stands, waited, tests, tests_failed, sent_back, by, in_short (the review's summary), draft, item (the request the builder built it for), clashes (true: it has passed but clashes with what was merged since, so GitHub cannot merge it; false: GitHub can; null: not at the merge point, or GitHub has not worked it out)
accepted, closed, summaryWhat merged or closed lately, and the counts
linesFor each line that names its workflow: now, last_released, recent, stopped_by, and counts. Null when the releases could not be read
hoppersWhat waits in each hopper, what is in the bin and why, and how long requests take. Each waiting request says whether work on it may start by itself (start, why), and builder.next names the one the builder would take. On a hosted server, somebody who may only read the project is not given those three for a hopper an agent may start from: GitHub tells them only to people who may write, and the reading may be a writer's
githubWhat this reading cost: last_reading (asked, free, paid) and allowance (limit, remaining, resets) as GitHub gave it when this reading of this project ended (it is kept with the reading and handed out again with it, so it is as old as as_of, and is not what is left now), or null when not known. A reading somebody else made, handed to you on a hosted server, has no github

#Who is looking

/projects lists the projects: {"projects": [{"id": "OWNER/REPO"}, …]}. On a server people sign in to, it lists only the projects the person asking is let in to, each with what they may do there: standing (admin, write or read), archived, and may (as in /access, below). It is never empty on a guess. When GitHub says no to the person on every project, their sign-in is ended: this request, or the one about their last project, answers 401 with sign_in: true, ended: "no_access" and error in words, the session cookie is cleared, and their tokens are forgotten. When none can be listed because GitHub could not be asked about some, it answers 503 with error in words, and nobody is signed out. Every other request about a project they are not let in to answers 404 with the same body as a project the server has never heard of.

/me says who is asking: login and name as GitHub gives them, where (local), and may (what they may do through this API: look, and decide when the server allows it and GitHub lets them write). On your own machine you are whoever the GitHub access belongs to (gh auth login, or the token given); GitHub is asked once, and again after five minutes if it would not say.

/projects/OWNER/REPO/access says what GitHub lets that person do on the project: standing is admin, write, read or none, from the repository's own permissions. could_decide is true for admin and write on a project that is not archived. archived says whether it is: then nobody can change it, could_decide is false whatever the standing, and may never carries decide. merge_kind is how a merge from here would be made, in GitHub's word (merge, squash or rebase): the kind the factory file asks for with merge:, or, when it asks for none, a merge commit where the project allows one and otherwise the kind it does allow. may carries decide only when the server was started to allow it; otherwise this says what to expect. When the app acts, it acts as the person, so GitHub enforces the standing itself and its record names them; could_decide is a courtesy, not the lock.

On a server whose copies share a store, state also carries serial: the number the store gave the reading as it was begun. A later reading has a larger one, whichever copy made it and whatever its clock said.

On a server that leaves its readings to be done later (a floor on AWS Lambda), state and history never wait for a reading of GitHub. Where there is an older reading it is given at once with reading: true, as on any server. Where there is none at all (a floor nobody has opened before) the answer is 503 with error in words and reading: true: a reading is being made, and asking again shortly will have it. "Read again" (state?fresh=1) answers the same two ways, and cost answers working: true at once.

Either answer that says reading: true may also carry progress, once a run is at work on the reading: {"stage": "changes", "done": 12, "total": 31, "age": 3, "stale_after": 40}. stage is changes (with how many of how many have been read), lines or hoppers; age is how many seconds ago that was written, and past stale_after it is no longer said, since whoever was writing it has stopped. It is something to show while waiting and nothing else: there is no progress on a server that reads inside its requests, and none until the run has begun (what it is and what it costs).

#Deciding

Off unless the server was started to allow it (--decide); access.deciding says which, and access.may then carries decide for someone GitHub lets write to a project that is not archived.

RequestBody
Release what a line's gate is holdingPOST /api/v1/projects/OWNER/REPO/release{"line": "<line id>"}
Give a waiting request the go-aheadPOST /api/v1/projects/OWNER/REPO/go-ahead{"request": <number>}
Merge a change that has passed and waits for a personPOST /api/v1/projects/OWNER/REPO/merge{"change": <number>}
Send a change back to be alteredPOST /api/v1/projects/OWNER/REPO/send-back{"change": <number>, "reason": "<3 to 600 characters>"}
Reject a change to the binPOST /api/v1/projects/OWNER/REPO/bin{"change": <number>, "reason": "<3 to 600 characters>"}
Take out of the bin what a person put therePOST /api/v1/projects/OWNER/REPO/unbin{"item": <number>}

Each answers 200 with what was asked of GitHub (did, and the line and what it carries, or the request and the label), or 409 with error in words fit to show: nothing is held, the request is not waiting, the server only looks, the project is archived, the factory file asks for a kind of merge the project does not allow, it was already asked for, GitHub refused. Each is one request to GitHub with the person's access, sent once (factory/decide.py). A POST is taken only from the server's own page: the Host must be this machine, Origin must match it, and it must carry Content-Type: application/json and X-Factory: decide, which a page on another site cannot send without a permission this server never gives.

Asked once. The same thing (a line's release, a change's merge, one version's send-back, a request's go-ahead) is refused for fifteen minutes after it was asked for from here, whether or not GitHub's answer came back: the reading goes on saying "held" or "waiting" for a while after. On a hosted server that is one memory for everybody let in to the project, and decisions are taken one at a time across all of them, so two people pressing the same button get one request to GitHub and one refusal. The reading a decision is checked against is taken before its turn, so one person's slow reading does not keep another's decision waiting; if anybody's decision had its turn in between, the reading is taken again. The note in the kept history carries it over a restart.

What was decided is added to the kept history as a step (asked_release with line and carries, asked_merge with change, or go_ahead with request; by is the login). GitHub's own record is the proof; these say who in the replay.

A hosted server will fill the same two answers from a sign-in with GitHub in place of the machine's own access. Nothing else about the API changes.

#Signing in

On a server started with --sign-in, /me answers sign_in: true and signed_in, and one_click says which of the two ways the server signs people in. sign_ins_kept says whether a restart leaves people signed in (true only on a hosted server told to keep sign-ins, or one whose copies share a store). No other answer changes with it. A hosted server, whose cookie outlives the browser being closed, also says in /me what a page needs to tell people the truth about that: sign_in_lasts, the most seconds a sign-in lasts (for somebody signed in, their own session's whole length, not what is left of it; for anybody else, the length of the last session made, or the 604800 of a renewable one while nobody has signed in yet), and sign_in_survives_restart, true when the server keeps sign-ins and that sign-in's token can be renewed. A server on your own machine gives neither. Until a person has signed in every other request answers 401 with sign_in: true. A session that has ended (its token could not be renewed) answers the same 401. The request that ends one because GitHub no longer lets the person in to any project here says so: the 401 also carries ended: "no_access".

With a code (one_click: false):

RequestAnswer
BeginPOST /api/v1/sign-inuser_code, verification_uri, interval, expires_in; sets the session cookie
Has the person approved?POST /api/v1/sign-in/checkstate: waiting, signed_in, refused or expired; on a hosted server also not_allowed (GitHub does not let them in to this project) and unchecked (GitHub would not say). On a hosted server, signed_in comes with the session cookie given again, now with its lifetime (below)
EndPOST /api/v1/sign-outclears the session

These are POSTs under the same rules as deciding (the server's own page only). GitHub is asked no more often than it said. A server that signs people in with one click (one_click: true) refuses the first two: 403 with error in words, no cookie, and nothing asked of GitHub (why). The device code and the token never leave the server (factory/signin.py), with one exception: on a server whose copies share a store each goes to AWS KMS to be locked, and is kept locked in the store.

With one click (one_click: true: a hosted server given the GitHub App's client secret). These two are visits a browser makes, not requests a script sends, and they answer with a redirect or a small page, never JSON:

RequestAnswer
BeginGET /signin/start302 to https://github.com/login/oauth/authorize, with the app's client id, this server's own callback address, a state and a PKCE challenge (S256); sets __Host-factory_signin (SameSite=Lax, ten minutes), which ties the browser to the attempt. Takes nothing from its own address: a query string is ignored. Refused when another website began the visit (Sec-Fetch-Site: cross-site), with 429 for a caller who begins too often, and with 503 when the house is full, each on a page in words
GitHub sends the browser backGET /signin/callback?code=…&state=…200 with a small page that goes to /, and the session cookie, when the state is the attempt's, the attempt is this browser's and unused, GitHub swaps the code, and the door lets the person in. Otherwise a page saying why in words, with a link to begin again: 400 (not this browser's attempt, used already, older than ten minutes, or the state does not match: that last one leaves the attempt and its cookie as they were), 403 (refused on GitHub, the code would not swap, or the person is not let in), 503 (GitHub could not be asked, or would not say whether they are let in)

An attempt is good once: a callback that carries its state spends it, whatever comes of it. One without it, or with a wrong one, is refused without GitHub being asked and spends nothing, until the fifth such, which spends the attempt. Nothing from the callback's address is ever put into a page or decides where the browser goes next. The state's other half, the PKCE verifier, the client secret and the token never leave the server, and nor does the refresh token, with one exception: on a server told to keep sign-ins it goes to AWS KMS to be locked, and is kept locked beside the history. (On a server whose copies share a store, the verifier, the token and the refresh token are each locked by KMS and kept locked in the store, and a hash of the state is kept with them: never the client secret.) POST /api/v1/sign-out ends a session begun either way, and removes what was kept of it.

After a restart of a server that keeps sign-ins, the same cookie is good again, and nothing about any request changes. The first request that carries it waits while the server has the session's kept refresh token unlocked, renews it at GitHub and asks the door afresh. If any of those says no, or cannot be asked just then, the request answers 401 with sign_in: true as for anybody not signed in, and the cookie is left alone: when it was only that KMS or GitHub could not be asked, the same cookie works a little later.

A hosted server (--hosted https://NAME) answers at that address only, expects Origin: https://NAME on every POST, names its cookie __Host-factory_session and gives it, when somebody is signed in (the answer to /signin/callback, or to /sign-in/check saying signed_in), Max-Age equal to the whole seconds the session has left before it must end, so the browser keeps it across being closed and no longer than the server honours it; no other answer sets it again (not after a token is renewed, and not after a restart of a server that keeps sign-ins), and /sign-in/check asked again says the time left then, never more; says in /me whom it lets in (let_in: write or read), answers 429 to a caller who begins sign-ins too often (either way), and answers ok at /healthz. It may answer for several projects: then /projects is the list of the asker's own.

#History

What has happened, oldest first, as steps: everything the readings so far have seen, kept on the machine the server runs on. Each reading looks back seven days and adds what it finds to what earlier readings found, so the history grows longer than a week the longer the app is used, and keeps what GitHub itself forgets (a request that was in the bin and then left it).

KeyWhat it is
from, to, daysThe first step's time, when it was last read, and how far back one reading looks
stepsEach has when and step. For a change: change (its number), step one of opened, new_version, tests_passed, tests_failed, review_passed, sent_back, merged, closed, and by (@login when GitHub names the person, otherwise a person; the automatic reviewer; or null). For a release: line, step one of held, released, undone, failed, stopped (a station of the project's own stopped the run before the gate: nothing was released), carries (the changes in it), by, why, and station (where a failed or stopped release went wrong, when one job failed and could be named). For a request: request (its number), step one of arrived (with hopper), binned (with why), left (taken up or closed; with from). A change the line could not carry further has binned too
changesFor each change named in a step: its title, url, and item (the request the builder built it for)
requestsFor each request named in a step: its title and url
kept, kept_sinceWhether the steps are kept in a file between runs (true for the app on your machine), and when the first reading was added
readingTrue while the server is reading GitHub behind an older reading it has handed out (below)

One reading of GitHub answers both state and history.

Nobody waits for a reading who need not. A reading takes up to a minute. When the server has an older one (kept from before a restart, or aged past five minutes), it hands that out at once with reading: true, and reads GitHub behind it; ask again a little later and the new one is there. If that reading fails, reading_failed on state says why, and it is not tried again for five minutes. Only the first reading ever, and ?fresh=1, wait for GitHub. ?watch=1 on state starts a reading behind the scenes however young the last one is (one at a time) and hands out what there is: the page uses it for a few minutes after a decision. When the last reading behind the scenes failed less than a minute ago a watch starts nothing, with one exception: the first watch after this person decided something reads at once. left is timed by the reading that noticed it: when between two readings a request left is not known. The same goes for a request moved from a hopper into the bin, or back: seen for the first time it is timed by when it was made (all GitHub's listing says); moved since the last reading, it is timed by this reading and carries from.

#Cost

What the factory cost to run over the last seven days, and how that has moved. This is the one answer that no reading makes: it is several hundred questions to GitHub (far more on a busy project), so it is worked out only when this request asks, one at a time, and the one made is handed out again for an hour.

KeyWhat it is
reportThe cost report as python3 -m factory.cost --json gives it, for seven days, in the quick way (exact is false), with by_line and by_hopper when the factory file has lines and hoppers. Null when none has been made yet. workflows[].workflow is a file's name, somebody else's word: show it as text
age_seconds, kept_secondsHow old the report is, and how long one is handed out again (3600). Null age when there is no report
workingTrue while a report is being worked out behind the scenes. Ask again a little later: the request itself waits up to twenty seconds for it, so asking again at once is fair
failedWhy the last try at a newer report failed, in words fit to show, when there is an older report to show beside it. Otherwise null
questionsWhat the report cost to ask: asked, free, paid, or null when not known
historyA small summary of each report made, oldest first, one for a day, the last ninety: day, as_of, days, runs, machine_usd, model_usd, model_calls, outcomes (each ended, changes, runs, machine_usd, model_usd) and on_no_change. No workflow and no change is named in it. Kept with the history (History)
  • The first ask begins the work and answers working: true with report: null if it is not done within twenty seconds. A second ask within the hour gets the kept report and its age, and GitHub is asked nothing. Past the hour the next ask begins a new one, and is handed the old one, with its age, meanwhile.
  • When it cannot be worked out and there is no report to show, the answer is 503 with error in words, as for any other question. A try that failed is not made again for a minute.
  • On a hosted server the kept report is shared by everybody let in, like the shared reading: whoever asked first paid for it with their own access. It carries nothing that depends on one person's standing, and no login or allowance. Whether the asker is still let in is checked first, as for every shared answer.
  • The figures are at list price. They are not a bill (why).

#When it cannot answer

StatusMeaning
401Nobody is signed in with this request (sign_in: true). With ended: "no_access": the sign-in was ended just now, because GitHub no longer lets the person in to any project here
403You may not look at this
404Nothing at that address, or a project this server does not answer for. On a server people sign in to, a project the asker is not let in to answers this too, word for word
503A fair question that could not be answered now: GitHub refused or could not be reached, or the factory file cannot be used. error says which, in words safe to show. On a server people sign in to, this is also the answer about a project the asker has been let in to when GitHub refuses their token and could not be asked to renew it

#Built to be hosted

It runs on your own machine, or hosted for a team, for one project or several. Four rules are kept so that running it as a service for many projects adds to this and does not replace it.

  1. The app only talks to the API. The page reads no file and borrows no login from the machine it is shown on.
  2. Every request names its project. On your own machine there is one; a hosted server may have several, and nothing assumes there is only one.
  3. Who is asking is its own step. Today it is you, with the GitHub access you already have, and you may look. A hosted server puts a sign-in there and gives each person what they may do. Every route asks it.
  4. Anything kept is kept behind one interface. Today that is memory for five minutes (an hour for the cost report), a file of what GitHub said, and a file of the history (factory/history.py), which holds the short history of the cost too. A hosted server told to keep sign-ins keeps them in the same place, in an object of their own (factory/stay.py). A hosted server given a table keeps everything its copies must share in that table and in the kept place, so that several copies answer as one (factory/store.py, factory/shared.py, Several copies, one store).

On a server whose copies share a store, no request and no answer changes, and any copy may be sent any request. Three things are worth knowing. What the server would otherwise do behind the scenes (reading GitHub behind an older reading, working the cost out) is left as a job for whichever copy next has the chance, so reading: true and working: true mean "it has been left to be done", and asking again a little later is still the way to get the new one. reading_failed is said to the person whose reading failed, on every copy. And when the store itself cannot be asked, a request that needs somebody to be signed in answers 401 with sign_in: true and leaves the cookie alone (the same cookie works once the store answers), a sign-in or sign-out answers 503 with error in words, and a decision answers 409 saying that nothing was sent.

Deciding from the app is a small, fixed list of actions (Deciding), each naming its project, the thing acted on and who is asking, and each one something the factory already does another way. There are five so far: merging a change that has passed, sending one back or to the bin with a reason, releasing what a gate holds, and giving a go-ahead.

Other people's words (a title, a review's summary, an issue's title) are handed on exactly as written. Show them as text.

This guide is docs/API.md in the repository, and this page is made from it. Something wrong or unclear? Open an issue or send a pull request. Next: Hosting the app for a team.