Skip to content

fix: handle missing repo on archive plugin #751

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

Open
wants to merge 2 commits into
base: main-enterprise
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
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,32 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
return syncSettings(false, context)
})

robot.on('repository.archived', async context => {
const { payload } = context
const { sender } = payload

if (sender.type === 'Bot') {
robot.log.debug('Repository Archived by a Bot')
return
}
robot.log.debug('Repository Archived by a Human')

return syncSettings(false, context)
})

robot.on('repository.unarchived', async context => {
const { payload } = context
const { sender } = payload

if (sender.type === 'Bot') {
robot.log.debug('Repository Unarchived by a Bot')
return
}
robot.log.debug('Repository Unarchived by a Human')

return syncSettings(false, context)
})

if (process.env.CRON) {
/*
# ┌────────────── second (optional)
Expand Down
176 changes: 99 additions & 77 deletions lib/plugins/archive.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,108 @@
const NopCommand = require('../nopcommand');

function returnValue(shouldContinue, nop) {
return { shouldContinue, nopCommands: nop };
}
const NopCommand = require('../nopcommand')

module.exports = class Archive {
constructor(nop, github, repo, settings, log) {
this.github = github;
this.repo = repo;
this.settings = settings;
this.log = log;
this.nop = nop;
constructor (nop, github, repo, settings, log) {
this.github = github
this.repo = repo
this.settings = settings
this.log = log
this.nop = nop
}

// Returns true if plugin application should continue, false otherwise
async sync() {
// Fetch repository details using REST API
const { data: repoDetails } = await this.github.repos.get({
async getRepo () {
try {
const { data } = await this.github.repos.get({
owner: this.repo.owner,
repo: this.repo.repo
});
if (typeof this.settings?.archived !== 'undefined') {
this.log.debug(`Checking if ${this.repo.owner}/${this.repo.repo} is archived`);

this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is ${repoDetails.archived ? 'archived' : 'not archived'}`);

if (repoDetails.archived) {
if (this.settings.archived) {
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} already archived, inform other plugins should not run.`);
return returnValue(false);
}
else {
this.log.debug(`Unarchiving ${this.repo.owner}/${this.repo.repo}`);
if (this.nop) {
return returnValue(true, [new NopCommand(this.constructor.name, this.repo, this.github.repos.update.endpoint(this.settings), 'will unarchive')]);
}
else {
// Unarchive the repository using REST API
const updateResponse = await this.github.repos.update({
owner: this.repo.owner,
repo: this.repo.repo,
archived: false
});
this.log.debug(`Unarchive result ${JSON.stringify(updateResponse)}`);

return returnValue(true);
}
}
}
else {
if (this.settings.archived) {
this.log.debug(`Archiving ${this.repo.owner}/${this.repo.repo}`);
if (this.nop) {
return returnValue(false, [new NopCommand(this.constructor.name, this.repo, this.github.repos.update.endpoint(this.settings), 'will archive')]);
}
else {
// Archive the repository using REST API
const updateResponse = await this.github.repos.update({
owner: this.repo.owner,
repo: this.repo.repo,
archived: true
});
this.log.debug(`Archive result ${JSON.stringify(updateResponse)}`);

return returnValue(false);
}
}
else {
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is not archived, ignoring.`);
return returnValue(true);
}
})
return data
} catch (error) {
if (error.status === 404 && !this.getDesiredArchiveState()) {
return null
}
}
else {
if (repoDetails.archived) {
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is archived, ignoring.`);
return returnValue(false);
}
else {
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is not archived, proceed as usual.`);
return returnValue(true);
}
throw error
}
}
};

async updateRepoArchiveStatus (archived) {
const action = archived ? 'archive' : 'unarchive'

if (this.nop) {
const change = { msg: 'Change found', additions: {}, modifications: { archived: action }, deletions: {} }
return new NopCommand(
this.constructor.name,
this.repo,
this.github.repos.update.endpoint(this.settings),
change,
'INFO'
)
}

const { data } = await this.github.repos.update({
owner: this.repo.owner,
repo: this.repo.repo,
archived
})

this.log.debug({ result: data }, `Repo ${this.repo.owner}/${this.repo.repo} ${action}d`)
}

getDesiredArchiveState () {
if (typeof this.settings?.archived === 'undefined') {
return null
}
return typeof this.settings.archived === 'boolean'
? this.settings.archived
: this.settings.archived === 'true'
}

shouldArchive (repository = this.repository) {
const desiredState = this.getDesiredArchiveState()
if (desiredState === null) return false
return !repository.archived && desiredState
}

shouldUnarchive (repository = this.repository) {
const desiredState = this.getDesiredArchiveState()
if (desiredState === null) return false
return repository.archived && !desiredState
}

isArchived () {
return this.repository?.archived
}

async getState () {
this.repository = await this.getRepo()

return {
isArchived: this.isArchived(),
shouldArchive: this.shouldArchive(),
shouldUnarchive: this.shouldUnarchive()
}
}

async sync () {
this.repository = await this.getRepo()

const results = []

if (!this.repository) {
this.log.warn(`Repo ${this.repo.owner}/${this.repo.repo} not found, skipping archive sync`)
return results
}

const shouldArchive = this.shouldArchive()
const shouldUnarchive = this.shouldUnarchive()

if (!shouldArchive && !shouldUnarchive) {
this.log.debug(`No archive changes needed for ${this.repo.owner}/${this.repo.repo}`)
return results
}

const archived = shouldArchive
results.push(await this.updateRepoArchiveStatus(archived))

return results
}
}
3 changes: 2 additions & 1 deletion lib/plugins/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const ignorableFields = [
'org',
'force_create',
'auto_init',
'repo'
'repo',
'archived'
]

module.exports = class Repository extends ErrorStash {
Expand Down
81 changes: 47 additions & 34 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,48 +321,61 @@ ${this.results.reduce((x, y) => {
}
}

// Overlay repo config
// Overlay repo config
// RepoConfigs should be preloaded but checking anyway
const overrideRepoConfig = this.repoConfigs[`${repo.repo}.yml`]?.repository || this.repoConfigs[`${repo.repo}.yaml`]?.repository
if (overrideRepoConfig) {
repoConfig = this.mergeDeep.mergeDeep({}, repoConfig, overrideRepoConfig)
}
const {shouldContinue, nopCommands} = await new Archive(this.nop, this.github, repo, repoConfig, this.log).sync()
if (nopCommands) this.appendToResults(nopCommands)
if (shouldContinue) {
if (repoConfig) {
try {
this.log.debug(`found a matching repoconfig for this repo ${JSON.stringify(repoConfig)}`)
const childPlugins = this.childPluginsList(repo)
const RepoPlugin = Settings.PLUGINS.repository
return new RepoPlugin(this.nop, this.github, repo, repoConfig, this.installation_id, this.log, this.errors).sync().then(res => {
this.appendToResults(res)
return Promise.all(
childPlugins.map(([Plugin, config]) => {
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync()
}))
}).then(res => {
this.appendToResults(res)
})
} catch (e) {
if (this.nop) {
const nopcommand = new NopCommand(this.constructor.name, this.repo, null, `${e}`, 'ERROR')
this.log.error(`NOPCOMMAND ${JSON.stringify(nopcommand)}`)
this.appendToResults([nopcommand])
// throw e
} else {
throw e
}
}
} else {
this.log.debug(`Didnt find any a matching repoconfig for this repo ${JSON.stringify(repo)} in ${JSON.stringify(this.repoConfigs)}`)
if (repoConfig) {
try {
this.log.debug(`found a matching repoconfig for this repo ${JSON.stringify(repoConfig)}`)

const childPlugins = this.childPluginsList(repo)
return Promise.all(childPlugins.map(([Plugin, config]) => {
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync().then(res => {
this.appendToResults(res)
const RepoPlugin = Settings.PLUGINS.repository

const archivePlugin = new Archive(this.nop, this.github, repo, repoConfig, this.log)
const { shouldArchive, shouldUnarchive } = await archivePlugin.getState()

if (shouldUnarchive) {
this.log.debug(`Unarchiving repo ${repo.repo}`)
const unArchiveResults = await archivePlugin.sync()
this.appendToResults(unArchiveResults)
}

const repoResults = await new RepoPlugin(this.nop, this.github, repo, repoConfig, this.installation_id, this.log, this.errors).sync()
this.appendToResults(repoResults)

const childResults = await Promise.all(
childPlugins.map(([Plugin, config]) => {
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync()
})
}))
)
this.appendToResults(childResults)

if (shouldArchive) {
this.log.debug(`Archiving repo ${repo.repo}`)
const archiveResults = await archivePlugin.sync()
this.appendToResults(archiveResults)
}
} catch (e) {
if (this.nop) {
const nopcommand = new NopCommand(this.constructor.name, this.repo, null, `${e}`, 'ERROR')
this.log.error(`NOPCOMMAND ${JSON.stringify(nopcommand)}`)
this.appendToResults([nopcommand])
// throw e
} else {
throw e
}
}
} else {
this.log.debug(`Didnt find any a matching repoconfig for this repo ${JSON.stringify(repo)} in ${JSON.stringify(this.repoConfigs)}`)
const childPlugins = this.childPluginsList(repo)
return Promise.all(childPlugins.map(([Plugin, config]) => {
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync().then(res => {
this.appendToResults(res)
})
}))
}
}

Expand Down
Loading