Skip to content

Commit

Permalink
feat: add support for dynamic branch (#35)
Browse files Browse the repository at this point in the history
* feat: add support for dynamic branch

* fix: ensure repoBranch is set from project configuration
  • Loading branch information
bjohansebas authored Mar 4, 2025
1 parent e7fd72c commit 3094a1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
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

0 comments on commit 3094a1d

Please sign in to comment.