Skip to content

Commit 6299d64

Browse files
authored
Merge branch 'main-enterprise' into enable-copilot-review
2 parents 8846afe + 467f68b commit 6299d64

File tree

10 files changed

+336
-225
lines changed

10 files changed

+336
-225
lines changed

.github/workflows/create-pre-release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ jobs:
5050
cache: 'npm'
5151
- run: npm install
5252
- name: Set up Docker Buildx
53-
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2
53+
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435
5454
- name: Log in to the Container registry
55-
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772
55+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
5656
with:
5757
registry: ${{ env.REGISTRY }}
5858
username: ${{ github.actor }}
5959
password: ${{ secrets.GITHUB_TOKEN }}
6060
- name: Build Docker Image Locally
61-
uses: docker/build-push-action@1dc73863535b631f98b2378be8619f83b136f4a0
61+
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83
6262
with:
6363
context: .
6464
file: ./Dockerfile
@@ -85,7 +85,7 @@ jobs:
8585
commitish: ${{ github.ref }}
8686
- name: Push Docker Image
8787
if: ${{ success() }}
88-
uses: docker/build-push-action@1dc73863535b631f98b2378be8619f83b136f4a0
88+
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83
8989
with:
9090
context: .
9191
file: ./Dockerfile

.github/workflows/create-release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ jobs:
2828
cache: "npm"
2929
- run: npm install
3030
- name: Set up Docker Buildx
31-
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2
31+
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435
3232
- name: Log in to the Container registry
33-
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772
33+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
3434
with:
3535
registry: ${{ env.REGISTRY }}
3636
username: ${{ github.actor }}
3737
password: ${{ secrets.GITHUB_TOKEN }}
3838
- name: Build Docker Image Locally
39-
uses: docker/build-push-action@1dc73863535b631f98b2378be8619f83b136f4a0
39+
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83
4040
with:
4141
context: .
4242
file: ./Dockerfile
@@ -59,7 +59,7 @@ jobs:
5959
bump: final
6060
- name: Push Docker Image
6161
if: ${{ success() }}
62-
uses: docker/build-push-action@1dc73863535b631f98b2378be8619f83b136f4a0
62+
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83
6363
with:
6464
context: .
6565
file: ./Dockerfile

.github/workflows/rc-release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ jobs:
6060
- run: echo ${{ github.actor }}
6161

6262
- name: Log in to the Container registry
63-
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772
63+
uses: docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1
6464
with:
6565
registry: ${{ env.REGISTRY }}
6666
username: ${{ github.actor }}
6767
password: ${{ secrets.GITHUB_TOKEN }}
6868

6969
- name: Extract metadata
7070
id: meta
71-
uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804
71+
uses: docker/metadata-action@c1e51972afc2121e065aed6d45c65596fe445f3f
7272
with:
7373
images: ${{ env.REGISTRY }}/${{ github.repository_owner }}/${{ env.IMAGE_NAME }}
7474
- name: Set up QEMU
7575
uses: docker/setup-qemu-action@29109295f81e9208d7d86ff1c6c12d2833863392
7676
- name: Set up Docker Buildx
77-
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2
77+
uses: docker/setup-buildx-action@e468171a9de216ec08956ac3ada2f0791b6bd435
7878
- name: Build and push Docker image
79-
uses: docker/build-push-action@1dc73863535b631f98b2378be8619f83b136f4a0
79+
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83
8080
with:
8181
context: .
8282
push: true

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,32 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
624624
return syncSettings(false, context)
625625
})
626626

627+
robot.on('repository.archived', async context => {
628+
const { payload } = context
629+
const { sender } = payload
630+
631+
if (sender.type === 'Bot') {
632+
robot.log.debug('Repository Archived by a Bot')
633+
return
634+
}
635+
robot.log.debug('Repository Archived by a Human')
636+
637+
return syncSettings(false, context)
638+
})
639+
640+
robot.on('repository.unarchived', async context => {
641+
const { payload } = context
642+
const { sender } = payload
643+
644+
if (sender.type === 'Bot') {
645+
robot.log.debug('Repository Unarchived by a Bot')
646+
return
647+
}
648+
robot.log.debug('Repository Unarchived by a Human')
649+
650+
return syncSettings(false, context)
651+
})
652+
627653
if (process.env.CRON) {
628654
/*
629655
# ┌────────────── second (optional)

lib/plugins/archive.js

Lines changed: 99 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,108 @@
1-
const NopCommand = require('../nopcommand');
2-
3-
function returnValue(shouldContinue, nop) {
4-
return { shouldContinue, nopCommands: nop };
5-
}
1+
const NopCommand = require('../nopcommand')
62

73
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;
4+
constructor (nop, github, repo, settings, log) {
5+
this.github = github
6+
this.repo = repo
7+
this.settings = settings
8+
this.log = log
9+
this.nop = nop
1410
}
1511

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({
12+
async getRepo () {
13+
try {
14+
const { data } = await this.github.repos.get({
2015
owner: this.repo.owner,
2116
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-
}
17+
})
18+
return data
19+
} catch (error) {
20+
if (error.status === 404 && !this.getDesiredArchiveState()) {
21+
return null
7322
}
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-
}
23+
throw error
8424
}
8525
}
86-
};
26+
27+
async updateRepoArchiveStatus (archived) {
28+
const action = archived ? 'archive' : 'unarchive'
29+
30+
if (this.nop) {
31+
const change = { msg: 'Change found', additions: {}, modifications: { archived: action }, deletions: {} }
32+
return new NopCommand(
33+
this.constructor.name,
34+
this.repo,
35+
this.github.repos.update.endpoint(this.settings),
36+
change,
37+
'INFO'
38+
)
39+
}
40+
41+
const { data } = await this.github.repos.update({
42+
owner: this.repo.owner,
43+
repo: this.repo.repo,
44+
archived
45+
})
46+
47+
this.log.debug({ result: data }, `Repo ${this.repo.owner}/${this.repo.repo} ${action}d`)
48+
}
49+
50+
getDesiredArchiveState () {
51+
if (typeof this.settings?.archived === 'undefined') {
52+
return null
53+
}
54+
return typeof this.settings.archived === 'boolean'
55+
? this.settings.archived
56+
: this.settings.archived === 'true'
57+
}
58+
59+
shouldArchive (repository = this.repository) {
60+
const desiredState = this.getDesiredArchiveState()
61+
if (desiredState === null) return false
62+
return !repository.archived && desiredState
63+
}
64+
65+
shouldUnarchive (repository = this.repository) {
66+
const desiredState = this.getDesiredArchiveState()
67+
if (desiredState === null) return false
68+
return repository.archived && !desiredState
69+
}
70+
71+
isArchived () {
72+
return this.repository?.archived
73+
}
74+
75+
async getState () {
76+
this.repository = await this.getRepo()
77+
78+
return {
79+
isArchived: this.isArchived(),
80+
shouldArchive: this.shouldArchive(),
81+
shouldUnarchive: this.shouldUnarchive()
82+
}
83+
}
84+
85+
async sync () {
86+
this.repository = await this.getRepo()
87+
88+
const results = []
89+
90+
if (!this.repository) {
91+
this.log.warn(`Repo ${this.repo.owner}/${this.repo.repo} not found, skipping archive sync`)
92+
return results
93+
}
94+
95+
const shouldArchive = this.shouldArchive()
96+
const shouldUnarchive = this.shouldUnarchive()
97+
98+
if (!shouldArchive && !shouldUnarchive) {
99+
this.log.debug(`No archive changes needed for ${this.repo.owner}/${this.repo.repo}`)
100+
return results
101+
}
102+
103+
const archived = shouldArchive
104+
results.push(await this.updateRepoArchiveStatus(archived))
105+
106+
return results
107+
}
108+
}

lib/plugins/repository.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const ignorableFields = [
3636
'org',
3737
'force_create',
3838
'auto_init',
39-
'repo'
39+
'repo',
40+
'archived'
4041
]
4142

4243
module.exports = class Repository extends ErrorStash {

0 commit comments

Comments
 (0)