-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle missing repo on archive plugin
- Loading branch information
Showing
5 changed files
with
291 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.