Skip to content

Commit 774b2e5

Browse files
feat: add unarchiving plugin (#692)
* feat: add unarchiving plugin * fix: fixed some broken test cases and added trailing newling --------- Co-authored-by: Yadhav Jayaraman <[email protected]>
1 parent 3b7eb05 commit 774b2e5

File tree

3 files changed

+189
-30
lines changed

3 files changed

+189
-30
lines changed

lib/plugins/archive.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const NopCommand = require('../nopcommand');
2+
3+
function returnValue(shouldContinue, nop) {
4+
return { shouldContinue, nopCommands: nop };
5+
}
6+
7+
module.exports = class Archive {
8+
constructor(nop, github, repo, settings, log) {
9+
this.github = github;
10+
this.repo = repo;
11+
this.settings = settings;
12+
this.log = log;
13+
this.nop = nop;
14+
}
15+
16+
// Returns true if plugin application should continue, false otherwise
17+
async sync() {
18+
// Fetch repository details using REST API
19+
const { data: repoDetails } = await this.github.repos.get({
20+
owner: this.repo.owner,
21+
repo: this.repo.repo
22+
});
23+
if (typeof this.settings?.archived !== 'undefined') {
24+
this.log.debug(`Checking if ${this.repo.owner}/${this.repo.repo} is archived`);
25+
26+
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is ${repoDetails.archived ? 'archived' : 'not archived'}`);
27+
28+
if (repoDetails.archived) {
29+
if (this.settings.archived) {
30+
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} already archived, inform other plugins should not run.`);
31+
return returnValue(false);
32+
}
33+
else {
34+
this.log.debug(`Unarchiving ${this.repo.owner}/${this.repo.repo}`);
35+
if (this.nop) {
36+
return returnValue(true, [new NopCommand(this.constructor.name, this.repo, this.github.repos.update.endpoint(this.settings), 'will unarchive')]);
37+
}
38+
else {
39+
// Unarchive the repository using REST API
40+
const updateResponse = await this.github.repos.update({
41+
owner: this.repo.owner,
42+
repo: this.repo.repo,
43+
archived: false
44+
});
45+
this.log.debug(`Unarchive result ${JSON.stringify(updateResponse)}`);
46+
47+
return returnValue(true);
48+
}
49+
}
50+
}
51+
else {
52+
if (this.settings.archived) {
53+
this.log.debug(`Archiving ${this.repo.owner}/${this.repo.repo}`);
54+
if (this.nop) {
55+
return returnValue(false, [new NopCommand(this.constructor.name, this.repo, this.github.repos.update.endpoint(this.settings), 'will archive')]);
56+
}
57+
else {
58+
// Archive the repository using REST API
59+
const updateResponse = await this.github.repos.update({
60+
owner: this.repo.owner,
61+
repo: this.repo.repo,
62+
archived: true
63+
});
64+
this.log.debug(`Archive result ${JSON.stringify(updateResponse)}`);
65+
66+
return returnValue(false);
67+
}
68+
}
69+
else {
70+
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is not archived, ignoring.`);
71+
return returnValue(true);
72+
}
73+
}
74+
}
75+
else {
76+
if (repoDetails.archived) {
77+
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is archived, ignoring.`);
78+
return returnValue(false);
79+
}
80+
else {
81+
this.log.debug(`Repo ${this.repo.owner}/${this.repo.repo} is not archived, proceed as usual.`);
82+
return returnValue(true);
83+
}
84+
}
85+
}
86+
};

lib/settings.js

+35-30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const errorTemplate = require('./error')
55
const Glob = require('./glob')
66
const NopCommand = require('./nopcommand')
77
const MergeDeep = require('./mergeDeep')
8+
const Archive = require('./plugins/archive')
89
const env = require('./env')
910
const CONFIG_PATH = env.CONFIG_PATH
1011
const eta = new Eta({ views: path.join(__dirname) })
@@ -326,38 +327,42 @@ ${this.results.reduce((x, y) => {
326327
if (overrideRepoConfig) {
327328
repoConfig = this.mergeDeep.mergeDeep({}, repoConfig, overrideRepoConfig)
328329
}
329-
if (repoConfig) {
330-
try {
331-
this.log.debug(`found a matching repoconfig for this repo ${JSON.stringify(repoConfig)}`)
332-
const childPlugins = this.childPluginsList(repo)
333-
const RepoPlugin = Settings.PLUGINS.repository
334-
return new RepoPlugin(this.nop, this.github, repo, repoConfig, this.installation_id, this.log, this.errors).sync().then(res => {
335-
this.appendToResults(res)
336-
return Promise.all(
337-
childPlugins.map(([Plugin, config]) => {
338-
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync()
339-
}))
340-
}).then(res => {
341-
this.appendToResults(res)
342-
})
343-
} catch (e) {
344-
if (this.nop) {
345-
const nopcommand = new NopCommand(this.constructor.name, this.repo, null, `${e}`, 'ERROR')
346-
this.log.error(`NOPCOMMAND ${JSON.stringify(nopcommand)}`)
347-
this.appendToResults([nopcommand])
348-
// throw e
349-
} else {
350-
throw e
330+
const {shouldContinue, nopCommands} = await new Archive(this.nop, this.github, repo, repoConfig, this.log).sync()
331+
if (nopCommands) this.appendToResults(nopCommands)
332+
if (shouldContinue) {
333+
if (repoConfig) {
334+
try {
335+
this.log.debug(`found a matching repoconfig for this repo ${JSON.stringify(repoConfig)}`)
336+
const childPlugins = this.childPluginsList(repo)
337+
const RepoPlugin = Settings.PLUGINS.repository
338+
return new RepoPlugin(this.nop, this.github, repo, repoConfig, this.installation_id, this.log, this.errors).sync().then(res => {
339+
this.appendToResults(res)
340+
return Promise.all(
341+
childPlugins.map(([Plugin, config]) => {
342+
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync()
343+
}))
344+
}).then(res => {
345+
this.appendToResults(res)
346+
})
347+
} catch (e) {
348+
if (this.nop) {
349+
const nopcommand = new NopCommand(this.constructor.name, this.repo, null, `${e}`, 'ERROR')
350+
this.log.error(`NOPCOMMAND ${JSON.stringify(nopcommand)}`)
351+
this.appendToResults([nopcommand])
352+
// throw e
353+
} else {
354+
throw e
355+
}
351356
}
357+
} else {
358+
this.log.debug(`Didnt find any a matching repoconfig for this repo ${JSON.stringify(repo)} in ${JSON.stringify(this.repoConfigs)}`)
359+
const childPlugins = this.childPluginsList(repo)
360+
return Promise.all(childPlugins.map(([Plugin, config]) => {
361+
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync().then(res => {
362+
this.appendToResults(res)
363+
})
364+
}))
352365
}
353-
} else {
354-
this.log.debug(`Didnt find any a matching repoconfig for this repo ${JSON.stringify(repo)} in ${JSON.stringify(this.repoConfigs)}`)
355-
const childPlugins = this.childPluginsList(repo)
356-
return Promise.all(childPlugins.map(([Plugin, config]) => {
357-
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync().then(res => {
358-
this.appendToResults(res)
359-
})
360-
}))
361366
}
362367
}
363368

test/unit/lib/plugins/archive.test.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const Archive = require('../../../../lib/plugins/archive');
2+
const NopCommand = require('../../../../lib/nopcommand');
3+
4+
describe('Archive Plugin', () => {
5+
let github;
6+
let log;
7+
let repo;
8+
let settings;
9+
let nop;
10+
11+
beforeEach(() => {
12+
github = {
13+
repos: {
14+
get: jest.fn(),
15+
update: jest.fn()
16+
}
17+
};
18+
log = {
19+
debug: jest.fn(),
20+
};
21+
repo = { owner: 'test-owner', repo: 'test-repo' };
22+
settings = {};
23+
nop = false;
24+
});
25+
26+
it('should return false if the repository is archived and settings.archived is true', async () => {
27+
github.repos.get.mockResolvedValue({ data: { archived: true } });
28+
settings.archived = true;
29+
30+
const archive = new Archive(nop, github, repo, settings, log);
31+
const result = await archive.sync();
32+
33+
expect(result.shouldContinue).toBe(false);
34+
});
35+
36+
it('should return true if the repository is archived and settings.archived is false', async () => {
37+
github.repos.get.mockResolvedValue({ data: { archived: true } });
38+
settings.archived = false;
39+
40+
const archive = new Archive(nop, github, repo, settings, log);
41+
const result = await archive.sync();
42+
43+
expect(result.shouldContinue).toBe(true);
44+
expect(log.debug).toHaveBeenCalledWith('Unarchiving test-owner/test-repo');
45+
});
46+
47+
it('should return false if the repository is not archived and settings.archived is true', async () => {
48+
github.repos.get.mockResolvedValue({ data: { archived: false } });
49+
settings.archived = true;
50+
51+
const archive = new Archive(nop, github, repo, settings, log);
52+
const result = await archive.sync();
53+
54+
expect(result.shouldContinue).toBe(false);
55+
expect(log.debug).toHaveBeenCalledWith('Archiving test-owner/test-repo');
56+
});
57+
58+
it('should return true if the repository is not archived and settings.archived is false', async () => {
59+
github.repos.get.mockResolvedValue({ data: { archived: false } });
60+
settings.archived = false;
61+
62+
const archive = new Archive(nop, github, repo, settings, log);
63+
const result = await archive.sync();
64+
65+
expect(result.shouldContinue).toBe(true);
66+
expect(log.debug).toHaveBeenCalledWith('Repo test-owner/test-repo is not archived, ignoring.');
67+
});
68+
});

0 commit comments

Comments
 (0)