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

feat: add audit:repositories command #142

Merged
merged 1 commit into from
May 12, 2021
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
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",
},
},
],
},
},
}