Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Garbage Collection Question #9

Open
ro-savage opened this issue Feb 9, 2021 · 4 comments
Open

Garbage Collection Question #9

ro-savage opened this issue Feb 9, 2021 · 4 comments

Comments

@ro-savage
Copy link
Member

Does this code run on every PR creation? Every PR close? Every change? At certain intervals?

It looks like go through all the apps, and remove any that don't have PRs anymore?

const garbageCollectHerokuApps = async (
options: GarbageCollectHerokuAppsOptions,
): Promise<void> => {
const { githubAPIKey } = options
const octokit = github.getOctokit(githubAPIKey)
const { data: pullRequests } = await octokit.request(
'GET /repos/{owner}/{repo}/pulls',
{
owner: 'Runn-Fast',
repo: 'runn',
state: 'open',
},
)
const openPullRequestIDs = pullRequests.map((pr) => pr.number)
const herokuApps = await heroku.getAppList({ team: 'runn' })
for (const herokuApp of herokuApps) {
const { name } = herokuApp
const match = /^runn-pr-(\d+)-/.exec(name)
if (match == null) {
continue
}
const id = parseInt(match[1], 10)
if (openPullRequestIDs.includes(id)) {
console.log(`PR ${id} is still open, leaving app "${name}" as is`)
} else {
console.log(`PR ${id} has been closed, destroying app "${name}"`)
await heroku.destroyApp({ appName: name })
}
}
}

@stayradiated
Copy link
Collaborator

stayradiated commented Feb 9, 2021

Does this code run on every PR creation? Every PR close? Every change? At certain intervals?

The garbage collection runs whenever the workflow is triggered -- this currently happens whenever a commit is pushed to a Pull Request or the development / production branches.

As long as we keep pushing code to the Runn repo it will keep cleaning up the old apps.

It looks like go through all the apps, and remove any that don't have PRs anymore?

That's correct.

@ro-savage
Copy link
Member Author

I guess we can't run a github action on close that would just find the closed PR and remove it?

Probably not really a big deal to Runn this all the time, as it should be fast.

@stayradiated
Copy link
Collaborator

stayradiated commented Feb 10, 2021

I guess we can't run a github action on close that would just find the closed PR and remove it?

I don't think we can hook into the "pull request closed" event -- but what we can do is schedule it as a separate workflow that runs once a day.

Notes on scheduling actions: https://jasonet.co/posts/scheduled-actions/

@stayradiated
Copy link
Collaborator

I had a read through the docs and it seems like it might actually be possible to hook into the "closed" event for pull requests. I'll try it out.

Docs: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request

@ro-savage ro-savage reopened this Mar 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants