Skip to content

Commit

Permalink
feat: add audit:repositories command
Browse files Browse the repository at this point in the history
Fetch and display information for repositories within the Artsy GitHub
organization.

Initially to be used to aid our switch from a default branch of `master` to
`main`.

Usage:

```
$ artsy audit:repositories
$ artsy audit:repositories --csv
$ artsy audit:repositories --csv --default-branch=main
$ artsy audit:repositories --csv --default-branch=main --pushed=">2021-05-01"
```
  • Loading branch information
dblandin authored and kajatiger committed May 12, 2021
1 parent 8e55cda commit d816dd1
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/__generated__/graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/commands/audit/repositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { flags } from "@oclif/command"
import { cli } from "cli-ux"
import {
ArtsyRepositories,
ArtsyRepositoriesQuery,
} from "../../__generated__/graphql"
import Command from "../../base"
import { githubClient } from "../../utils/github"

export default class Repositories extends Command {
static description = "Audit GitHub repositories."

static flags = {
...Command.flags,
...cli.table.flags(),
"default-branch": flags.string({
description: "List only repos with a specific default branch",
}),
pushed: flags.string({
default: ">2021-01-01",
description: "Pushed to",
}),
}

async run() {
const { flags } = this.parse(Repositories)

const {
data: { search },
} = await githubClient().query<ArtsyRepositoriesQuery>({
query: ArtsyRepositories,
variables: { query: `org:artsy pushed:${flags.pushed}` },
})

let repositories = []
if (flags["default-branch"]) {
search.nodes?.forEach(repository => {
if (repository && repository.__typename === "Repository") {
if (repository.defaultBranchRef?.name === flags["default-branch"]) {
repositories.push(repository)
}
}
})
} else {
repositories = search.nodes as any[]
}

cli.table(
repositories,
{
name: {},
defaultBranch: {
header: "Default Branch",
get: row => row.defaultBranchRef && row.defaultBranchRef.name,
},
},
{
printLine: this.log,
...flags,
}
)
}
}
14 changes: 14 additions & 0 deletions src/queries/repositories.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
query ArtsyRepositories($query: String!) {
search(query: $query, type: REPOSITORY, first: 100) {
__typename
nodes {
__typename
... on Repository {
name
defaultBranchRef {
name
}
}
}
}
}
22 changes: 22 additions & 0 deletions test/commands/audit/repositories.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect, test } from "@oclif/test"
import { RepositoriesFixture } from "../../fixtures/repositories"

describe("scheduled:rfcs", () => {
beforeEach(() => {
process.env.GITHUB_TOKEN = "test"
})

afterEach(() => {
delete process.env.GITHUB_TOKEN
})

test
.nock("https://api.github.com", api =>
api.post("/graphql").reply(200, RepositoriesFixture)
)
.stdout()
.command(["audit:repositories", "--csv"])
.it("lists repositories in the artsy github org", ctx => {
expect(ctx.stdout.trim()).to.eq("Name,Default Branch\neigen,master")
})
})
17 changes: 17 additions & 0 deletions test/fixtures/repositories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const RepositoriesFixture = {
data: {
search: {
__typename: "SearchResultItemConnection",
nodes: [
{
__typename: "Repository",
name: "eigen",
defaultBranchRef: {
__typename: "Ref",
name: "master",
},
},
],
},
},
}

0 comments on commit d816dd1

Please sign in to comment.