packageexamples.stashimportcom.atlassian.plugin.PluginAccessorimportcom.atlassian.sal.api.component.ComponentLocatorimportcom.atlassian.stash.commit.CommitServiceimportcom.atlassian.stash.internal.branch.LatestChangesetMetadataProviderimportcom.atlassian.stash.internal.branch.list.aheadbehind.AheadBehindRefMetadataProviderimportcom.atlassian.stash.repository.*importcom.atlassian.stash.scm.ScmServiceimportcom.atlassian.stash.scm.git.GitCommandBuilderFactoryimportcom.atlassian.stash.util.Pageimportcom.atlassian.stash.util.PageProviderimportcom.atlassian.stash.util.PageRequestimportcom.atlassian.stash.util.PagedIterableimportcom.onresolve.scriptrunner.runner.ScriptRunnerImplimportcom.onresolve.scriptrunner.runner.customisers.WithPluginimportgroovy.transform.Canonicalimportgroovy.util.logging.Log4j@Log4j(category="com.onresolve.examples")@WithPlugin("com.atlassian.stash.stash-branch-utils")classAheadBehindReportextendsScript{defscmService=ComponentLocator.getComponent(ScmService)defrepositoryService=ComponentLocator.getComponent(RepositoryService)defrepositoryMetadataService=ComponentLocator.getComponent(RepositoryMetadataService)defpluginAccessor=ComponentLocator.getComponent(PluginAccessor)defgitCommandBuilderFactory=pluginAccessor.getEnabledPluginModule(ScriptRunnerImpl.PLUGIN_KEY+":gitCmdBuilderFactory").getModule()asGitCommandBuilderFactory/* Modify these three lines */defprojectKey="SR"defrepoSlug="sr"// this is what you are comparing your branches to, to see if they are ahead/behinddefbaseRefName="master"@OverrideObjectrun(){defrepository=repositoryService.getBySlug(projectKey,repoSlug)defbaseRef=repositoryMetadataService.resolveRef(repository,baseRefName)defbuilder=newRepositoryBranchesRequest.Builder().repository(repository).build()defbranches=newPagedIterable<Branch>(newPageProvider<Branch>(){@OverridePage<Branch>get(PageRequestpageRequest){repositoryMetadataService.getBranches(builder,pageRequest)}},PageRequest.MAX_PAGE_LIMIT).findAll{it.displayId!=baseRefName}defrefs=branchesasSet<Ref>deflatestChangesetMetadataProvider=newLatestChangesetMetadataProvider(scmService)deflatestChangesetData=latestChangesetMetadataProvider.getMetadata(newRefMetadataContextImpl(baseRef,refs,repository))defaheadBehindRefMetadataProvider=newAheadBehindRefMetadataProvider(ComponentLocator.getComponent(CommitService))defmetadata=aheadBehindRefMetadataProvider.getMetadata(newRefMetadataContextImpl(baseRef,refs,repository))defmergedBranches=metadata.findAll{ref,aheadBehindCount->aheadBehindCount.ahead==0&&aheadBehindCount.behind>0}mergedBranches.keySet().each{ref->log.warn("delete: ${ref.displayId}, last modified: ${latestChangesetData[ref].authorTimestamp.format("dd/MMM/yy")}")// uncomment this line to remove the branch// gitCommandBuilderFactory.builder(repository).branch().delete(ref.displayId).build().call()}// look for branches without a pull request for which the last commit was greater than six weeks agoCalendarcal=Calendar.getInstance()cal.add(Calendar.DAY_OF_YEAR,-42)metadata.findAll{ref,aheadBehindCount->aheadBehindCount.ahead>0&&aheadBehindCount.behind>0&&latestChangesetData[ref].authorTimestamp<cal.getTime()}.sort{latestChangesetData[it.key].authorTimestamp}.each{ref,aheadBehindCount->log.warn("Old branch: ${ref.displayId}, behind: ${latestChangesetData[ref].authorTimestamp.format("dd/MMM/yy")}")}}}@CanonicalclassRefMetadataContextImplimplementsRefMetadataContext{RefbaseRefSet<Ref>refsRepositoryrepository}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.