Skip to content

Commit

Permalink
fix: use git remote for branches
Browse files Browse the repository at this point in the history
Fixes #329
  • Loading branch information
lukekarrys committed Jul 8, 2023
1 parent 102e1ae commit 8180172
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 68 deletions.
26 changes: 0 additions & 26 deletions .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,3 @@ branches:
apps: []
users: []
teams: [ "cli-team" ]
- name: latest
protection:
required_status_checks: null
enforce_admins: true
required_pull_request_reviews:
required_approving_review_count: 1
require_code_owner_reviews: true
require_last_push_approval: true
dismiss_stale_reviews: true
restrictions:
apps: []
users: []
teams: [ "cli-team" ]
- name: release/v*
protection:
required_status_checks: null
enforce_admins: true
required_pull_request_reviews:
required_approving_review_count: 1
require_code_owner_reviews: true
require_last_push_approval: true
dismiss_stale_reviews: true
restrictions:
apps: []
users: []
teams: [ "cli-team" ]
2 changes: 0 additions & 2 deletions .github/workflows/ci-test-workspace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ on:
push:
branches:
- main
- latest
- release/v*
paths:
- workspace/test-workspace/**
schedule:
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ on:
push:
branches:
- main
- latest
- release/v*
paths-ignore:
- workspace/test-workspace/**
schedule:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ on:
push:
branches:
- main
- latest
- release/v*
pull_request:
branches:
- main
- latest
- release/v*
schedule:
# "At 10:00 UTC (03:00 PT) on Monday" https://crontab.guru/#0_10_*_*_1
- cron: "0 10 * * 1"
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ on:
push:
branches:
- main
- latest
- release/v*

permissions:
contents: write
Expand Down
8 changes: 6 additions & 2 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { relative, dirname, join, extname, posix, win32 } = require('path')
const { defaults, pick, omit, uniq } = require('lodash')
const semver = require('semver')
const parseCIVersions = require('./util/parse-ci-versions.js')
const getGitUrl = require('./util/get-git-url.js')
const git = require('./util/git.js')
const gitignore = require('./util/gitignore.js')
const { mergeWithArrays } = require('./util/merge.js')
const { FILE_KEYS, parseConfig: parseFiles, getAddedFiles, mergeFiles } = require('./util/files.js')
Expand Down Expand Up @@ -261,7 +261,7 @@ const getFullConfig = async ({
}
}

const gitUrl = await getGitUrl(rootPkg.path)
const gitUrl = await git.getUrl(rootPkg.path)
if (gitUrl) {
derived.repository = {
type: 'git',
Expand All @@ -270,6 +270,10 @@ const getFullConfig = async ({
}
}

const remoteBranches = await git.getBranches(rootPkg.path, pkgConfig.branches)
derived.branches = remoteBranches.branches
derived.branchPatterns = remoteBranches.patterns

return {
...pkgConfig,
...derived,
Expand Down
2 changes: 1 addition & 1 deletion lib/content/_on-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pull_request:
{{/if}}
push:
branches:
{{#each branches}}
{{#each branchPatterns}}
- {{ . }}
{{/each}}
{{#if isWorkspace}}
Expand Down
4 changes: 2 additions & 2 deletions lib/content/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ name: CodeQL
on:
push:
branches:
{{#each branches}}
{{#each branchPatterns}}
- {{ . }}
{{/each}}
pull_request:
branches:
{{#each branches}}
{{#each branchPatterns}}
- {{ . }}
{{/each}}
schedule:
Expand Down
2 changes: 1 addition & 1 deletion lib/content/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
type: string
push:
branches:
{{#each branches}}
{{#each branchPatterns}}
- {{ . }}
{{/each}}

Expand Down
26 changes: 0 additions & 26 deletions lib/util/get-git-url.js

This file was deleted.

63 changes: 63 additions & 0 deletions lib/util/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const hgi = require('hosted-git-info')
const git = require('@npmcli/git')
const { minimatch } = require('minimatch')

const cache = new Map()

const tryGit = async (path, ...args) => {
if (!await git.is({ cwd: path })) {
throw new Error('no git')
}
const key = [path, ...args].join(',')
if (cache.has(key)) {
return cache.get(key)
}
const res = git.spawn(args, { cwd: path }).then(r => r.stdout.trim())
cache.set(key, res)
return res
}

// parse a repo from a git origin into a format
// for a package.json#repository object
const getUrl = async (path) => {
try {
const urlStr = await tryGit(path, 'remote', 'get-url', 'origin')
const { domain, user, project } = hgi.fromUrl(urlStr)
const url = new URL(`https://${domain}`)
url.pathname = `/${user}/${project}.git`
return url.toString()
} catch {
// errors are ignored
}
}

const getBranches = async (path, branchPatterns) => {
let matchingBranches = new Set()
let matchingPatterns = new Set()

try {
const res = await tryGit(path, 'ls-remote', '--heads', 'origin').then(r => r.split('\n'))
const remotes = res.map((h) => h.match(/refs\/heads\/(.*)$/)).filter(Boolean).map(h => h[1])
for (const branch of remotes) {
for (const pattern of branchPatterns) {
if (minimatch(branch, pattern)) {
matchingBranches.add(branch)
matchingPatterns.add(pattern)
}
}
}
} catch {
matchingBranches = new Set()
matchingPatterns = new Set(branchPatterns)
}

return {
branches: [...matchingBranches],
patterns: [...matchingPatterns],
}
}

module.exports = {
getUrl,
getBranches,
}

0 comments on commit 8180172

Please sign in to comment.