Skip to content

Commit 834caf9

Browse files
authored
feat: Implement archive script (#1)
* feat: Implement archive script * docs: Update package refs
1 parent 75a3768 commit 834caf9

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

CONTRIBUTING.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md).
44
By participating in this project you agree to abide by its terms.
55

6-
We appreciate you taking the time to contribute to `@octoherd/script-delete-repository`. Especially as a new contributor, you have a valuable perspective that we lost a long time ago: you will find things confusing and run into problems that no longer occur to us. Please share them with us, so we can make the experience for future contributors the best it could be.
6+
We appreciate you taking the time to contribute to `@octoherd/script-archive-repository`. Especially as a new contributor, you have a valuable perspective that we lost a long time ago: you will find things confusing and run into problems that no longer occur to us. Please share them with us, so we can make the experience for future contributors the best it could be.
77

88
Thank you 💖
99

1010
## Creating an Issue
1111

1212
Before you create a new Issue:
1313

14-
1. Please make sure there is no [open issue](https://github.com/octoherd/script-delete-repository/issues?utf8=%E2%9C%93&q=is%3Aissue) yet.
14+
1. Please make sure there is no [open issue](https://github.com/octoherd/script-archive-repository/issues?utf8=%E2%9C%93&q=is%3Aissue) yet.
1515
2. If it is a bug report, include the steps to reproduce the issue
1616
3. If it is a feature request, please share the motivation for the new feature, what alternatives you tried, and how you would implement it.
1717

@@ -22,8 +22,8 @@ First, fork the repository.
2222
Setup the repository locally. Replace `<your account name>` with the name of the account you forked to.
2323

2424
```shell
25-
git clone https://github.com/<your account name>/script-delete-repository.git
26-
cd script-delete-repository
25+
git clone https://github.com/<your account name>/script-archive-repository.git
26+
cd script-archive-repository
2727
npm install
2828
```
2929

@@ -37,7 +37,7 @@ npm test
3737

3838
- Create a new branch locally.
3939
- Make your changes in that branch and push them to your fork
40-
- Submit a pull request from your topic branch to the main branch on the `octoherd/script-delete-repository` repository.
40+
- Submit a pull request from your topic branch to the main branch on the `octoherd/script-archive-repository` repository.
4141
- Be sure to tag any issues your pull request is taking care of / contributing to. Adding "Closes #123" to a pull request description will automatically close the issue once the pull request is merged in.
4242

4343
## Maintainers only

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
# script-delete-repository
1+
# script-archive-repository
22

3-
> An Octoherd script to delete repositories
3+
> An Octoherd script to archive repositories
44
5-
[![@latest](https://img.shields.io/npm/v/@octoherd/script-delete-repository.svg)](https://www.npmjs.com/package/@octoherd/script-delete-repository)
6-
[![Build Status](https://github.com/octoherd/script-delete-repository/workflows/Test/badge.svg)](https://github.com/octoherd/script-delete-repository/actions?query=workflow%3ATest+branch%3Amain)
5+
[![@latest](https://img.shields.io/npm/v/@octoherd/script-archive-repository.svg)](https://www.npmjs.com/package/@octoherd/script-archive-repository)
6+
[![Build Status](https://github.com/octoherd/script-archive-repository/workflows/Test/badge.svg)](https://github.com/octoherd/script-archive-repository/actions?query=workflow%3ATest+branch%3Amain)
77

88
## Usage
99

10-
Minimal usage (You have to set a token because the built-in OAuth flow creates a token with the `repo` scope, but this script requires the `delete_repo` scope. You can create such a token at https://github.com/settings/tokens/new?scopes=delete_repo)
11-
1210
```js
13-
npx @octoherd/script-delete-repository \
11+
npx @octoherd/script-archive-repository \
1412
-T ghp_0123456789abcdefghjklmnopqrstuvwxyzA \
1513
```
1614

1715
Pass all options as CLI flags to avoid user prompts
1816

1917
```js
20-
npx @octoherd/script-delete-repository \
18+
npx @octoherd/script-archive-repository \
2119
-T ghp_0123456789abcdefghjklmnopqrstuvwxyzA \
22-
-R "octoherd/*"
20+
-R "octoherd/Hello-World" \
21+
--octoherd-bypass-confirms
2322
```
2423

2524
## Options

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
2-
"name": "@octoherd/script-delete-repository",
2+
"name": "@octoherd/script-archive-repository",
33
"version": "0.0.0-development",
44
"type": "module",
55
"exports": "./script.js",
66
"bin": {
7-
"octoherd-script-delete-repository": "./cli.js"
7+
"octoherd-script-archive-repository": "./cli.js"
88
},
9-
"description": "An Octoherd script to delete repositories",
9+
"description": "An Octoherd script to archive repositories",
1010
"scripts": {
1111
"start": "node cli.js",
1212
"test": "node script.js"
1313
},
14-
"repository": "https://github.com/octoherd/script-delete-repository",
14+
"repository": "https://github.com/octoherd/script-archive-repository",
1515
"keywords": [
1616
"octoherd-script"
1717
],

script.js

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// @ts-check
22

33
/** @type boolean */
4-
let hasDeleteRepoScope;
4+
let hasRepoScope;
55

66
/**
77
* An Octoherd script to delete repositories
@@ -10,21 +10,37 @@ let hasDeleteRepoScope;
1010
* @param {import('@octoherd/cli').Repository} repository
1111
*/
1212
export async function script(octokit, repository) {
13-
if (!hasDeleteRepoScope) {
13+
if (!hasRepoScope) {
1414
const { headers } = await octokit.request("HEAD /");
1515
const scopes = new Set(headers["x-oauth-scopes"].split(", "));
1616

17-
if (!scopes.has("delete_repo")) {
17+
if (!scopes.has("repo")) {
1818
throw new Error(
19-
`The "delete_repo" scope is required for this script. Create a token at https://github.com/settings/tokens/new?scopes=delete_repo then run the script with "-T <paste token here>"`
19+
`The "repo" scope is required for this script. Create a token at https://github.com/settings/tokens/new?scopes=repo then run the script with "-T <paste token here>"`
2020
);
2121
}
22-
hasDeleteRepoScope = true;
22+
hasRepoScope = true;
2323
}
2424

25-
await octokit.request("DELETE /repos/{owner}/{repo}", {
25+
const archived = await octokit.request("PATCH /repos/{owner}/{repo}", {
2626
owner: repository.owner.login,
2727
repo: repository.name,
28-
});
29-
octokit.log.info("Repository deleted");
28+
data: {
29+
archived: true,
30+
}
31+
})
32+
.then(() => true)
33+
.catch(error => {
34+
// error.message: Repository was archived so is read-only
35+
if (error.status === 403 && error.message.includes("archived")) {
36+
octokit.log.warn(`Repository is already archived`);
37+
return false;
38+
}
39+
40+
throw error;
41+
})
42+
43+
if (archived) {
44+
octokit.log.info("Repository archived");
45+
}
3046
}

0 commit comments

Comments
 (0)