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 support for dynamic branch #35

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion lib/db/build-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ async function * loadProject (octokit, graphQL, project, config, _repo) {
}
}

// Check config for repoBranch
project.repoBranch = project.repoBranch ?? repo.branch

let pkg
try {
pkg = await files.getPackageJson(project)
Expand Down Expand Up @@ -146,7 +149,7 @@ async function * loadProject (octokit, graphQL, project, config, _repo) {
}

try {
for await (const commit of github.getRepoCommits(graphQL, project.repoOwner, project.repoName)) {
for await (const commit of github.getRepoCommits(graphQL, project.repoOwner, project.repoName, project.repoBranch)) {
yield projectDetail('COMMIT', project, commit)
}
} catch (e) {
Expand Down
26 changes: 16 additions & 10 deletions lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const repoQuerySnip = `{
name
}
homepageUrl
defaultBranchRef {
name
}
}`

const issueQuerySnip = `pageInfo {
Expand Down Expand Up @@ -136,6 +139,7 @@ class Repo {
this.license = repo.licenseInfo && (repo.licenseInfo.spdx_id || repo.licenseInfo.name)
this.language = repo.primaryLanguage ? repo.primaryLanguage.name : repo.primaryLanguage
this.homepage = repo.homepageUrl
this.branch = repo.defaultBranchRef.name
}
}

Expand Down Expand Up @@ -465,12 +469,12 @@ class Commit {
}
}

async function getRemainingCommits (graphQL, owner, repo, cursor) {
async function getRemainingCommits (graphQL, owner, repo, cursor, branch) {
try {
const resp = await graphQL({
query: `query ($org: String!, $repo: String!, $cursor: String!) {
query: `query ($org: String!, $repo: String!, $cursor: String!, $branch: String!) {
repository(name: $repo, owner: $org) {
object(expression: "master") {
object(expression: $branch) {
... on Commit {
history(first: 100, after: $cursor) {
nodes {
Expand Down Expand Up @@ -502,10 +506,11 @@ async function getRemainingCommits (graphQL, owner, repo, cursor) {
`,
org: owner,
repo,
cursor
cursor,
branch
})
const { pageInfo, nodes } = resp.repository.object.history
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor))
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor, branch))

return commits
} catch (error) {
Expand All @@ -515,12 +520,12 @@ async function getRemainingCommits (graphQL, owner, repo, cursor) {
}

module.exports.getRepoCommits =
async function * getRepoCommits (graphQL, owner, repo) {
async function * getRepoCommits (graphQL, owner, repo, branch) {
try {
const resp = await graphQL({
query: `query ($org: String!, $repo: String!) {
query: `query ($org: String!, $repo: String!, $branch: String!) {
repository(name: $repo, owner: $org) {
object(expression: "master") {
object(expression: $branch) {
... on Commit {
history(first: 100) {
nodes {
Expand Down Expand Up @@ -551,10 +556,11 @@ async function * getRepoCommits (graphQL, owner, repo) {
}
`,
org: owner,
repo
repo,
branch
})
const { pageInfo, nodes } = resp.repository.object.history
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor))
const commits = !pageInfo.hasNextPage ? nodes : nodes.concat(await getRemainingCommits(graphQL, owner, repo, pageInfo.endCursor, branch))

for (const c of commits) {
yield new Commit(owner, repo, c)
Expand Down
2 changes: 1 addition & 1 deletion lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports.Project = class Project {
this.repo = repo
this.repoOwner = repoOwner
this.repoName = repoName
this.repoBranch = proj.repoBranch || 'HEAD'
this.repoBranch = proj.repoBranch
this.repoDirectory = proj.repoDirectory || '/'
this.packageName = null
}
Expand Down